blob: 4648ad3a23fa5d15716f099e9672b36f469c2eda [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
Andreas Huber4c19bf92010-09-08 14:32:20 -0700507 err = codec->configureCodec(meta, flags);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700508
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 Huber4c19bf92010-09-08 14:32:20 -0700520status_t OMXCodec::configureCodec(const sp<MetaData> &meta, uint32_t flags) {
521 if (!(flags & kIgnoreCodecSpecificData)) {
522 uint32_t type;
523 const void *data;
524 size_t size;
525 if (meta->findData(kKeyESDS, &type, &data, &size)) {
526 ESDS esds((const char *)data, size);
527 CHECK_EQ(esds.InitCheck(), OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700528
Andreas Huber4c19bf92010-09-08 14:32:20 -0700529 const void *codec_specific_data;
530 size_t codec_specific_data_size;
531 esds.getCodecSpecificInfo(
532 &codec_specific_data, &codec_specific_data_size);
Andreas Huberbe06d262009-08-14 14:37:10 -0700533
Andreas Huber4c19bf92010-09-08 14:32:20 -0700534 addCodecSpecificData(
535 codec_specific_data, codec_specific_data_size);
536 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
537 // Parse the AVCDecoderConfigurationRecord
Andreas Huberebf66ea2009-08-19 13:32:58 -0700538
Andreas Huber4c19bf92010-09-08 14:32:20 -0700539 const uint8_t *ptr = (const uint8_t *)data;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700540
Andreas Huber4c19bf92010-09-08 14:32:20 -0700541 CHECK(size >= 7);
542 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
543 uint8_t profile = ptr[1];
544 uint8_t level = ptr[3];
Andreas Huberebf66ea2009-08-19 13:32:58 -0700545
Andreas Huber4c19bf92010-09-08 14:32:20 -0700546 // There is decodable content out there that fails the following
547 // assertion, let's be lenient for now...
548 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700549
Andreas Huber4c19bf92010-09-08 14:32:20 -0700550 size_t lengthSize = 1 + (ptr[4] & 3);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700551
Andreas Huber4c19bf92010-09-08 14:32:20 -0700552 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
553 // violates it...
554 // CHECK((ptr[5] >> 5) == 7); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700555
Andreas Huber4c19bf92010-09-08 14:32:20 -0700556 size_t numSeqParameterSets = ptr[5] & 31;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700557
Andreas Huber4c19bf92010-09-08 14:32:20 -0700558 ptr += 6;
559 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700560
Andreas Huber4c19bf92010-09-08 14:32:20 -0700561 for (size_t i = 0; i < numSeqParameterSets; ++i) {
562 CHECK(size >= 2);
563 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700564
Andreas Huber4c19bf92010-09-08 14:32:20 -0700565 ptr += 2;
566 size -= 2;
Andreas Huberbe06d262009-08-14 14:37:10 -0700567
Andreas Huber4c19bf92010-09-08 14:32:20 -0700568 CHECK(size >= length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700569
Andreas Huber4c19bf92010-09-08 14:32:20 -0700570 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700571
Andreas Huber4c19bf92010-09-08 14:32:20 -0700572 ptr += length;
573 size -= length;
574 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700575
Andreas Huber4c19bf92010-09-08 14:32:20 -0700576 CHECK(size >= 1);
577 size_t numPictureParameterSets = *ptr;
578 ++ptr;
579 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700580
Andreas Huber4c19bf92010-09-08 14:32:20 -0700581 for (size_t i = 0; i < numPictureParameterSets; ++i) {
582 CHECK(size >= 2);
583 size_t length = U16_AT(ptr);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700584
Andreas Huber4c19bf92010-09-08 14:32:20 -0700585 ptr += 2;
586 size -= 2;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700587
Andreas Huber4c19bf92010-09-08 14:32:20 -0700588 CHECK(size >= length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700589
Andreas Huber4c19bf92010-09-08 14:32:20 -0700590 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700591
Andreas Huber4c19bf92010-09-08 14:32:20 -0700592 ptr += length;
593 size -= length;
594 }
Andreas Huberebf66ea2009-08-19 13:32:58 -0700595
Andreas Huber4c19bf92010-09-08 14:32:20 -0700596 CODEC_LOGV(
597 "AVC profile = %d (%s), level = %d",
598 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700599
Andreas Huber4c19bf92010-09-08 14:32:20 -0700600 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
601 && (profile != kAVCProfileBaseline || level > 30)) {
602 // This stream exceeds the decoder's capabilities. The decoder
603 // does not handle this gracefully and would clobber the heap
604 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700605
Andreas Huber4c19bf92010-09-08 14:32:20 -0700606 LOGE("Profile and/or level exceed the decoder's capabilities.");
607 return ERROR_UNSUPPORTED;
608 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700609 }
610 }
611
James Dong17299ab2010-05-14 15:45:22 -0700612 int32_t bitRate = 0;
613 if (mIsEncoder) {
614 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
615 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700616 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700617 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700618 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700619 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700620 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700621 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700622 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700623 int32_t numChannels, sampleRate;
624 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
625 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
626
James Dong17299ab2010-05-14 15:45:22 -0700627 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700628 }
James Dongabed93a2010-04-22 17:27:04 -0700629
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700630 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700631
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700632 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700633 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700634 } else {
James Dong1244eab2010-06-08 11:58:53 -0700635 int32_t width, height;
636 bool success = meta->findInt32(kKeyWidth, &width);
637 success = success && meta->findInt32(kKeyHeight, &height);
638 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700639 status_t err = setVideoOutputFormat(
640 mMIME, width, height);
641
642 if (err != OK) {
643 return err;
644 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700645 }
646 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700647
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700648 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
649 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700650 OMX_COLOR_FORMATTYPE format =
651 OMX_COLOR_Format32bitARGB8888;
652 // OMX_COLOR_FormatYUV420PackedPlanar;
653 // OMX_COLOR_FormatCbYCrY;
654 // OMX_COLOR_FormatYUV411Planar;
655
656 int32_t width, height;
657 bool success = meta->findInt32(kKeyWidth, &width);
658 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700659
660 int32_t compressedSize;
661 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700662 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700663
664 CHECK(success);
665 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700666
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700667 setImageOutputFormat(format, width, height);
668 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700669 }
670
Andreas Huberda050cf22009-09-02 14:01:43 -0700671 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800672 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700673 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700674 }
675
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700676 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700677 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
678 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700679 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700680 }
681
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700682 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700683
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700684 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700685}
686
Andreas Huberda050cf22009-09-02 14:01:43 -0700687void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
688 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700689 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700690 def.nPortIndex = portIndex;
691
Andreas Huber784202e2009-10-15 13:46:54 -0700692 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700693 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
694 CHECK_EQ(err, OK);
695
Andreas Huberb8de9572010-02-22 14:58:45 -0800696 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
697 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700698 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700699 }
700
Andreas Huber784202e2009-10-15 13:46:54 -0700701 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700702 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
703 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800704
705 err = mOMX->getParameter(
706 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
707 CHECK_EQ(err, OK);
708
709 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800710 if (portIndex == kPortIndexInput
711 && (mQuirks & kInputBufferSizesAreBogus)) {
712 CHECK_EQ(def.nBufferSize, size);
713 } else {
714 CHECK(def.nBufferSize >= size);
715 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700716}
717
Andreas Huberbe06d262009-08-14 14:37:10 -0700718status_t OMXCodec::setVideoPortFormatType(
719 OMX_U32 portIndex,
720 OMX_VIDEO_CODINGTYPE compressionFormat,
721 OMX_COLOR_FORMATTYPE colorFormat) {
722 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700723 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700724 format.nPortIndex = portIndex;
725 format.nIndex = 0;
726 bool found = false;
727
728 OMX_U32 index = 0;
729 for (;;) {
730 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700731 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700732 mNode, OMX_IndexParamVideoPortFormat,
733 &format, sizeof(format));
734
735 if (err != OK) {
736 return err;
737 }
738
739 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700740 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700741
742#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700743 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700744 portIndex,
745 index, format.eCompressionFormat, format.eColorFormat);
746#endif
747
748 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
749 if (portIndex == kPortIndexInput
750 && colorFormat == format.eColorFormat) {
751 // eCompressionFormat does not seem right.
752 found = true;
753 break;
754 }
755 if (portIndex == kPortIndexOutput
756 && compressionFormat == format.eCompressionFormat) {
757 // eColorFormat does not seem right.
758 found = true;
759 break;
760 }
761 }
762
763 if (format.eCompressionFormat == compressionFormat
764 && format.eColorFormat == colorFormat) {
765 found = true;
766 break;
767 }
768
769 ++index;
770 }
771
772 if (!found) {
773 return UNKNOWN_ERROR;
774 }
775
Andreas Huber53a76bd2009-10-06 16:20:44 -0700776 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700777 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700778 mNode, OMX_IndexParamVideoPortFormat,
779 &format, sizeof(format));
780
781 return err;
782}
783
Andreas Huberb482ce82009-10-29 12:02:48 -0700784static size_t getFrameSize(
785 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
786 switch (colorFormat) {
787 case OMX_COLOR_FormatYCbYCr:
788 case OMX_COLOR_FormatCbYCrY:
789 return width * height * 2;
790
Andreas Huber71c27d92010-03-19 11:43:15 -0700791 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700792 case OMX_COLOR_FormatYUV420SemiPlanar:
793 return (width * height * 3) / 2;
794
795 default:
796 CHECK(!"Should not be here. Unsupported color format.");
797 break;
798 }
799}
800
James Dongafd97e82010-08-03 17:19:23 -0700801status_t OMXCodec::findTargetColorFormat(
802 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) {
803 LOGV("findTargetColorFormat");
804 CHECK(mIsEncoder);
805
806 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
807 int32_t targetColorFormat;
808 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) {
809 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat;
810 } else {
811 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
812 *colorFormat = OMX_COLOR_FormatYCbYCr;
813 }
814 }
815
816 // Check whether the target color format is supported.
817 return isColorFormatSupported(*colorFormat, kPortIndexInput);
818}
819
820status_t OMXCodec::isColorFormatSupported(
821 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) {
822 LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat));
823
824 // Enumerate all the color formats supported by
825 // the omx component to see whether the given
826 // color format is supported.
827 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
828 InitOMXParams(&portFormat);
829 portFormat.nPortIndex = portIndex;
830 OMX_U32 index = 0;
831 portFormat.nIndex = index;
832 while (true) {
833 if (OMX_ErrorNone != mOMX->getParameter(
834 mNode, OMX_IndexParamVideoPortFormat,
835 &portFormat, sizeof(portFormat))) {
James Dongb5024da2010-09-13 16:30:51 -0700836 break;
James Dongafd97e82010-08-03 17:19:23 -0700837 }
838 // Make sure that omx component does not overwrite
839 // the incremented index (bug 2897413).
840 CHECK_EQ(index, portFormat.nIndex);
841 if ((portFormat.eColorFormat == colorFormat)) {
842 LOGV("Found supported color format: %d", portFormat.eColorFormat);
843 return OK; // colorFormat is supported!
844 }
845 ++index;
846 portFormat.nIndex = index;
847
848 // OMX Spec defines less than 50 color formats
849 // 1000 is more than enough for us to tell whether the omx
850 // component in question is buggy or not.
851 if (index >= 1000) {
852 LOGE("More than %ld color formats are supported???", index);
853 break;
854 }
855 }
James Dongb5024da2010-09-13 16:30:51 -0700856
857 LOGE("color format %d is not supported", colorFormat);
James Dongafd97e82010-08-03 17:19:23 -0700858 return UNKNOWN_ERROR;
859}
860
Andreas Huberbe06d262009-08-14 14:37:10 -0700861void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700862 const char *mime, const sp<MetaData>& meta) {
863
864 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
865 bool success = meta->findInt32(kKeyWidth, &width);
866 success = success && meta->findInt32(kKeyHeight, &height);
867 success = success && meta->findInt32(kKeySampleRate, &frameRate);
868 success = success && meta->findInt32(kKeyBitRate, &bitRate);
869 success = success && meta->findInt32(kKeyStride, &stride);
870 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
871 CHECK(success);
872 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700873
874 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700875 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700876 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700877 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700878 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700879 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700880 compressionFormat = OMX_VIDEO_CodingH263;
881 } else {
882 LOGE("Not a supported video mime type: %s", mime);
883 CHECK(!"Should not be here. Not a supported video mime type.");
884 }
885
James Dongafd97e82010-08-03 17:19:23 -0700886 OMX_COLOR_FORMATTYPE colorFormat;
887 CHECK_EQ(OK, findTargetColorFormat(meta, &colorFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -0700888
James Dongb00e2462010-04-26 17:48:26 -0700889 status_t err;
890 OMX_PARAM_PORTDEFINITIONTYPE def;
891 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
892
893 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700894 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700895 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700896 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700897
James Dongb00e2462010-04-26 17:48:26 -0700898 InitOMXParams(&def);
899 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700900
James Dongb00e2462010-04-26 17:48:26 -0700901 err = mOMX->getParameter(
902 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
903 CHECK_EQ(err, OK);
904
James Dong1244eab2010-06-08 11:58:53 -0700905 def.nBufferSize = getFrameSize(colorFormat,
906 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700907
908 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
909
910 video_def->nFrameWidth = width;
911 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700912 video_def->nStride = stride;
913 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700914 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700915 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
916 video_def->eColorFormat = colorFormat;
917
James Dongb00e2462010-04-26 17:48:26 -0700918 err = mOMX->setParameter(
919 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
920 CHECK_EQ(err, OK);
921
922 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700923 CHECK_EQ(setVideoPortFormatType(
924 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
925 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700926 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700927 def.nPortIndex = kPortIndexOutput;
928
James Dongb00e2462010-04-26 17:48:26 -0700929 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700930 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
931
932 CHECK_EQ(err, OK);
933 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
934
935 video_def->nFrameWidth = width;
936 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700937 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700938 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700939 video_def->eCompressionFormat = compressionFormat;
940 video_def->eColorFormat = OMX_COLOR_FormatUnused;
James Dong90862e22010-08-26 19:12:59 -0700941 if (mQuirks & kRequiresLargerEncoderOutputBuffer) {
942 // Increases the output buffer size
943 def.nBufferSize = ((def.nBufferSize * 3) >> 1);
944 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700945
Andreas Huber784202e2009-10-15 13:46:54 -0700946 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700947 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
948 CHECK_EQ(err, OK);
949
James Dongb00e2462010-04-26 17:48:26 -0700950 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700951 switch (compressionFormat) {
952 case OMX_VIDEO_CodingMPEG4:
953 {
James Dong1244eab2010-06-08 11:58:53 -0700954 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700955 break;
956 }
957
958 case OMX_VIDEO_CodingH263:
James Dongc0ab2a62010-06-29 16:29:19 -0700959 CHECK_EQ(setupH263EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700960 break;
961
Andreas Huberea6a38c2009-11-16 15:43:38 -0800962 case OMX_VIDEO_CodingAVC:
963 {
James Dong1244eab2010-06-08 11:58:53 -0700964 CHECK_EQ(setupAVCEncoderParameters(meta), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -0800965 break;
966 }
967
Andreas Huberb482ce82009-10-29 12:02:48 -0700968 default:
969 CHECK(!"Support for this compressionFormat to be implemented.");
970 break;
971 }
972}
973
James Dong1244eab2010-06-08 11:58:53 -0700974static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
975 if (iFramesInterval < 0) {
976 return 0xFFFFFFFF;
977 } else if (iFramesInterval == 0) {
978 return 0;
979 }
980 OMX_U32 ret = frameRate * iFramesInterval;
981 CHECK(ret > 1);
982 return ret;
983}
984
James Dongc0ab2a62010-06-29 16:29:19 -0700985status_t OMXCodec::setupErrorCorrectionParameters() {
986 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
987 InitOMXParams(&errorCorrectionType);
988 errorCorrectionType.nPortIndex = kPortIndexOutput;
989
990 status_t err = mOMX->getParameter(
991 mNode, OMX_IndexParamVideoErrorCorrection,
992 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -0700993 if (err != OK) {
994 LOGW("Error correction param query is not supported");
995 return OK; // Optional feature. Ignore this failure
996 }
James Dongc0ab2a62010-06-29 16:29:19 -0700997
998 errorCorrectionType.bEnableHEC = OMX_FALSE;
999 errorCorrectionType.bEnableResync = OMX_TRUE;
1000 errorCorrectionType.nResynchMarkerSpacing = 256;
1001 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
1002 errorCorrectionType.bEnableRVLC = OMX_FALSE;
1003
1004 err = mOMX->setParameter(
1005 mNode, OMX_IndexParamVideoErrorCorrection,
1006 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001007 if (err != OK) {
1008 LOGW("Error correction param configuration is not supported");
1009 }
1010
1011 // Optional feature. Ignore the failure.
James Dongc0ab2a62010-06-29 16:29:19 -07001012 return OK;
1013}
1014
1015status_t OMXCodec::setupBitRate(int32_t bitRate) {
1016 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
1017 InitOMXParams(&bitrateType);
1018 bitrateType.nPortIndex = kPortIndexOutput;
1019
1020 status_t err = mOMX->getParameter(
1021 mNode, OMX_IndexParamVideoBitrate,
1022 &bitrateType, sizeof(bitrateType));
1023 CHECK_EQ(err, OK);
1024
1025 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
1026 bitrateType.nTargetBitrate = bitRate;
1027
1028 err = mOMX->setParameter(
1029 mNode, OMX_IndexParamVideoBitrate,
1030 &bitrateType, sizeof(bitrateType));
1031 CHECK_EQ(err, OK);
1032 return OK;
1033}
1034
James Dong81c929a2010-07-01 15:02:14 -07001035status_t OMXCodec::getVideoProfileLevel(
1036 const sp<MetaData>& meta,
1037 const CodecProfileLevel& defaultProfileLevel,
1038 CodecProfileLevel &profileLevel) {
1039 CODEC_LOGV("Default profile: %ld, level %ld",
1040 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
1041
1042 // Are the default profile and level overwriten?
1043 int32_t profile, level;
1044 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
1045 profile = defaultProfileLevel.mProfile;
1046 }
1047 if (!meta->findInt32(kKeyVideoLevel, &level)) {
1048 level = defaultProfileLevel.mLevel;
1049 }
1050 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
1051
1052 // Are the target profile and level supported by the encoder?
1053 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
1054 InitOMXParams(&param);
1055 param.nPortIndex = kPortIndexOutput;
1056 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
1057 status_t err = mOMX->getParameter(
1058 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
1059 &param, sizeof(param));
1060
James Dongdfb89912010-09-15 21:07:52 -07001061 if (err != OK) break;
James Dong81c929a2010-07-01 15:02:14 -07001062
1063 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
1064 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -07001065 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -07001066 supportedProfile, supportedLevel);
1067
1068 if (profile == supportedProfile &&
James Dongdfb89912010-09-15 21:07:52 -07001069 level <= supportedLevel) {
1070 // We can further check whether the level is a valid
1071 // value; but we will leave that to the omx encoder component
1072 // via OMX_SetParameter call.
James Dong81c929a2010-07-01 15:02:14 -07001073 profileLevel.mProfile = profile;
1074 profileLevel.mLevel = level;
1075 return OK;
1076 }
1077 }
1078
1079 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
1080 profile, level);
1081 return BAD_VALUE;
1082}
1083
James Dongc0ab2a62010-06-29 16:29:19 -07001084status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
1085 int32_t iFramesInterval, frameRate, bitRate;
1086 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1087 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1088 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1089 CHECK(success);
1090 OMX_VIDEO_PARAM_H263TYPE h263type;
1091 InitOMXParams(&h263type);
1092 h263type.nPortIndex = kPortIndexOutput;
1093
1094 status_t err = mOMX->getParameter(
1095 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1096 CHECK_EQ(err, OK);
1097
1098 h263type.nAllowedPictureTypes =
1099 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1100
1101 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1102 if (h263type.nPFrames == 0) {
1103 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1104 }
1105 h263type.nBFrames = 0;
1106
James Dong81c929a2010-07-01 15:02:14 -07001107 // Check profile and level parameters
1108 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001109 defaultProfileLevel.mProfile = h263type.eProfile;
1110 defaultProfileLevel.mLevel = h263type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001111 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1112 if (err != OK) return err;
1113 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1114 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001115
1116 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1117 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1118 h263type.nPictureHeaderRepetition = 0;
1119 h263type.nGOBHeaderInterval = 0;
1120
1121 err = mOMX->setParameter(
1122 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1123 CHECK_EQ(err, OK);
1124
1125 CHECK_EQ(setupBitRate(bitRate), OK);
1126 CHECK_EQ(setupErrorCorrectionParameters(), OK);
1127
1128 return OK;
1129}
1130
James Dong1244eab2010-06-08 11:58:53 -07001131status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1132 int32_t iFramesInterval, frameRate, bitRate;
1133 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1134 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1135 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1136 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001137 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1138 InitOMXParams(&mpeg4type);
1139 mpeg4type.nPortIndex = kPortIndexOutput;
1140
1141 status_t err = mOMX->getParameter(
1142 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1143 CHECK_EQ(err, OK);
1144
1145 mpeg4type.nSliceHeaderSpacing = 0;
1146 mpeg4type.bSVH = OMX_FALSE;
1147 mpeg4type.bGov = OMX_FALSE;
1148
1149 mpeg4type.nAllowedPictureTypes =
1150 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1151
James Dong1244eab2010-06-08 11:58:53 -07001152 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1153 if (mpeg4type.nPFrames == 0) {
1154 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1155 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001156 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001157 mpeg4type.nIDCVLCThreshold = 0;
1158 mpeg4type.bACPred = OMX_TRUE;
1159 mpeg4type.nMaxPacketSize = 256;
1160 mpeg4type.nTimeIncRes = 1000;
1161 mpeg4type.nHeaderExtension = 0;
1162 mpeg4type.bReversibleVLC = OMX_FALSE;
1163
James Dong81c929a2010-07-01 15:02:14 -07001164 // Check profile and level parameters
1165 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001166 defaultProfileLevel.mProfile = mpeg4type.eProfile;
1167 defaultProfileLevel.mLevel = mpeg4type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001168 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1169 if (err != OK) return err;
1170 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1171 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001172
1173 err = mOMX->setParameter(
1174 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1175 CHECK_EQ(err, OK);
1176
James Dongc0ab2a62010-06-29 16:29:19 -07001177 CHECK_EQ(setupBitRate(bitRate), OK);
1178 CHECK_EQ(setupErrorCorrectionParameters(), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001179
1180 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001181}
1182
James Dong1244eab2010-06-08 11:58:53 -07001183status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1184 int32_t iFramesInterval, frameRate, bitRate;
1185 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1186 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1187 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1188 CHECK(success);
1189
Andreas Huberea6a38c2009-11-16 15:43:38 -08001190 OMX_VIDEO_PARAM_AVCTYPE h264type;
1191 InitOMXParams(&h264type);
1192 h264type.nPortIndex = kPortIndexOutput;
1193
1194 status_t err = mOMX->getParameter(
1195 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1196 CHECK_EQ(err, OK);
1197
1198 h264type.nAllowedPictureTypes =
1199 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1200
1201 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001202 h264type.nBFrames = 0; // No B frames support yet
1203 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1204 if (h264type.nPFrames == 0) {
1205 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1206 }
James Dong81c929a2010-07-01 15:02:14 -07001207
1208 // Check profile and level parameters
1209 CodecProfileLevel defaultProfileLevel, profileLevel;
1210 defaultProfileLevel.mProfile = h264type.eProfile;
1211 defaultProfileLevel.mLevel = h264type.eLevel;
1212 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1213 if (err != OK) return err;
1214 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1215 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1216
1217 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1218 h264type.bUseHadamard = OMX_TRUE;
1219 h264type.nRefFrames = 1;
1220 h264type.nRefIdx10ActiveMinus1 = 0;
1221 h264type.nRefIdx11ActiveMinus1 = 0;
1222 h264type.bEntropyCodingCABAC = OMX_FALSE;
1223 h264type.bWeightedPPrediction = OMX_FALSE;
1224 h264type.bconstIpred = OMX_FALSE;
1225 h264type.bDirect8x8Inference = OMX_FALSE;
1226 h264type.bDirectSpatialTemporal = OMX_FALSE;
1227 h264type.nCabacInitIdc = 0;
1228 }
1229
1230 if (h264type.nBFrames != 0) {
1231 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1232 }
1233
Andreas Huberea6a38c2009-11-16 15:43:38 -08001234 h264type.bEnableUEP = OMX_FALSE;
1235 h264type.bEnableFMO = OMX_FALSE;
1236 h264type.bEnableASO = OMX_FALSE;
1237 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001238 h264type.bFrameMBsOnly = OMX_TRUE;
1239 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001240 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1241
1242 err = mOMX->setParameter(
1243 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1244 CHECK_EQ(err, OK);
1245
James Dongc0ab2a62010-06-29 16:29:19 -07001246 CHECK_EQ(setupBitRate(bitRate), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001247
1248 return OK;
1249}
1250
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001251status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001252 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001253 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001254
Andreas Huberbe06d262009-08-14 14:37:10 -07001255 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001256 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001257 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001258 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001259 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001260 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001261 compressionFormat = OMX_VIDEO_CodingH263;
1262 } else {
1263 LOGE("Not a supported video mime type: %s", mime);
1264 CHECK(!"Should not be here. Not a supported video mime type.");
1265 }
1266
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001267 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001268 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1269
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001270 if (err != OK) {
1271 return err;
1272 }
1273
Andreas Huberbe06d262009-08-14 14:37:10 -07001274#if 1
1275 {
1276 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001277 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001278 format.nPortIndex = kPortIndexOutput;
1279 format.nIndex = 0;
1280
Andreas Huber784202e2009-10-15 13:46:54 -07001281 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001282 mNode, OMX_IndexParamVideoPortFormat,
1283 &format, sizeof(format));
1284 CHECK_EQ(err, OK);
1285 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1286
1287 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1288
1289 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1290 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1291 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1292 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1293
Andreas Huber784202e2009-10-15 13:46:54 -07001294 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001295 mNode, OMX_IndexParamVideoPortFormat,
1296 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001297
1298 if (err != OK) {
1299 return err;
1300 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001301 }
1302#endif
1303
1304 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001305 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001306 def.nPortIndex = kPortIndexInput;
1307
Andreas Huber4c483422009-09-02 16:05:36 -07001308 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1309
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001310 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001311 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1312
1313 CHECK_EQ(err, OK);
1314
1315#if 1
1316 // XXX Need a (much) better heuristic to compute input buffer sizes.
1317 const size_t X = 64 * 1024;
1318 if (def.nBufferSize < X) {
1319 def.nBufferSize = X;
1320 }
1321#endif
1322
1323 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1324
1325 video_def->nFrameWidth = width;
1326 video_def->nFrameHeight = height;
1327
Andreas Huberb482ce82009-10-29 12:02:48 -07001328 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001329 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1330
Andreas Huber784202e2009-10-15 13:46:54 -07001331 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001332 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001333
1334 if (err != OK) {
1335 return err;
1336 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001337
1338 ////////////////////////////////////////////////////////////////////////////
1339
Andreas Huber4c483422009-09-02 16:05:36 -07001340 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001341 def.nPortIndex = kPortIndexOutput;
1342
Andreas Huber784202e2009-10-15 13:46:54 -07001343 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001344 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1345 CHECK_EQ(err, OK);
1346 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1347
1348#if 0
1349 def.nBufferSize =
1350 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1351#endif
1352
1353 video_def->nFrameWidth = width;
1354 video_def->nFrameHeight = height;
1355
Andreas Huber784202e2009-10-15 13:46:54 -07001356 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001357 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001358
1359 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001360}
1361
Andreas Huberbe06d262009-08-14 14:37:10 -07001362OMXCodec::OMXCodec(
1363 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001364 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001365 const char *mime,
1366 const char *componentName,
1367 const sp<MediaSource> &source)
1368 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001369 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001370 mNode(node),
1371 mQuirks(quirks),
1372 mIsEncoder(isEncoder),
1373 mMIME(strdup(mime)),
1374 mComponentName(strdup(componentName)),
1375 mSource(source),
1376 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001377 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001378 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001379 mSignalledEOS(false),
1380 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001381 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001382 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001383 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1384 mTargetTimeUs(-1),
James Dong53d4e0d2010-07-21 14:51:35 -07001385 mSkipTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001386 mLeftOverBuffer(NULL),
1387 mPaused(false) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001388 mPortStatus[kPortIndexInput] = ENABLED;
1389 mPortStatus[kPortIndexOutput] = ENABLED;
1390
Andreas Huber4c483422009-09-02 16:05:36 -07001391 setComponentRole();
1392}
1393
Andreas Hubere6c40962009-09-10 14:13:30 -07001394// static
1395void OMXCodec::setComponentRole(
1396 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1397 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001398 struct MimeToRole {
1399 const char *mime;
1400 const char *decoderRole;
1401 const char *encoderRole;
1402 };
1403
1404 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001405 { MEDIA_MIMETYPE_AUDIO_MPEG,
1406 "audio_decoder.mp3", "audio_encoder.mp3" },
1407 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1408 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1409 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1410 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1411 { MEDIA_MIMETYPE_AUDIO_AAC,
1412 "audio_decoder.aac", "audio_encoder.aac" },
1413 { MEDIA_MIMETYPE_VIDEO_AVC,
1414 "video_decoder.avc", "video_encoder.avc" },
1415 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1416 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1417 { MEDIA_MIMETYPE_VIDEO_H263,
1418 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001419 };
1420
1421 static const size_t kNumMimeToRole =
1422 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1423
1424 size_t i;
1425 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001426 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001427 break;
1428 }
1429 }
1430
1431 if (i == kNumMimeToRole) {
1432 return;
1433 }
1434
1435 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001436 isEncoder ? kMimeToRole[i].encoderRole
1437 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001438
1439 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001440 OMX_PARAM_COMPONENTROLETYPE roleParams;
1441 InitOMXParams(&roleParams);
1442
1443 strncpy((char *)roleParams.cRole,
1444 role, OMX_MAX_STRINGNAME_SIZE - 1);
1445
1446 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1447
Andreas Huber784202e2009-10-15 13:46:54 -07001448 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001449 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001450 &roleParams, sizeof(roleParams));
1451
1452 if (err != OK) {
1453 LOGW("Failed to set standard component role '%s'.", role);
1454 }
1455 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001456}
1457
Andreas Hubere6c40962009-09-10 14:13:30 -07001458void OMXCodec::setComponentRole() {
1459 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1460}
1461
Andreas Huberbe06d262009-08-14 14:37:10 -07001462OMXCodec::~OMXCodec() {
Andreas Huberf98197a2010-09-17 11:49:39 -07001463 mSource.clear();
1464
Andreas Huber4f5e6022009-08-19 09:29:34 -07001465 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001466
Andreas Huber784202e2009-10-15 13:46:54 -07001467 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001468 CHECK_EQ(err, OK);
1469
1470 mNode = NULL;
1471 setState(DEAD);
1472
1473 clearCodecSpecificData();
1474
1475 free(mComponentName);
1476 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001477
Andreas Huberbe06d262009-08-14 14:37:10 -07001478 free(mMIME);
1479 mMIME = NULL;
1480}
1481
1482status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001483 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001484
1485 CHECK_EQ(mState, LOADED);
1486
1487 status_t err;
1488 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001489 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001490 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001491 setState(LOADED_TO_IDLE);
1492 }
1493
1494 err = allocateBuffers();
1495 CHECK_EQ(err, OK);
1496
1497 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001498 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001499 CHECK_EQ(err, OK);
1500
1501 setState(LOADED_TO_IDLE);
1502 }
1503
1504 while (mState != EXECUTING && mState != ERROR) {
1505 mAsyncCompletion.wait(mLock);
1506 }
1507
1508 return mState == ERROR ? UNKNOWN_ERROR : OK;
1509}
1510
1511// static
1512bool OMXCodec::isIntermediateState(State state) {
1513 return state == LOADED_TO_IDLE
1514 || state == IDLE_TO_EXECUTING
1515 || state == EXECUTING_TO_IDLE
1516 || state == IDLE_TO_LOADED
1517 || state == RECONFIGURING;
1518}
1519
1520status_t OMXCodec::allocateBuffers() {
1521 status_t err = allocateBuffersOnPort(kPortIndexInput);
1522
1523 if (err != OK) {
1524 return err;
1525 }
1526
1527 return allocateBuffersOnPort(kPortIndexOutput);
1528}
1529
1530status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1531 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001532 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001533 def.nPortIndex = portIndex;
1534
Andreas Huber784202e2009-10-15 13:46:54 -07001535 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001536 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1537
1538 if (err != OK) {
1539 return err;
1540 }
1541
Andreas Huber57648e42010-08-04 10:14:30 -07001542 CODEC_LOGI("allocating %lu buffers of size %lu on %s port",
1543 def.nBufferCountActual, def.nBufferSize,
1544 portIndex == kPortIndexInput ? "input" : "output");
1545
Andreas Huber5c0a9132009-08-20 11:16:40 -07001546 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001547 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001548
Andreas Huberbe06d262009-08-14 14:37:10 -07001549 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001550 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001551 CHECK(mem.get() != NULL);
1552
Andreas Huberc712b9f2010-01-20 15:05:46 -08001553 BufferInfo info;
1554 info.mData = NULL;
1555 info.mSize = def.nBufferSize;
1556
Andreas Huberbe06d262009-08-14 14:37:10 -07001557 IOMX::buffer_id buffer;
1558 if (portIndex == kPortIndexInput
1559 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001560 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001561 mem.clear();
1562
Andreas Huberf1fe0642010-01-15 15:28:19 -08001563 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001564 mNode, portIndex, def.nBufferSize, &buffer,
1565 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001566 } else {
1567 err = mOMX->allocateBufferWithBackup(
1568 mNode, portIndex, mem, &buffer);
1569 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001570 } else if (portIndex == kPortIndexOutput
1571 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001572 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001573 mem.clear();
1574
Andreas Huberf1fe0642010-01-15 15:28:19 -08001575 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001576 mNode, portIndex, def.nBufferSize, &buffer,
1577 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001578 } else {
1579 err = mOMX->allocateBufferWithBackup(
1580 mNode, portIndex, mem, &buffer);
1581 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001582 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001583 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001584 }
1585
1586 if (err != OK) {
1587 LOGE("allocate_buffer_with_backup failed");
1588 return err;
1589 }
1590
Andreas Huberc712b9f2010-01-20 15:05:46 -08001591 if (mem != NULL) {
1592 info.mData = mem->pointer();
1593 }
1594
Andreas Huberbe06d262009-08-14 14:37:10 -07001595 info.mBuffer = buffer;
1596 info.mOwnedByComponent = false;
1597 info.mMem = mem;
1598 info.mMediaBuffer = NULL;
1599
1600 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001601 if (!(mOMXLivesLocally
1602 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1603 && (mQuirks & kDefersOutputBufferAllocation))) {
1604 // If the node does not fill in the buffer ptr at this time,
1605 // we will defer creating the MediaBuffer until receiving
1606 // the first FILL_BUFFER_DONE notification instead.
1607 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1608 info.mMediaBuffer->setObserver(this);
1609 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001610 }
1611
1612 mPortBuffers[portIndex].push(info);
1613
Andreas Huber4c483422009-09-02 16:05:36 -07001614 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001615 portIndex == kPortIndexInput ? "input" : "output");
1616 }
1617
Andreas Huber2ea14e22009-12-16 09:30:55 -08001618 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001619
1620 return OK;
1621}
1622
1623void OMXCodec::on_message(const omx_message &msg) {
1624 Mutex::Autolock autoLock(mLock);
1625
1626 switch (msg.type) {
1627 case omx_message::EVENT:
1628 {
1629 onEvent(
1630 msg.u.event_data.event, msg.u.event_data.data1,
1631 msg.u.event_data.data2);
1632
1633 break;
1634 }
1635
1636 case omx_message::EMPTY_BUFFER_DONE:
1637 {
1638 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1639
Andreas Huber4c483422009-09-02 16:05:36 -07001640 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001641
1642 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1643 size_t i = 0;
1644 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1645 ++i;
1646 }
1647
1648 CHECK(i < buffers->size());
1649 if (!(*buffers)[i].mOwnedByComponent) {
1650 LOGW("We already own input buffer %p, yet received "
1651 "an EMPTY_BUFFER_DONE.", buffer);
1652 }
1653
1654 buffers->editItemAt(i).mOwnedByComponent = false;
1655
1656 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001657 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001658
1659 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001660 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001661 CHECK_EQ(err, OK);
1662
1663 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001664 } else if (mState != ERROR
1665 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001666 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1667 drainInputBuffer(&buffers->editItemAt(i));
1668 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001669 break;
1670 }
1671
1672 case omx_message::FILL_BUFFER_DONE:
1673 {
1674 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1675 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1676
Andreas Huber2ea14e22009-12-16 09:30:55 -08001677 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001678 buffer,
1679 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001680 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001681 msg.u.extended_buffer_data.timestamp,
1682 msg.u.extended_buffer_data.timestamp / 1E6);
1683
1684 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1685 size_t i = 0;
1686 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1687 ++i;
1688 }
1689
1690 CHECK(i < buffers->size());
1691 BufferInfo *info = &buffers->editItemAt(i);
1692
1693 if (!info->mOwnedByComponent) {
1694 LOGW("We already own output buffer %p, yet received "
1695 "a FILL_BUFFER_DONE.", buffer);
1696 }
1697
1698 info->mOwnedByComponent = false;
1699
1700 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001701 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001702
1703 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001704 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001705 CHECK_EQ(err, OK);
1706
1707 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001708#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001709 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1710 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001711 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001712 mNoMoreOutputData = true;
1713 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001714#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001715 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1716 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001717
Andreas Huber52733b82010-01-25 10:41:35 -08001718 if (info->mMediaBuffer == NULL) {
1719 CHECK(mOMXLivesLocally);
1720 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1721 CHECK(mQuirks & kDefersOutputBufferAllocation);
1722
1723 // The qcom video decoders on Nexus don't actually allocate
1724 // output buffer memory on a call to OMX_AllocateBuffer
1725 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1726 // structure is only filled in later.
1727
1728 info->mMediaBuffer = new MediaBuffer(
1729 msg.u.extended_buffer_data.data_ptr,
1730 info->mSize);
1731 info->mMediaBuffer->setObserver(this);
1732 }
1733
Andreas Huberbe06d262009-08-14 14:37:10 -07001734 MediaBuffer *buffer = info->mMediaBuffer;
1735
Andreas Huberf88f8442010-08-10 11:18:36 -07001736 if (msg.u.extended_buffer_data.range_offset
1737 + msg.u.extended_buffer_data.range_length
1738 > buffer->size()) {
1739 CODEC_LOGE(
1740 "Codec lied about its buffer size requirements, "
1741 "sending a buffer larger than the originally "
1742 "advertised size in FILL_BUFFER_DONE!");
1743 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001744 buffer->set_range(
1745 msg.u.extended_buffer_data.range_offset,
1746 msg.u.extended_buffer_data.range_length);
1747
1748 buffer->meta_data()->clear();
1749
Andreas Huberfa8de752009-10-08 10:07:49 -07001750 buffer->meta_data()->setInt64(
1751 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001752
1753 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1754 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1755 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001756 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1757 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1758 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001759
Andreas Huber1e194162010-10-06 16:43:57 -07001760 if (mQuirks & kOutputBuffersAreUnreadable) {
1761 buffer->meta_data()->setInt32(kKeyIsUnreadable, true);
1762 }
1763
Andreas Huberbe06d262009-08-14 14:37:10 -07001764 buffer->meta_data()->setPointer(
1765 kKeyPlatformPrivate,
1766 msg.u.extended_buffer_data.platform_private);
1767
1768 buffer->meta_data()->setPointer(
1769 kKeyBufferID,
1770 msg.u.extended_buffer_data.buffer);
1771
Andreas Huber2ea14e22009-12-16 09:30:55 -08001772 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1773 CODEC_LOGV("No more output data.");
1774 mNoMoreOutputData = true;
1775 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07001776
1777 if (mTargetTimeUs >= 0) {
1778 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
1779
1780 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
1781 CODEC_LOGV(
1782 "skipping output buffer at timestamp %lld us",
1783 msg.u.extended_buffer_data.timestamp);
1784
1785 fillOutputBuffer(info);
1786 break;
1787 }
1788
1789 CODEC_LOGV(
1790 "returning output buffer at target timestamp "
1791 "%lld us",
1792 msg.u.extended_buffer_data.timestamp);
1793
1794 mTargetTimeUs = -1;
1795 }
1796
1797 mFilledBuffers.push_back(i);
1798 mBufferFilled.signal();
Andreas Huberbe06d262009-08-14 14:37:10 -07001799 }
1800
1801 break;
1802 }
1803
1804 default:
1805 {
1806 CHECK(!"should not be here.");
1807 break;
1808 }
1809 }
1810}
1811
1812void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1813 switch (event) {
1814 case OMX_EventCmdComplete:
1815 {
1816 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1817 break;
1818 }
1819
1820 case OMX_EventError:
1821 {
Andreas Huberaf0a1882010-09-21 15:08:52 -07001822 CODEC_LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001823
1824 setState(ERROR);
1825 break;
1826 }
1827
1828 case OMX_EventPortSettingsChanged:
1829 {
1830 onPortSettingsChanged(data1);
1831 break;
1832 }
1833
Andreas Huber2ea14e22009-12-16 09:30:55 -08001834#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001835 case OMX_EventBufferFlag:
1836 {
Andreas Huber4c483422009-09-02 16:05:36 -07001837 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001838
1839 if (data1 == kPortIndexOutput) {
1840 mNoMoreOutputData = true;
1841 }
1842 break;
1843 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001844#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001845
1846 default:
1847 {
Andreas Huber4c483422009-09-02 16:05:36 -07001848 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001849 break;
1850 }
1851 }
1852}
1853
Andreas Huberb1678602009-10-19 13:06:40 -07001854// Has the format changed in any way that the client would have to be aware of?
1855static bool formatHasNotablyChanged(
1856 const sp<MetaData> &from, const sp<MetaData> &to) {
1857 if (from.get() == NULL && to.get() == NULL) {
1858 return false;
1859 }
1860
Andreas Huberf68c1682009-10-21 14:01:30 -07001861 if ((from.get() == NULL && to.get() != NULL)
1862 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001863 return true;
1864 }
1865
1866 const char *mime_from, *mime_to;
1867 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1868 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1869
1870 if (strcasecmp(mime_from, mime_to)) {
1871 return true;
1872 }
1873
1874 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1875 int32_t colorFormat_from, colorFormat_to;
1876 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1877 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1878
1879 if (colorFormat_from != colorFormat_to) {
1880 return true;
1881 }
1882
1883 int32_t width_from, width_to;
1884 CHECK(from->findInt32(kKeyWidth, &width_from));
1885 CHECK(to->findInt32(kKeyWidth, &width_to));
1886
1887 if (width_from != width_to) {
1888 return true;
1889 }
1890
1891 int32_t height_from, height_to;
1892 CHECK(from->findInt32(kKeyHeight, &height_from));
1893 CHECK(to->findInt32(kKeyHeight, &height_to));
1894
1895 if (height_from != height_to) {
1896 return true;
1897 }
1898 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1899 int32_t numChannels_from, numChannels_to;
1900 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1901 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1902
1903 if (numChannels_from != numChannels_to) {
1904 return true;
1905 }
1906
1907 int32_t sampleRate_from, sampleRate_to;
1908 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1909 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1910
1911 if (sampleRate_from != sampleRate_to) {
1912 return true;
1913 }
1914 }
1915
1916 return false;
1917}
1918
Andreas Huberbe06d262009-08-14 14:37:10 -07001919void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1920 switch (cmd) {
1921 case OMX_CommandStateSet:
1922 {
1923 onStateChange((OMX_STATETYPE)data);
1924 break;
1925 }
1926
1927 case OMX_CommandPortDisable:
1928 {
1929 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001930 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001931
1932 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1933 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1934 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1935
1936 mPortStatus[portIndex] = DISABLED;
1937
1938 if (mState == RECONFIGURING) {
1939 CHECK_EQ(portIndex, kPortIndexOutput);
1940
Andreas Huberb1678602009-10-19 13:06:40 -07001941 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001942 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001943
1944 // Don't notify clients if the output port settings change
1945 // wasn't of importance to them, i.e. it may be that just the
1946 // number of buffers has changed and nothing else.
1947 mOutputPortSettingsHaveChanged =
1948 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001949
Andreas Huberbe06d262009-08-14 14:37:10 -07001950 enablePortAsync(portIndex);
1951
1952 status_t err = allocateBuffersOnPort(portIndex);
1953 CHECK_EQ(err, OK);
1954 }
1955 break;
1956 }
1957
1958 case OMX_CommandPortEnable:
1959 {
1960 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001961 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001962
1963 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1964 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1965
1966 mPortStatus[portIndex] = ENABLED;
1967
1968 if (mState == RECONFIGURING) {
1969 CHECK_EQ(portIndex, kPortIndexOutput);
1970
1971 setState(EXECUTING);
1972
1973 fillOutputBuffers();
1974 }
1975 break;
1976 }
1977
1978 case OMX_CommandFlush:
1979 {
1980 OMX_U32 portIndex = data;
1981
Andreas Huber4c483422009-09-02 16:05:36 -07001982 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001983
1984 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1985 mPortStatus[portIndex] = ENABLED;
1986
1987 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1988 mPortBuffers[portIndex].size());
1989
1990 if (mState == RECONFIGURING) {
1991 CHECK_EQ(portIndex, kPortIndexOutput);
1992
1993 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001994 } else if (mState == EXECUTING_TO_IDLE) {
1995 if (mPortStatus[kPortIndexInput] == ENABLED
1996 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001997 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001998 "transition from EXECUTING to IDLE.");
1999
2000 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2001 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2002
2003 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002004 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002005 CHECK_EQ(err, OK);
2006 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002007 } else {
2008 // We're flushing both ports in preparation for seeking.
2009
2010 if (mPortStatus[kPortIndexInput] == ENABLED
2011 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002012 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07002013 " seek-time.");
2014
Andreas Huber1f24b302010-06-10 11:12:39 -07002015 // We implicitly resume pulling on our upstream source.
2016 mPaused = false;
2017
Andreas Huberbe06d262009-08-14 14:37:10 -07002018 drainInputBuffers();
2019 fillOutputBuffers();
2020 }
2021 }
2022
2023 break;
2024 }
2025
2026 default:
2027 {
Andreas Huber4c483422009-09-02 16:05:36 -07002028 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07002029 break;
2030 }
2031 }
2032}
2033
2034void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002035 CODEC_LOGV("onStateChange %d", newState);
2036
Andreas Huberbe06d262009-08-14 14:37:10 -07002037 switch (newState) {
2038 case OMX_StateIdle:
2039 {
Andreas Huber4c483422009-09-02 16:05:36 -07002040 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002041 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07002042 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002043 mNode, OMX_CommandStateSet, OMX_StateExecuting);
2044
2045 CHECK_EQ(err, OK);
2046
2047 setState(IDLE_TO_EXECUTING);
2048 } else {
2049 CHECK_EQ(mState, EXECUTING_TO_IDLE);
2050
2051 CHECK_EQ(
2052 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
2053 mPortBuffers[kPortIndexInput].size());
2054
2055 CHECK_EQ(
2056 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
2057 mPortBuffers[kPortIndexOutput].size());
2058
Andreas Huber784202e2009-10-15 13:46:54 -07002059 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002060 mNode, OMX_CommandStateSet, OMX_StateLoaded);
2061
2062 CHECK_EQ(err, OK);
2063
2064 err = freeBuffersOnPort(kPortIndexInput);
2065 CHECK_EQ(err, OK);
2066
2067 err = freeBuffersOnPort(kPortIndexOutput);
2068 CHECK_EQ(err, OK);
2069
2070 mPortStatus[kPortIndexInput] = ENABLED;
2071 mPortStatus[kPortIndexOutput] = ENABLED;
2072
2073 setState(IDLE_TO_LOADED);
2074 }
2075 break;
2076 }
2077
2078 case OMX_StateExecuting:
2079 {
2080 CHECK_EQ(mState, IDLE_TO_EXECUTING);
2081
Andreas Huber4c483422009-09-02 16:05:36 -07002082 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002083
2084 setState(EXECUTING);
2085
Andreas Huber42978e52009-08-27 10:08:39 -07002086 // Buffers will be submitted to the component in the first
2087 // call to OMXCodec::read as mInitialBufferSubmit is true at
2088 // this point. This ensures that this on_message call returns,
2089 // releases the lock and ::init can notice the state change and
2090 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07002091 break;
2092 }
2093
2094 case OMX_StateLoaded:
2095 {
2096 CHECK_EQ(mState, IDLE_TO_LOADED);
2097
Andreas Huber4c483422009-09-02 16:05:36 -07002098 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002099
2100 setState(LOADED);
2101 break;
2102 }
2103
Andreas Huberc712b9f2010-01-20 15:05:46 -08002104 case OMX_StateInvalid:
2105 {
2106 setState(ERROR);
2107 break;
2108 }
2109
Andreas Huberbe06d262009-08-14 14:37:10 -07002110 default:
2111 {
2112 CHECK(!"should not be here.");
2113 break;
2114 }
2115 }
2116}
2117
2118// static
2119size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
2120 size_t n = 0;
2121 for (size_t i = 0; i < buffers.size(); ++i) {
2122 if (!buffers[i].mOwnedByComponent) {
2123 ++n;
2124 }
2125 }
2126
2127 return n;
2128}
2129
2130status_t OMXCodec::freeBuffersOnPort(
2131 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2132 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2133
2134 status_t stickyErr = OK;
2135
2136 for (size_t i = buffers->size(); i-- > 0;) {
2137 BufferInfo *info = &buffers->editItemAt(i);
2138
2139 if (onlyThoseWeOwn && info->mOwnedByComponent) {
2140 continue;
2141 }
2142
2143 CHECK_EQ(info->mOwnedByComponent, false);
2144
Andreas Huber92022852009-09-14 15:24:14 -07002145 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2146
Andreas Huberbe06d262009-08-14 14:37:10 -07002147 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002148 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07002149
2150 if (err != OK) {
2151 stickyErr = err;
2152 }
2153
2154 if (info->mMediaBuffer != NULL) {
2155 info->mMediaBuffer->setObserver(NULL);
2156
2157 // Make sure nobody but us owns this buffer at this point.
2158 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2159
2160 info->mMediaBuffer->release();
2161 }
2162
2163 buffers->removeAt(i);
2164 }
2165
2166 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2167
2168 return stickyErr;
2169}
2170
2171void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002172 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002173
2174 CHECK_EQ(mState, EXECUTING);
2175 CHECK_EQ(portIndex, kPortIndexOutput);
2176 setState(RECONFIGURING);
2177
2178 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002179 if (!flushPortAsync(portIndex)) {
2180 onCmdComplete(OMX_CommandFlush, portIndex);
2181 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002182 } else {
2183 disablePortAsync(portIndex);
2184 }
2185}
2186
Andreas Huber404cc412009-08-25 14:26:05 -07002187bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002188 CHECK(mState == EXECUTING || mState == RECONFIGURING
2189 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002190
Andreas Huber4c483422009-09-02 16:05:36 -07002191 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002192 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2193 mPortBuffers[portIndex].size());
2194
Andreas Huberbe06d262009-08-14 14:37:10 -07002195 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2196 mPortStatus[portIndex] = SHUTTING_DOWN;
2197
Andreas Huber404cc412009-08-25 14:26:05 -07002198 if ((mQuirks & kRequiresFlushCompleteEmulation)
2199 && countBuffersWeOwn(mPortBuffers[portIndex])
2200 == mPortBuffers[portIndex].size()) {
2201 // No flush is necessary and this component fails to send a
2202 // flush-complete event in this case.
2203
2204 return false;
2205 }
2206
Andreas Huberbe06d262009-08-14 14:37:10 -07002207 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002208 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002209 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002210
2211 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002212}
2213
2214void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2215 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2216
2217 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2218 mPortStatus[portIndex] = DISABLING;
2219
2220 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002221 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002222 CHECK_EQ(err, OK);
2223
2224 freeBuffersOnPort(portIndex, true);
2225}
2226
2227void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2228 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2229
2230 CHECK_EQ(mPortStatus[portIndex], DISABLED);
2231 mPortStatus[portIndex] = ENABLING;
2232
2233 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002234 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002235 CHECK_EQ(err, OK);
2236}
2237
2238void OMXCodec::fillOutputBuffers() {
2239 CHECK_EQ(mState, EXECUTING);
2240
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002241 // This is a workaround for some decoders not properly reporting
2242 // end-of-output-stream. If we own all input buffers and also own
2243 // all output buffers and we already signalled end-of-input-stream,
2244 // the end-of-output-stream is implied.
2245 if (mSignalledEOS
2246 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2247 == mPortBuffers[kPortIndexInput].size()
2248 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2249 == mPortBuffers[kPortIndexOutput].size()) {
2250 mNoMoreOutputData = true;
2251 mBufferFilled.signal();
2252
2253 return;
2254 }
2255
Andreas Huberbe06d262009-08-14 14:37:10 -07002256 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2257 for (size_t i = 0; i < buffers->size(); ++i) {
2258 fillOutputBuffer(&buffers->editItemAt(i));
2259 }
2260}
2261
2262void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002263 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002264
2265 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2266 for (size_t i = 0; i < buffers->size(); ++i) {
2267 drainInputBuffer(&buffers->editItemAt(i));
2268 }
2269}
2270
2271void OMXCodec::drainInputBuffer(BufferInfo *info) {
2272 CHECK_EQ(info->mOwnedByComponent, false);
2273
2274 if (mSignalledEOS) {
2275 return;
2276 }
2277
2278 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2279 const CodecSpecificData *specific =
2280 mCodecSpecificData[mCodecSpecificDataIndex];
2281
2282 size_t size = specific->mSize;
2283
Andreas Hubere6c40962009-09-10 14:13:30 -07002284 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002285 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002286 static const uint8_t kNALStartCode[4] =
2287 { 0x00, 0x00, 0x00, 0x01 };
2288
Andreas Huberc712b9f2010-01-20 15:05:46 -08002289 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002290
2291 size += 4;
2292
Andreas Huberc712b9f2010-01-20 15:05:46 -08002293 memcpy(info->mData, kNALStartCode, 4);
2294 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002295 specific->mData, specific->mSize);
2296 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002297 CHECK(info->mSize >= specific->mSize);
2298 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002299 }
2300
Andreas Huber2ea14e22009-12-16 09:30:55 -08002301 mNoMoreOutputData = false;
2302
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002303 CODEC_LOGV("calling emptyBuffer with codec specific data");
2304
Andreas Huber784202e2009-10-15 13:46:54 -07002305 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002306 mNode, info->mBuffer, 0, size,
2307 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2308 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002309 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002310
2311 info->mOwnedByComponent = true;
2312
2313 ++mCodecSpecificDataIndex;
2314 return;
2315 }
2316
Andreas Huber1f24b302010-06-10 11:12:39 -07002317 if (mPaused) {
2318 return;
2319 }
2320
Andreas Huberbe06d262009-08-14 14:37:10 -07002321 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002322
Andreas Hubera4357ad2010-04-02 12:49:54 -07002323 bool signalEOS = false;
2324 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002325
Andreas Hubera4357ad2010-04-02 12:49:54 -07002326 size_t offset = 0;
2327 int32_t n = 0;
2328 for (;;) {
2329 MediaBuffer *srcBuffer;
James Dong53d4e0d2010-07-21 14:51:35 -07002330 MediaSource::ReadOptions options;
2331 if (mSkipTimeUs >= 0) {
2332 options.setSkipFrame(mSkipTimeUs);
2333 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002334 if (mSeekTimeUs >= 0) {
2335 if (mLeftOverBuffer) {
2336 mLeftOverBuffer->release();
2337 mLeftOverBuffer = NULL;
2338 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002339 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002340
2341 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002342 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002343 mBufferFilled.signal();
2344
2345 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002346
2347 if (err == OK) {
2348 int64_t targetTimeUs;
2349 if (srcBuffer->meta_data()->findInt64(
2350 kKeyTargetTime, &targetTimeUs)
2351 && targetTimeUs >= 0) {
2352 mTargetTimeUs = targetTimeUs;
2353 } else {
2354 mTargetTimeUs = -1;
2355 }
2356 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002357 } else if (mLeftOverBuffer) {
2358 srcBuffer = mLeftOverBuffer;
2359 mLeftOverBuffer = NULL;
2360
2361 err = OK;
2362 } else {
James Dong53d4e0d2010-07-21 14:51:35 -07002363 err = mSource->read(&srcBuffer, &options);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002364 }
2365
2366 if (err != OK) {
2367 signalEOS = true;
2368 mFinalStatus = err;
2369 mSignalledEOS = true;
2370 break;
2371 }
2372
2373 size_t remainingBytes = info->mSize - offset;
2374
2375 if (srcBuffer->range_length() > remainingBytes) {
2376 if (offset == 0) {
2377 CODEC_LOGE(
2378 "Codec's input buffers are too small to accomodate "
2379 "buffer read from source (info->mSize = %d, srcLength = %d)",
2380 info->mSize, srcBuffer->range_length());
2381
2382 srcBuffer->release();
2383 srcBuffer = NULL;
2384
2385 setState(ERROR);
2386 return;
2387 }
2388
2389 mLeftOverBuffer = srcBuffer;
2390 break;
2391 }
2392
James Dong4f501f02010-06-07 14:41:41 -07002393 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2394 CHECK(mOMXLivesLocally && offset == 0);
2395 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2396 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2397 } else {
2398 memcpy((uint8_t *)info->mData + offset,
2399 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2400 srcBuffer->range_length());
2401 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002402
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002403 int64_t lastBufferTimeUs;
2404 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002405 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002406
Andreas Hubera4357ad2010-04-02 12:49:54 -07002407 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002408 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002409 }
2410
2411 offset += srcBuffer->range_length();
2412
2413 srcBuffer->release();
2414 srcBuffer = NULL;
2415
2416 ++n;
2417
2418 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2419 break;
2420 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002421
2422 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2423
2424 if (coalescedDurationUs > 250000ll) {
2425 // Don't coalesce more than 250ms worth of encoded data at once.
2426 break;
2427 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002428 }
2429
2430 if (n > 1) {
2431 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002432 }
2433
2434 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002435
Andreas Hubera4357ad2010-04-02 12:49:54 -07002436 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002437 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002438 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002439 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002440 }
2441
Andreas Hubera4357ad2010-04-02 12:49:54 -07002442 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2443 "timestamp %lld us (%.2f secs)",
2444 info->mBuffer, offset,
2445 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002446
Andreas Huber784202e2009-10-15 13:46:54 -07002447 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002448 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002449 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002450
2451 if (err != OK) {
2452 setState(ERROR);
2453 return;
2454 }
2455
2456 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002457
2458 // This component does not ever signal the EOS flag on output buffers,
2459 // Thanks for nothing.
2460 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2461 mNoMoreOutputData = true;
2462 mBufferFilled.signal();
2463 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002464}
2465
2466void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2467 CHECK_EQ(info->mOwnedByComponent, false);
2468
Andreas Huber404cc412009-08-25 14:26:05 -07002469 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002470 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002471 "calling fillOutputBuffer");
2472 return;
2473 }
2474
Andreas Huber4c483422009-09-02 16:05:36 -07002475 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002476 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002477
2478 if (err != OK) {
2479 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2480
2481 setState(ERROR);
2482 return;
2483 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002484
2485 info->mOwnedByComponent = true;
2486}
2487
2488void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2489 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2490 for (size_t i = 0; i < buffers->size(); ++i) {
2491 if ((*buffers)[i].mBuffer == buffer) {
2492 drainInputBuffer(&buffers->editItemAt(i));
2493 return;
2494 }
2495 }
2496
2497 CHECK(!"should not be here.");
2498}
2499
2500void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2501 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2502 for (size_t i = 0; i < buffers->size(); ++i) {
2503 if ((*buffers)[i].mBuffer == buffer) {
2504 fillOutputBuffer(&buffers->editItemAt(i));
2505 return;
2506 }
2507 }
2508
2509 CHECK(!"should not be here.");
2510}
2511
2512void OMXCodec::setState(State newState) {
2513 mState = newState;
2514 mAsyncCompletion.signal();
2515
2516 // This may cause some spurious wakeups but is necessary to
2517 // unblock the reader if we enter ERROR state.
2518 mBufferFilled.signal();
2519}
2520
Andreas Huberda050cf22009-09-02 14:01:43 -07002521void OMXCodec::setRawAudioFormat(
2522 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002523
2524 // port definition
2525 OMX_PARAM_PORTDEFINITIONTYPE def;
2526 InitOMXParams(&def);
2527 def.nPortIndex = portIndex;
2528 status_t err = mOMX->getParameter(
2529 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2530 CHECK_EQ(err, OK);
2531 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2532 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2533 &def, sizeof(def)), OK);
2534
2535 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002536 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002537 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002538 pcmParams.nPortIndex = portIndex;
2539
James Dongabed93a2010-04-22 17:27:04 -07002540 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002541 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2542
2543 CHECK_EQ(err, OK);
2544
2545 pcmParams.nChannels = numChannels;
2546 pcmParams.eNumData = OMX_NumericalDataSigned;
2547 pcmParams.bInterleaved = OMX_TRUE;
2548 pcmParams.nBitPerSample = 16;
2549 pcmParams.nSamplingRate = sampleRate;
2550 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2551
2552 if (numChannels == 1) {
2553 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2554 } else {
2555 CHECK_EQ(numChannels, 2);
2556
2557 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2558 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2559 }
2560
Andreas Huber784202e2009-10-15 13:46:54 -07002561 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002562 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2563
2564 CHECK_EQ(err, OK);
2565}
2566
James Dong17299ab2010-05-14 15:45:22 -07002567static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2568 if (isAMRWB) {
2569 if (bps <= 6600) {
2570 return OMX_AUDIO_AMRBandModeWB0;
2571 } else if (bps <= 8850) {
2572 return OMX_AUDIO_AMRBandModeWB1;
2573 } else if (bps <= 12650) {
2574 return OMX_AUDIO_AMRBandModeWB2;
2575 } else if (bps <= 14250) {
2576 return OMX_AUDIO_AMRBandModeWB3;
2577 } else if (bps <= 15850) {
2578 return OMX_AUDIO_AMRBandModeWB4;
2579 } else if (bps <= 18250) {
2580 return OMX_AUDIO_AMRBandModeWB5;
2581 } else if (bps <= 19850) {
2582 return OMX_AUDIO_AMRBandModeWB6;
2583 } else if (bps <= 23050) {
2584 return OMX_AUDIO_AMRBandModeWB7;
2585 }
2586
2587 // 23850 bps
2588 return OMX_AUDIO_AMRBandModeWB8;
2589 } else { // AMRNB
2590 if (bps <= 4750) {
2591 return OMX_AUDIO_AMRBandModeNB0;
2592 } else if (bps <= 5150) {
2593 return OMX_AUDIO_AMRBandModeNB1;
2594 } else if (bps <= 5900) {
2595 return OMX_AUDIO_AMRBandModeNB2;
2596 } else if (bps <= 6700) {
2597 return OMX_AUDIO_AMRBandModeNB3;
2598 } else if (bps <= 7400) {
2599 return OMX_AUDIO_AMRBandModeNB4;
2600 } else if (bps <= 7950) {
2601 return OMX_AUDIO_AMRBandModeNB5;
2602 } else if (bps <= 10200) {
2603 return OMX_AUDIO_AMRBandModeNB6;
2604 }
2605
2606 // 12200 bps
2607 return OMX_AUDIO_AMRBandModeNB7;
2608 }
2609}
2610
2611void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002612 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002613
Andreas Huber8768f2c2009-12-01 15:26:54 -08002614 OMX_AUDIO_PARAM_AMRTYPE def;
2615 InitOMXParams(&def);
2616 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002617
Andreas Huber8768f2c2009-12-01 15:26:54 -08002618 status_t err =
2619 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002620
Andreas Huber8768f2c2009-12-01 15:26:54 -08002621 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002622
Andreas Huber8768f2c2009-12-01 15:26:54 -08002623 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002624
James Dong17299ab2010-05-14 15:45:22 -07002625 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002626 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2627 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002628
2629 ////////////////////////
2630
2631 if (mIsEncoder) {
2632 sp<MetaData> format = mSource->getFormat();
2633 int32_t sampleRate;
2634 int32_t numChannels;
2635 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2636 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2637
2638 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2639 }
2640}
2641
James Dong17299ab2010-05-14 15:45:22 -07002642void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002643 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002644 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002645 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002646 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002647
2648 //////////////// output port ////////////////////
2649 // format
2650 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2651 format.nPortIndex = kPortIndexOutput;
2652 format.nIndex = 0;
2653 status_t err = OMX_ErrorNone;
2654 while (OMX_ErrorNone == err) {
2655 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2656 &format, sizeof(format)), OK);
2657 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2658 break;
2659 }
2660 format.nIndex++;
2661 }
2662 CHECK_EQ(OK, err);
2663 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2664 &format, sizeof(format)), OK);
2665
2666 // port definition
2667 OMX_PARAM_PORTDEFINITIONTYPE def;
2668 InitOMXParams(&def);
2669 def.nPortIndex = kPortIndexOutput;
2670 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2671 &def, sizeof(def)), OK);
2672 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2673 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2674 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2675 &def, sizeof(def)), OK);
2676
2677 // profile
2678 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2679 InitOMXParams(&profile);
2680 profile.nPortIndex = kPortIndexOutput;
2681 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2682 &profile, sizeof(profile)), OK);
2683 profile.nChannels = numChannels;
2684 profile.eChannelMode = (numChannels == 1?
2685 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2686 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002687 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002688 profile.nAudioBandWidth = 0;
2689 profile.nFrameLength = 0;
2690 profile.nAACtools = OMX_AUDIO_AACToolAll;
2691 profile.nAACERtools = OMX_AUDIO_AACERNone;
2692 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2693 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2694 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2695 &profile, sizeof(profile)), OK);
2696
Andreas Huberda050cf22009-09-02 14:01:43 -07002697 } else {
2698 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002699 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002700 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002701
Andreas Huber784202e2009-10-15 13:46:54 -07002702 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002703 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2704 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002705
Andreas Huberda050cf22009-09-02 14:01:43 -07002706 profile.nChannels = numChannels;
2707 profile.nSampleRate = sampleRate;
2708 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002709
Andreas Huber784202e2009-10-15 13:46:54 -07002710 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002711 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2712 CHECK_EQ(err, OK);
2713 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002714}
2715
2716void OMXCodec::setImageOutputFormat(
2717 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002718 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002719
2720#if 0
2721 OMX_INDEXTYPE index;
2722 status_t err = mOMX->get_extension_index(
2723 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2724 CHECK_EQ(err, OK);
2725
2726 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2727 CHECK_EQ(err, OK);
2728#endif
2729
2730 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002731 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002732 def.nPortIndex = kPortIndexOutput;
2733
Andreas Huber784202e2009-10-15 13:46:54 -07002734 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002735 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2736 CHECK_EQ(err, OK);
2737
2738 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2739
2740 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002741
Andreas Huberbe06d262009-08-14 14:37:10 -07002742 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2743 imageDef->eColorFormat = format;
2744 imageDef->nFrameWidth = width;
2745 imageDef->nFrameHeight = height;
2746
2747 switch (format) {
2748 case OMX_COLOR_FormatYUV420PackedPlanar:
2749 case OMX_COLOR_FormatYUV411Planar:
2750 {
2751 def.nBufferSize = (width * height * 3) / 2;
2752 break;
2753 }
2754
2755 case OMX_COLOR_FormatCbYCrY:
2756 {
2757 def.nBufferSize = width * height * 2;
2758 break;
2759 }
2760
2761 case OMX_COLOR_Format32bitARGB8888:
2762 {
2763 def.nBufferSize = width * height * 4;
2764 break;
2765 }
2766
Andreas Huber201511c2009-09-08 14:01:44 -07002767 case OMX_COLOR_Format16bitARGB4444:
2768 case OMX_COLOR_Format16bitARGB1555:
2769 case OMX_COLOR_Format16bitRGB565:
2770 case OMX_COLOR_Format16bitBGR565:
2771 {
2772 def.nBufferSize = width * height * 2;
2773 break;
2774 }
2775
Andreas Huberbe06d262009-08-14 14:37:10 -07002776 default:
2777 CHECK(!"Should not be here. Unknown color format.");
2778 break;
2779 }
2780
Andreas Huber5c0a9132009-08-20 11:16:40 -07002781 def.nBufferCountActual = def.nBufferCountMin;
2782
Andreas Huber784202e2009-10-15 13:46:54 -07002783 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002784 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2785 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002786}
Andreas Huberbe06d262009-08-14 14:37:10 -07002787
Andreas Huber5c0a9132009-08-20 11:16:40 -07002788void OMXCodec::setJPEGInputFormat(
2789 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2790 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002791 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002792 def.nPortIndex = kPortIndexInput;
2793
Andreas Huber784202e2009-10-15 13:46:54 -07002794 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002795 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2796 CHECK_EQ(err, OK);
2797
Andreas Huber5c0a9132009-08-20 11:16:40 -07002798 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2799 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2800
Andreas Huberbe06d262009-08-14 14:37:10 -07002801 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2802 imageDef->nFrameWidth = width;
2803 imageDef->nFrameHeight = height;
2804
Andreas Huber5c0a9132009-08-20 11:16:40 -07002805 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002806 def.nBufferCountActual = def.nBufferCountMin;
2807
Andreas Huber784202e2009-10-15 13:46:54 -07002808 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002809 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2810 CHECK_EQ(err, OK);
2811}
2812
2813void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2814 CodecSpecificData *specific =
2815 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2816
2817 specific->mSize = size;
2818 memcpy(specific->mData, data, size);
2819
2820 mCodecSpecificData.push(specific);
2821}
2822
2823void OMXCodec::clearCodecSpecificData() {
2824 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2825 free(mCodecSpecificData.editItemAt(i));
2826 }
2827 mCodecSpecificData.clear();
2828 mCodecSpecificDataIndex = 0;
2829}
2830
James Dong36e573b2010-06-19 09:04:18 -07002831status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07002832 Mutex::Autolock autoLock(mLock);
2833
Andreas Huberbe06d262009-08-14 14:37:10 -07002834 if (mState != LOADED) {
2835 return UNKNOWN_ERROR;
2836 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002837
Andreas Huberbe06d262009-08-14 14:37:10 -07002838 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002839 if (mQuirks & kWantsNALFragments) {
2840 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002841 }
James Dong36e573b2010-06-19 09:04:18 -07002842 if (meta) {
2843 int64_t startTimeUs = 0;
2844 int64_t timeUs;
2845 if (meta->findInt64(kKeyTime, &timeUs)) {
2846 startTimeUs = timeUs;
2847 }
2848 params->setInt64(kKeyTime, startTimeUs);
2849 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002850 status_t err = mSource->start(params.get());
2851
2852 if (err != OK) {
2853 return err;
2854 }
2855
2856 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002857 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002858 mSignalledEOS = false;
2859 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002860 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002861 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002862 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
2863 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07002864 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07002865 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002866
2867 return init();
2868}
2869
2870status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002871 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002872
2873 Mutex::Autolock autoLock(mLock);
2874
2875 while (isIntermediateState(mState)) {
2876 mAsyncCompletion.wait(mLock);
2877 }
2878
2879 switch (mState) {
2880 case LOADED:
2881 case ERROR:
2882 break;
2883
2884 case EXECUTING:
2885 {
2886 setState(EXECUTING_TO_IDLE);
2887
Andreas Huber127fcdc2009-08-26 16:27:02 -07002888 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002889 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002890 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002891
Andreas Huber127fcdc2009-08-26 16:27:02 -07002892 bool emulateInputFlushCompletion =
2893 !flushPortAsync(kPortIndexInput);
2894
2895 bool emulateOutputFlushCompletion =
2896 !flushPortAsync(kPortIndexOutput);
2897
2898 if (emulateInputFlushCompletion) {
2899 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2900 }
2901
2902 if (emulateOutputFlushCompletion) {
2903 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2904 }
2905 } else {
2906 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2907 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2908
2909 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002910 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002911 CHECK_EQ(err, OK);
2912 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002913
2914 while (mState != LOADED && mState != ERROR) {
2915 mAsyncCompletion.wait(mLock);
2916 }
2917
2918 break;
2919 }
2920
2921 default:
2922 {
2923 CHECK(!"should not be here.");
2924 break;
2925 }
2926 }
2927
Andreas Hubera4357ad2010-04-02 12:49:54 -07002928 if (mLeftOverBuffer) {
2929 mLeftOverBuffer->release();
2930 mLeftOverBuffer = NULL;
2931 }
2932
Andreas Huberbe06d262009-08-14 14:37:10 -07002933 mSource->stop();
2934
Andreas Huber4a9375e2010-02-09 11:54:33 -08002935 CODEC_LOGV("stopped");
2936
Andreas Huberbe06d262009-08-14 14:37:10 -07002937 return OK;
2938}
2939
2940sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002941 Mutex::Autolock autoLock(mLock);
2942
Andreas Huberbe06d262009-08-14 14:37:10 -07002943 return mOutputFormat;
2944}
2945
2946status_t OMXCodec::read(
2947 MediaBuffer **buffer, const ReadOptions *options) {
2948 *buffer = NULL;
2949
2950 Mutex::Autolock autoLock(mLock);
2951
Andreas Huberd06e5b82009-08-28 13:18:14 -07002952 if (mState != EXECUTING && mState != RECONFIGURING) {
2953 return UNKNOWN_ERROR;
2954 }
2955
Andreas Hubere981c332009-10-22 13:49:30 -07002956 bool seeking = false;
2957 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002958 ReadOptions::SeekMode seekMode;
2959 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07002960 seeking = true;
2961 }
James Dong53d4e0d2010-07-21 14:51:35 -07002962 int64_t skipTimeUs;
2963 if (options && options->getSkipFrame(&skipTimeUs)) {
2964 mSkipTimeUs = skipTimeUs;
2965 } else {
2966 mSkipTimeUs = -1;
2967 }
Andreas Hubere981c332009-10-22 13:49:30 -07002968
Andreas Huber42978e52009-08-27 10:08:39 -07002969 if (mInitialBufferSubmit) {
2970 mInitialBufferSubmit = false;
2971
Andreas Hubere981c332009-10-22 13:49:30 -07002972 if (seeking) {
2973 CHECK(seekTimeUs >= 0);
2974 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002975 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07002976
2977 // There's no reason to trigger the code below, there's
2978 // nothing to flush yet.
2979 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002980 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07002981 }
2982
Andreas Huber42978e52009-08-27 10:08:39 -07002983 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002984
Andreas Huberd06e5b82009-08-28 13:18:14 -07002985 if (mState == EXECUTING) {
2986 // Otherwise mState == RECONFIGURING and this code will trigger
2987 // after the output port is reenabled.
2988 fillOutputBuffers();
2989 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002990 }
2991
Andreas Hubere981c332009-10-22 13:49:30 -07002992 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002993 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002994
2995 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002996
2997 CHECK(seekTimeUs >= 0);
2998 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002999 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07003000
3001 mFilledBuffers.clear();
3002
3003 CHECK_EQ(mState, EXECUTING);
3004
Andreas Huber404cc412009-08-25 14:26:05 -07003005 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
3006 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
3007
3008 if (emulateInputFlushCompletion) {
3009 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3010 }
3011
3012 if (emulateOutputFlushCompletion) {
3013 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3014 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08003015
3016 while (mSeekTimeUs >= 0) {
3017 mBufferFilled.wait(mLock);
3018 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003019 }
3020
3021 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
3022 mBufferFilled.wait(mLock);
3023 }
3024
3025 if (mState == ERROR) {
3026 return UNKNOWN_ERROR;
3027 }
3028
3029 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08003030 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07003031 }
3032
Andreas Hubercfd55572009-10-09 14:11:28 -07003033 if (mOutputPortSettingsHaveChanged) {
3034 mOutputPortSettingsHaveChanged = false;
3035
3036 return INFO_FORMAT_CHANGED;
3037 }
3038
Andreas Huberbe06d262009-08-14 14:37:10 -07003039 size_t index = *mFilledBuffers.begin();
3040 mFilledBuffers.erase(mFilledBuffers.begin());
3041
3042 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
3043 info->mMediaBuffer->add_ref();
3044 *buffer = info->mMediaBuffer;
3045
3046 return OK;
3047}
3048
3049void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
3050 Mutex::Autolock autoLock(mLock);
3051
3052 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
3053 for (size_t i = 0; i < buffers->size(); ++i) {
3054 BufferInfo *info = &buffers->editItemAt(i);
3055
3056 if (info->mMediaBuffer == buffer) {
3057 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
3058 fillOutputBuffer(info);
3059 return;
3060 }
3061 }
3062
3063 CHECK(!"should not be here.");
3064}
3065
3066static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
3067 static const char *kNames[] = {
3068 "OMX_IMAGE_CodingUnused",
3069 "OMX_IMAGE_CodingAutoDetect",
3070 "OMX_IMAGE_CodingJPEG",
3071 "OMX_IMAGE_CodingJPEG2K",
3072 "OMX_IMAGE_CodingEXIF",
3073 "OMX_IMAGE_CodingTIFF",
3074 "OMX_IMAGE_CodingGIF",
3075 "OMX_IMAGE_CodingPNG",
3076 "OMX_IMAGE_CodingLZW",
3077 "OMX_IMAGE_CodingBMP",
3078 };
3079
3080 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3081
3082 if (type < 0 || (size_t)type >= numNames) {
3083 return "UNKNOWN";
3084 } else {
3085 return kNames[type];
3086 }
3087}
3088
3089static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
3090 static const char *kNames[] = {
3091 "OMX_COLOR_FormatUnused",
3092 "OMX_COLOR_FormatMonochrome",
3093 "OMX_COLOR_Format8bitRGB332",
3094 "OMX_COLOR_Format12bitRGB444",
3095 "OMX_COLOR_Format16bitARGB4444",
3096 "OMX_COLOR_Format16bitARGB1555",
3097 "OMX_COLOR_Format16bitRGB565",
3098 "OMX_COLOR_Format16bitBGR565",
3099 "OMX_COLOR_Format18bitRGB666",
3100 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003101 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07003102 "OMX_COLOR_Format24bitRGB888",
3103 "OMX_COLOR_Format24bitBGR888",
3104 "OMX_COLOR_Format24bitARGB1887",
3105 "OMX_COLOR_Format25bitARGB1888",
3106 "OMX_COLOR_Format32bitBGRA8888",
3107 "OMX_COLOR_Format32bitARGB8888",
3108 "OMX_COLOR_FormatYUV411Planar",
3109 "OMX_COLOR_FormatYUV411PackedPlanar",
3110 "OMX_COLOR_FormatYUV420Planar",
3111 "OMX_COLOR_FormatYUV420PackedPlanar",
3112 "OMX_COLOR_FormatYUV420SemiPlanar",
3113 "OMX_COLOR_FormatYUV422Planar",
3114 "OMX_COLOR_FormatYUV422PackedPlanar",
3115 "OMX_COLOR_FormatYUV422SemiPlanar",
3116 "OMX_COLOR_FormatYCbYCr",
3117 "OMX_COLOR_FormatYCrYCb",
3118 "OMX_COLOR_FormatCbYCrY",
3119 "OMX_COLOR_FormatCrYCbY",
3120 "OMX_COLOR_FormatYUV444Interleaved",
3121 "OMX_COLOR_FormatRawBayer8bit",
3122 "OMX_COLOR_FormatRawBayer10bit",
3123 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003124 "OMX_COLOR_FormatL2",
3125 "OMX_COLOR_FormatL4",
3126 "OMX_COLOR_FormatL8",
3127 "OMX_COLOR_FormatL16",
3128 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07003129 "OMX_COLOR_FormatL32",
3130 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
3131 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
3132 "OMX_COLOR_Format18BitBGR666",
3133 "OMX_COLOR_Format24BitARGB6666",
3134 "OMX_COLOR_Format24BitABGR6666",
3135 };
3136
3137 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3138
Andreas Huberbe06d262009-08-14 14:37:10 -07003139 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3140 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3141 } else if (type < 0 || (size_t)type >= numNames) {
3142 return "UNKNOWN";
3143 } else {
3144 return kNames[type];
3145 }
3146}
3147
3148static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3149 static const char *kNames[] = {
3150 "OMX_VIDEO_CodingUnused",
3151 "OMX_VIDEO_CodingAutoDetect",
3152 "OMX_VIDEO_CodingMPEG2",
3153 "OMX_VIDEO_CodingH263",
3154 "OMX_VIDEO_CodingMPEG4",
3155 "OMX_VIDEO_CodingWMV",
3156 "OMX_VIDEO_CodingRV",
3157 "OMX_VIDEO_CodingAVC",
3158 "OMX_VIDEO_CodingMJPEG",
3159 };
3160
3161 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3162
3163 if (type < 0 || (size_t)type >= numNames) {
3164 return "UNKNOWN";
3165 } else {
3166 return kNames[type];
3167 }
3168}
3169
3170static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3171 static const char *kNames[] = {
3172 "OMX_AUDIO_CodingUnused",
3173 "OMX_AUDIO_CodingAutoDetect",
3174 "OMX_AUDIO_CodingPCM",
3175 "OMX_AUDIO_CodingADPCM",
3176 "OMX_AUDIO_CodingAMR",
3177 "OMX_AUDIO_CodingGSMFR",
3178 "OMX_AUDIO_CodingGSMEFR",
3179 "OMX_AUDIO_CodingGSMHR",
3180 "OMX_AUDIO_CodingPDCFR",
3181 "OMX_AUDIO_CodingPDCEFR",
3182 "OMX_AUDIO_CodingPDCHR",
3183 "OMX_AUDIO_CodingTDMAFR",
3184 "OMX_AUDIO_CodingTDMAEFR",
3185 "OMX_AUDIO_CodingQCELP8",
3186 "OMX_AUDIO_CodingQCELP13",
3187 "OMX_AUDIO_CodingEVRC",
3188 "OMX_AUDIO_CodingSMV",
3189 "OMX_AUDIO_CodingG711",
3190 "OMX_AUDIO_CodingG723",
3191 "OMX_AUDIO_CodingG726",
3192 "OMX_AUDIO_CodingG729",
3193 "OMX_AUDIO_CodingAAC",
3194 "OMX_AUDIO_CodingMP3",
3195 "OMX_AUDIO_CodingSBC",
3196 "OMX_AUDIO_CodingVORBIS",
3197 "OMX_AUDIO_CodingWMA",
3198 "OMX_AUDIO_CodingRA",
3199 "OMX_AUDIO_CodingMIDI",
3200 };
3201
3202 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3203
3204 if (type < 0 || (size_t)type >= numNames) {
3205 return "UNKNOWN";
3206 } else {
3207 return kNames[type];
3208 }
3209}
3210
3211static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3212 static const char *kNames[] = {
3213 "OMX_AUDIO_PCMModeLinear",
3214 "OMX_AUDIO_PCMModeALaw",
3215 "OMX_AUDIO_PCMModeMULaw",
3216 };
3217
3218 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3219
3220 if (type < 0 || (size_t)type >= numNames) {
3221 return "UNKNOWN";
3222 } else {
3223 return kNames[type];
3224 }
3225}
3226
Andreas Huber7ae02c82009-09-09 16:29:47 -07003227static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3228 static const char *kNames[] = {
3229 "OMX_AUDIO_AMRBandModeUnused",
3230 "OMX_AUDIO_AMRBandModeNB0",
3231 "OMX_AUDIO_AMRBandModeNB1",
3232 "OMX_AUDIO_AMRBandModeNB2",
3233 "OMX_AUDIO_AMRBandModeNB3",
3234 "OMX_AUDIO_AMRBandModeNB4",
3235 "OMX_AUDIO_AMRBandModeNB5",
3236 "OMX_AUDIO_AMRBandModeNB6",
3237 "OMX_AUDIO_AMRBandModeNB7",
3238 "OMX_AUDIO_AMRBandModeWB0",
3239 "OMX_AUDIO_AMRBandModeWB1",
3240 "OMX_AUDIO_AMRBandModeWB2",
3241 "OMX_AUDIO_AMRBandModeWB3",
3242 "OMX_AUDIO_AMRBandModeWB4",
3243 "OMX_AUDIO_AMRBandModeWB5",
3244 "OMX_AUDIO_AMRBandModeWB6",
3245 "OMX_AUDIO_AMRBandModeWB7",
3246 "OMX_AUDIO_AMRBandModeWB8",
3247 };
3248
3249 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3250
3251 if (type < 0 || (size_t)type >= numNames) {
3252 return "UNKNOWN";
3253 } else {
3254 return kNames[type];
3255 }
3256}
3257
3258static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3259 static const char *kNames[] = {
3260 "OMX_AUDIO_AMRFrameFormatConformance",
3261 "OMX_AUDIO_AMRFrameFormatIF1",
3262 "OMX_AUDIO_AMRFrameFormatIF2",
3263 "OMX_AUDIO_AMRFrameFormatFSF",
3264 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3265 "OMX_AUDIO_AMRFrameFormatITU",
3266 };
3267
3268 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3269
3270 if (type < 0 || (size_t)type >= numNames) {
3271 return "UNKNOWN";
3272 } else {
3273 return kNames[type];
3274 }
3275}
Andreas Huberbe06d262009-08-14 14:37:10 -07003276
3277void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3278 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003279 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003280 def.nPortIndex = portIndex;
3281
Andreas Huber784202e2009-10-15 13:46:54 -07003282 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003283 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3284 CHECK_EQ(err, OK);
3285
3286 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3287
3288 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3289 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3290
3291 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3292 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3293 printf(" nBufferSize = %ld\n", def.nBufferSize);
3294
3295 switch (def.eDomain) {
3296 case OMX_PortDomainImage:
3297 {
3298 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3299
3300 printf("\n");
3301 printf(" // Image\n");
3302 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3303 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3304 printf(" nStride = %ld\n", imageDef->nStride);
3305
3306 printf(" eCompressionFormat = %s\n",
3307 imageCompressionFormatString(imageDef->eCompressionFormat));
3308
3309 printf(" eColorFormat = %s\n",
3310 colorFormatString(imageDef->eColorFormat));
3311
3312 break;
3313 }
3314
3315 case OMX_PortDomainVideo:
3316 {
3317 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3318
3319 printf("\n");
3320 printf(" // Video\n");
3321 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3322 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3323 printf(" nStride = %ld\n", videoDef->nStride);
3324
3325 printf(" eCompressionFormat = %s\n",
3326 videoCompressionFormatString(videoDef->eCompressionFormat));
3327
3328 printf(" eColorFormat = %s\n",
3329 colorFormatString(videoDef->eColorFormat));
3330
3331 break;
3332 }
3333
3334 case OMX_PortDomainAudio:
3335 {
3336 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3337
3338 printf("\n");
3339 printf(" // Audio\n");
3340 printf(" eEncoding = %s\n",
3341 audioCodingTypeString(audioDef->eEncoding));
3342
3343 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3344 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003345 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003346 params.nPortIndex = portIndex;
3347
Andreas Huber784202e2009-10-15 13:46:54 -07003348 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003349 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3350 CHECK_EQ(err, OK);
3351
3352 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3353 printf(" nChannels = %ld\n", params.nChannels);
3354 printf(" bInterleaved = %d\n", params.bInterleaved);
3355 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3356
3357 printf(" eNumData = %s\n",
3358 params.eNumData == OMX_NumericalDataSigned
3359 ? "signed" : "unsigned");
3360
3361 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003362 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3363 OMX_AUDIO_PARAM_AMRTYPE amr;
3364 InitOMXParams(&amr);
3365 amr.nPortIndex = portIndex;
3366
Andreas Huber784202e2009-10-15 13:46:54 -07003367 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003368 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3369 CHECK_EQ(err, OK);
3370
3371 printf(" nChannels = %ld\n", amr.nChannels);
3372 printf(" eAMRBandMode = %s\n",
3373 amrBandModeString(amr.eAMRBandMode));
3374 printf(" eAMRFrameFormat = %s\n",
3375 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003376 }
3377
3378 break;
3379 }
3380
3381 default:
3382 {
3383 printf(" // Unknown\n");
3384 break;
3385 }
3386 }
3387
3388 printf("}\n");
3389}
3390
3391void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3392 mOutputFormat = new MetaData;
3393 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003394 if (mIsEncoder) {
3395 int32_t timeScale;
3396 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3397 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3398 }
3399 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003400
3401 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003402 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003403 def.nPortIndex = kPortIndexOutput;
3404
Andreas Huber784202e2009-10-15 13:46:54 -07003405 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003406 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3407 CHECK_EQ(err, OK);
3408
3409 switch (def.eDomain) {
3410 case OMX_PortDomainImage:
3411 {
3412 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3413 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3414
Andreas Hubere6c40962009-09-10 14:13:30 -07003415 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003416 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3417 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3418 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3419 break;
3420 }
3421
3422 case OMX_PortDomainAudio:
3423 {
3424 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3425
Andreas Huberda050cf22009-09-02 14:01:43 -07003426 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3427 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003428 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003429 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003430
Andreas Huber784202e2009-10-15 13:46:54 -07003431 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003432 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3433 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003434
Andreas Huberda050cf22009-09-02 14:01:43 -07003435 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3436 CHECK_EQ(params.nBitPerSample, 16);
3437 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003438
Andreas Huberda050cf22009-09-02 14:01:43 -07003439 int32_t numChannels, sampleRate;
3440 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3441 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003442
Andreas Huberda050cf22009-09-02 14:01:43 -07003443 if ((OMX_U32)numChannels != params.nChannels) {
3444 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003445 "the input stream contains (contains %d channels, "
3446 "codec outputs %ld channels).",
3447 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003448 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003449
Andreas Hubere6c40962009-09-10 14:13:30 -07003450 mOutputFormat->setCString(
3451 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003452
3453 // Use the codec-advertised number of channels, as some
3454 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003455 // mono. If we know the codec lies about this information,
3456 // use the actual number of channels instead.
3457 mOutputFormat->setInt32(
3458 kKeyChannelCount,
3459 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3460 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003461
3462 // The codec-reported sampleRate is not reliable...
3463 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3464 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003465 OMX_AUDIO_PARAM_AMRTYPE amr;
3466 InitOMXParams(&amr);
3467 amr.nPortIndex = kPortIndexOutput;
3468
Andreas Huber784202e2009-10-15 13:46:54 -07003469 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003470 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3471 CHECK_EQ(err, OK);
3472
3473 CHECK_EQ(amr.nChannels, 1);
3474 mOutputFormat->setInt32(kKeyChannelCount, 1);
3475
3476 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3477 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003478 mOutputFormat->setCString(
3479 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003480 mOutputFormat->setInt32(kKeySampleRate, 8000);
3481 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3482 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003483 mOutputFormat->setCString(
3484 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003485 mOutputFormat->setInt32(kKeySampleRate, 16000);
3486 } else {
3487 CHECK(!"Unknown AMR band mode.");
3488 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003489 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003490 mOutputFormat->setCString(
3491 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003492 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003493 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3494 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003495 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003496 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3497 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003498 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003499 } else {
3500 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003501 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003502 break;
3503 }
3504
3505 case OMX_PortDomainVideo:
3506 {
3507 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3508
3509 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003510 mOutputFormat->setCString(
3511 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003512 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003513 mOutputFormat->setCString(
3514 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003515 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003516 mOutputFormat->setCString(
3517 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003518 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003519 mOutputFormat->setCString(
3520 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003521 } else {
3522 CHECK(!"Unknown compression format.");
3523 }
3524
3525 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3526 // This component appears to be lying to me.
3527 mOutputFormat->setInt32(
3528 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3529 mOutputFormat->setInt32(
3530 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3531 } else {
3532 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3533 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3534 }
3535
3536 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3537 break;
3538 }
3539
3540 default:
3541 {
3542 CHECK(!"should not be here, neither audio nor video.");
3543 break;
3544 }
3545 }
3546}
3547
Andreas Huber1f24b302010-06-10 11:12:39 -07003548status_t OMXCodec::pause() {
3549 Mutex::Autolock autoLock(mLock);
3550
3551 mPaused = true;
3552
3553 return OK;
3554}
3555
Andreas Hubere6c40962009-09-10 14:13:30 -07003556////////////////////////////////////////////////////////////////////////////////
3557
3558status_t QueryCodecs(
3559 const sp<IOMX> &omx,
3560 const char *mime, bool queryDecoders,
3561 Vector<CodecCapabilities> *results) {
3562 results->clear();
3563
3564 for (int index = 0;; ++index) {
3565 const char *componentName;
3566
3567 if (!queryDecoders) {
3568 componentName = GetCodec(
3569 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3570 mime, index);
3571 } else {
3572 componentName = GetCodec(
3573 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3574 mime, index);
3575 }
3576
3577 if (!componentName) {
3578 return OK;
3579 }
3580
Andreas Huber1a189a82010-03-24 13:49:20 -07003581 if (strncmp(componentName, "OMX.", 4)) {
3582 // Not an OpenMax component but a software codec.
3583
3584 results->push();
3585 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3586 caps->mComponentName = componentName;
3587
3588 continue;
3589 }
3590
Andreas Huber784202e2009-10-15 13:46:54 -07003591 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003592 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003593 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003594
3595 if (err != OK) {
3596 continue;
3597 }
3598
James Dong722d5912010-04-13 10:56:59 -07003599 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003600
3601 results->push();
3602 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3603 caps->mComponentName = componentName;
3604
3605 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3606 InitOMXParams(&param);
3607
3608 param.nPortIndex = queryDecoders ? 0 : 1;
3609
3610 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003611 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003612 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3613 &param, sizeof(param));
3614
3615 if (err != OK) {
3616 break;
3617 }
3618
3619 CodecProfileLevel profileLevel;
3620 profileLevel.mProfile = param.eProfile;
3621 profileLevel.mLevel = param.eLevel;
3622
3623 caps->mProfileLevels.push(profileLevel);
3624 }
3625
Andreas Huber784202e2009-10-15 13:46:54 -07003626 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003627 }
3628}
3629
Andreas Huberbe06d262009-08-14 14:37:10 -07003630} // namespace android