blob: e6cdac1471c22cb51c2ec0b6ca07a05d8fec5dd7 [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
Andreas Huber8946ab22010-09-15 16:20:42 -070055#include "include/ThreadedSource.h"
56
Andreas Huberbe06d262009-08-14 14:37:10 -070057namespace android {
58
Andreas Huber8b432b12009-10-07 13:36:52 -070059static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
60
Andreas Huberbe06d262009-08-14 14:37:10 -070061struct CodecInfo {
62 const char *mime;
63 const char *codec;
64};
65
Andreas Huberfb1c2f82009-12-15 13:25:11 -080066#define FACTORY_CREATE(name) \
67static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
68 return new name(source); \
69}
70
James Dong17299ab2010-05-14 15:45:22 -070071#define FACTORY_CREATE_ENCODER(name) \
72static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \
73 return new name(source, meta); \
74}
75
Andreas Huberfb1c2f82009-12-15 13:25:11 -080076#define FACTORY_REF(name) { #name, Make##name },
77
78FACTORY_CREATE(MP3Decoder)
79FACTORY_CREATE(AMRNBDecoder)
80FACTORY_CREATE(AMRWBDecoder)
81FACTORY_CREATE(AACDecoder)
82FACTORY_CREATE(AVCDecoder)
Andreas Huber520b2a72010-08-09 09:54:59 -070083FACTORY_CREATE(G711Decoder)
James Dong02f5b542009-12-15 16:26:55 -080084FACTORY_CREATE(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -070085FACTORY_CREATE(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -070086FACTORY_CREATE(VPXDecoder)
James Dong17299ab2010-05-14 15:45:22 -070087FACTORY_CREATE_ENCODER(AMRNBEncoder)
88FACTORY_CREATE_ENCODER(AMRWBEncoder)
89FACTORY_CREATE_ENCODER(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -070090FACTORY_CREATE_ENCODER(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -070091FACTORY_CREATE_ENCODER(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -070092
93static sp<MediaSource> InstantiateSoftwareEncoder(
94 const char *name, const sp<MediaSource> &source,
95 const sp<MetaData> &meta) {
96 struct FactoryInfo {
97 const char *name;
98 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &);
99 };
100
101 static const FactoryInfo kFactoryInfo[] = {
102 FACTORY_REF(AMRNBEncoder)
103 FACTORY_REF(AMRWBEncoder)
104 FACTORY_REF(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -0700105 FACTORY_REF(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -0700106 FACTORY_REF(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -0700107 };
108 for (size_t i = 0;
109 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
110 if (!strcmp(name, kFactoryInfo[i].name)) {
111 return (*kFactoryInfo[i].CreateFunc)(source, meta);
112 }
113 }
114
115 return NULL;
116}
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800117
118static sp<MediaSource> InstantiateSoftwareCodec(
119 const char *name, const sp<MediaSource> &source) {
120 struct FactoryInfo {
121 const char *name;
122 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
123 };
124
125 static const FactoryInfo kFactoryInfo[] = {
126 FACTORY_REF(MP3Decoder)
127 FACTORY_REF(AMRNBDecoder)
128 FACTORY_REF(AMRWBDecoder)
129 FACTORY_REF(AACDecoder)
130 FACTORY_REF(AVCDecoder)
Andreas Huber520b2a72010-08-09 09:54:59 -0700131 FACTORY_REF(G711Decoder)
James Dong02f5b542009-12-15 16:26:55 -0800132 FACTORY_REF(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -0700133 FACTORY_REF(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -0700134 FACTORY_REF(VPXDecoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800135 };
136 for (size_t i = 0;
137 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
138 if (!strcmp(name, kFactoryInfo[i].name)) {
Andreas Huber8946ab22010-09-15 16:20:42 -0700139 if (!strcmp(name, "VPXDecoder")) {
140 return new ThreadedSource(
141 (*kFactoryInfo[i].CreateFunc)(source));
142 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800143 return (*kFactoryInfo[i].CreateFunc)(source);
144 }
145 }
146
147 return NULL;
148}
149
150#undef FACTORY_REF
151#undef FACTORY_CREATE
152
Andreas Huberbe06d262009-08-14 14:37:10 -0700153static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700154 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
James Dong374aee62010-04-26 10:23:30 -0700155// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800156 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700157// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700158// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800159 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700160// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700161 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800162 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700163// { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700164 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800165 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700166// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
Andreas Huber520b2a72010-08-09 09:54:59 -0700167 { MEDIA_MIMETYPE_AUDIO_G711_ALAW, "G711Decoder" },
168 { MEDIA_MIMETYPE_AUDIO_G711_MLAW, "G711Decoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700169 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.decoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700170 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
171 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700172 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800173 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700174// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700175 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.decoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700176 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700177 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800178 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700179// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700180 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.decoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700181 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
182 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700183 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800184 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700185// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huber388379f2010-05-07 10:35:13 -0700186 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" },
Andreas Huber47ba30e2010-05-24 14:38:02 -0700187 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700188};
189
190static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700191 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800192 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700193 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700194 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700195 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700196 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700197// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
198 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.encoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700199 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
200 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700201 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700202 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700203// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
204 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.encoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700205 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
206 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700207 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700208 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700209// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
210 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.encoder.avc" },
Andreas Huber71c27d92010-03-19 11:43:15 -0700211 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700212 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700213 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Encoder" },
James Dong1cc31e62010-07-02 17:44:44 -0700214 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700215// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700216};
217
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800218#undef OPTIONAL
219
Andreas Hubere0873732009-09-10 09:57:53 -0700220#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700221#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber42c444a2010-02-09 10:20:00 -0800222#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700223
Andreas Huberbe06d262009-08-14 14:37:10 -0700224struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700225 OMXCodecObserver() {
226 }
227
228 void setCodec(const sp<OMXCodec> &target) {
229 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700230 }
231
232 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700233 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700234 sp<OMXCodec> codec = mTarget.promote();
235
236 if (codec.get() != NULL) {
237 codec->on_message(msg);
238 }
239 }
240
241protected:
242 virtual ~OMXCodecObserver() {}
243
244private:
245 wp<OMXCodec> mTarget;
246
247 OMXCodecObserver(const OMXCodecObserver &);
248 OMXCodecObserver &operator=(const OMXCodecObserver &);
249};
250
251static const char *GetCodec(const CodecInfo *info, size_t numInfos,
252 const char *mime, int index) {
253 CHECK(index >= 0);
254 for(size_t i = 0; i < numInfos; ++i) {
255 if (!strcasecmp(mime, info[i].mime)) {
256 if (index == 0) {
257 return info[i].codec;
258 }
259
260 --index;
261 }
262 }
263
264 return NULL;
265}
266
Andreas Huberebf66ea2009-08-19 13:32:58 -0700267enum {
268 kAVCProfileBaseline = 0x42,
269 kAVCProfileMain = 0x4d,
270 kAVCProfileExtended = 0x58,
271 kAVCProfileHigh = 0x64,
272 kAVCProfileHigh10 = 0x6e,
273 kAVCProfileHigh422 = 0x7a,
274 kAVCProfileHigh444 = 0xf4,
275 kAVCProfileCAVLC444Intra = 0x2c
276};
277
278static const char *AVCProfileToString(uint8_t profile) {
279 switch (profile) {
280 case kAVCProfileBaseline:
281 return "Baseline";
282 case kAVCProfileMain:
283 return "Main";
284 case kAVCProfileExtended:
285 return "Extended";
286 case kAVCProfileHigh:
287 return "High";
288 case kAVCProfileHigh10:
289 return "High 10";
290 case kAVCProfileHigh422:
291 return "High 422";
292 case kAVCProfileHigh444:
293 return "High 444";
294 case kAVCProfileCAVLC444Intra:
295 return "CAVLC 444 Intra";
296 default: return "Unknown";
297 }
298}
299
Andreas Huber4c483422009-09-02 16:05:36 -0700300template<class T>
301static void InitOMXParams(T *params) {
302 params->nSize = sizeof(T);
303 params->nVersion.s.nVersionMajor = 1;
304 params->nVersion.s.nVersionMinor = 0;
305 params->nVersion.s.nRevision = 0;
306 params->nVersion.s.nStep = 0;
307}
308
Andreas Hubere13526a2009-10-22 10:43:34 -0700309static bool IsSoftwareCodec(const char *componentName) {
310 if (!strncmp("OMX.PV.", componentName, 7)) {
311 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700312 }
313
Andreas Hubere13526a2009-10-22 10:43:34 -0700314 return false;
315}
316
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800317// A sort order in which non-OMX components are first,
318// followed by software codecs, i.e. OMX.PV.*, followed
319// by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700320static int CompareSoftwareCodecsFirst(
321 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800322 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
323 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
324
325 if (isNotOMX1) {
326 if (isNotOMX2) { return 0; }
327 return -1;
328 }
329 if (isNotOMX2) {
330 return 1;
331 }
332
Andreas Hubere13526a2009-10-22 10:43:34 -0700333 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
334 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
335
336 if (isSoftwareCodec1) {
337 if (isSoftwareCodec2) { return 0; }
338 return -1;
339 }
340
341 if (isSoftwareCodec2) {
342 return 1;
343 }
344
345 return 0;
346}
347
348// static
Andreas Huber1e194162010-10-06 16:43:57 -0700349uint32_t OMXCodec::getComponentQuirks(
350 const char *componentName, bool isEncoder) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700351 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700352
Andreas Huberbe06d262009-08-14 14:37:10 -0700353 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700354 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700355 }
356 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
357 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800358 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700359 }
360 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
361 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700362 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700363 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700364 }
365 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
366 quirks |= kRequiresLoadedToIdleAfterAllocation;
367 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700368 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dong90862e22010-08-26 19:12:59 -0700369 if (!strncmp(componentName, "OMX.qcom.video.encoder.avc", 26)) {
370
371 // The AVC encoder advertises the size of output buffers
372 // based on the input video resolution and assumes
373 // the worst/least compression ratio is 0.5. It is found that
374 // sometimes, the output buffer size is larger than
375 // size advertised by the encoder.
376 quirks |= kRequiresLargerEncoderOutputBuffer;
377 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700378 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700379 if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) {
380 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700381 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700382 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800383 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700384 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700385 if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) {
386 quirks |= kRequiresAllocateBufferOnInputPorts;
387 quirks |= kRequiresAllocateBufferOnOutputPorts;
388 quirks |= kDefersOutputBufferAllocation;
389 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700390
Andreas Huber2dc64d82009-09-11 12:58:53 -0700391 if (!strncmp(componentName, "OMX.TI.", 7)) {
392 // Apparently I must not use OMX_UseBuffer on either input or
393 // output ports on any of the TI components or quote:
394 // "(I) may have unexpected problem (sic) which can be timing related
395 // and hard to reproduce."
396
397 quirks |= kRequiresAllocateBufferOnInputPorts;
398 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dongdca66e12010-06-14 11:14:38 -0700399 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) {
James Dong4f501f02010-06-07 14:41:41 -0700400 quirks |= kAvoidMemcopyInputRecordingFrames;
401 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700402 }
403
Andreas Huberb8de9572010-02-22 14:58:45 -0800404 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
405 quirks |= kInputBufferSizesAreBogus;
406 }
407
Andreas Huber1e194162010-10-06 16:43:57 -0700408 if (!strncmp(componentName, "OMX.SEC.", 8) && !isEncoder) {
409 // These output buffers contain no video data, just some
410 // opaque information that allows the overlay to display their
411 // contents.
412 quirks |= kOutputBuffersAreUnreadable;
413 }
414
Andreas Hubere13526a2009-10-22 10:43:34 -0700415 return quirks;
416}
417
418// static
419void OMXCodec::findMatchingCodecs(
420 const char *mime,
421 bool createEncoder, const char *matchComponentName,
422 uint32_t flags,
423 Vector<String8> *matchingCodecs) {
424 matchingCodecs->clear();
425
426 for (int index = 0;; ++index) {
427 const char *componentName;
428
429 if (createEncoder) {
430 componentName = GetCodec(
431 kEncoderInfo,
432 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
433 mime, index);
434 } else {
435 componentName = GetCodec(
436 kDecoderInfo,
437 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
438 mime, index);
439 }
440
441 if (!componentName) {
442 break;
443 }
444
445 // If a specific codec is requested, skip the non-matching ones.
446 if (matchComponentName && strcmp(componentName, matchComponentName)) {
447 continue;
448 }
449
450 matchingCodecs->push(String8(componentName));
451 }
452
453 if (flags & kPreferSoftwareCodecs) {
454 matchingCodecs->sort(CompareSoftwareCodecsFirst);
455 }
456}
457
458// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800459sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700460 const sp<IOMX> &omx,
461 const sp<MetaData> &meta, bool createEncoder,
462 const sp<MediaSource> &source,
463 const char *matchComponentName,
464 uint32_t flags) {
465 const char *mime;
466 bool success = meta->findCString(kKeyMIMEType, &mime);
467 CHECK(success);
468
469 Vector<String8> matchingCodecs;
470 findMatchingCodecs(
471 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
472
473 if (matchingCodecs.isEmpty()) {
474 return NULL;
475 }
476
477 sp<OMXCodecObserver> observer = new OMXCodecObserver;
478 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700479
480 const char *componentName;
481 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
482 componentName = matchingCodecs[i].string();
483
James Dong17299ab2010-05-14 15:45:22 -0700484 sp<MediaSource> softwareCodec = createEncoder?
485 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800486 InstantiateSoftwareCodec(componentName, source);
487
488 if (softwareCodec != NULL) {
489 LOGV("Successfully allocated software codec '%s'", componentName);
490
491 return softwareCodec;
492 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800493
Andreas Hubere13526a2009-10-22 10:43:34 -0700494 LOGV("Attempting to allocate OMX node '%s'", componentName);
495
496 status_t err = omx->allocateNode(componentName, observer, &node);
497 if (err == OK) {
498 LOGV("Successfully allocated OMX node '%s'", componentName);
499
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700500 sp<OMXCodec> codec = new OMXCodec(
Andreas Huber1e194162010-10-06 16:43:57 -0700501 omx, node, getComponentQuirks(componentName, createEncoder),
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700502 createEncoder, mime, componentName,
503 source);
504
505 observer->setCodec(codec);
506
507 err = codec->configureCodec(meta);
508
509 if (err == OK) {
510 return codec;
511 }
512
513 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700514 }
515 }
516
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700517 return NULL;
518}
Andreas Hubere13526a2009-10-22 10:43:34 -0700519
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700520status_t OMXCodec::configureCodec(const sp<MetaData> &meta) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700521 uint32_t type;
522 const void *data;
523 size_t size;
524 if (meta->findData(kKeyESDS, &type, &data, &size)) {
525 ESDS esds((const char *)data, size);
526 CHECK_EQ(esds.InitCheck(), OK);
527
528 const void *codec_specific_data;
529 size_t codec_specific_data_size;
530 esds.getCodecSpecificInfo(
531 &codec_specific_data, &codec_specific_data_size);
532
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700533 addCodecSpecificData(
Andreas Huberbe06d262009-08-14 14:37:10 -0700534 codec_specific_data, codec_specific_data_size);
535 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
Andreas Huberebf66ea2009-08-19 13:32:58 -0700536 // Parse the AVCDecoderConfigurationRecord
537
538 const uint8_t *ptr = (const uint8_t *)data;
539
540 CHECK(size >= 7);
541 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
542 uint8_t profile = ptr[1];
543 uint8_t level = ptr[3];
544
Andreas Huber44e15c42009-11-23 14:39:38 -0800545 // There is decodable content out there that fails the following
546 // assertion, let's be lenient for now...
547 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700548
549 size_t lengthSize = 1 + (ptr[4] & 3);
550
551 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
552 // violates it...
553 // CHECK((ptr[5] >> 5) == 7); // reserved
554
555 size_t numSeqParameterSets = ptr[5] & 31;
556
557 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700558 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700559
560 for (size_t i = 0; i < numSeqParameterSets; ++i) {
561 CHECK(size >= 2);
562 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700563
564 ptr += 2;
565 size -= 2;
566
Andreas Huberbe06d262009-08-14 14:37:10 -0700567 CHECK(size >= length);
568
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700569 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700570
571 ptr += length;
572 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700573 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700574
Andreas Huberebf66ea2009-08-19 13:32:58 -0700575 CHECK(size >= 1);
576 size_t numPictureParameterSets = *ptr;
577 ++ptr;
578 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700579
Andreas Huberebf66ea2009-08-19 13:32:58 -0700580 for (size_t i = 0; i < numPictureParameterSets; ++i) {
581 CHECK(size >= 2);
582 size_t length = U16_AT(ptr);
583
584 ptr += 2;
585 size -= 2;
586
587 CHECK(size >= length);
588
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700589 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700590
591 ptr += length;
592 size -= length;
593 }
594
Andreas Hubere2f85072010-06-10 09:51:27 -0700595 CODEC_LOGV(
596 "AVC profile = %d (%s), level = %d",
597 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700598
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700599 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
Andreas Hubere2f85072010-06-10 09:51:27 -0700600 && (profile != kAVCProfileBaseline || level > 30)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700601 // This stream exceeds the decoder's capabilities. The decoder
602 // does not handle this gracefully and would clobber the heap
603 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700604
605 LOGE("Profile and/or level exceed the decoder's capabilities.");
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700606 return ERROR_UNSUPPORTED;
Andreas Huberbe06d262009-08-14 14:37:10 -0700607 }
608 }
609
James Dong17299ab2010-05-14 15:45:22 -0700610 int32_t bitRate = 0;
611 if (mIsEncoder) {
612 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
613 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700614 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700615 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700616 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700617 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700618 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700619 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700620 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700621 int32_t numChannels, sampleRate;
622 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
623 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
624
James Dong17299ab2010-05-14 15:45:22 -0700625 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700626 }
James Dongabed93a2010-04-22 17:27:04 -0700627
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700628 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700629
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700630 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700631 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700632 } else {
James Dong1244eab2010-06-08 11:58:53 -0700633 int32_t width, height;
634 bool success = meta->findInt32(kKeyWidth, &width);
635 success = success && meta->findInt32(kKeyHeight, &height);
636 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700637 status_t err = setVideoOutputFormat(
638 mMIME, width, height);
639
640 if (err != OK) {
641 return err;
642 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700643 }
644 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700645
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700646 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
647 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700648 OMX_COLOR_FORMATTYPE format =
649 OMX_COLOR_Format32bitARGB8888;
650 // OMX_COLOR_FormatYUV420PackedPlanar;
651 // OMX_COLOR_FormatCbYCrY;
652 // OMX_COLOR_FormatYUV411Planar;
653
654 int32_t width, height;
655 bool success = meta->findInt32(kKeyWidth, &width);
656 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700657
658 int32_t compressedSize;
659 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700660 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700661
662 CHECK(success);
663 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700664
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700665 setImageOutputFormat(format, width, height);
666 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700667 }
668
Andreas Huberda050cf22009-09-02 14:01:43 -0700669 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800670 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700671 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700672 }
673
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700674 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700675 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
676 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700677 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700678 }
679
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700680 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700681
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700682 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700683}
684
Andreas Huberda050cf22009-09-02 14:01:43 -0700685void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
686 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700687 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700688 def.nPortIndex = portIndex;
689
Andreas Huber784202e2009-10-15 13:46:54 -0700690 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700691 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
692 CHECK_EQ(err, OK);
693
Andreas Huberb8de9572010-02-22 14:58:45 -0800694 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
695 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700696 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700697 }
698
Andreas Huber784202e2009-10-15 13:46:54 -0700699 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700700 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
701 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800702
703 err = mOMX->getParameter(
704 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
705 CHECK_EQ(err, OK);
706
707 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800708 if (portIndex == kPortIndexInput
709 && (mQuirks & kInputBufferSizesAreBogus)) {
710 CHECK_EQ(def.nBufferSize, size);
711 } else {
712 CHECK(def.nBufferSize >= size);
713 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700714}
715
Andreas Huberbe06d262009-08-14 14:37:10 -0700716status_t OMXCodec::setVideoPortFormatType(
717 OMX_U32 portIndex,
718 OMX_VIDEO_CODINGTYPE compressionFormat,
719 OMX_COLOR_FORMATTYPE colorFormat) {
720 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700721 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700722 format.nPortIndex = portIndex;
723 format.nIndex = 0;
724 bool found = false;
725
726 OMX_U32 index = 0;
727 for (;;) {
728 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700729 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700730 mNode, OMX_IndexParamVideoPortFormat,
731 &format, sizeof(format));
732
733 if (err != OK) {
734 return err;
735 }
736
737 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700738 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700739
740#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700741 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700742 portIndex,
743 index, format.eCompressionFormat, format.eColorFormat);
744#endif
745
746 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
747 if (portIndex == kPortIndexInput
748 && colorFormat == format.eColorFormat) {
749 // eCompressionFormat does not seem right.
750 found = true;
751 break;
752 }
753 if (portIndex == kPortIndexOutput
754 && compressionFormat == format.eCompressionFormat) {
755 // eColorFormat does not seem right.
756 found = true;
757 break;
758 }
759 }
760
761 if (format.eCompressionFormat == compressionFormat
762 && format.eColorFormat == colorFormat) {
763 found = true;
764 break;
765 }
766
767 ++index;
768 }
769
770 if (!found) {
771 return UNKNOWN_ERROR;
772 }
773
Andreas Huber53a76bd2009-10-06 16:20:44 -0700774 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700775 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700776 mNode, OMX_IndexParamVideoPortFormat,
777 &format, sizeof(format));
778
779 return err;
780}
781
Andreas Huberb482ce82009-10-29 12:02:48 -0700782static size_t getFrameSize(
783 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
784 switch (colorFormat) {
785 case OMX_COLOR_FormatYCbYCr:
786 case OMX_COLOR_FormatCbYCrY:
787 return width * height * 2;
788
Andreas Huber71c27d92010-03-19 11:43:15 -0700789 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700790 case OMX_COLOR_FormatYUV420SemiPlanar:
791 return (width * height * 3) / 2;
792
793 default:
794 CHECK(!"Should not be here. Unsupported color format.");
795 break;
796 }
797}
798
James Dongafd97e82010-08-03 17:19:23 -0700799status_t OMXCodec::findTargetColorFormat(
800 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) {
801 LOGV("findTargetColorFormat");
802 CHECK(mIsEncoder);
803
804 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
805 int32_t targetColorFormat;
806 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) {
807 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat;
808 } else {
809 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
810 *colorFormat = OMX_COLOR_FormatYCbYCr;
811 }
812 }
813
814 // Check whether the target color format is supported.
815 return isColorFormatSupported(*colorFormat, kPortIndexInput);
816}
817
818status_t OMXCodec::isColorFormatSupported(
819 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) {
820 LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat));
821
822 // Enumerate all the color formats supported by
823 // the omx component to see whether the given
824 // color format is supported.
825 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
826 InitOMXParams(&portFormat);
827 portFormat.nPortIndex = portIndex;
828 OMX_U32 index = 0;
829 portFormat.nIndex = index;
830 while (true) {
831 if (OMX_ErrorNone != mOMX->getParameter(
832 mNode, OMX_IndexParamVideoPortFormat,
833 &portFormat, sizeof(portFormat))) {
James Dongb5024da2010-09-13 16:30:51 -0700834 break;
James Dongafd97e82010-08-03 17:19:23 -0700835 }
836 // Make sure that omx component does not overwrite
837 // the incremented index (bug 2897413).
838 CHECK_EQ(index, portFormat.nIndex);
839 if ((portFormat.eColorFormat == colorFormat)) {
840 LOGV("Found supported color format: %d", portFormat.eColorFormat);
841 return OK; // colorFormat is supported!
842 }
843 ++index;
844 portFormat.nIndex = index;
845
846 // OMX Spec defines less than 50 color formats
847 // 1000 is more than enough for us to tell whether the omx
848 // component in question is buggy or not.
849 if (index >= 1000) {
850 LOGE("More than %ld color formats are supported???", index);
851 break;
852 }
853 }
James Dongb5024da2010-09-13 16:30:51 -0700854
855 LOGE("color format %d is not supported", colorFormat);
James Dongafd97e82010-08-03 17:19:23 -0700856 return UNKNOWN_ERROR;
857}
858
Andreas Huberbe06d262009-08-14 14:37:10 -0700859void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700860 const char *mime, const sp<MetaData>& meta) {
861
862 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
863 bool success = meta->findInt32(kKeyWidth, &width);
864 success = success && meta->findInt32(kKeyHeight, &height);
865 success = success && meta->findInt32(kKeySampleRate, &frameRate);
866 success = success && meta->findInt32(kKeyBitRate, &bitRate);
867 success = success && meta->findInt32(kKeyStride, &stride);
868 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
869 CHECK(success);
870 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700871
872 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700873 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700874 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700875 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700876 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700877 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700878 compressionFormat = OMX_VIDEO_CodingH263;
879 } else {
880 LOGE("Not a supported video mime type: %s", mime);
881 CHECK(!"Should not be here. Not a supported video mime type.");
882 }
883
James Dongafd97e82010-08-03 17:19:23 -0700884 OMX_COLOR_FORMATTYPE colorFormat;
885 CHECK_EQ(OK, findTargetColorFormat(meta, &colorFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -0700886
James Dongb00e2462010-04-26 17:48:26 -0700887 status_t err;
888 OMX_PARAM_PORTDEFINITIONTYPE def;
889 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
890
891 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700892 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700893 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700894 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700895
James Dongb00e2462010-04-26 17:48:26 -0700896 InitOMXParams(&def);
897 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700898
James Dongb00e2462010-04-26 17:48:26 -0700899 err = mOMX->getParameter(
900 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
901 CHECK_EQ(err, OK);
902
James Dong1244eab2010-06-08 11:58:53 -0700903 def.nBufferSize = getFrameSize(colorFormat,
904 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700905
906 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
907
908 video_def->nFrameWidth = width;
909 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700910 video_def->nStride = stride;
911 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700912 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700913 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
914 video_def->eColorFormat = colorFormat;
915
James Dongb00e2462010-04-26 17:48:26 -0700916 err = mOMX->setParameter(
917 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
918 CHECK_EQ(err, OK);
919
920 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700921 CHECK_EQ(setVideoPortFormatType(
922 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
923 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700924 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700925 def.nPortIndex = kPortIndexOutput;
926
James Dongb00e2462010-04-26 17:48:26 -0700927 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700928 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
929
930 CHECK_EQ(err, OK);
931 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
932
933 video_def->nFrameWidth = width;
934 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700935 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700936 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700937 video_def->eCompressionFormat = compressionFormat;
938 video_def->eColorFormat = OMX_COLOR_FormatUnused;
James Dong90862e22010-08-26 19:12:59 -0700939 if (mQuirks & kRequiresLargerEncoderOutputBuffer) {
940 // Increases the output buffer size
941 def.nBufferSize = ((def.nBufferSize * 3) >> 1);
942 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700943
Andreas Huber784202e2009-10-15 13:46:54 -0700944 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700945 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
946 CHECK_EQ(err, OK);
947
James Dongb00e2462010-04-26 17:48:26 -0700948 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700949 switch (compressionFormat) {
950 case OMX_VIDEO_CodingMPEG4:
951 {
James Dong1244eab2010-06-08 11:58:53 -0700952 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700953 break;
954 }
955
956 case OMX_VIDEO_CodingH263:
James Dongc0ab2a62010-06-29 16:29:19 -0700957 CHECK_EQ(setupH263EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700958 break;
959
Andreas Huberea6a38c2009-11-16 15:43:38 -0800960 case OMX_VIDEO_CodingAVC:
961 {
James Dong1244eab2010-06-08 11:58:53 -0700962 CHECK_EQ(setupAVCEncoderParameters(meta), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -0800963 break;
964 }
965
Andreas Huberb482ce82009-10-29 12:02:48 -0700966 default:
967 CHECK(!"Support for this compressionFormat to be implemented.");
968 break;
969 }
970}
971
James Dong1244eab2010-06-08 11:58:53 -0700972static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
973 if (iFramesInterval < 0) {
974 return 0xFFFFFFFF;
975 } else if (iFramesInterval == 0) {
976 return 0;
977 }
978 OMX_U32 ret = frameRate * iFramesInterval;
979 CHECK(ret > 1);
980 return ret;
981}
982
James Dongc0ab2a62010-06-29 16:29:19 -0700983status_t OMXCodec::setupErrorCorrectionParameters() {
984 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
985 InitOMXParams(&errorCorrectionType);
986 errorCorrectionType.nPortIndex = kPortIndexOutput;
987
988 status_t err = mOMX->getParameter(
989 mNode, OMX_IndexParamVideoErrorCorrection,
990 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -0700991 if (err != OK) {
992 LOGW("Error correction param query is not supported");
993 return OK; // Optional feature. Ignore this failure
994 }
James Dongc0ab2a62010-06-29 16:29:19 -0700995
996 errorCorrectionType.bEnableHEC = OMX_FALSE;
997 errorCorrectionType.bEnableResync = OMX_TRUE;
998 errorCorrectionType.nResynchMarkerSpacing = 256;
999 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
1000 errorCorrectionType.bEnableRVLC = OMX_FALSE;
1001
1002 err = mOMX->setParameter(
1003 mNode, OMX_IndexParamVideoErrorCorrection,
1004 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001005 if (err != OK) {
1006 LOGW("Error correction param configuration is not supported");
1007 }
1008
1009 // Optional feature. Ignore the failure.
James Dongc0ab2a62010-06-29 16:29:19 -07001010 return OK;
1011}
1012
1013status_t OMXCodec::setupBitRate(int32_t bitRate) {
1014 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
1015 InitOMXParams(&bitrateType);
1016 bitrateType.nPortIndex = kPortIndexOutput;
1017
1018 status_t err = mOMX->getParameter(
1019 mNode, OMX_IndexParamVideoBitrate,
1020 &bitrateType, sizeof(bitrateType));
1021 CHECK_EQ(err, OK);
1022
1023 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
1024 bitrateType.nTargetBitrate = bitRate;
1025
1026 err = mOMX->setParameter(
1027 mNode, OMX_IndexParamVideoBitrate,
1028 &bitrateType, sizeof(bitrateType));
1029 CHECK_EQ(err, OK);
1030 return OK;
1031}
1032
James Dong81c929a2010-07-01 15:02:14 -07001033status_t OMXCodec::getVideoProfileLevel(
1034 const sp<MetaData>& meta,
1035 const CodecProfileLevel& defaultProfileLevel,
1036 CodecProfileLevel &profileLevel) {
1037 CODEC_LOGV("Default profile: %ld, level %ld",
1038 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
1039
1040 // Are the default profile and level overwriten?
1041 int32_t profile, level;
1042 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
1043 profile = defaultProfileLevel.mProfile;
1044 }
1045 if (!meta->findInt32(kKeyVideoLevel, &level)) {
1046 level = defaultProfileLevel.mLevel;
1047 }
1048 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
1049
1050 // Are the target profile and level supported by the encoder?
1051 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
1052 InitOMXParams(&param);
1053 param.nPortIndex = kPortIndexOutput;
1054 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
1055 status_t err = mOMX->getParameter(
1056 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
1057 &param, sizeof(param));
1058
James Dongdfb89912010-09-15 21:07:52 -07001059 if (err != OK) break;
James Dong81c929a2010-07-01 15:02:14 -07001060
1061 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
1062 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -07001063 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -07001064 supportedProfile, supportedLevel);
1065
1066 if (profile == supportedProfile &&
James Dongdfb89912010-09-15 21:07:52 -07001067 level <= supportedLevel) {
1068 // We can further check whether the level is a valid
1069 // value; but we will leave that to the omx encoder component
1070 // via OMX_SetParameter call.
James Dong81c929a2010-07-01 15:02:14 -07001071 profileLevel.mProfile = profile;
1072 profileLevel.mLevel = level;
1073 return OK;
1074 }
1075 }
1076
1077 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
1078 profile, level);
1079 return BAD_VALUE;
1080}
1081
James Dongc0ab2a62010-06-29 16:29:19 -07001082status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
1083 int32_t iFramesInterval, frameRate, bitRate;
1084 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1085 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1086 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1087 CHECK(success);
1088 OMX_VIDEO_PARAM_H263TYPE h263type;
1089 InitOMXParams(&h263type);
1090 h263type.nPortIndex = kPortIndexOutput;
1091
1092 status_t err = mOMX->getParameter(
1093 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1094 CHECK_EQ(err, OK);
1095
1096 h263type.nAllowedPictureTypes =
1097 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1098
1099 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1100 if (h263type.nPFrames == 0) {
1101 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1102 }
1103 h263type.nBFrames = 0;
1104
James Dong81c929a2010-07-01 15:02:14 -07001105 // Check profile and level parameters
1106 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001107 defaultProfileLevel.mProfile = h263type.eProfile;
1108 defaultProfileLevel.mLevel = h263type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001109 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1110 if (err != OK) return err;
1111 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1112 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001113
1114 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1115 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1116 h263type.nPictureHeaderRepetition = 0;
1117 h263type.nGOBHeaderInterval = 0;
1118
1119 err = mOMX->setParameter(
1120 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1121 CHECK_EQ(err, OK);
1122
1123 CHECK_EQ(setupBitRate(bitRate), OK);
1124 CHECK_EQ(setupErrorCorrectionParameters(), OK);
1125
1126 return OK;
1127}
1128
James Dong1244eab2010-06-08 11:58:53 -07001129status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1130 int32_t iFramesInterval, frameRate, bitRate;
1131 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1132 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1133 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1134 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001135 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1136 InitOMXParams(&mpeg4type);
1137 mpeg4type.nPortIndex = kPortIndexOutput;
1138
1139 status_t err = mOMX->getParameter(
1140 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1141 CHECK_EQ(err, OK);
1142
1143 mpeg4type.nSliceHeaderSpacing = 0;
1144 mpeg4type.bSVH = OMX_FALSE;
1145 mpeg4type.bGov = OMX_FALSE;
1146
1147 mpeg4type.nAllowedPictureTypes =
1148 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1149
James Dong1244eab2010-06-08 11:58:53 -07001150 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1151 if (mpeg4type.nPFrames == 0) {
1152 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1153 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001154 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001155 mpeg4type.nIDCVLCThreshold = 0;
1156 mpeg4type.bACPred = OMX_TRUE;
1157 mpeg4type.nMaxPacketSize = 256;
1158 mpeg4type.nTimeIncRes = 1000;
1159 mpeg4type.nHeaderExtension = 0;
1160 mpeg4type.bReversibleVLC = OMX_FALSE;
1161
James Dong81c929a2010-07-01 15:02:14 -07001162 // Check profile and level parameters
1163 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001164 defaultProfileLevel.mProfile = mpeg4type.eProfile;
1165 defaultProfileLevel.mLevel = mpeg4type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001166 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1167 if (err != OK) return err;
1168 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1169 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001170
1171 err = mOMX->setParameter(
1172 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1173 CHECK_EQ(err, OK);
1174
James Dongc0ab2a62010-06-29 16:29:19 -07001175 CHECK_EQ(setupBitRate(bitRate), OK);
1176 CHECK_EQ(setupErrorCorrectionParameters(), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001177
1178 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001179}
1180
James Dong1244eab2010-06-08 11:58:53 -07001181status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1182 int32_t iFramesInterval, frameRate, bitRate;
1183 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1184 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1185 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1186 CHECK(success);
1187
Andreas Huberea6a38c2009-11-16 15:43:38 -08001188 OMX_VIDEO_PARAM_AVCTYPE h264type;
1189 InitOMXParams(&h264type);
1190 h264type.nPortIndex = kPortIndexOutput;
1191
1192 status_t err = mOMX->getParameter(
1193 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1194 CHECK_EQ(err, OK);
1195
1196 h264type.nAllowedPictureTypes =
1197 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1198
1199 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001200 h264type.nBFrames = 0; // No B frames support yet
1201 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1202 if (h264type.nPFrames == 0) {
1203 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1204 }
James Dong81c929a2010-07-01 15:02:14 -07001205
1206 // Check profile and level parameters
1207 CodecProfileLevel defaultProfileLevel, profileLevel;
1208 defaultProfileLevel.mProfile = h264type.eProfile;
1209 defaultProfileLevel.mLevel = h264type.eLevel;
1210 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1211 if (err != OK) return err;
1212 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1213 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1214
1215 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1216 h264type.bUseHadamard = OMX_TRUE;
1217 h264type.nRefFrames = 1;
1218 h264type.nRefIdx10ActiveMinus1 = 0;
1219 h264type.nRefIdx11ActiveMinus1 = 0;
1220 h264type.bEntropyCodingCABAC = OMX_FALSE;
1221 h264type.bWeightedPPrediction = OMX_FALSE;
1222 h264type.bconstIpred = OMX_FALSE;
1223 h264type.bDirect8x8Inference = OMX_FALSE;
1224 h264type.bDirectSpatialTemporal = OMX_FALSE;
1225 h264type.nCabacInitIdc = 0;
1226 }
1227
1228 if (h264type.nBFrames != 0) {
1229 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1230 }
1231
Andreas Huberea6a38c2009-11-16 15:43:38 -08001232 h264type.bEnableUEP = OMX_FALSE;
1233 h264type.bEnableFMO = OMX_FALSE;
1234 h264type.bEnableASO = OMX_FALSE;
1235 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001236 h264type.bFrameMBsOnly = OMX_TRUE;
1237 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001238 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1239
1240 err = mOMX->setParameter(
1241 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1242 CHECK_EQ(err, OK);
1243
James Dongc0ab2a62010-06-29 16:29:19 -07001244 CHECK_EQ(setupBitRate(bitRate), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001245
1246 return OK;
1247}
1248
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001249status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001250 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001251 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001252
Andreas Huberbe06d262009-08-14 14:37:10 -07001253 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001254 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001255 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001256 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001257 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001258 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001259 compressionFormat = OMX_VIDEO_CodingH263;
1260 } else {
1261 LOGE("Not a supported video mime type: %s", mime);
1262 CHECK(!"Should not be here. Not a supported video mime type.");
1263 }
1264
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001265 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001266 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1267
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001268 if (err != OK) {
1269 return err;
1270 }
1271
Andreas Huberbe06d262009-08-14 14:37:10 -07001272#if 1
1273 {
1274 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001275 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001276 format.nPortIndex = kPortIndexOutput;
1277 format.nIndex = 0;
1278
Andreas Huber784202e2009-10-15 13:46:54 -07001279 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001280 mNode, OMX_IndexParamVideoPortFormat,
1281 &format, sizeof(format));
1282 CHECK_EQ(err, OK);
1283 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1284
1285 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1286
1287 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1288 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1289 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1290 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1291
Andreas Huber784202e2009-10-15 13:46:54 -07001292 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001293 mNode, OMX_IndexParamVideoPortFormat,
1294 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001295
1296 if (err != OK) {
1297 return err;
1298 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001299 }
1300#endif
1301
1302 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001303 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001304 def.nPortIndex = kPortIndexInput;
1305
Andreas Huber4c483422009-09-02 16:05:36 -07001306 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1307
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001308 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001309 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1310
1311 CHECK_EQ(err, OK);
1312
1313#if 1
1314 // XXX Need a (much) better heuristic to compute input buffer sizes.
1315 const size_t X = 64 * 1024;
1316 if (def.nBufferSize < X) {
1317 def.nBufferSize = X;
1318 }
1319#endif
1320
1321 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1322
1323 video_def->nFrameWidth = width;
1324 video_def->nFrameHeight = height;
1325
Andreas Huberb482ce82009-10-29 12:02:48 -07001326 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001327 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1328
Andreas Huber784202e2009-10-15 13:46:54 -07001329 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001330 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001331
1332 if (err != OK) {
1333 return err;
1334 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001335
1336 ////////////////////////////////////////////////////////////////////////////
1337
Andreas Huber4c483422009-09-02 16:05:36 -07001338 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001339 def.nPortIndex = kPortIndexOutput;
1340
Andreas Huber784202e2009-10-15 13:46:54 -07001341 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001342 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1343 CHECK_EQ(err, OK);
1344 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1345
1346#if 0
1347 def.nBufferSize =
1348 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1349#endif
1350
1351 video_def->nFrameWidth = width;
1352 video_def->nFrameHeight = height;
1353
Andreas Huber784202e2009-10-15 13:46:54 -07001354 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001355 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001356
1357 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001358}
1359
Andreas Huberbe06d262009-08-14 14:37:10 -07001360OMXCodec::OMXCodec(
1361 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001362 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001363 const char *mime,
1364 const char *componentName,
1365 const sp<MediaSource> &source)
1366 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001367 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001368 mNode(node),
1369 mQuirks(quirks),
1370 mIsEncoder(isEncoder),
1371 mMIME(strdup(mime)),
1372 mComponentName(strdup(componentName)),
1373 mSource(source),
1374 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001375 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001376 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001377 mSignalledEOS(false),
1378 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001379 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001380 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001381 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1382 mTargetTimeUs(-1),
James Dong53d4e0d2010-07-21 14:51:35 -07001383 mSkipTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001384 mLeftOverBuffer(NULL),
1385 mPaused(false) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001386 mPortStatus[kPortIndexInput] = ENABLED;
1387 mPortStatus[kPortIndexOutput] = ENABLED;
1388
Andreas Huber4c483422009-09-02 16:05:36 -07001389 setComponentRole();
1390}
1391
Andreas Hubere6c40962009-09-10 14:13:30 -07001392// static
1393void OMXCodec::setComponentRole(
1394 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1395 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001396 struct MimeToRole {
1397 const char *mime;
1398 const char *decoderRole;
1399 const char *encoderRole;
1400 };
1401
1402 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001403 { MEDIA_MIMETYPE_AUDIO_MPEG,
1404 "audio_decoder.mp3", "audio_encoder.mp3" },
1405 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1406 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1407 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1408 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1409 { MEDIA_MIMETYPE_AUDIO_AAC,
1410 "audio_decoder.aac", "audio_encoder.aac" },
1411 { MEDIA_MIMETYPE_VIDEO_AVC,
1412 "video_decoder.avc", "video_encoder.avc" },
1413 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1414 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1415 { MEDIA_MIMETYPE_VIDEO_H263,
1416 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001417 };
1418
1419 static const size_t kNumMimeToRole =
1420 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1421
1422 size_t i;
1423 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001424 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001425 break;
1426 }
1427 }
1428
1429 if (i == kNumMimeToRole) {
1430 return;
1431 }
1432
1433 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001434 isEncoder ? kMimeToRole[i].encoderRole
1435 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001436
1437 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001438 OMX_PARAM_COMPONENTROLETYPE roleParams;
1439 InitOMXParams(&roleParams);
1440
1441 strncpy((char *)roleParams.cRole,
1442 role, OMX_MAX_STRINGNAME_SIZE - 1);
1443
1444 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1445
Andreas Huber784202e2009-10-15 13:46:54 -07001446 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001447 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001448 &roleParams, sizeof(roleParams));
1449
1450 if (err != OK) {
1451 LOGW("Failed to set standard component role '%s'.", role);
1452 }
1453 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001454}
1455
Andreas Hubere6c40962009-09-10 14:13:30 -07001456void OMXCodec::setComponentRole() {
1457 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1458}
1459
Andreas Huberbe06d262009-08-14 14:37:10 -07001460OMXCodec::~OMXCodec() {
Andreas Huberf98197a2010-09-17 11:49:39 -07001461 mSource.clear();
1462
Andreas Huber4f5e6022009-08-19 09:29:34 -07001463 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001464
Andreas Huber784202e2009-10-15 13:46:54 -07001465 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001466 CHECK_EQ(err, OK);
1467
1468 mNode = NULL;
1469 setState(DEAD);
1470
1471 clearCodecSpecificData();
1472
1473 free(mComponentName);
1474 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001475
Andreas Huberbe06d262009-08-14 14:37:10 -07001476 free(mMIME);
1477 mMIME = NULL;
1478}
1479
1480status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001481 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001482
1483 CHECK_EQ(mState, LOADED);
1484
1485 status_t err;
1486 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001487 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001488 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001489 setState(LOADED_TO_IDLE);
1490 }
1491
1492 err = allocateBuffers();
1493 CHECK_EQ(err, OK);
1494
1495 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001496 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001497 CHECK_EQ(err, OK);
1498
1499 setState(LOADED_TO_IDLE);
1500 }
1501
1502 while (mState != EXECUTING && mState != ERROR) {
1503 mAsyncCompletion.wait(mLock);
1504 }
1505
1506 return mState == ERROR ? UNKNOWN_ERROR : OK;
1507}
1508
1509// static
1510bool OMXCodec::isIntermediateState(State state) {
1511 return state == LOADED_TO_IDLE
1512 || state == IDLE_TO_EXECUTING
1513 || state == EXECUTING_TO_IDLE
1514 || state == IDLE_TO_LOADED
1515 || state == RECONFIGURING;
1516}
1517
1518status_t OMXCodec::allocateBuffers() {
1519 status_t err = allocateBuffersOnPort(kPortIndexInput);
1520
1521 if (err != OK) {
1522 return err;
1523 }
1524
1525 return allocateBuffersOnPort(kPortIndexOutput);
1526}
1527
1528status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1529 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001530 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001531 def.nPortIndex = portIndex;
1532
Andreas Huber784202e2009-10-15 13:46:54 -07001533 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001534 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1535
1536 if (err != OK) {
1537 return err;
1538 }
1539
Andreas Huber57648e42010-08-04 10:14:30 -07001540 CODEC_LOGI("allocating %lu buffers of size %lu on %s port",
1541 def.nBufferCountActual, def.nBufferSize,
1542 portIndex == kPortIndexInput ? "input" : "output");
1543
Andreas Huber5c0a9132009-08-20 11:16:40 -07001544 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001545 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001546
Andreas Huberbe06d262009-08-14 14:37:10 -07001547 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001548 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001549 CHECK(mem.get() != NULL);
1550
Andreas Huberc712b9f2010-01-20 15:05:46 -08001551 BufferInfo info;
1552 info.mData = NULL;
1553 info.mSize = def.nBufferSize;
1554
Andreas Huberbe06d262009-08-14 14:37:10 -07001555 IOMX::buffer_id buffer;
1556 if (portIndex == kPortIndexInput
1557 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001558 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001559 mem.clear();
1560
Andreas Huberf1fe0642010-01-15 15:28:19 -08001561 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001562 mNode, portIndex, def.nBufferSize, &buffer,
1563 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001564 } else {
1565 err = mOMX->allocateBufferWithBackup(
1566 mNode, portIndex, mem, &buffer);
1567 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001568 } else if (portIndex == kPortIndexOutput
1569 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001570 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001571 mem.clear();
1572
Andreas Huberf1fe0642010-01-15 15:28:19 -08001573 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001574 mNode, portIndex, def.nBufferSize, &buffer,
1575 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001576 } else {
1577 err = mOMX->allocateBufferWithBackup(
1578 mNode, portIndex, mem, &buffer);
1579 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001580 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001581 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001582 }
1583
1584 if (err != OK) {
1585 LOGE("allocate_buffer_with_backup failed");
1586 return err;
1587 }
1588
Andreas Huberc712b9f2010-01-20 15:05:46 -08001589 if (mem != NULL) {
1590 info.mData = mem->pointer();
1591 }
1592
Andreas Huberbe06d262009-08-14 14:37:10 -07001593 info.mBuffer = buffer;
1594 info.mOwnedByComponent = false;
1595 info.mMem = mem;
1596 info.mMediaBuffer = NULL;
1597
1598 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001599 if (!(mOMXLivesLocally
1600 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1601 && (mQuirks & kDefersOutputBufferAllocation))) {
1602 // If the node does not fill in the buffer ptr at this time,
1603 // we will defer creating the MediaBuffer until receiving
1604 // the first FILL_BUFFER_DONE notification instead.
1605 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1606 info.mMediaBuffer->setObserver(this);
1607 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001608 }
1609
1610 mPortBuffers[portIndex].push(info);
1611
Andreas Huber4c483422009-09-02 16:05:36 -07001612 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001613 portIndex == kPortIndexInput ? "input" : "output");
1614 }
1615
Andreas Huber2ea14e22009-12-16 09:30:55 -08001616 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001617
1618 return OK;
1619}
1620
1621void OMXCodec::on_message(const omx_message &msg) {
1622 Mutex::Autolock autoLock(mLock);
1623
1624 switch (msg.type) {
1625 case omx_message::EVENT:
1626 {
1627 onEvent(
1628 msg.u.event_data.event, msg.u.event_data.data1,
1629 msg.u.event_data.data2);
1630
1631 break;
1632 }
1633
1634 case omx_message::EMPTY_BUFFER_DONE:
1635 {
1636 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1637
Andreas Huber4c483422009-09-02 16:05:36 -07001638 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001639
1640 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1641 size_t i = 0;
1642 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1643 ++i;
1644 }
1645
1646 CHECK(i < buffers->size());
1647 if (!(*buffers)[i].mOwnedByComponent) {
1648 LOGW("We already own input buffer %p, yet received "
1649 "an EMPTY_BUFFER_DONE.", buffer);
1650 }
1651
1652 buffers->editItemAt(i).mOwnedByComponent = false;
1653
1654 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001655 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001656
1657 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001658 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001659 CHECK_EQ(err, OK);
1660
1661 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001662 } else if (mState != ERROR
1663 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001664 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1665 drainInputBuffer(&buffers->editItemAt(i));
1666 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001667 break;
1668 }
1669
1670 case omx_message::FILL_BUFFER_DONE:
1671 {
1672 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1673 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1674
Andreas Huber2ea14e22009-12-16 09:30:55 -08001675 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001676 buffer,
1677 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001678 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001679 msg.u.extended_buffer_data.timestamp,
1680 msg.u.extended_buffer_data.timestamp / 1E6);
1681
1682 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1683 size_t i = 0;
1684 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1685 ++i;
1686 }
1687
1688 CHECK(i < buffers->size());
1689 BufferInfo *info = &buffers->editItemAt(i);
1690
1691 if (!info->mOwnedByComponent) {
1692 LOGW("We already own output buffer %p, yet received "
1693 "a FILL_BUFFER_DONE.", buffer);
1694 }
1695
1696 info->mOwnedByComponent = false;
1697
1698 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001699 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001700
1701 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001702 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001703 CHECK_EQ(err, OK);
1704
1705 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001706#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001707 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1708 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001709 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001710 mNoMoreOutputData = true;
1711 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001712#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001713 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1714 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001715
Andreas Huber52733b82010-01-25 10:41:35 -08001716 if (info->mMediaBuffer == NULL) {
1717 CHECK(mOMXLivesLocally);
1718 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1719 CHECK(mQuirks & kDefersOutputBufferAllocation);
1720
1721 // The qcom video decoders on Nexus don't actually allocate
1722 // output buffer memory on a call to OMX_AllocateBuffer
1723 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1724 // structure is only filled in later.
1725
1726 info->mMediaBuffer = new MediaBuffer(
1727 msg.u.extended_buffer_data.data_ptr,
1728 info->mSize);
1729 info->mMediaBuffer->setObserver(this);
1730 }
1731
Andreas Huberbe06d262009-08-14 14:37:10 -07001732 MediaBuffer *buffer = info->mMediaBuffer;
1733
Andreas Huberf88f8442010-08-10 11:18:36 -07001734 if (msg.u.extended_buffer_data.range_offset
1735 + msg.u.extended_buffer_data.range_length
1736 > buffer->size()) {
1737 CODEC_LOGE(
1738 "Codec lied about its buffer size requirements, "
1739 "sending a buffer larger than the originally "
1740 "advertised size in FILL_BUFFER_DONE!");
1741 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001742 buffer->set_range(
1743 msg.u.extended_buffer_data.range_offset,
1744 msg.u.extended_buffer_data.range_length);
1745
1746 buffer->meta_data()->clear();
1747
Andreas Huberfa8de752009-10-08 10:07:49 -07001748 buffer->meta_data()->setInt64(
1749 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001750
1751 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1752 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1753 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001754 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1755 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1756 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001757
Andreas Huber1e194162010-10-06 16:43:57 -07001758 if (mQuirks & kOutputBuffersAreUnreadable) {
1759 buffer->meta_data()->setInt32(kKeyIsUnreadable, true);
1760 }
1761
Andreas Huberbe06d262009-08-14 14:37:10 -07001762 buffer->meta_data()->setPointer(
1763 kKeyPlatformPrivate,
1764 msg.u.extended_buffer_data.platform_private);
1765
1766 buffer->meta_data()->setPointer(
1767 kKeyBufferID,
1768 msg.u.extended_buffer_data.buffer);
1769
Andreas Huber2ea14e22009-12-16 09:30:55 -08001770 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1771 CODEC_LOGV("No more output data.");
1772 mNoMoreOutputData = true;
1773 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07001774
1775 if (mTargetTimeUs >= 0) {
1776 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
1777
1778 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
1779 CODEC_LOGV(
1780 "skipping output buffer at timestamp %lld us",
1781 msg.u.extended_buffer_data.timestamp);
1782
1783 fillOutputBuffer(info);
1784 break;
1785 }
1786
1787 CODEC_LOGV(
1788 "returning output buffer at target timestamp "
1789 "%lld us",
1790 msg.u.extended_buffer_data.timestamp);
1791
1792 mTargetTimeUs = -1;
1793 }
1794
1795 mFilledBuffers.push_back(i);
1796 mBufferFilled.signal();
Andreas Huberbe06d262009-08-14 14:37:10 -07001797 }
1798
1799 break;
1800 }
1801
1802 default:
1803 {
1804 CHECK(!"should not be here.");
1805 break;
1806 }
1807 }
1808}
1809
1810void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1811 switch (event) {
1812 case OMX_EventCmdComplete:
1813 {
1814 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1815 break;
1816 }
1817
1818 case OMX_EventError:
1819 {
Andreas Huberaf0a1882010-09-21 15:08:52 -07001820 CODEC_LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001821
1822 setState(ERROR);
1823 break;
1824 }
1825
1826 case OMX_EventPortSettingsChanged:
1827 {
1828 onPortSettingsChanged(data1);
1829 break;
1830 }
1831
Andreas Huber2ea14e22009-12-16 09:30:55 -08001832#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001833 case OMX_EventBufferFlag:
1834 {
Andreas Huber4c483422009-09-02 16:05:36 -07001835 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001836
1837 if (data1 == kPortIndexOutput) {
1838 mNoMoreOutputData = true;
1839 }
1840 break;
1841 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001842#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001843
1844 default:
1845 {
Andreas Huber4c483422009-09-02 16:05:36 -07001846 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001847 break;
1848 }
1849 }
1850}
1851
Andreas Huberb1678602009-10-19 13:06:40 -07001852// Has the format changed in any way that the client would have to be aware of?
1853static bool formatHasNotablyChanged(
1854 const sp<MetaData> &from, const sp<MetaData> &to) {
1855 if (from.get() == NULL && to.get() == NULL) {
1856 return false;
1857 }
1858
Andreas Huberf68c1682009-10-21 14:01:30 -07001859 if ((from.get() == NULL && to.get() != NULL)
1860 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001861 return true;
1862 }
1863
1864 const char *mime_from, *mime_to;
1865 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1866 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1867
1868 if (strcasecmp(mime_from, mime_to)) {
1869 return true;
1870 }
1871
1872 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1873 int32_t colorFormat_from, colorFormat_to;
1874 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1875 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1876
1877 if (colorFormat_from != colorFormat_to) {
1878 return true;
1879 }
1880
1881 int32_t width_from, width_to;
1882 CHECK(from->findInt32(kKeyWidth, &width_from));
1883 CHECK(to->findInt32(kKeyWidth, &width_to));
1884
1885 if (width_from != width_to) {
1886 return true;
1887 }
1888
1889 int32_t height_from, height_to;
1890 CHECK(from->findInt32(kKeyHeight, &height_from));
1891 CHECK(to->findInt32(kKeyHeight, &height_to));
1892
1893 if (height_from != height_to) {
1894 return true;
1895 }
1896 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1897 int32_t numChannels_from, numChannels_to;
1898 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1899 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1900
1901 if (numChannels_from != numChannels_to) {
1902 return true;
1903 }
1904
1905 int32_t sampleRate_from, sampleRate_to;
1906 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1907 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1908
1909 if (sampleRate_from != sampleRate_to) {
1910 return true;
1911 }
1912 }
1913
1914 return false;
1915}
1916
Andreas Huberbe06d262009-08-14 14:37:10 -07001917void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1918 switch (cmd) {
1919 case OMX_CommandStateSet:
1920 {
1921 onStateChange((OMX_STATETYPE)data);
1922 break;
1923 }
1924
1925 case OMX_CommandPortDisable:
1926 {
1927 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001928 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001929
1930 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1931 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1932 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1933
1934 mPortStatus[portIndex] = DISABLED;
1935
1936 if (mState == RECONFIGURING) {
1937 CHECK_EQ(portIndex, kPortIndexOutput);
1938
Andreas Huberb1678602009-10-19 13:06:40 -07001939 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001940 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001941
1942 // Don't notify clients if the output port settings change
1943 // wasn't of importance to them, i.e. it may be that just the
1944 // number of buffers has changed and nothing else.
1945 mOutputPortSettingsHaveChanged =
1946 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001947
Andreas Huberbe06d262009-08-14 14:37:10 -07001948 enablePortAsync(portIndex);
1949
1950 status_t err = allocateBuffersOnPort(portIndex);
1951 CHECK_EQ(err, OK);
1952 }
1953 break;
1954 }
1955
1956 case OMX_CommandPortEnable:
1957 {
1958 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001959 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001960
1961 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1962 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1963
1964 mPortStatus[portIndex] = ENABLED;
1965
1966 if (mState == RECONFIGURING) {
1967 CHECK_EQ(portIndex, kPortIndexOutput);
1968
1969 setState(EXECUTING);
1970
1971 fillOutputBuffers();
1972 }
1973 break;
1974 }
1975
1976 case OMX_CommandFlush:
1977 {
1978 OMX_U32 portIndex = data;
1979
Andreas Huber4c483422009-09-02 16:05:36 -07001980 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001981
1982 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1983 mPortStatus[portIndex] = ENABLED;
1984
1985 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1986 mPortBuffers[portIndex].size());
1987
1988 if (mState == RECONFIGURING) {
1989 CHECK_EQ(portIndex, kPortIndexOutput);
1990
1991 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001992 } else if (mState == EXECUTING_TO_IDLE) {
1993 if (mPortStatus[kPortIndexInput] == ENABLED
1994 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001995 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001996 "transition from EXECUTING to IDLE.");
1997
1998 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1999 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2000
2001 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002002 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002003 CHECK_EQ(err, OK);
2004 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002005 } else {
2006 // We're flushing both ports in preparation for seeking.
2007
2008 if (mPortStatus[kPortIndexInput] == ENABLED
2009 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002010 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07002011 " seek-time.");
2012
Andreas Huber1f24b302010-06-10 11:12:39 -07002013 // We implicitly resume pulling on our upstream source.
2014 mPaused = false;
2015
Andreas Huberbe06d262009-08-14 14:37:10 -07002016 drainInputBuffers();
2017 fillOutputBuffers();
2018 }
2019 }
2020
2021 break;
2022 }
2023
2024 default:
2025 {
Andreas Huber4c483422009-09-02 16:05:36 -07002026 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07002027 break;
2028 }
2029 }
2030}
2031
2032void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002033 CODEC_LOGV("onStateChange %d", newState);
2034
Andreas Huberbe06d262009-08-14 14:37:10 -07002035 switch (newState) {
2036 case OMX_StateIdle:
2037 {
Andreas Huber4c483422009-09-02 16:05:36 -07002038 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002039 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07002040 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002041 mNode, OMX_CommandStateSet, OMX_StateExecuting);
2042
2043 CHECK_EQ(err, OK);
2044
2045 setState(IDLE_TO_EXECUTING);
2046 } else {
2047 CHECK_EQ(mState, EXECUTING_TO_IDLE);
2048
2049 CHECK_EQ(
2050 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
2051 mPortBuffers[kPortIndexInput].size());
2052
2053 CHECK_EQ(
2054 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
2055 mPortBuffers[kPortIndexOutput].size());
2056
Andreas Huber784202e2009-10-15 13:46:54 -07002057 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002058 mNode, OMX_CommandStateSet, OMX_StateLoaded);
2059
2060 CHECK_EQ(err, OK);
2061
2062 err = freeBuffersOnPort(kPortIndexInput);
2063 CHECK_EQ(err, OK);
2064
2065 err = freeBuffersOnPort(kPortIndexOutput);
2066 CHECK_EQ(err, OK);
2067
2068 mPortStatus[kPortIndexInput] = ENABLED;
2069 mPortStatus[kPortIndexOutput] = ENABLED;
2070
2071 setState(IDLE_TO_LOADED);
2072 }
2073 break;
2074 }
2075
2076 case OMX_StateExecuting:
2077 {
2078 CHECK_EQ(mState, IDLE_TO_EXECUTING);
2079
Andreas Huber4c483422009-09-02 16:05:36 -07002080 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002081
2082 setState(EXECUTING);
2083
Andreas Huber42978e52009-08-27 10:08:39 -07002084 // Buffers will be submitted to the component in the first
2085 // call to OMXCodec::read as mInitialBufferSubmit is true at
2086 // this point. This ensures that this on_message call returns,
2087 // releases the lock and ::init can notice the state change and
2088 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07002089 break;
2090 }
2091
2092 case OMX_StateLoaded:
2093 {
2094 CHECK_EQ(mState, IDLE_TO_LOADED);
2095
Andreas Huber4c483422009-09-02 16:05:36 -07002096 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002097
2098 setState(LOADED);
2099 break;
2100 }
2101
Andreas Huberc712b9f2010-01-20 15:05:46 -08002102 case OMX_StateInvalid:
2103 {
2104 setState(ERROR);
2105 break;
2106 }
2107
Andreas Huberbe06d262009-08-14 14:37:10 -07002108 default:
2109 {
2110 CHECK(!"should not be here.");
2111 break;
2112 }
2113 }
2114}
2115
2116// static
2117size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
2118 size_t n = 0;
2119 for (size_t i = 0; i < buffers.size(); ++i) {
2120 if (!buffers[i].mOwnedByComponent) {
2121 ++n;
2122 }
2123 }
2124
2125 return n;
2126}
2127
2128status_t OMXCodec::freeBuffersOnPort(
2129 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2130 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2131
2132 status_t stickyErr = OK;
2133
2134 for (size_t i = buffers->size(); i-- > 0;) {
2135 BufferInfo *info = &buffers->editItemAt(i);
2136
2137 if (onlyThoseWeOwn && info->mOwnedByComponent) {
2138 continue;
2139 }
2140
2141 CHECK_EQ(info->mOwnedByComponent, false);
2142
Andreas Huber92022852009-09-14 15:24:14 -07002143 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2144
Andreas Huberbe06d262009-08-14 14:37:10 -07002145 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002146 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07002147
2148 if (err != OK) {
2149 stickyErr = err;
2150 }
2151
2152 if (info->mMediaBuffer != NULL) {
2153 info->mMediaBuffer->setObserver(NULL);
2154
2155 // Make sure nobody but us owns this buffer at this point.
2156 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2157
2158 info->mMediaBuffer->release();
2159 }
2160
2161 buffers->removeAt(i);
2162 }
2163
2164 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2165
2166 return stickyErr;
2167}
2168
2169void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002170 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002171
2172 CHECK_EQ(mState, EXECUTING);
2173 CHECK_EQ(portIndex, kPortIndexOutput);
2174 setState(RECONFIGURING);
2175
2176 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002177 if (!flushPortAsync(portIndex)) {
2178 onCmdComplete(OMX_CommandFlush, portIndex);
2179 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002180 } else {
2181 disablePortAsync(portIndex);
2182 }
2183}
2184
Andreas Huber404cc412009-08-25 14:26:05 -07002185bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002186 CHECK(mState == EXECUTING || mState == RECONFIGURING
2187 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002188
Andreas Huber4c483422009-09-02 16:05:36 -07002189 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002190 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2191 mPortBuffers[portIndex].size());
2192
Andreas Huberbe06d262009-08-14 14:37:10 -07002193 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2194 mPortStatus[portIndex] = SHUTTING_DOWN;
2195
Andreas Huber404cc412009-08-25 14:26:05 -07002196 if ((mQuirks & kRequiresFlushCompleteEmulation)
2197 && countBuffersWeOwn(mPortBuffers[portIndex])
2198 == mPortBuffers[portIndex].size()) {
2199 // No flush is necessary and this component fails to send a
2200 // flush-complete event in this case.
2201
2202 return false;
2203 }
2204
Andreas Huberbe06d262009-08-14 14:37:10 -07002205 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002206 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002207 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002208
2209 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002210}
2211
2212void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2213 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2214
2215 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2216 mPortStatus[portIndex] = DISABLING;
2217
2218 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002219 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002220 CHECK_EQ(err, OK);
2221
2222 freeBuffersOnPort(portIndex, true);
2223}
2224
2225void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2226 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2227
2228 CHECK_EQ(mPortStatus[portIndex], DISABLED);
2229 mPortStatus[portIndex] = ENABLING;
2230
2231 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002232 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002233 CHECK_EQ(err, OK);
2234}
2235
2236void OMXCodec::fillOutputBuffers() {
2237 CHECK_EQ(mState, EXECUTING);
2238
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002239 // This is a workaround for some decoders not properly reporting
2240 // end-of-output-stream. If we own all input buffers and also own
2241 // all output buffers and we already signalled end-of-input-stream,
2242 // the end-of-output-stream is implied.
2243 if (mSignalledEOS
2244 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2245 == mPortBuffers[kPortIndexInput].size()
2246 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2247 == mPortBuffers[kPortIndexOutput].size()) {
2248 mNoMoreOutputData = true;
2249 mBufferFilled.signal();
2250
2251 return;
2252 }
2253
Andreas Huberbe06d262009-08-14 14:37:10 -07002254 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2255 for (size_t i = 0; i < buffers->size(); ++i) {
2256 fillOutputBuffer(&buffers->editItemAt(i));
2257 }
2258}
2259
2260void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002261 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002262
2263 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2264 for (size_t i = 0; i < buffers->size(); ++i) {
2265 drainInputBuffer(&buffers->editItemAt(i));
2266 }
2267}
2268
2269void OMXCodec::drainInputBuffer(BufferInfo *info) {
2270 CHECK_EQ(info->mOwnedByComponent, false);
2271
2272 if (mSignalledEOS) {
2273 return;
2274 }
2275
2276 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2277 const CodecSpecificData *specific =
2278 mCodecSpecificData[mCodecSpecificDataIndex];
2279
2280 size_t size = specific->mSize;
2281
Andreas Hubere6c40962009-09-10 14:13:30 -07002282 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002283 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002284 static const uint8_t kNALStartCode[4] =
2285 { 0x00, 0x00, 0x00, 0x01 };
2286
Andreas Huberc712b9f2010-01-20 15:05:46 -08002287 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002288
2289 size += 4;
2290
Andreas Huberc712b9f2010-01-20 15:05:46 -08002291 memcpy(info->mData, kNALStartCode, 4);
2292 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002293 specific->mData, specific->mSize);
2294 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002295 CHECK(info->mSize >= specific->mSize);
2296 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002297 }
2298
Andreas Huber2ea14e22009-12-16 09:30:55 -08002299 mNoMoreOutputData = false;
2300
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002301 CODEC_LOGV("calling emptyBuffer with codec specific data");
2302
Andreas Huber784202e2009-10-15 13:46:54 -07002303 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002304 mNode, info->mBuffer, 0, size,
2305 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2306 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002307 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002308
2309 info->mOwnedByComponent = true;
2310
2311 ++mCodecSpecificDataIndex;
2312 return;
2313 }
2314
Andreas Huber1f24b302010-06-10 11:12:39 -07002315 if (mPaused) {
2316 return;
2317 }
2318
Andreas Huberbe06d262009-08-14 14:37:10 -07002319 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002320
Andreas Hubera4357ad2010-04-02 12:49:54 -07002321 bool signalEOS = false;
2322 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002323
Andreas Hubera4357ad2010-04-02 12:49:54 -07002324 size_t offset = 0;
2325 int32_t n = 0;
2326 for (;;) {
2327 MediaBuffer *srcBuffer;
James Dong53d4e0d2010-07-21 14:51:35 -07002328 MediaSource::ReadOptions options;
2329 if (mSkipTimeUs >= 0) {
2330 options.setSkipFrame(mSkipTimeUs);
2331 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002332 if (mSeekTimeUs >= 0) {
2333 if (mLeftOverBuffer) {
2334 mLeftOverBuffer->release();
2335 mLeftOverBuffer = NULL;
2336 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002337 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002338
2339 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002340 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002341 mBufferFilled.signal();
2342
2343 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002344
2345 if (err == OK) {
2346 int64_t targetTimeUs;
2347 if (srcBuffer->meta_data()->findInt64(
2348 kKeyTargetTime, &targetTimeUs)
2349 && targetTimeUs >= 0) {
2350 mTargetTimeUs = targetTimeUs;
2351 } else {
2352 mTargetTimeUs = -1;
2353 }
2354 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002355 } else if (mLeftOverBuffer) {
2356 srcBuffer = mLeftOverBuffer;
2357 mLeftOverBuffer = NULL;
2358
2359 err = OK;
2360 } else {
James Dong53d4e0d2010-07-21 14:51:35 -07002361 err = mSource->read(&srcBuffer, &options);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002362 }
2363
2364 if (err != OK) {
2365 signalEOS = true;
2366 mFinalStatus = err;
2367 mSignalledEOS = true;
2368 break;
2369 }
2370
2371 size_t remainingBytes = info->mSize - offset;
2372
2373 if (srcBuffer->range_length() > remainingBytes) {
2374 if (offset == 0) {
2375 CODEC_LOGE(
2376 "Codec's input buffers are too small to accomodate "
2377 "buffer read from source (info->mSize = %d, srcLength = %d)",
2378 info->mSize, srcBuffer->range_length());
2379
2380 srcBuffer->release();
2381 srcBuffer = NULL;
2382
2383 setState(ERROR);
2384 return;
2385 }
2386
2387 mLeftOverBuffer = srcBuffer;
2388 break;
2389 }
2390
James Dong4f501f02010-06-07 14:41:41 -07002391 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2392 CHECK(mOMXLivesLocally && offset == 0);
2393 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2394 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2395 } else {
2396 memcpy((uint8_t *)info->mData + offset,
2397 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2398 srcBuffer->range_length());
2399 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002400
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002401 int64_t lastBufferTimeUs;
2402 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002403 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002404
Andreas Hubera4357ad2010-04-02 12:49:54 -07002405 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002406 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002407 }
2408
2409 offset += srcBuffer->range_length();
2410
2411 srcBuffer->release();
2412 srcBuffer = NULL;
2413
2414 ++n;
2415
2416 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2417 break;
2418 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002419
2420 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2421
2422 if (coalescedDurationUs > 250000ll) {
2423 // Don't coalesce more than 250ms worth of encoded data at once.
2424 break;
2425 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002426 }
2427
2428 if (n > 1) {
2429 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002430 }
2431
2432 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002433
Andreas Hubera4357ad2010-04-02 12:49:54 -07002434 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002435 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002436 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002437 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002438 }
2439
Andreas Hubera4357ad2010-04-02 12:49:54 -07002440 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2441 "timestamp %lld us (%.2f secs)",
2442 info->mBuffer, offset,
2443 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002444
Andreas Huber784202e2009-10-15 13:46:54 -07002445 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002446 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002447 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002448
2449 if (err != OK) {
2450 setState(ERROR);
2451 return;
2452 }
2453
2454 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002455
2456 // This component does not ever signal the EOS flag on output buffers,
2457 // Thanks for nothing.
2458 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2459 mNoMoreOutputData = true;
2460 mBufferFilled.signal();
2461 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002462}
2463
2464void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2465 CHECK_EQ(info->mOwnedByComponent, false);
2466
Andreas Huber404cc412009-08-25 14:26:05 -07002467 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002468 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002469 "calling fillOutputBuffer");
2470 return;
2471 }
2472
Andreas Huber4c483422009-09-02 16:05:36 -07002473 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002474 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002475
2476 if (err != OK) {
2477 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2478
2479 setState(ERROR);
2480 return;
2481 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002482
2483 info->mOwnedByComponent = true;
2484}
2485
2486void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2487 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2488 for (size_t i = 0; i < buffers->size(); ++i) {
2489 if ((*buffers)[i].mBuffer == buffer) {
2490 drainInputBuffer(&buffers->editItemAt(i));
2491 return;
2492 }
2493 }
2494
2495 CHECK(!"should not be here.");
2496}
2497
2498void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2499 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2500 for (size_t i = 0; i < buffers->size(); ++i) {
2501 if ((*buffers)[i].mBuffer == buffer) {
2502 fillOutputBuffer(&buffers->editItemAt(i));
2503 return;
2504 }
2505 }
2506
2507 CHECK(!"should not be here.");
2508}
2509
2510void OMXCodec::setState(State newState) {
2511 mState = newState;
2512 mAsyncCompletion.signal();
2513
2514 // This may cause some spurious wakeups but is necessary to
2515 // unblock the reader if we enter ERROR state.
2516 mBufferFilled.signal();
2517}
2518
Andreas Huberda050cf22009-09-02 14:01:43 -07002519void OMXCodec::setRawAudioFormat(
2520 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002521
2522 // port definition
2523 OMX_PARAM_PORTDEFINITIONTYPE def;
2524 InitOMXParams(&def);
2525 def.nPortIndex = portIndex;
2526 status_t err = mOMX->getParameter(
2527 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2528 CHECK_EQ(err, OK);
2529 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2530 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2531 &def, sizeof(def)), OK);
2532
2533 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002534 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002535 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002536 pcmParams.nPortIndex = portIndex;
2537
James Dongabed93a2010-04-22 17:27:04 -07002538 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002539 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2540
2541 CHECK_EQ(err, OK);
2542
2543 pcmParams.nChannels = numChannels;
2544 pcmParams.eNumData = OMX_NumericalDataSigned;
2545 pcmParams.bInterleaved = OMX_TRUE;
2546 pcmParams.nBitPerSample = 16;
2547 pcmParams.nSamplingRate = sampleRate;
2548 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2549
2550 if (numChannels == 1) {
2551 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2552 } else {
2553 CHECK_EQ(numChannels, 2);
2554
2555 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2556 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2557 }
2558
Andreas Huber784202e2009-10-15 13:46:54 -07002559 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002560 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2561
2562 CHECK_EQ(err, OK);
2563}
2564
James Dong17299ab2010-05-14 15:45:22 -07002565static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2566 if (isAMRWB) {
2567 if (bps <= 6600) {
2568 return OMX_AUDIO_AMRBandModeWB0;
2569 } else if (bps <= 8850) {
2570 return OMX_AUDIO_AMRBandModeWB1;
2571 } else if (bps <= 12650) {
2572 return OMX_AUDIO_AMRBandModeWB2;
2573 } else if (bps <= 14250) {
2574 return OMX_AUDIO_AMRBandModeWB3;
2575 } else if (bps <= 15850) {
2576 return OMX_AUDIO_AMRBandModeWB4;
2577 } else if (bps <= 18250) {
2578 return OMX_AUDIO_AMRBandModeWB5;
2579 } else if (bps <= 19850) {
2580 return OMX_AUDIO_AMRBandModeWB6;
2581 } else if (bps <= 23050) {
2582 return OMX_AUDIO_AMRBandModeWB7;
2583 }
2584
2585 // 23850 bps
2586 return OMX_AUDIO_AMRBandModeWB8;
2587 } else { // AMRNB
2588 if (bps <= 4750) {
2589 return OMX_AUDIO_AMRBandModeNB0;
2590 } else if (bps <= 5150) {
2591 return OMX_AUDIO_AMRBandModeNB1;
2592 } else if (bps <= 5900) {
2593 return OMX_AUDIO_AMRBandModeNB2;
2594 } else if (bps <= 6700) {
2595 return OMX_AUDIO_AMRBandModeNB3;
2596 } else if (bps <= 7400) {
2597 return OMX_AUDIO_AMRBandModeNB4;
2598 } else if (bps <= 7950) {
2599 return OMX_AUDIO_AMRBandModeNB5;
2600 } else if (bps <= 10200) {
2601 return OMX_AUDIO_AMRBandModeNB6;
2602 }
2603
2604 // 12200 bps
2605 return OMX_AUDIO_AMRBandModeNB7;
2606 }
2607}
2608
2609void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002610 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002611
Andreas Huber8768f2c2009-12-01 15:26:54 -08002612 OMX_AUDIO_PARAM_AMRTYPE def;
2613 InitOMXParams(&def);
2614 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002615
Andreas Huber8768f2c2009-12-01 15:26:54 -08002616 status_t err =
2617 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002618
Andreas Huber8768f2c2009-12-01 15:26:54 -08002619 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002620
Andreas Huber8768f2c2009-12-01 15:26:54 -08002621 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002622
James Dong17299ab2010-05-14 15:45:22 -07002623 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002624 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2625 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002626
2627 ////////////////////////
2628
2629 if (mIsEncoder) {
2630 sp<MetaData> format = mSource->getFormat();
2631 int32_t sampleRate;
2632 int32_t numChannels;
2633 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2634 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2635
2636 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2637 }
2638}
2639
James Dong17299ab2010-05-14 15:45:22 -07002640void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002641 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002642 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002643 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002644 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002645
2646 //////////////// output port ////////////////////
2647 // format
2648 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2649 format.nPortIndex = kPortIndexOutput;
2650 format.nIndex = 0;
2651 status_t err = OMX_ErrorNone;
2652 while (OMX_ErrorNone == err) {
2653 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2654 &format, sizeof(format)), OK);
2655 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2656 break;
2657 }
2658 format.nIndex++;
2659 }
2660 CHECK_EQ(OK, err);
2661 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2662 &format, sizeof(format)), OK);
2663
2664 // port definition
2665 OMX_PARAM_PORTDEFINITIONTYPE def;
2666 InitOMXParams(&def);
2667 def.nPortIndex = kPortIndexOutput;
2668 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2669 &def, sizeof(def)), OK);
2670 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2671 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2672 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2673 &def, sizeof(def)), OK);
2674
2675 // profile
2676 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2677 InitOMXParams(&profile);
2678 profile.nPortIndex = kPortIndexOutput;
2679 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2680 &profile, sizeof(profile)), OK);
2681 profile.nChannels = numChannels;
2682 profile.eChannelMode = (numChannels == 1?
2683 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2684 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002685 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002686 profile.nAudioBandWidth = 0;
2687 profile.nFrameLength = 0;
2688 profile.nAACtools = OMX_AUDIO_AACToolAll;
2689 profile.nAACERtools = OMX_AUDIO_AACERNone;
2690 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2691 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2692 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2693 &profile, sizeof(profile)), OK);
2694
Andreas Huberda050cf22009-09-02 14:01:43 -07002695 } else {
2696 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002697 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002698 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002699
Andreas Huber784202e2009-10-15 13:46:54 -07002700 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002701 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2702 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002703
Andreas Huberda050cf22009-09-02 14:01:43 -07002704 profile.nChannels = numChannels;
2705 profile.nSampleRate = sampleRate;
2706 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002707
Andreas Huber784202e2009-10-15 13:46:54 -07002708 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002709 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2710 CHECK_EQ(err, OK);
2711 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002712}
2713
2714void OMXCodec::setImageOutputFormat(
2715 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002716 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002717
2718#if 0
2719 OMX_INDEXTYPE index;
2720 status_t err = mOMX->get_extension_index(
2721 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2722 CHECK_EQ(err, OK);
2723
2724 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2725 CHECK_EQ(err, OK);
2726#endif
2727
2728 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002729 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002730 def.nPortIndex = kPortIndexOutput;
2731
Andreas Huber784202e2009-10-15 13:46:54 -07002732 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002733 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2734 CHECK_EQ(err, OK);
2735
2736 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2737
2738 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002739
Andreas Huberbe06d262009-08-14 14:37:10 -07002740 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2741 imageDef->eColorFormat = format;
2742 imageDef->nFrameWidth = width;
2743 imageDef->nFrameHeight = height;
2744
2745 switch (format) {
2746 case OMX_COLOR_FormatYUV420PackedPlanar:
2747 case OMX_COLOR_FormatYUV411Planar:
2748 {
2749 def.nBufferSize = (width * height * 3) / 2;
2750 break;
2751 }
2752
2753 case OMX_COLOR_FormatCbYCrY:
2754 {
2755 def.nBufferSize = width * height * 2;
2756 break;
2757 }
2758
2759 case OMX_COLOR_Format32bitARGB8888:
2760 {
2761 def.nBufferSize = width * height * 4;
2762 break;
2763 }
2764
Andreas Huber201511c2009-09-08 14:01:44 -07002765 case OMX_COLOR_Format16bitARGB4444:
2766 case OMX_COLOR_Format16bitARGB1555:
2767 case OMX_COLOR_Format16bitRGB565:
2768 case OMX_COLOR_Format16bitBGR565:
2769 {
2770 def.nBufferSize = width * height * 2;
2771 break;
2772 }
2773
Andreas Huberbe06d262009-08-14 14:37:10 -07002774 default:
2775 CHECK(!"Should not be here. Unknown color format.");
2776 break;
2777 }
2778
Andreas Huber5c0a9132009-08-20 11:16:40 -07002779 def.nBufferCountActual = def.nBufferCountMin;
2780
Andreas Huber784202e2009-10-15 13:46:54 -07002781 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002782 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2783 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002784}
Andreas Huberbe06d262009-08-14 14:37:10 -07002785
Andreas Huber5c0a9132009-08-20 11:16:40 -07002786void OMXCodec::setJPEGInputFormat(
2787 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2788 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002789 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002790 def.nPortIndex = kPortIndexInput;
2791
Andreas Huber784202e2009-10-15 13:46:54 -07002792 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002793 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2794 CHECK_EQ(err, OK);
2795
Andreas Huber5c0a9132009-08-20 11:16:40 -07002796 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2797 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2798
Andreas Huberbe06d262009-08-14 14:37:10 -07002799 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2800 imageDef->nFrameWidth = width;
2801 imageDef->nFrameHeight = height;
2802
Andreas Huber5c0a9132009-08-20 11:16:40 -07002803 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002804 def.nBufferCountActual = def.nBufferCountMin;
2805
Andreas Huber784202e2009-10-15 13:46:54 -07002806 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002807 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2808 CHECK_EQ(err, OK);
2809}
2810
2811void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2812 CodecSpecificData *specific =
2813 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2814
2815 specific->mSize = size;
2816 memcpy(specific->mData, data, size);
2817
2818 mCodecSpecificData.push(specific);
2819}
2820
2821void OMXCodec::clearCodecSpecificData() {
2822 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2823 free(mCodecSpecificData.editItemAt(i));
2824 }
2825 mCodecSpecificData.clear();
2826 mCodecSpecificDataIndex = 0;
2827}
2828
James Dong36e573b2010-06-19 09:04:18 -07002829status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07002830 Mutex::Autolock autoLock(mLock);
2831
Andreas Huberbe06d262009-08-14 14:37:10 -07002832 if (mState != LOADED) {
2833 return UNKNOWN_ERROR;
2834 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002835
Andreas Huberbe06d262009-08-14 14:37:10 -07002836 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002837 if (mQuirks & kWantsNALFragments) {
2838 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002839 }
James Dong36e573b2010-06-19 09:04:18 -07002840 if (meta) {
2841 int64_t startTimeUs = 0;
2842 int64_t timeUs;
2843 if (meta->findInt64(kKeyTime, &timeUs)) {
2844 startTimeUs = timeUs;
2845 }
2846 params->setInt64(kKeyTime, startTimeUs);
2847 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002848 status_t err = mSource->start(params.get());
2849
2850 if (err != OK) {
2851 return err;
2852 }
2853
2854 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002855 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002856 mSignalledEOS = false;
2857 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002858 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002859 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002860 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
2861 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07002862 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07002863 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002864
2865 return init();
2866}
2867
2868status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002869 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002870
2871 Mutex::Autolock autoLock(mLock);
2872
2873 while (isIntermediateState(mState)) {
2874 mAsyncCompletion.wait(mLock);
2875 }
2876
2877 switch (mState) {
2878 case LOADED:
2879 case ERROR:
2880 break;
2881
2882 case EXECUTING:
2883 {
2884 setState(EXECUTING_TO_IDLE);
2885
Andreas Huber127fcdc2009-08-26 16:27:02 -07002886 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002887 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002888 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002889
Andreas Huber127fcdc2009-08-26 16:27:02 -07002890 bool emulateInputFlushCompletion =
2891 !flushPortAsync(kPortIndexInput);
2892
2893 bool emulateOutputFlushCompletion =
2894 !flushPortAsync(kPortIndexOutput);
2895
2896 if (emulateInputFlushCompletion) {
2897 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2898 }
2899
2900 if (emulateOutputFlushCompletion) {
2901 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2902 }
2903 } else {
2904 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2905 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2906
2907 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002908 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002909 CHECK_EQ(err, OK);
2910 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002911
2912 while (mState != LOADED && mState != ERROR) {
2913 mAsyncCompletion.wait(mLock);
2914 }
2915
2916 break;
2917 }
2918
2919 default:
2920 {
2921 CHECK(!"should not be here.");
2922 break;
2923 }
2924 }
2925
Andreas Hubera4357ad2010-04-02 12:49:54 -07002926 if (mLeftOverBuffer) {
2927 mLeftOverBuffer->release();
2928 mLeftOverBuffer = NULL;
2929 }
2930
Andreas Huberbe06d262009-08-14 14:37:10 -07002931 mSource->stop();
2932
Andreas Huber4a9375e2010-02-09 11:54:33 -08002933 CODEC_LOGV("stopped");
2934
Andreas Huberbe06d262009-08-14 14:37:10 -07002935 return OK;
2936}
2937
2938sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002939 Mutex::Autolock autoLock(mLock);
2940
Andreas Huberbe06d262009-08-14 14:37:10 -07002941 return mOutputFormat;
2942}
2943
2944status_t OMXCodec::read(
2945 MediaBuffer **buffer, const ReadOptions *options) {
2946 *buffer = NULL;
2947
2948 Mutex::Autolock autoLock(mLock);
2949
Andreas Huberd06e5b82009-08-28 13:18:14 -07002950 if (mState != EXECUTING && mState != RECONFIGURING) {
2951 return UNKNOWN_ERROR;
2952 }
2953
Andreas Hubere981c332009-10-22 13:49:30 -07002954 bool seeking = false;
2955 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002956 ReadOptions::SeekMode seekMode;
2957 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07002958 seeking = true;
2959 }
James Dong53d4e0d2010-07-21 14:51:35 -07002960 int64_t skipTimeUs;
2961 if (options && options->getSkipFrame(&skipTimeUs)) {
2962 mSkipTimeUs = skipTimeUs;
2963 } else {
2964 mSkipTimeUs = -1;
2965 }
Andreas Hubere981c332009-10-22 13:49:30 -07002966
Andreas Huber42978e52009-08-27 10:08:39 -07002967 if (mInitialBufferSubmit) {
2968 mInitialBufferSubmit = false;
2969
Andreas Hubere981c332009-10-22 13:49:30 -07002970 if (seeking) {
2971 CHECK(seekTimeUs >= 0);
2972 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002973 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07002974
2975 // There's no reason to trigger the code below, there's
2976 // nothing to flush yet.
2977 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002978 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07002979 }
2980
Andreas Huber42978e52009-08-27 10:08:39 -07002981 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002982
Andreas Huberd06e5b82009-08-28 13:18:14 -07002983 if (mState == EXECUTING) {
2984 // Otherwise mState == RECONFIGURING and this code will trigger
2985 // after the output port is reenabled.
2986 fillOutputBuffers();
2987 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002988 }
2989
Andreas Hubere981c332009-10-22 13:49:30 -07002990 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002991 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002992
2993 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002994
2995 CHECK(seekTimeUs >= 0);
2996 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002997 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07002998
2999 mFilledBuffers.clear();
3000
3001 CHECK_EQ(mState, EXECUTING);
3002
Andreas Huber404cc412009-08-25 14:26:05 -07003003 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
3004 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
3005
3006 if (emulateInputFlushCompletion) {
3007 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3008 }
3009
3010 if (emulateOutputFlushCompletion) {
3011 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3012 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08003013
3014 while (mSeekTimeUs >= 0) {
3015 mBufferFilled.wait(mLock);
3016 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003017 }
3018
3019 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
3020 mBufferFilled.wait(mLock);
3021 }
3022
3023 if (mState == ERROR) {
3024 return UNKNOWN_ERROR;
3025 }
3026
3027 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08003028 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07003029 }
3030
Andreas Hubercfd55572009-10-09 14:11:28 -07003031 if (mOutputPortSettingsHaveChanged) {
3032 mOutputPortSettingsHaveChanged = false;
3033
3034 return INFO_FORMAT_CHANGED;
3035 }
3036
Andreas Huberbe06d262009-08-14 14:37:10 -07003037 size_t index = *mFilledBuffers.begin();
3038 mFilledBuffers.erase(mFilledBuffers.begin());
3039
3040 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
3041 info->mMediaBuffer->add_ref();
3042 *buffer = info->mMediaBuffer;
3043
3044 return OK;
3045}
3046
3047void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
3048 Mutex::Autolock autoLock(mLock);
3049
3050 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
3051 for (size_t i = 0; i < buffers->size(); ++i) {
3052 BufferInfo *info = &buffers->editItemAt(i);
3053
3054 if (info->mMediaBuffer == buffer) {
3055 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
3056 fillOutputBuffer(info);
3057 return;
3058 }
3059 }
3060
3061 CHECK(!"should not be here.");
3062}
3063
3064static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
3065 static const char *kNames[] = {
3066 "OMX_IMAGE_CodingUnused",
3067 "OMX_IMAGE_CodingAutoDetect",
3068 "OMX_IMAGE_CodingJPEG",
3069 "OMX_IMAGE_CodingJPEG2K",
3070 "OMX_IMAGE_CodingEXIF",
3071 "OMX_IMAGE_CodingTIFF",
3072 "OMX_IMAGE_CodingGIF",
3073 "OMX_IMAGE_CodingPNG",
3074 "OMX_IMAGE_CodingLZW",
3075 "OMX_IMAGE_CodingBMP",
3076 };
3077
3078 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3079
3080 if (type < 0 || (size_t)type >= numNames) {
3081 return "UNKNOWN";
3082 } else {
3083 return kNames[type];
3084 }
3085}
3086
3087static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
3088 static const char *kNames[] = {
3089 "OMX_COLOR_FormatUnused",
3090 "OMX_COLOR_FormatMonochrome",
3091 "OMX_COLOR_Format8bitRGB332",
3092 "OMX_COLOR_Format12bitRGB444",
3093 "OMX_COLOR_Format16bitARGB4444",
3094 "OMX_COLOR_Format16bitARGB1555",
3095 "OMX_COLOR_Format16bitRGB565",
3096 "OMX_COLOR_Format16bitBGR565",
3097 "OMX_COLOR_Format18bitRGB666",
3098 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003099 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07003100 "OMX_COLOR_Format24bitRGB888",
3101 "OMX_COLOR_Format24bitBGR888",
3102 "OMX_COLOR_Format24bitARGB1887",
3103 "OMX_COLOR_Format25bitARGB1888",
3104 "OMX_COLOR_Format32bitBGRA8888",
3105 "OMX_COLOR_Format32bitARGB8888",
3106 "OMX_COLOR_FormatYUV411Planar",
3107 "OMX_COLOR_FormatYUV411PackedPlanar",
3108 "OMX_COLOR_FormatYUV420Planar",
3109 "OMX_COLOR_FormatYUV420PackedPlanar",
3110 "OMX_COLOR_FormatYUV420SemiPlanar",
3111 "OMX_COLOR_FormatYUV422Planar",
3112 "OMX_COLOR_FormatYUV422PackedPlanar",
3113 "OMX_COLOR_FormatYUV422SemiPlanar",
3114 "OMX_COLOR_FormatYCbYCr",
3115 "OMX_COLOR_FormatYCrYCb",
3116 "OMX_COLOR_FormatCbYCrY",
3117 "OMX_COLOR_FormatCrYCbY",
3118 "OMX_COLOR_FormatYUV444Interleaved",
3119 "OMX_COLOR_FormatRawBayer8bit",
3120 "OMX_COLOR_FormatRawBayer10bit",
3121 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003122 "OMX_COLOR_FormatL2",
3123 "OMX_COLOR_FormatL4",
3124 "OMX_COLOR_FormatL8",
3125 "OMX_COLOR_FormatL16",
3126 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07003127 "OMX_COLOR_FormatL32",
3128 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
3129 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
3130 "OMX_COLOR_Format18BitBGR666",
3131 "OMX_COLOR_Format24BitARGB6666",
3132 "OMX_COLOR_Format24BitABGR6666",
3133 };
3134
3135 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3136
Andreas Huberbe06d262009-08-14 14:37:10 -07003137 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3138 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3139 } else if (type < 0 || (size_t)type >= numNames) {
3140 return "UNKNOWN";
3141 } else {
3142 return kNames[type];
3143 }
3144}
3145
3146static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3147 static const char *kNames[] = {
3148 "OMX_VIDEO_CodingUnused",
3149 "OMX_VIDEO_CodingAutoDetect",
3150 "OMX_VIDEO_CodingMPEG2",
3151 "OMX_VIDEO_CodingH263",
3152 "OMX_VIDEO_CodingMPEG4",
3153 "OMX_VIDEO_CodingWMV",
3154 "OMX_VIDEO_CodingRV",
3155 "OMX_VIDEO_CodingAVC",
3156 "OMX_VIDEO_CodingMJPEG",
3157 };
3158
3159 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3160
3161 if (type < 0 || (size_t)type >= numNames) {
3162 return "UNKNOWN";
3163 } else {
3164 return kNames[type];
3165 }
3166}
3167
3168static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3169 static const char *kNames[] = {
3170 "OMX_AUDIO_CodingUnused",
3171 "OMX_AUDIO_CodingAutoDetect",
3172 "OMX_AUDIO_CodingPCM",
3173 "OMX_AUDIO_CodingADPCM",
3174 "OMX_AUDIO_CodingAMR",
3175 "OMX_AUDIO_CodingGSMFR",
3176 "OMX_AUDIO_CodingGSMEFR",
3177 "OMX_AUDIO_CodingGSMHR",
3178 "OMX_AUDIO_CodingPDCFR",
3179 "OMX_AUDIO_CodingPDCEFR",
3180 "OMX_AUDIO_CodingPDCHR",
3181 "OMX_AUDIO_CodingTDMAFR",
3182 "OMX_AUDIO_CodingTDMAEFR",
3183 "OMX_AUDIO_CodingQCELP8",
3184 "OMX_AUDIO_CodingQCELP13",
3185 "OMX_AUDIO_CodingEVRC",
3186 "OMX_AUDIO_CodingSMV",
3187 "OMX_AUDIO_CodingG711",
3188 "OMX_AUDIO_CodingG723",
3189 "OMX_AUDIO_CodingG726",
3190 "OMX_AUDIO_CodingG729",
3191 "OMX_AUDIO_CodingAAC",
3192 "OMX_AUDIO_CodingMP3",
3193 "OMX_AUDIO_CodingSBC",
3194 "OMX_AUDIO_CodingVORBIS",
3195 "OMX_AUDIO_CodingWMA",
3196 "OMX_AUDIO_CodingRA",
3197 "OMX_AUDIO_CodingMIDI",
3198 };
3199
3200 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3201
3202 if (type < 0 || (size_t)type >= numNames) {
3203 return "UNKNOWN";
3204 } else {
3205 return kNames[type];
3206 }
3207}
3208
3209static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3210 static const char *kNames[] = {
3211 "OMX_AUDIO_PCMModeLinear",
3212 "OMX_AUDIO_PCMModeALaw",
3213 "OMX_AUDIO_PCMModeMULaw",
3214 };
3215
3216 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3217
3218 if (type < 0 || (size_t)type >= numNames) {
3219 return "UNKNOWN";
3220 } else {
3221 return kNames[type];
3222 }
3223}
3224
Andreas Huber7ae02c82009-09-09 16:29:47 -07003225static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3226 static const char *kNames[] = {
3227 "OMX_AUDIO_AMRBandModeUnused",
3228 "OMX_AUDIO_AMRBandModeNB0",
3229 "OMX_AUDIO_AMRBandModeNB1",
3230 "OMX_AUDIO_AMRBandModeNB2",
3231 "OMX_AUDIO_AMRBandModeNB3",
3232 "OMX_AUDIO_AMRBandModeNB4",
3233 "OMX_AUDIO_AMRBandModeNB5",
3234 "OMX_AUDIO_AMRBandModeNB6",
3235 "OMX_AUDIO_AMRBandModeNB7",
3236 "OMX_AUDIO_AMRBandModeWB0",
3237 "OMX_AUDIO_AMRBandModeWB1",
3238 "OMX_AUDIO_AMRBandModeWB2",
3239 "OMX_AUDIO_AMRBandModeWB3",
3240 "OMX_AUDIO_AMRBandModeWB4",
3241 "OMX_AUDIO_AMRBandModeWB5",
3242 "OMX_AUDIO_AMRBandModeWB6",
3243 "OMX_AUDIO_AMRBandModeWB7",
3244 "OMX_AUDIO_AMRBandModeWB8",
3245 };
3246
3247 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3248
3249 if (type < 0 || (size_t)type >= numNames) {
3250 return "UNKNOWN";
3251 } else {
3252 return kNames[type];
3253 }
3254}
3255
3256static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3257 static const char *kNames[] = {
3258 "OMX_AUDIO_AMRFrameFormatConformance",
3259 "OMX_AUDIO_AMRFrameFormatIF1",
3260 "OMX_AUDIO_AMRFrameFormatIF2",
3261 "OMX_AUDIO_AMRFrameFormatFSF",
3262 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3263 "OMX_AUDIO_AMRFrameFormatITU",
3264 };
3265
3266 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3267
3268 if (type < 0 || (size_t)type >= numNames) {
3269 return "UNKNOWN";
3270 } else {
3271 return kNames[type];
3272 }
3273}
Andreas Huberbe06d262009-08-14 14:37:10 -07003274
3275void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3276 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003277 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003278 def.nPortIndex = portIndex;
3279
Andreas Huber784202e2009-10-15 13:46:54 -07003280 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003281 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3282 CHECK_EQ(err, OK);
3283
3284 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3285
3286 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3287 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3288
3289 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3290 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3291 printf(" nBufferSize = %ld\n", def.nBufferSize);
3292
3293 switch (def.eDomain) {
3294 case OMX_PortDomainImage:
3295 {
3296 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3297
3298 printf("\n");
3299 printf(" // Image\n");
3300 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3301 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3302 printf(" nStride = %ld\n", imageDef->nStride);
3303
3304 printf(" eCompressionFormat = %s\n",
3305 imageCompressionFormatString(imageDef->eCompressionFormat));
3306
3307 printf(" eColorFormat = %s\n",
3308 colorFormatString(imageDef->eColorFormat));
3309
3310 break;
3311 }
3312
3313 case OMX_PortDomainVideo:
3314 {
3315 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3316
3317 printf("\n");
3318 printf(" // Video\n");
3319 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3320 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3321 printf(" nStride = %ld\n", videoDef->nStride);
3322
3323 printf(" eCompressionFormat = %s\n",
3324 videoCompressionFormatString(videoDef->eCompressionFormat));
3325
3326 printf(" eColorFormat = %s\n",
3327 colorFormatString(videoDef->eColorFormat));
3328
3329 break;
3330 }
3331
3332 case OMX_PortDomainAudio:
3333 {
3334 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3335
3336 printf("\n");
3337 printf(" // Audio\n");
3338 printf(" eEncoding = %s\n",
3339 audioCodingTypeString(audioDef->eEncoding));
3340
3341 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3342 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003343 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003344 params.nPortIndex = portIndex;
3345
Andreas Huber784202e2009-10-15 13:46:54 -07003346 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003347 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3348 CHECK_EQ(err, OK);
3349
3350 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3351 printf(" nChannels = %ld\n", params.nChannels);
3352 printf(" bInterleaved = %d\n", params.bInterleaved);
3353 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3354
3355 printf(" eNumData = %s\n",
3356 params.eNumData == OMX_NumericalDataSigned
3357 ? "signed" : "unsigned");
3358
3359 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003360 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3361 OMX_AUDIO_PARAM_AMRTYPE amr;
3362 InitOMXParams(&amr);
3363 amr.nPortIndex = portIndex;
3364
Andreas Huber784202e2009-10-15 13:46:54 -07003365 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003366 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3367 CHECK_EQ(err, OK);
3368
3369 printf(" nChannels = %ld\n", amr.nChannels);
3370 printf(" eAMRBandMode = %s\n",
3371 amrBandModeString(amr.eAMRBandMode));
3372 printf(" eAMRFrameFormat = %s\n",
3373 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003374 }
3375
3376 break;
3377 }
3378
3379 default:
3380 {
3381 printf(" // Unknown\n");
3382 break;
3383 }
3384 }
3385
3386 printf("}\n");
3387}
3388
3389void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3390 mOutputFormat = new MetaData;
3391 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003392 if (mIsEncoder) {
3393 int32_t timeScale;
3394 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3395 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3396 }
3397 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003398
3399 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003400 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003401 def.nPortIndex = kPortIndexOutput;
3402
Andreas Huber784202e2009-10-15 13:46:54 -07003403 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003404 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3405 CHECK_EQ(err, OK);
3406
3407 switch (def.eDomain) {
3408 case OMX_PortDomainImage:
3409 {
3410 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3411 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3412
Andreas Hubere6c40962009-09-10 14:13:30 -07003413 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003414 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3415 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3416 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3417 break;
3418 }
3419
3420 case OMX_PortDomainAudio:
3421 {
3422 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3423
Andreas Huberda050cf22009-09-02 14:01:43 -07003424 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3425 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003426 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003427 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003428
Andreas Huber784202e2009-10-15 13:46:54 -07003429 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003430 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3431 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003432
Andreas Huberda050cf22009-09-02 14:01:43 -07003433 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3434 CHECK_EQ(params.nBitPerSample, 16);
3435 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003436
Andreas Huberda050cf22009-09-02 14:01:43 -07003437 int32_t numChannels, sampleRate;
3438 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3439 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003440
Andreas Huberda050cf22009-09-02 14:01:43 -07003441 if ((OMX_U32)numChannels != params.nChannels) {
3442 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003443 "the input stream contains (contains %d channels, "
3444 "codec outputs %ld channels).",
3445 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003446 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003447
Andreas Hubere6c40962009-09-10 14:13:30 -07003448 mOutputFormat->setCString(
3449 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003450
3451 // Use the codec-advertised number of channels, as some
3452 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003453 // mono. If we know the codec lies about this information,
3454 // use the actual number of channels instead.
3455 mOutputFormat->setInt32(
3456 kKeyChannelCount,
3457 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3458 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003459
3460 // The codec-reported sampleRate is not reliable...
3461 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3462 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003463 OMX_AUDIO_PARAM_AMRTYPE amr;
3464 InitOMXParams(&amr);
3465 amr.nPortIndex = kPortIndexOutput;
3466
Andreas Huber784202e2009-10-15 13:46:54 -07003467 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003468 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3469 CHECK_EQ(err, OK);
3470
3471 CHECK_EQ(amr.nChannels, 1);
3472 mOutputFormat->setInt32(kKeyChannelCount, 1);
3473
3474 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3475 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003476 mOutputFormat->setCString(
3477 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003478 mOutputFormat->setInt32(kKeySampleRate, 8000);
3479 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3480 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003481 mOutputFormat->setCString(
3482 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003483 mOutputFormat->setInt32(kKeySampleRate, 16000);
3484 } else {
3485 CHECK(!"Unknown AMR band mode.");
3486 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003487 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003488 mOutputFormat->setCString(
3489 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003490 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003491 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3492 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003493 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003494 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3495 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003496 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003497 } else {
3498 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003499 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003500 break;
3501 }
3502
3503 case OMX_PortDomainVideo:
3504 {
3505 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3506
3507 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003508 mOutputFormat->setCString(
3509 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003510 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003511 mOutputFormat->setCString(
3512 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003513 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003514 mOutputFormat->setCString(
3515 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003516 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003517 mOutputFormat->setCString(
3518 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003519 } else {
3520 CHECK(!"Unknown compression format.");
3521 }
3522
3523 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3524 // This component appears to be lying to me.
3525 mOutputFormat->setInt32(
3526 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3527 mOutputFormat->setInt32(
3528 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3529 } else {
3530 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3531 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3532 }
3533
3534 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3535 break;
3536 }
3537
3538 default:
3539 {
3540 CHECK(!"should not be here, neither audio nor video.");
3541 break;
3542 }
3543 }
3544}
3545
Andreas Huber1f24b302010-06-10 11:12:39 -07003546status_t OMXCodec::pause() {
3547 Mutex::Autolock autoLock(mLock);
3548
3549 mPaused = true;
3550
3551 return OK;
3552}
3553
Andreas Hubere6c40962009-09-10 14:13:30 -07003554////////////////////////////////////////////////////////////////////////////////
3555
3556status_t QueryCodecs(
3557 const sp<IOMX> &omx,
3558 const char *mime, bool queryDecoders,
3559 Vector<CodecCapabilities> *results) {
3560 results->clear();
3561
3562 for (int index = 0;; ++index) {
3563 const char *componentName;
3564
3565 if (!queryDecoders) {
3566 componentName = GetCodec(
3567 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3568 mime, index);
3569 } else {
3570 componentName = GetCodec(
3571 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3572 mime, index);
3573 }
3574
3575 if (!componentName) {
3576 return OK;
3577 }
3578
Andreas Huber1a189a82010-03-24 13:49:20 -07003579 if (strncmp(componentName, "OMX.", 4)) {
3580 // Not an OpenMax component but a software codec.
3581
3582 results->push();
3583 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3584 caps->mComponentName = componentName;
3585
3586 continue;
3587 }
3588
Andreas Huber784202e2009-10-15 13:46:54 -07003589 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003590 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003591 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003592
3593 if (err != OK) {
3594 continue;
3595 }
3596
James Dong722d5912010-04-13 10:56:59 -07003597 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003598
3599 results->push();
3600 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3601 caps->mComponentName = componentName;
3602
3603 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3604 InitOMXParams(&param);
3605
3606 param.nPortIndex = queryDecoders ? 0 : 1;
3607
3608 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003609 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003610 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3611 &param, sizeof(param));
3612
3613 if (err != OK) {
3614 break;
3615 }
3616
3617 CodecProfileLevel profileLevel;
3618 profileLevel.mProfile = param.eProfile;
3619 profileLevel.mLevel = param.eLevel;
3620
3621 caps->mProfileLevels.push(profileLevel);
3622 }
3623
Andreas Huber784202e2009-10-15 13:46:54 -07003624 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003625 }
3626}
3627
Andreas Huberbe06d262009-08-14 14:37:10 -07003628} // namespace android