blob: 4d69dd351b05487333d79dc09a70f322d779c430 [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" },
Andreas Huberd222c842010-08-26 14:29:34 -0700155// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.Nvidia.mp3.decoder" },
James Dong374aee62010-04-26 10:23:30 -0700156// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800157 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700158// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700159// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberd222c842010-08-26 14:29:34 -0700160// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.Nvidia.amr.decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800161 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700162// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
Andreas Huberd222c842010-08-26 14:29:34 -0700163// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.Nvidia.amrwb.decoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700164 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800165 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700166// { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
Andreas Huberd222c842010-08-26 14:29:34 -0700167// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.Nvidia.aac.decoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700168 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800169 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700170// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
Andreas Huber520b2a72010-08-09 09:54:59 -0700171 { MEDIA_MIMETYPE_AUDIO_G711_ALAW, "G711Decoder" },
172 { MEDIA_MIMETYPE_AUDIO_G711_MLAW, "G711Decoder" },
Andreas Huberd222c842010-08-26 14:29:34 -0700173// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.Nvidia.mp4.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700174 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.decoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700175 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
176 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700177 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800178 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700179// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
Andreas Huberd222c842010-08-26 14:29:34 -0700180// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.Nvidia.h263.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700181 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.decoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700182 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700183 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800184 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700185// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
pgudadhe6ad2c352010-07-26 15:04:33 -0700186 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.Nvidia.h264.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700187 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.decoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700188 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
189 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700190 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800191 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700192// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huber388379f2010-05-07 10:35:13 -0700193 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" },
Andreas Huber47ba30e2010-05-24 14:38:02 -0700194 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700195};
196
197static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700198 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800199 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700200 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700201 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700202 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700203 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700204// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
205 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.encoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700206 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
207 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
James Dong2e87f7b2010-09-23 17:46:34 -0700208 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.Nvidia.mp4.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700209 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700210 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700211// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
212 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.encoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700213 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
214 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
James Dong2e87f7b2010-09-23 17:46:34 -0700215 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.Nvidia.h263.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700216 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700217 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700218// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
219 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.encoder.avc" },
Andreas Huber71c27d92010-03-19 11:43:15 -0700220 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700221 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
pgudadhe9c305322010-07-26 13:59:29 -0700222 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.Nvidia.h264.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700223 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Encoder" },
James Dong1cc31e62010-07-02 17:44:44 -0700224 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700225// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700226};
227
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800228#undef OPTIONAL
229
Andreas Hubere0873732009-09-10 09:57:53 -0700230#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700231#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber42c444a2010-02-09 10:20:00 -0800232#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700233
Andreas Huberbe06d262009-08-14 14:37:10 -0700234struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700235 OMXCodecObserver() {
236 }
237
238 void setCodec(const sp<OMXCodec> &target) {
239 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700240 }
241
242 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700243 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700244 sp<OMXCodec> codec = mTarget.promote();
245
246 if (codec.get() != NULL) {
247 codec->on_message(msg);
248 }
249 }
250
251protected:
252 virtual ~OMXCodecObserver() {}
253
254private:
255 wp<OMXCodec> mTarget;
256
257 OMXCodecObserver(const OMXCodecObserver &);
258 OMXCodecObserver &operator=(const OMXCodecObserver &);
259};
260
261static const char *GetCodec(const CodecInfo *info, size_t numInfos,
262 const char *mime, int index) {
263 CHECK(index >= 0);
264 for(size_t i = 0; i < numInfos; ++i) {
265 if (!strcasecmp(mime, info[i].mime)) {
266 if (index == 0) {
267 return info[i].codec;
268 }
269
270 --index;
271 }
272 }
273
274 return NULL;
275}
276
Andreas Huberebf66ea2009-08-19 13:32:58 -0700277enum {
278 kAVCProfileBaseline = 0x42,
279 kAVCProfileMain = 0x4d,
280 kAVCProfileExtended = 0x58,
281 kAVCProfileHigh = 0x64,
282 kAVCProfileHigh10 = 0x6e,
283 kAVCProfileHigh422 = 0x7a,
284 kAVCProfileHigh444 = 0xf4,
285 kAVCProfileCAVLC444Intra = 0x2c
286};
287
288static const char *AVCProfileToString(uint8_t profile) {
289 switch (profile) {
290 case kAVCProfileBaseline:
291 return "Baseline";
292 case kAVCProfileMain:
293 return "Main";
294 case kAVCProfileExtended:
295 return "Extended";
296 case kAVCProfileHigh:
297 return "High";
298 case kAVCProfileHigh10:
299 return "High 10";
300 case kAVCProfileHigh422:
301 return "High 422";
302 case kAVCProfileHigh444:
303 return "High 444";
304 case kAVCProfileCAVLC444Intra:
305 return "CAVLC 444 Intra";
306 default: return "Unknown";
307 }
308}
309
Andreas Huber4c483422009-09-02 16:05:36 -0700310template<class T>
311static void InitOMXParams(T *params) {
312 params->nSize = sizeof(T);
313 params->nVersion.s.nVersionMajor = 1;
314 params->nVersion.s.nVersionMinor = 0;
315 params->nVersion.s.nRevision = 0;
316 params->nVersion.s.nStep = 0;
317}
318
Andreas Hubere13526a2009-10-22 10:43:34 -0700319static bool IsSoftwareCodec(const char *componentName) {
320 if (!strncmp("OMX.PV.", componentName, 7)) {
321 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700322 }
323
Andreas Hubere13526a2009-10-22 10:43:34 -0700324 return false;
325}
326
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800327// A sort order in which non-OMX components are first,
328// followed by software codecs, i.e. OMX.PV.*, followed
329// by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700330static int CompareSoftwareCodecsFirst(
331 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800332 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
333 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
334
335 if (isNotOMX1) {
336 if (isNotOMX2) { return 0; }
337 return -1;
338 }
339 if (isNotOMX2) {
340 return 1;
341 }
342
Andreas Hubere13526a2009-10-22 10:43:34 -0700343 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
344 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
345
346 if (isSoftwareCodec1) {
347 if (isSoftwareCodec2) { return 0; }
348 return -1;
349 }
350
351 if (isSoftwareCodec2) {
352 return 1;
353 }
354
355 return 0;
356}
357
358// static
359uint32_t OMXCodec::getComponentQuirks(const char *componentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700360 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700361
Dima Zavin30ba6cb2010-08-23 11:10:03 -0700362 if (!strcmp(componentName, "OMX.Nvidia.amr.decoder") ||
363 !strcmp(componentName, "OMX.Nvidia.amrwb.decoder") ||
364 !strcmp(componentName, "OMX.Nvidia.aac.decoder") ||
365 !strcmp(componentName, "OMX.Nvidia.mp3.decoder")) {
366 quirks |= kDecoderLiesAboutNumberOfChannels;
367 }
368
Andreas Huberbe06d262009-08-14 14:37:10 -0700369 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700370 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700371 }
372 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
373 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800374 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700375 }
376 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
377 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700378 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700379 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700380 }
381 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
382 quirks |= kRequiresLoadedToIdleAfterAllocation;
383 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700384 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dong90862e22010-08-26 19:12:59 -0700385 if (!strncmp(componentName, "OMX.qcom.video.encoder.avc", 26)) {
386
387 // The AVC encoder advertises the size of output buffers
388 // based on the input video resolution and assumes
389 // the worst/least compression ratio is 0.5. It is found that
390 // sometimes, the output buffer size is larger than
391 // size advertised by the encoder.
392 quirks |= kRequiresLargerEncoderOutputBuffer;
393 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700394 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700395 if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) {
396 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700397 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700398 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800399 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700400 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700401 if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) {
402 quirks |= kRequiresAllocateBufferOnInputPorts;
403 quirks |= kRequiresAllocateBufferOnOutputPorts;
404 quirks |= kDefersOutputBufferAllocation;
405 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700406
Andreas Huber2dc64d82009-09-11 12:58:53 -0700407 if (!strncmp(componentName, "OMX.TI.", 7)) {
408 // Apparently I must not use OMX_UseBuffer on either input or
409 // output ports on any of the TI components or quote:
410 // "(I) may have unexpected problem (sic) which can be timing related
411 // and hard to reproduce."
412
413 quirks |= kRequiresAllocateBufferOnInputPorts;
414 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dongdca66e12010-06-14 11:14:38 -0700415 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) {
James Dong4f501f02010-06-07 14:41:41 -0700416 quirks |= kAvoidMemcopyInputRecordingFrames;
417 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700418 }
419
Andreas Huberb8de9572010-02-22 14:58:45 -0800420 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
421 quirks |= kInputBufferSizesAreBogus;
422 }
423
Andreas Hubere13526a2009-10-22 10:43:34 -0700424 return quirks;
425}
426
427// static
428void OMXCodec::findMatchingCodecs(
429 const char *mime,
430 bool createEncoder, const char *matchComponentName,
431 uint32_t flags,
432 Vector<String8> *matchingCodecs) {
433 matchingCodecs->clear();
434
435 for (int index = 0;; ++index) {
436 const char *componentName;
437
438 if (createEncoder) {
439 componentName = GetCodec(
440 kEncoderInfo,
441 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
442 mime, index);
443 } else {
444 componentName = GetCodec(
445 kDecoderInfo,
446 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
447 mime, index);
448 }
449
450 if (!componentName) {
451 break;
452 }
453
454 // If a specific codec is requested, skip the non-matching ones.
455 if (matchComponentName && strcmp(componentName, matchComponentName)) {
456 continue;
457 }
458
459 matchingCodecs->push(String8(componentName));
460 }
461
462 if (flags & kPreferSoftwareCodecs) {
463 matchingCodecs->sort(CompareSoftwareCodecsFirst);
464 }
465}
466
467// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800468sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700469 const sp<IOMX> &omx,
470 const sp<MetaData> &meta, bool createEncoder,
471 const sp<MediaSource> &source,
472 const char *matchComponentName,
473 uint32_t flags) {
474 const char *mime;
475 bool success = meta->findCString(kKeyMIMEType, &mime);
476 CHECK(success);
477
478 Vector<String8> matchingCodecs;
479 findMatchingCodecs(
480 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
481
482 if (matchingCodecs.isEmpty()) {
483 return NULL;
484 }
485
486 sp<OMXCodecObserver> observer = new OMXCodecObserver;
487 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700488
489 const char *componentName;
490 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
491 componentName = matchingCodecs[i].string();
492
James Dong17299ab2010-05-14 15:45:22 -0700493 sp<MediaSource> softwareCodec = createEncoder?
494 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800495 InstantiateSoftwareCodec(componentName, source);
496
497 if (softwareCodec != NULL) {
498 LOGV("Successfully allocated software codec '%s'", componentName);
499
500 return softwareCodec;
501 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800502
Andreas Hubere13526a2009-10-22 10:43:34 -0700503 LOGV("Attempting to allocate OMX node '%s'", componentName);
504
505 status_t err = omx->allocateNode(componentName, observer, &node);
506 if (err == OK) {
507 LOGV("Successfully allocated OMX node '%s'", componentName);
508
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700509 sp<OMXCodec> codec = new OMXCodec(
510 omx, node, getComponentQuirks(componentName),
511 createEncoder, mime, componentName,
512 source);
513
514 observer->setCodec(codec);
515
516 err = codec->configureCodec(meta);
517
518 if (err == OK) {
519 return codec;
520 }
521
522 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700523 }
524 }
525
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700526 return NULL;
527}
Andreas Hubere13526a2009-10-22 10:43:34 -0700528
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700529status_t OMXCodec::configureCodec(const sp<MetaData> &meta) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700530 uint32_t type;
531 const void *data;
532 size_t size;
533 if (meta->findData(kKeyESDS, &type, &data, &size)) {
534 ESDS esds((const char *)data, size);
535 CHECK_EQ(esds.InitCheck(), OK);
536
537 const void *codec_specific_data;
538 size_t codec_specific_data_size;
539 esds.getCodecSpecificInfo(
540 &codec_specific_data, &codec_specific_data_size);
541
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700542 addCodecSpecificData(
Andreas Huberbe06d262009-08-14 14:37:10 -0700543 codec_specific_data, codec_specific_data_size);
544 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
Andreas Huberebf66ea2009-08-19 13:32:58 -0700545 // Parse the AVCDecoderConfigurationRecord
546
547 const uint8_t *ptr = (const uint8_t *)data;
548
549 CHECK(size >= 7);
550 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
551 uint8_t profile = ptr[1];
552 uint8_t level = ptr[3];
553
Andreas Huber44e15c42009-11-23 14:39:38 -0800554 // There is decodable content out there that fails the following
555 // assertion, let's be lenient for now...
556 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700557
558 size_t lengthSize = 1 + (ptr[4] & 3);
559
560 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
561 // violates it...
562 // CHECK((ptr[5] >> 5) == 7); // reserved
563
564 size_t numSeqParameterSets = ptr[5] & 31;
565
566 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700567 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700568
569 for (size_t i = 0; i < numSeqParameterSets; ++i) {
570 CHECK(size >= 2);
571 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700572
573 ptr += 2;
574 size -= 2;
575
Andreas Huberbe06d262009-08-14 14:37:10 -0700576 CHECK(size >= length);
577
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700578 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700579
580 ptr += length;
581 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700582 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700583
Andreas Huberebf66ea2009-08-19 13:32:58 -0700584 CHECK(size >= 1);
585 size_t numPictureParameterSets = *ptr;
586 ++ptr;
587 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700588
Andreas Huberebf66ea2009-08-19 13:32:58 -0700589 for (size_t i = 0; i < numPictureParameterSets; ++i) {
590 CHECK(size >= 2);
591 size_t length = U16_AT(ptr);
592
593 ptr += 2;
594 size -= 2;
595
596 CHECK(size >= length);
597
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700598 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700599
600 ptr += length;
601 size -= length;
602 }
603
Andreas Hubere2f85072010-06-10 09:51:27 -0700604 CODEC_LOGV(
605 "AVC profile = %d (%s), level = %d",
606 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700607
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700608 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
Andreas Hubere2f85072010-06-10 09:51:27 -0700609 && (profile != kAVCProfileBaseline || level > 30)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700610 // This stream exceeds the decoder's capabilities. The decoder
611 // does not handle this gracefully and would clobber the heap
612 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700613
614 LOGE("Profile and/or level exceed the decoder's capabilities.");
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700615 return ERROR_UNSUPPORTED;
Andreas Huberbe06d262009-08-14 14:37:10 -0700616 }
617 }
618
James Dong17299ab2010-05-14 15:45:22 -0700619 int32_t bitRate = 0;
620 if (mIsEncoder) {
621 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
622 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700623 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700624 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700625 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700626 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700627 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700628 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700629 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700630 int32_t numChannels, sampleRate;
631 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
632 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
633
James Dong17299ab2010-05-14 15:45:22 -0700634 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700635 }
James Dongabed93a2010-04-22 17:27:04 -0700636
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700637 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700638
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700639 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700640 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700641 } else {
James Dong1244eab2010-06-08 11:58:53 -0700642 int32_t width, height;
643 bool success = meta->findInt32(kKeyWidth, &width);
644 success = success && meta->findInt32(kKeyHeight, &height);
645 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700646 status_t err = setVideoOutputFormat(
647 mMIME, width, height);
648
649 if (err != OK) {
650 return err;
651 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700652 }
653 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700654
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700655 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
656 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700657 OMX_COLOR_FORMATTYPE format =
658 OMX_COLOR_Format32bitARGB8888;
659 // OMX_COLOR_FormatYUV420PackedPlanar;
660 // OMX_COLOR_FormatCbYCrY;
661 // OMX_COLOR_FormatYUV411Planar;
662
663 int32_t width, height;
664 bool success = meta->findInt32(kKeyWidth, &width);
665 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700666
667 int32_t compressedSize;
668 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700669 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700670
671 CHECK(success);
672 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700673
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700674 setImageOutputFormat(format, width, height);
675 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700676 }
677
Andreas Huberda050cf22009-09-02 14:01:43 -0700678 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800679 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700680 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700681 }
682
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700683 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700684 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
685 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700686 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700687 }
688
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700689 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700690
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700691 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700692}
693
Andreas Huberda050cf22009-09-02 14:01:43 -0700694void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
695 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700696 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700697 def.nPortIndex = portIndex;
698
Andreas Huber784202e2009-10-15 13:46:54 -0700699 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700700 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
701 CHECK_EQ(err, OK);
702
Andreas Huberb8de9572010-02-22 14:58:45 -0800703 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
704 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700705 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700706 }
707
Andreas Huber784202e2009-10-15 13:46:54 -0700708 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700709 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
710 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800711
712 err = mOMX->getParameter(
713 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
714 CHECK_EQ(err, OK);
715
716 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800717 if (portIndex == kPortIndexInput
718 && (mQuirks & kInputBufferSizesAreBogus)) {
719 CHECK_EQ(def.nBufferSize, size);
720 } else {
721 CHECK(def.nBufferSize >= size);
722 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700723}
724
Andreas Huberbe06d262009-08-14 14:37:10 -0700725status_t OMXCodec::setVideoPortFormatType(
726 OMX_U32 portIndex,
727 OMX_VIDEO_CODINGTYPE compressionFormat,
728 OMX_COLOR_FORMATTYPE colorFormat) {
729 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700730 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700731 format.nPortIndex = portIndex;
732 format.nIndex = 0;
733 bool found = false;
734
735 OMX_U32 index = 0;
736 for (;;) {
737 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700738 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700739 mNode, OMX_IndexParamVideoPortFormat,
740 &format, sizeof(format));
741
742 if (err != OK) {
743 return err;
744 }
745
746 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700747 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700748
749#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700750 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700751 portIndex,
752 index, format.eCompressionFormat, format.eColorFormat);
753#endif
754
755 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
756 if (portIndex == kPortIndexInput
757 && colorFormat == format.eColorFormat) {
758 // eCompressionFormat does not seem right.
759 found = true;
760 break;
761 }
762 if (portIndex == kPortIndexOutput
763 && compressionFormat == format.eCompressionFormat) {
764 // eColorFormat does not seem right.
765 found = true;
766 break;
767 }
768 }
769
770 if (format.eCompressionFormat == compressionFormat
771 && format.eColorFormat == colorFormat) {
772 found = true;
773 break;
774 }
775
776 ++index;
777 }
778
779 if (!found) {
780 return UNKNOWN_ERROR;
781 }
782
Andreas Huber53a76bd2009-10-06 16:20:44 -0700783 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700784 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700785 mNode, OMX_IndexParamVideoPortFormat,
786 &format, sizeof(format));
787
788 return err;
789}
790
Andreas Huberb482ce82009-10-29 12:02:48 -0700791static size_t getFrameSize(
792 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
793 switch (colorFormat) {
794 case OMX_COLOR_FormatYCbYCr:
795 case OMX_COLOR_FormatCbYCrY:
796 return width * height * 2;
797
Andreas Huber71c27d92010-03-19 11:43:15 -0700798 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700799 case OMX_COLOR_FormatYUV420SemiPlanar:
800 return (width * height * 3) / 2;
801
802 default:
803 CHECK(!"Should not be here. Unsupported color format.");
804 break;
805 }
806}
807
James Dongafd97e82010-08-03 17:19:23 -0700808status_t OMXCodec::findTargetColorFormat(
809 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) {
810 LOGV("findTargetColorFormat");
811 CHECK(mIsEncoder);
812
813 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
814 int32_t targetColorFormat;
815 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) {
816 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat;
817 } else {
818 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
819 *colorFormat = OMX_COLOR_FormatYCbYCr;
820 }
821 }
822
823 // Check whether the target color format is supported.
824 return isColorFormatSupported(*colorFormat, kPortIndexInput);
825}
826
827status_t OMXCodec::isColorFormatSupported(
828 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) {
829 LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat));
830
831 // Enumerate all the color formats supported by
832 // the omx component to see whether the given
833 // color format is supported.
834 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
835 InitOMXParams(&portFormat);
836 portFormat.nPortIndex = portIndex;
837 OMX_U32 index = 0;
838 portFormat.nIndex = index;
839 while (true) {
840 if (OMX_ErrorNone != mOMX->getParameter(
841 mNode, OMX_IndexParamVideoPortFormat,
842 &portFormat, sizeof(portFormat))) {
James Dongb5024da2010-09-13 16:30:51 -0700843 break;
James Dongafd97e82010-08-03 17:19:23 -0700844 }
845 // Make sure that omx component does not overwrite
846 // the incremented index (bug 2897413).
847 CHECK_EQ(index, portFormat.nIndex);
848 if ((portFormat.eColorFormat == colorFormat)) {
849 LOGV("Found supported color format: %d", portFormat.eColorFormat);
850 return OK; // colorFormat is supported!
851 }
852 ++index;
853 portFormat.nIndex = index;
854
855 // OMX Spec defines less than 50 color formats
856 // 1000 is more than enough for us to tell whether the omx
857 // component in question is buggy or not.
858 if (index >= 1000) {
859 LOGE("More than %ld color formats are supported???", index);
860 break;
861 }
862 }
James Dongb5024da2010-09-13 16:30:51 -0700863
864 LOGE("color format %d is not supported", colorFormat);
James Dongafd97e82010-08-03 17:19:23 -0700865 return UNKNOWN_ERROR;
866}
867
Andreas Huberbe06d262009-08-14 14:37:10 -0700868void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700869 const char *mime, const sp<MetaData>& meta) {
870
871 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
872 bool success = meta->findInt32(kKeyWidth, &width);
873 success = success && meta->findInt32(kKeyHeight, &height);
874 success = success && meta->findInt32(kKeySampleRate, &frameRate);
875 success = success && meta->findInt32(kKeyBitRate, &bitRate);
876 success = success && meta->findInt32(kKeyStride, &stride);
877 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
878 CHECK(success);
879 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700880
881 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700882 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700883 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700884 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700885 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700886 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700887 compressionFormat = OMX_VIDEO_CodingH263;
888 } else {
889 LOGE("Not a supported video mime type: %s", mime);
890 CHECK(!"Should not be here. Not a supported video mime type.");
891 }
892
James Dongafd97e82010-08-03 17:19:23 -0700893 OMX_COLOR_FORMATTYPE colorFormat;
894 CHECK_EQ(OK, findTargetColorFormat(meta, &colorFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -0700895
pgudadhe9c305322010-07-26 13:59:29 -0700896 if (!strcasecmp("OMX.Nvidia.h264.encoder", mComponentName)) {
897 colorFormat = OMX_COLOR_FormatYUV420Planar;
898 }
899
James Dongb00e2462010-04-26 17:48:26 -0700900 status_t err;
901 OMX_PARAM_PORTDEFINITIONTYPE def;
902 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
903
904 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700905 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700906 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700907 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700908
James Dongb00e2462010-04-26 17:48:26 -0700909 InitOMXParams(&def);
910 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700911
James Dongb00e2462010-04-26 17:48:26 -0700912 err = mOMX->getParameter(
913 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
914 CHECK_EQ(err, OK);
915
James Dong1244eab2010-06-08 11:58:53 -0700916 def.nBufferSize = getFrameSize(colorFormat,
917 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700918
919 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
920
921 video_def->nFrameWidth = width;
922 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700923 video_def->nStride = stride;
924 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700925 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700926 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
927 video_def->eColorFormat = colorFormat;
928
James Dongb00e2462010-04-26 17:48:26 -0700929 err = mOMX->setParameter(
930 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
931 CHECK_EQ(err, OK);
932
933 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700934 CHECK_EQ(setVideoPortFormatType(
935 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
936 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700937 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700938 def.nPortIndex = kPortIndexOutput;
939
James Dongb00e2462010-04-26 17:48:26 -0700940 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700941 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
942
943 CHECK_EQ(err, OK);
944 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
945
946 video_def->nFrameWidth = width;
947 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700948 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700949 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700950 video_def->eCompressionFormat = compressionFormat;
951 video_def->eColorFormat = OMX_COLOR_FormatUnused;
James Dong90862e22010-08-26 19:12:59 -0700952 if (mQuirks & kRequiresLargerEncoderOutputBuffer) {
953 // Increases the output buffer size
954 def.nBufferSize = ((def.nBufferSize * 3) >> 1);
955 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700956
Andreas Huber784202e2009-10-15 13:46:54 -0700957 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700958 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
959 CHECK_EQ(err, OK);
960
James Dongb00e2462010-04-26 17:48:26 -0700961 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700962 switch (compressionFormat) {
963 case OMX_VIDEO_CodingMPEG4:
964 {
James Dong1244eab2010-06-08 11:58:53 -0700965 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700966 break;
967 }
968
969 case OMX_VIDEO_CodingH263:
James Dongc0ab2a62010-06-29 16:29:19 -0700970 CHECK_EQ(setupH263EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700971 break;
972
Andreas Huberea6a38c2009-11-16 15:43:38 -0800973 case OMX_VIDEO_CodingAVC:
974 {
James Dong1244eab2010-06-08 11:58:53 -0700975 CHECK_EQ(setupAVCEncoderParameters(meta), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -0800976 break;
977 }
978
Andreas Huberb482ce82009-10-29 12:02:48 -0700979 default:
980 CHECK(!"Support for this compressionFormat to be implemented.");
981 break;
982 }
983}
984
James Dong1244eab2010-06-08 11:58:53 -0700985static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
986 if (iFramesInterval < 0) {
987 return 0xFFFFFFFF;
988 } else if (iFramesInterval == 0) {
989 return 0;
990 }
991 OMX_U32 ret = frameRate * iFramesInterval;
992 CHECK(ret > 1);
993 return ret;
994}
995
James Dongc0ab2a62010-06-29 16:29:19 -0700996status_t OMXCodec::setupErrorCorrectionParameters() {
997 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
998 InitOMXParams(&errorCorrectionType);
999 errorCorrectionType.nPortIndex = kPortIndexOutput;
1000
1001 status_t err = mOMX->getParameter(
1002 mNode, OMX_IndexParamVideoErrorCorrection,
1003 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001004 if (err != OK) {
1005 LOGW("Error correction param query is not supported");
1006 return OK; // Optional feature. Ignore this failure
1007 }
James Dongc0ab2a62010-06-29 16:29:19 -07001008
1009 errorCorrectionType.bEnableHEC = OMX_FALSE;
1010 errorCorrectionType.bEnableResync = OMX_TRUE;
1011 errorCorrectionType.nResynchMarkerSpacing = 256;
1012 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
1013 errorCorrectionType.bEnableRVLC = OMX_FALSE;
1014
1015 err = mOMX->setParameter(
1016 mNode, OMX_IndexParamVideoErrorCorrection,
1017 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001018 if (err != OK) {
1019 LOGW("Error correction param configuration is not supported");
1020 }
1021
1022 // Optional feature. Ignore the failure.
James Dongc0ab2a62010-06-29 16:29:19 -07001023 return OK;
1024}
1025
1026status_t OMXCodec::setupBitRate(int32_t bitRate) {
1027 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
1028 InitOMXParams(&bitrateType);
1029 bitrateType.nPortIndex = kPortIndexOutput;
1030
1031 status_t err = mOMX->getParameter(
1032 mNode, OMX_IndexParamVideoBitrate,
1033 &bitrateType, sizeof(bitrateType));
1034 CHECK_EQ(err, OK);
1035
1036 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
1037 bitrateType.nTargetBitrate = bitRate;
1038
1039 err = mOMX->setParameter(
1040 mNode, OMX_IndexParamVideoBitrate,
1041 &bitrateType, sizeof(bitrateType));
1042 CHECK_EQ(err, OK);
1043 return OK;
1044}
1045
James Dong81c929a2010-07-01 15:02:14 -07001046status_t OMXCodec::getVideoProfileLevel(
1047 const sp<MetaData>& meta,
1048 const CodecProfileLevel& defaultProfileLevel,
1049 CodecProfileLevel &profileLevel) {
1050 CODEC_LOGV("Default profile: %ld, level %ld",
1051 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
1052
1053 // Are the default profile and level overwriten?
1054 int32_t profile, level;
1055 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
1056 profile = defaultProfileLevel.mProfile;
1057 }
1058 if (!meta->findInt32(kKeyVideoLevel, &level)) {
1059 level = defaultProfileLevel.mLevel;
1060 }
1061 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
1062
1063 // Are the target profile and level supported by the encoder?
1064 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
1065 InitOMXParams(&param);
1066 param.nPortIndex = kPortIndexOutput;
1067 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
1068 status_t err = mOMX->getParameter(
1069 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
1070 &param, sizeof(param));
1071
James Dongdfb89912010-09-15 21:07:52 -07001072 if (err != OK) break;
James Dong81c929a2010-07-01 15:02:14 -07001073
1074 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
1075 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -07001076 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -07001077 supportedProfile, supportedLevel);
1078
1079 if (profile == supportedProfile &&
James Dongdfb89912010-09-15 21:07:52 -07001080 level <= supportedLevel) {
1081 // We can further check whether the level is a valid
1082 // value; but we will leave that to the omx encoder component
1083 // via OMX_SetParameter call.
James Dong81c929a2010-07-01 15:02:14 -07001084 profileLevel.mProfile = profile;
1085 profileLevel.mLevel = level;
1086 return OK;
1087 }
1088 }
1089
1090 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
1091 profile, level);
1092 return BAD_VALUE;
1093}
1094
James Dongc0ab2a62010-06-29 16:29:19 -07001095status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
1096 int32_t iFramesInterval, frameRate, bitRate;
1097 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1098 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1099 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1100 CHECK(success);
1101 OMX_VIDEO_PARAM_H263TYPE h263type;
1102 InitOMXParams(&h263type);
1103 h263type.nPortIndex = kPortIndexOutput;
1104
1105 status_t err = mOMX->getParameter(
1106 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1107 CHECK_EQ(err, OK);
1108
1109 h263type.nAllowedPictureTypes =
1110 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1111
1112 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1113 if (h263type.nPFrames == 0) {
1114 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1115 }
1116 h263type.nBFrames = 0;
1117
James Dong81c929a2010-07-01 15:02:14 -07001118 // Check profile and level parameters
1119 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001120 defaultProfileLevel.mProfile = h263type.eProfile;
1121 defaultProfileLevel.mLevel = h263type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001122 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1123 if (err != OK) return err;
1124 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1125 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001126
1127 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1128 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1129 h263type.nPictureHeaderRepetition = 0;
1130 h263type.nGOBHeaderInterval = 0;
1131
1132 err = mOMX->setParameter(
1133 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1134 CHECK_EQ(err, OK);
1135
1136 CHECK_EQ(setupBitRate(bitRate), OK);
1137 CHECK_EQ(setupErrorCorrectionParameters(), OK);
1138
1139 return OK;
1140}
1141
James Dong1244eab2010-06-08 11:58:53 -07001142status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1143 int32_t iFramesInterval, frameRate, bitRate;
1144 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1145 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1146 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1147 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001148 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1149 InitOMXParams(&mpeg4type);
1150 mpeg4type.nPortIndex = kPortIndexOutput;
1151
1152 status_t err = mOMX->getParameter(
1153 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1154 CHECK_EQ(err, OK);
1155
1156 mpeg4type.nSliceHeaderSpacing = 0;
1157 mpeg4type.bSVH = OMX_FALSE;
1158 mpeg4type.bGov = OMX_FALSE;
1159
1160 mpeg4type.nAllowedPictureTypes =
1161 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1162
James Dong1244eab2010-06-08 11:58:53 -07001163 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1164 if (mpeg4type.nPFrames == 0) {
1165 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1166 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001167 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001168 mpeg4type.nIDCVLCThreshold = 0;
1169 mpeg4type.bACPred = OMX_TRUE;
1170 mpeg4type.nMaxPacketSize = 256;
1171 mpeg4type.nTimeIncRes = 1000;
1172 mpeg4type.nHeaderExtension = 0;
1173 mpeg4type.bReversibleVLC = OMX_FALSE;
1174
James Dong81c929a2010-07-01 15:02:14 -07001175 // Check profile and level parameters
1176 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001177 defaultProfileLevel.mProfile = mpeg4type.eProfile;
1178 defaultProfileLevel.mLevel = mpeg4type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001179 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1180 if (err != OK) return err;
1181 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1182 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001183
1184 err = mOMX->setParameter(
1185 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1186 CHECK_EQ(err, OK);
1187
James Dongc0ab2a62010-06-29 16:29:19 -07001188 CHECK_EQ(setupBitRate(bitRate), OK);
1189 CHECK_EQ(setupErrorCorrectionParameters(), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001190
1191 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001192}
1193
James Dong1244eab2010-06-08 11:58:53 -07001194status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1195 int32_t iFramesInterval, frameRate, bitRate;
1196 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1197 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1198 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1199 CHECK(success);
1200
Andreas Huberea6a38c2009-11-16 15:43:38 -08001201 OMX_VIDEO_PARAM_AVCTYPE h264type;
1202 InitOMXParams(&h264type);
1203 h264type.nPortIndex = kPortIndexOutput;
1204
1205 status_t err = mOMX->getParameter(
1206 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1207 CHECK_EQ(err, OK);
1208
1209 h264type.nAllowedPictureTypes =
1210 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1211
1212 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001213 h264type.nBFrames = 0; // No B frames support yet
1214 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1215 if (h264type.nPFrames == 0) {
1216 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1217 }
James Dong81c929a2010-07-01 15:02:14 -07001218
1219 // Check profile and level parameters
1220 CodecProfileLevel defaultProfileLevel, profileLevel;
1221 defaultProfileLevel.mProfile = h264type.eProfile;
1222 defaultProfileLevel.mLevel = h264type.eLevel;
1223 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1224 if (err != OK) return err;
1225 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1226 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1227
1228 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1229 h264type.bUseHadamard = OMX_TRUE;
1230 h264type.nRefFrames = 1;
1231 h264type.nRefIdx10ActiveMinus1 = 0;
1232 h264type.nRefIdx11ActiveMinus1 = 0;
1233 h264type.bEntropyCodingCABAC = OMX_FALSE;
1234 h264type.bWeightedPPrediction = OMX_FALSE;
1235 h264type.bconstIpred = OMX_FALSE;
1236 h264type.bDirect8x8Inference = OMX_FALSE;
1237 h264type.bDirectSpatialTemporal = OMX_FALSE;
1238 h264type.nCabacInitIdc = 0;
1239 }
1240
1241 if (h264type.nBFrames != 0) {
1242 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1243 }
1244
Andreas Huberea6a38c2009-11-16 15:43:38 -08001245 h264type.bEnableUEP = OMX_FALSE;
1246 h264type.bEnableFMO = OMX_FALSE;
1247 h264type.bEnableASO = OMX_FALSE;
1248 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001249 h264type.bFrameMBsOnly = OMX_TRUE;
1250 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001251 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1252
pgudadhe9c305322010-07-26 13:59:29 -07001253 if (!strcasecmp("OMX.Nvidia.h264.encoder", mComponentName)) {
1254 h264type.eLevel = OMX_VIDEO_AVCLevelMax;
1255 }
1256
Andreas Huberea6a38c2009-11-16 15:43:38 -08001257 err = mOMX->setParameter(
1258 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1259 CHECK_EQ(err, OK);
1260
James Dongc0ab2a62010-06-29 16:29:19 -07001261 CHECK_EQ(setupBitRate(bitRate), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001262
1263 return OK;
1264}
1265
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001266status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001267 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001268 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001269
Andreas Huberbe06d262009-08-14 14:37:10 -07001270 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001271 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001272 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001273 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001274 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001275 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001276 compressionFormat = OMX_VIDEO_CodingH263;
1277 } else {
1278 LOGE("Not a supported video mime type: %s", mime);
1279 CHECK(!"Should not be here. Not a supported video mime type.");
1280 }
1281
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001282 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001283 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1284
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001285 if (err != OK) {
1286 return err;
1287 }
1288
Andreas Huberbe06d262009-08-14 14:37:10 -07001289#if 1
1290 {
1291 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001292 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001293 format.nPortIndex = kPortIndexOutput;
1294 format.nIndex = 0;
1295
Andreas Huber784202e2009-10-15 13:46:54 -07001296 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001297 mNode, OMX_IndexParamVideoPortFormat,
1298 &format, sizeof(format));
1299 CHECK_EQ(err, OK);
1300 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1301
1302 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1303
1304 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1305 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1306 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1307 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1308
Andreas Huber784202e2009-10-15 13:46:54 -07001309 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001310 mNode, OMX_IndexParamVideoPortFormat,
1311 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001312
1313 if (err != OK) {
1314 return err;
1315 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001316 }
1317#endif
1318
1319 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001320 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001321 def.nPortIndex = kPortIndexInput;
1322
Andreas Huber4c483422009-09-02 16:05:36 -07001323 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1324
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001325 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001326 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1327
1328 CHECK_EQ(err, OK);
1329
1330#if 1
1331 // XXX Need a (much) better heuristic to compute input buffer sizes.
1332 const size_t X = 64 * 1024;
1333 if (def.nBufferSize < X) {
1334 def.nBufferSize = X;
1335 }
1336#endif
1337
1338 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1339
1340 video_def->nFrameWidth = width;
1341 video_def->nFrameHeight = height;
1342
Andreas Huberb482ce82009-10-29 12:02:48 -07001343 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001344 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1345
Andreas Huber784202e2009-10-15 13:46:54 -07001346 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001347 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001348
1349 if (err != OK) {
1350 return err;
1351 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001352
1353 ////////////////////////////////////////////////////////////////////////////
1354
Andreas Huber4c483422009-09-02 16:05:36 -07001355 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001356 def.nPortIndex = kPortIndexOutput;
1357
Andreas Huber784202e2009-10-15 13:46:54 -07001358 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001359 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1360 CHECK_EQ(err, OK);
1361 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1362
1363#if 0
1364 def.nBufferSize =
1365 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1366#endif
1367
1368 video_def->nFrameWidth = width;
1369 video_def->nFrameHeight = height;
1370
Andreas Huber784202e2009-10-15 13:46:54 -07001371 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001372 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001373
1374 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001375}
1376
Andreas Huberbe06d262009-08-14 14:37:10 -07001377OMXCodec::OMXCodec(
1378 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001379 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001380 const char *mime,
1381 const char *componentName,
1382 const sp<MediaSource> &source)
1383 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001384 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001385 mNode(node),
1386 mQuirks(quirks),
1387 mIsEncoder(isEncoder),
1388 mMIME(strdup(mime)),
1389 mComponentName(strdup(componentName)),
1390 mSource(source),
1391 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001392 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001393 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001394 mSignalledEOS(false),
1395 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001396 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001397 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001398 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1399 mTargetTimeUs(-1),
James Dong53d4e0d2010-07-21 14:51:35 -07001400 mSkipTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001401 mLeftOverBuffer(NULL),
1402 mPaused(false) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001403 mPortStatus[kPortIndexInput] = ENABLED;
1404 mPortStatus[kPortIndexOutput] = ENABLED;
1405
Andreas Huber4c483422009-09-02 16:05:36 -07001406 setComponentRole();
1407}
1408
Andreas Hubere6c40962009-09-10 14:13:30 -07001409// static
1410void OMXCodec::setComponentRole(
1411 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1412 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001413 struct MimeToRole {
1414 const char *mime;
1415 const char *decoderRole;
1416 const char *encoderRole;
1417 };
1418
1419 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001420 { MEDIA_MIMETYPE_AUDIO_MPEG,
1421 "audio_decoder.mp3", "audio_encoder.mp3" },
1422 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1423 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1424 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1425 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1426 { MEDIA_MIMETYPE_AUDIO_AAC,
1427 "audio_decoder.aac", "audio_encoder.aac" },
1428 { MEDIA_MIMETYPE_VIDEO_AVC,
1429 "video_decoder.avc", "video_encoder.avc" },
1430 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1431 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1432 { MEDIA_MIMETYPE_VIDEO_H263,
1433 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001434 };
1435
1436 static const size_t kNumMimeToRole =
1437 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1438
1439 size_t i;
1440 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001441 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001442 break;
1443 }
1444 }
1445
1446 if (i == kNumMimeToRole) {
1447 return;
1448 }
1449
1450 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001451 isEncoder ? kMimeToRole[i].encoderRole
1452 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001453
1454 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001455 OMX_PARAM_COMPONENTROLETYPE roleParams;
1456 InitOMXParams(&roleParams);
1457
1458 strncpy((char *)roleParams.cRole,
1459 role, OMX_MAX_STRINGNAME_SIZE - 1);
1460
1461 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1462
Andreas Huber784202e2009-10-15 13:46:54 -07001463 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001464 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001465 &roleParams, sizeof(roleParams));
1466
1467 if (err != OK) {
1468 LOGW("Failed to set standard component role '%s'.", role);
1469 }
1470 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001471}
1472
Andreas Hubere6c40962009-09-10 14:13:30 -07001473void OMXCodec::setComponentRole() {
1474 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1475}
1476
Andreas Huberbe06d262009-08-14 14:37:10 -07001477OMXCodec::~OMXCodec() {
Andreas Huberf98197a2010-09-17 11:49:39 -07001478 mSource.clear();
1479
Andreas Huber4f5e6022009-08-19 09:29:34 -07001480 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001481
Andreas Huber784202e2009-10-15 13:46:54 -07001482 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001483 CHECK_EQ(err, OK);
1484
1485 mNode = NULL;
1486 setState(DEAD);
1487
1488 clearCodecSpecificData();
1489
1490 free(mComponentName);
1491 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001492
Andreas Huberbe06d262009-08-14 14:37:10 -07001493 free(mMIME);
1494 mMIME = NULL;
1495}
1496
1497status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001498 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001499
1500 CHECK_EQ(mState, LOADED);
1501
1502 status_t err;
1503 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001504 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001505 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001506 setState(LOADED_TO_IDLE);
1507 }
1508
1509 err = allocateBuffers();
1510 CHECK_EQ(err, OK);
1511
1512 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001513 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001514 CHECK_EQ(err, OK);
1515
1516 setState(LOADED_TO_IDLE);
1517 }
1518
1519 while (mState != EXECUTING && mState != ERROR) {
1520 mAsyncCompletion.wait(mLock);
1521 }
1522
1523 return mState == ERROR ? UNKNOWN_ERROR : OK;
1524}
1525
1526// static
1527bool OMXCodec::isIntermediateState(State state) {
1528 return state == LOADED_TO_IDLE
1529 || state == IDLE_TO_EXECUTING
1530 || state == EXECUTING_TO_IDLE
1531 || state == IDLE_TO_LOADED
1532 || state == RECONFIGURING;
1533}
1534
1535status_t OMXCodec::allocateBuffers() {
1536 status_t err = allocateBuffersOnPort(kPortIndexInput);
1537
1538 if (err != OK) {
1539 return err;
1540 }
1541
1542 return allocateBuffersOnPort(kPortIndexOutput);
1543}
1544
1545status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1546 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001547 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001548 def.nPortIndex = portIndex;
1549
Andreas Huber784202e2009-10-15 13:46:54 -07001550 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001551 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1552
1553 if (err != OK) {
1554 return err;
1555 }
1556
Andreas Huber57648e42010-08-04 10:14:30 -07001557 CODEC_LOGI("allocating %lu buffers of size %lu on %s port",
1558 def.nBufferCountActual, def.nBufferSize,
1559 portIndex == kPortIndexInput ? "input" : "output");
1560
Andreas Huber5c0a9132009-08-20 11:16:40 -07001561 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001562 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001563
Andreas Huberbe06d262009-08-14 14:37:10 -07001564 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001565 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001566 CHECK(mem.get() != NULL);
1567
Andreas Huberc712b9f2010-01-20 15:05:46 -08001568 BufferInfo info;
1569 info.mData = NULL;
1570 info.mSize = def.nBufferSize;
1571
Andreas Huberbe06d262009-08-14 14:37:10 -07001572 IOMX::buffer_id buffer;
1573 if (portIndex == kPortIndexInput
1574 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001575 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001576 mem.clear();
1577
Andreas Huberf1fe0642010-01-15 15:28:19 -08001578 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001579 mNode, portIndex, def.nBufferSize, &buffer,
1580 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001581 } else {
1582 err = mOMX->allocateBufferWithBackup(
1583 mNode, portIndex, mem, &buffer);
1584 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001585 } else if (portIndex == kPortIndexOutput
1586 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001587 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001588 mem.clear();
1589
Andreas Huberf1fe0642010-01-15 15:28:19 -08001590 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001591 mNode, portIndex, def.nBufferSize, &buffer,
1592 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001593 } else {
1594 err = mOMX->allocateBufferWithBackup(
1595 mNode, portIndex, mem, &buffer);
1596 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001597 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001598 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001599 }
1600
1601 if (err != OK) {
1602 LOGE("allocate_buffer_with_backup failed");
1603 return err;
1604 }
1605
Andreas Huberc712b9f2010-01-20 15:05:46 -08001606 if (mem != NULL) {
1607 info.mData = mem->pointer();
1608 }
1609
Andreas Huberbe06d262009-08-14 14:37:10 -07001610 info.mBuffer = buffer;
1611 info.mOwnedByComponent = false;
1612 info.mMem = mem;
1613 info.mMediaBuffer = NULL;
1614
1615 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001616 if (!(mOMXLivesLocally
1617 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1618 && (mQuirks & kDefersOutputBufferAllocation))) {
1619 // If the node does not fill in the buffer ptr at this time,
1620 // we will defer creating the MediaBuffer until receiving
1621 // the first FILL_BUFFER_DONE notification instead.
1622 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1623 info.mMediaBuffer->setObserver(this);
1624 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001625 }
1626
1627 mPortBuffers[portIndex].push(info);
1628
Andreas Huber4c483422009-09-02 16:05:36 -07001629 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001630 portIndex == kPortIndexInput ? "input" : "output");
1631 }
1632
Andreas Huber2ea14e22009-12-16 09:30:55 -08001633 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001634
1635 return OK;
1636}
1637
1638void OMXCodec::on_message(const omx_message &msg) {
1639 Mutex::Autolock autoLock(mLock);
1640
1641 switch (msg.type) {
1642 case omx_message::EVENT:
1643 {
1644 onEvent(
1645 msg.u.event_data.event, msg.u.event_data.data1,
1646 msg.u.event_data.data2);
1647
1648 break;
1649 }
1650
1651 case omx_message::EMPTY_BUFFER_DONE:
1652 {
1653 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1654
Andreas Huber4c483422009-09-02 16:05:36 -07001655 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001656
1657 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1658 size_t i = 0;
1659 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1660 ++i;
1661 }
1662
1663 CHECK(i < buffers->size());
1664 if (!(*buffers)[i].mOwnedByComponent) {
1665 LOGW("We already own input buffer %p, yet received "
1666 "an EMPTY_BUFFER_DONE.", buffer);
1667 }
1668
1669 buffers->editItemAt(i).mOwnedByComponent = false;
1670
1671 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001672 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001673
1674 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001675 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001676 CHECK_EQ(err, OK);
1677
1678 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001679 } else if (mState != ERROR
1680 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001681 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1682 drainInputBuffer(&buffers->editItemAt(i));
1683 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001684 break;
1685 }
1686
1687 case omx_message::FILL_BUFFER_DONE:
1688 {
1689 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1690 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1691
Andreas Huber2ea14e22009-12-16 09:30:55 -08001692 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001693 buffer,
1694 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001695 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001696 msg.u.extended_buffer_data.timestamp,
1697 msg.u.extended_buffer_data.timestamp / 1E6);
1698
1699 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1700 size_t i = 0;
1701 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1702 ++i;
1703 }
1704
1705 CHECK(i < buffers->size());
1706 BufferInfo *info = &buffers->editItemAt(i);
1707
1708 if (!info->mOwnedByComponent) {
1709 LOGW("We already own output buffer %p, yet received "
1710 "a FILL_BUFFER_DONE.", buffer);
1711 }
1712
1713 info->mOwnedByComponent = false;
1714
1715 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001716 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001717
1718 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001719 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001720 CHECK_EQ(err, OK);
1721
1722 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001723#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001724 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1725 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001726 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001727 mNoMoreOutputData = true;
1728 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001729#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001730 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1731 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001732
Andreas Huber52733b82010-01-25 10:41:35 -08001733 if (info->mMediaBuffer == NULL) {
1734 CHECK(mOMXLivesLocally);
1735 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1736 CHECK(mQuirks & kDefersOutputBufferAllocation);
1737
1738 // The qcom video decoders on Nexus don't actually allocate
1739 // output buffer memory on a call to OMX_AllocateBuffer
1740 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1741 // structure is only filled in later.
1742
1743 info->mMediaBuffer = new MediaBuffer(
1744 msg.u.extended_buffer_data.data_ptr,
1745 info->mSize);
1746 info->mMediaBuffer->setObserver(this);
1747 }
1748
Andreas Huberbe06d262009-08-14 14:37:10 -07001749 MediaBuffer *buffer = info->mMediaBuffer;
1750
Andreas Huberf88f8442010-08-10 11:18:36 -07001751 if (msg.u.extended_buffer_data.range_offset
1752 + msg.u.extended_buffer_data.range_length
1753 > buffer->size()) {
1754 CODEC_LOGE(
1755 "Codec lied about its buffer size requirements, "
1756 "sending a buffer larger than the originally "
1757 "advertised size in FILL_BUFFER_DONE!");
1758 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001759 buffer->set_range(
1760 msg.u.extended_buffer_data.range_offset,
1761 msg.u.extended_buffer_data.range_length);
1762
1763 buffer->meta_data()->clear();
1764
Andreas Huberfa8de752009-10-08 10:07:49 -07001765 buffer->meta_data()->setInt64(
1766 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001767
1768 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1769 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1770 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001771 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1772 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1773 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001774
1775 buffer->meta_data()->setPointer(
1776 kKeyPlatformPrivate,
1777 msg.u.extended_buffer_data.platform_private);
1778
1779 buffer->meta_data()->setPointer(
1780 kKeyBufferID,
1781 msg.u.extended_buffer_data.buffer);
1782
Andreas Huber2ea14e22009-12-16 09:30:55 -08001783 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1784 CODEC_LOGV("No more output data.");
1785 mNoMoreOutputData = true;
1786 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07001787
1788 if (mTargetTimeUs >= 0) {
1789 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
1790
1791 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
1792 CODEC_LOGV(
1793 "skipping output buffer at timestamp %lld us",
1794 msg.u.extended_buffer_data.timestamp);
1795
1796 fillOutputBuffer(info);
1797 break;
1798 }
1799
1800 CODEC_LOGV(
1801 "returning output buffer at target timestamp "
1802 "%lld us",
1803 msg.u.extended_buffer_data.timestamp);
1804
1805 mTargetTimeUs = -1;
1806 }
1807
1808 mFilledBuffers.push_back(i);
1809 mBufferFilled.signal();
Andreas Huberbe06d262009-08-14 14:37:10 -07001810 }
1811
1812 break;
1813 }
1814
1815 default:
1816 {
1817 CHECK(!"should not be here.");
1818 break;
1819 }
1820 }
1821}
1822
1823void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1824 switch (event) {
1825 case OMX_EventCmdComplete:
1826 {
1827 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1828 break;
1829 }
1830
1831 case OMX_EventError:
1832 {
Andreas Huberaf0a1882010-09-21 15:08:52 -07001833 CODEC_LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001834
1835 setState(ERROR);
1836 break;
1837 }
1838
1839 case OMX_EventPortSettingsChanged:
1840 {
Andreas Huber29c03c62010-08-30 16:23:15 -07001841 if (data2 == 0 || data2 == OMX_IndexParamPortDefinition) {
1842 onPortSettingsChanged(data1);
1843 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001844 break;
1845 }
1846
Andreas Huber2ea14e22009-12-16 09:30:55 -08001847#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001848 case OMX_EventBufferFlag:
1849 {
Andreas Huber4c483422009-09-02 16:05:36 -07001850 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001851
1852 if (data1 == kPortIndexOutput) {
1853 mNoMoreOutputData = true;
1854 }
1855 break;
1856 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001857#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001858
1859 default:
1860 {
Andreas Huber4c483422009-09-02 16:05:36 -07001861 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001862 break;
1863 }
1864 }
1865}
1866
Andreas Huberb1678602009-10-19 13:06:40 -07001867// Has the format changed in any way that the client would have to be aware of?
1868static bool formatHasNotablyChanged(
1869 const sp<MetaData> &from, const sp<MetaData> &to) {
1870 if (from.get() == NULL && to.get() == NULL) {
1871 return false;
1872 }
1873
Andreas Huberf68c1682009-10-21 14:01:30 -07001874 if ((from.get() == NULL && to.get() != NULL)
1875 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001876 return true;
1877 }
1878
1879 const char *mime_from, *mime_to;
1880 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1881 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1882
1883 if (strcasecmp(mime_from, mime_to)) {
1884 return true;
1885 }
1886
1887 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1888 int32_t colorFormat_from, colorFormat_to;
1889 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1890 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1891
1892 if (colorFormat_from != colorFormat_to) {
1893 return true;
1894 }
1895
1896 int32_t width_from, width_to;
1897 CHECK(from->findInt32(kKeyWidth, &width_from));
1898 CHECK(to->findInt32(kKeyWidth, &width_to));
1899
1900 if (width_from != width_to) {
1901 return true;
1902 }
1903
1904 int32_t height_from, height_to;
1905 CHECK(from->findInt32(kKeyHeight, &height_from));
1906 CHECK(to->findInt32(kKeyHeight, &height_to));
1907
1908 if (height_from != height_to) {
1909 return true;
1910 }
1911 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1912 int32_t numChannels_from, numChannels_to;
1913 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1914 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1915
1916 if (numChannels_from != numChannels_to) {
1917 return true;
1918 }
1919
1920 int32_t sampleRate_from, sampleRate_to;
1921 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1922 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1923
1924 if (sampleRate_from != sampleRate_to) {
1925 return true;
1926 }
1927 }
1928
1929 return false;
1930}
1931
Andreas Huberbe06d262009-08-14 14:37:10 -07001932void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1933 switch (cmd) {
1934 case OMX_CommandStateSet:
1935 {
1936 onStateChange((OMX_STATETYPE)data);
1937 break;
1938 }
1939
1940 case OMX_CommandPortDisable:
1941 {
1942 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001943 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001944
1945 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1946 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1947 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1948
1949 mPortStatus[portIndex] = DISABLED;
1950
1951 if (mState == RECONFIGURING) {
1952 CHECK_EQ(portIndex, kPortIndexOutput);
1953
Andreas Huberb1678602009-10-19 13:06:40 -07001954 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001955 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001956
1957 // Don't notify clients if the output port settings change
1958 // wasn't of importance to them, i.e. it may be that just the
1959 // number of buffers has changed and nothing else.
1960 mOutputPortSettingsHaveChanged =
1961 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001962
Andreas Huberbe06d262009-08-14 14:37:10 -07001963 enablePortAsync(portIndex);
1964
1965 status_t err = allocateBuffersOnPort(portIndex);
1966 CHECK_EQ(err, OK);
1967 }
1968 break;
1969 }
1970
1971 case OMX_CommandPortEnable:
1972 {
1973 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001974 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001975
1976 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1977 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1978
1979 mPortStatus[portIndex] = ENABLED;
1980
1981 if (mState == RECONFIGURING) {
1982 CHECK_EQ(portIndex, kPortIndexOutput);
1983
1984 setState(EXECUTING);
1985
1986 fillOutputBuffers();
1987 }
1988 break;
1989 }
1990
1991 case OMX_CommandFlush:
1992 {
1993 OMX_U32 portIndex = data;
1994
Andreas Huber4c483422009-09-02 16:05:36 -07001995 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001996
1997 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1998 mPortStatus[portIndex] = ENABLED;
1999
2000 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
2001 mPortBuffers[portIndex].size());
2002
2003 if (mState == RECONFIGURING) {
2004 CHECK_EQ(portIndex, kPortIndexOutput);
2005
2006 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002007 } else if (mState == EXECUTING_TO_IDLE) {
2008 if (mPortStatus[kPortIndexInput] == ENABLED
2009 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002010 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002011 "transition from EXECUTING to IDLE.");
2012
2013 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2014 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2015
2016 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002017 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002018 CHECK_EQ(err, OK);
2019 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002020 } else {
2021 // We're flushing both ports in preparation for seeking.
2022
2023 if (mPortStatus[kPortIndexInput] == ENABLED
2024 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002025 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07002026 " seek-time.");
2027
Andreas Huber1f24b302010-06-10 11:12:39 -07002028 // We implicitly resume pulling on our upstream source.
2029 mPaused = false;
2030
Andreas Huberbe06d262009-08-14 14:37:10 -07002031 drainInputBuffers();
2032 fillOutputBuffers();
2033 }
2034 }
2035
2036 break;
2037 }
2038
2039 default:
2040 {
Andreas Huber4c483422009-09-02 16:05:36 -07002041 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07002042 break;
2043 }
2044 }
2045}
2046
2047void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002048 CODEC_LOGV("onStateChange %d", newState);
2049
Andreas Huberbe06d262009-08-14 14:37:10 -07002050 switch (newState) {
2051 case OMX_StateIdle:
2052 {
Andreas Huber4c483422009-09-02 16:05:36 -07002053 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002054 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07002055 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002056 mNode, OMX_CommandStateSet, OMX_StateExecuting);
2057
2058 CHECK_EQ(err, OK);
2059
2060 setState(IDLE_TO_EXECUTING);
2061 } else {
2062 CHECK_EQ(mState, EXECUTING_TO_IDLE);
2063
2064 CHECK_EQ(
2065 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
2066 mPortBuffers[kPortIndexInput].size());
2067
2068 CHECK_EQ(
2069 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
2070 mPortBuffers[kPortIndexOutput].size());
2071
Andreas Huber784202e2009-10-15 13:46:54 -07002072 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002073 mNode, OMX_CommandStateSet, OMX_StateLoaded);
2074
2075 CHECK_EQ(err, OK);
2076
2077 err = freeBuffersOnPort(kPortIndexInput);
2078 CHECK_EQ(err, OK);
2079
2080 err = freeBuffersOnPort(kPortIndexOutput);
2081 CHECK_EQ(err, OK);
2082
2083 mPortStatus[kPortIndexInput] = ENABLED;
2084 mPortStatus[kPortIndexOutput] = ENABLED;
2085
2086 setState(IDLE_TO_LOADED);
2087 }
2088 break;
2089 }
2090
2091 case OMX_StateExecuting:
2092 {
2093 CHECK_EQ(mState, IDLE_TO_EXECUTING);
2094
Andreas Huber4c483422009-09-02 16:05:36 -07002095 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002096
2097 setState(EXECUTING);
2098
Andreas Huber42978e52009-08-27 10:08:39 -07002099 // Buffers will be submitted to the component in the first
2100 // call to OMXCodec::read as mInitialBufferSubmit is true at
2101 // this point. This ensures that this on_message call returns,
2102 // releases the lock and ::init can notice the state change and
2103 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07002104 break;
2105 }
2106
2107 case OMX_StateLoaded:
2108 {
2109 CHECK_EQ(mState, IDLE_TO_LOADED);
2110
Andreas Huber4c483422009-09-02 16:05:36 -07002111 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002112
2113 setState(LOADED);
2114 break;
2115 }
2116
Andreas Huberc712b9f2010-01-20 15:05:46 -08002117 case OMX_StateInvalid:
2118 {
2119 setState(ERROR);
2120 break;
2121 }
2122
Andreas Huberbe06d262009-08-14 14:37:10 -07002123 default:
2124 {
2125 CHECK(!"should not be here.");
2126 break;
2127 }
2128 }
2129}
2130
2131// static
2132size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
2133 size_t n = 0;
2134 for (size_t i = 0; i < buffers.size(); ++i) {
2135 if (!buffers[i].mOwnedByComponent) {
2136 ++n;
2137 }
2138 }
2139
2140 return n;
2141}
2142
2143status_t OMXCodec::freeBuffersOnPort(
2144 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2145 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2146
2147 status_t stickyErr = OK;
2148
2149 for (size_t i = buffers->size(); i-- > 0;) {
2150 BufferInfo *info = &buffers->editItemAt(i);
2151
2152 if (onlyThoseWeOwn && info->mOwnedByComponent) {
2153 continue;
2154 }
2155
2156 CHECK_EQ(info->mOwnedByComponent, false);
2157
Andreas Huber92022852009-09-14 15:24:14 -07002158 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2159
Andreas Huberbe06d262009-08-14 14:37:10 -07002160 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002161 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07002162
2163 if (err != OK) {
2164 stickyErr = err;
2165 }
2166
2167 if (info->mMediaBuffer != NULL) {
2168 info->mMediaBuffer->setObserver(NULL);
2169
2170 // Make sure nobody but us owns this buffer at this point.
2171 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2172
2173 info->mMediaBuffer->release();
2174 }
2175
2176 buffers->removeAt(i);
2177 }
2178
2179 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2180
2181 return stickyErr;
2182}
2183
2184void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002185 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002186
2187 CHECK_EQ(mState, EXECUTING);
2188 CHECK_EQ(portIndex, kPortIndexOutput);
2189 setState(RECONFIGURING);
2190
2191 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002192 if (!flushPortAsync(portIndex)) {
2193 onCmdComplete(OMX_CommandFlush, portIndex);
2194 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002195 } else {
2196 disablePortAsync(portIndex);
2197 }
2198}
2199
Andreas Huber404cc412009-08-25 14:26:05 -07002200bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002201 CHECK(mState == EXECUTING || mState == RECONFIGURING
2202 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002203
Andreas Huber4c483422009-09-02 16:05:36 -07002204 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002205 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2206 mPortBuffers[portIndex].size());
2207
Andreas Huberbe06d262009-08-14 14:37:10 -07002208 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2209 mPortStatus[portIndex] = SHUTTING_DOWN;
2210
Andreas Huber404cc412009-08-25 14:26:05 -07002211 if ((mQuirks & kRequiresFlushCompleteEmulation)
2212 && countBuffersWeOwn(mPortBuffers[portIndex])
2213 == mPortBuffers[portIndex].size()) {
2214 // No flush is necessary and this component fails to send a
2215 // flush-complete event in this case.
2216
2217 return false;
2218 }
2219
Andreas Huberbe06d262009-08-14 14:37:10 -07002220 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002221 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002222 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002223
2224 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002225}
2226
2227void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2228 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2229
2230 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2231 mPortStatus[portIndex] = DISABLING;
2232
Andreas Huberd222c842010-08-26 14:29:34 -07002233 CODEC_LOGV("sending OMX_CommandPortDisable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002234 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002235 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002236 CHECK_EQ(err, OK);
2237
2238 freeBuffersOnPort(portIndex, true);
2239}
2240
2241void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2242 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2243
2244 CHECK_EQ(mPortStatus[portIndex], DISABLED);
2245 mPortStatus[portIndex] = ENABLING;
2246
2247 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002248 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002249 CHECK_EQ(err, OK);
2250}
2251
2252void OMXCodec::fillOutputBuffers() {
2253 CHECK_EQ(mState, EXECUTING);
2254
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002255 // This is a workaround for some decoders not properly reporting
2256 // end-of-output-stream. If we own all input buffers and also own
2257 // all output buffers and we already signalled end-of-input-stream,
2258 // the end-of-output-stream is implied.
2259 if (mSignalledEOS
2260 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2261 == mPortBuffers[kPortIndexInput].size()
2262 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2263 == mPortBuffers[kPortIndexOutput].size()) {
2264 mNoMoreOutputData = true;
2265 mBufferFilled.signal();
2266
2267 return;
2268 }
2269
Andreas Huberbe06d262009-08-14 14:37:10 -07002270 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2271 for (size_t i = 0; i < buffers->size(); ++i) {
2272 fillOutputBuffer(&buffers->editItemAt(i));
2273 }
2274}
2275
2276void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002277 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002278
2279 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2280 for (size_t i = 0; i < buffers->size(); ++i) {
2281 drainInputBuffer(&buffers->editItemAt(i));
2282 }
2283}
2284
2285void OMXCodec::drainInputBuffer(BufferInfo *info) {
2286 CHECK_EQ(info->mOwnedByComponent, false);
2287
2288 if (mSignalledEOS) {
2289 return;
2290 }
2291
2292 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2293 const CodecSpecificData *specific =
2294 mCodecSpecificData[mCodecSpecificDataIndex];
2295
2296 size_t size = specific->mSize;
2297
Andreas Hubere6c40962009-09-10 14:13:30 -07002298 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002299 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002300 static const uint8_t kNALStartCode[4] =
2301 { 0x00, 0x00, 0x00, 0x01 };
2302
Andreas Huberc712b9f2010-01-20 15:05:46 -08002303 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002304
2305 size += 4;
2306
Andreas Huberc712b9f2010-01-20 15:05:46 -08002307 memcpy(info->mData, kNALStartCode, 4);
2308 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002309 specific->mData, specific->mSize);
2310 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002311 CHECK(info->mSize >= specific->mSize);
2312 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002313 }
2314
Andreas Huber2ea14e22009-12-16 09:30:55 -08002315 mNoMoreOutputData = false;
2316
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002317 CODEC_LOGV("calling emptyBuffer with codec specific data");
2318
Andreas Huber784202e2009-10-15 13:46:54 -07002319 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002320 mNode, info->mBuffer, 0, size,
2321 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2322 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002323 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002324
2325 info->mOwnedByComponent = true;
2326
2327 ++mCodecSpecificDataIndex;
2328 return;
2329 }
2330
Andreas Huber1f24b302010-06-10 11:12:39 -07002331 if (mPaused) {
2332 return;
2333 }
2334
Andreas Huberbe06d262009-08-14 14:37:10 -07002335 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002336
Andreas Hubera4357ad2010-04-02 12:49:54 -07002337 bool signalEOS = false;
2338 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002339
Andreas Hubera4357ad2010-04-02 12:49:54 -07002340 size_t offset = 0;
2341 int32_t n = 0;
2342 for (;;) {
2343 MediaBuffer *srcBuffer;
James Dong53d4e0d2010-07-21 14:51:35 -07002344 MediaSource::ReadOptions options;
2345 if (mSkipTimeUs >= 0) {
2346 options.setSkipFrame(mSkipTimeUs);
2347 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002348 if (mSeekTimeUs >= 0) {
2349 if (mLeftOverBuffer) {
2350 mLeftOverBuffer->release();
2351 mLeftOverBuffer = NULL;
2352 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002353 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002354
2355 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002356 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002357 mBufferFilled.signal();
2358
2359 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002360
2361 if (err == OK) {
2362 int64_t targetTimeUs;
2363 if (srcBuffer->meta_data()->findInt64(
2364 kKeyTargetTime, &targetTimeUs)
2365 && targetTimeUs >= 0) {
2366 mTargetTimeUs = targetTimeUs;
2367 } else {
2368 mTargetTimeUs = -1;
2369 }
2370 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002371 } else if (mLeftOverBuffer) {
2372 srcBuffer = mLeftOverBuffer;
2373 mLeftOverBuffer = NULL;
2374
2375 err = OK;
2376 } else {
James Dong53d4e0d2010-07-21 14:51:35 -07002377 err = mSource->read(&srcBuffer, &options);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002378 }
2379
2380 if (err != OK) {
2381 signalEOS = true;
2382 mFinalStatus = err;
2383 mSignalledEOS = true;
2384 break;
2385 }
2386
2387 size_t remainingBytes = info->mSize - offset;
2388
2389 if (srcBuffer->range_length() > remainingBytes) {
2390 if (offset == 0) {
2391 CODEC_LOGE(
2392 "Codec's input buffers are too small to accomodate "
2393 "buffer read from source (info->mSize = %d, srcLength = %d)",
2394 info->mSize, srcBuffer->range_length());
2395
2396 srcBuffer->release();
2397 srcBuffer = NULL;
2398
2399 setState(ERROR);
2400 return;
2401 }
2402
2403 mLeftOverBuffer = srcBuffer;
2404 break;
2405 }
2406
James Dong4f501f02010-06-07 14:41:41 -07002407 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2408 CHECK(mOMXLivesLocally && offset == 0);
2409 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2410 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2411 } else {
2412 memcpy((uint8_t *)info->mData + offset,
2413 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2414 srcBuffer->range_length());
2415 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002416
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002417 int64_t lastBufferTimeUs;
2418 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002419 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002420
Andreas Hubera4357ad2010-04-02 12:49:54 -07002421 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002422 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002423 }
2424
2425 offset += srcBuffer->range_length();
2426
2427 srcBuffer->release();
2428 srcBuffer = NULL;
2429
2430 ++n;
2431
2432 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2433 break;
2434 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002435
2436 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2437
2438 if (coalescedDurationUs > 250000ll) {
2439 // Don't coalesce more than 250ms worth of encoded data at once.
2440 break;
2441 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002442 }
2443
2444 if (n > 1) {
2445 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002446 }
2447
2448 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002449
Andreas Hubera4357ad2010-04-02 12:49:54 -07002450 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002451 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002452 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002453 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002454 }
2455
Andreas Hubera4357ad2010-04-02 12:49:54 -07002456 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2457 "timestamp %lld us (%.2f secs)",
2458 info->mBuffer, offset,
2459 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002460
Andreas Huber784202e2009-10-15 13:46:54 -07002461 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002462 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002463 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002464
2465 if (err != OK) {
2466 setState(ERROR);
2467 return;
2468 }
2469
2470 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002471
2472 // This component does not ever signal the EOS flag on output buffers,
2473 // Thanks for nothing.
2474 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2475 mNoMoreOutputData = true;
2476 mBufferFilled.signal();
2477 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002478}
2479
2480void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2481 CHECK_EQ(info->mOwnedByComponent, false);
2482
Andreas Huber404cc412009-08-25 14:26:05 -07002483 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002484 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002485 "calling fillOutputBuffer");
2486 return;
2487 }
2488
Andreas Huber4c483422009-09-02 16:05:36 -07002489 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002490 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002491
2492 if (err != OK) {
2493 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2494
2495 setState(ERROR);
2496 return;
2497 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002498
2499 info->mOwnedByComponent = true;
2500}
2501
2502void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2503 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2504 for (size_t i = 0; i < buffers->size(); ++i) {
2505 if ((*buffers)[i].mBuffer == buffer) {
2506 drainInputBuffer(&buffers->editItemAt(i));
2507 return;
2508 }
2509 }
2510
2511 CHECK(!"should not be here.");
2512}
2513
2514void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2515 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2516 for (size_t i = 0; i < buffers->size(); ++i) {
2517 if ((*buffers)[i].mBuffer == buffer) {
2518 fillOutputBuffer(&buffers->editItemAt(i));
2519 return;
2520 }
2521 }
2522
2523 CHECK(!"should not be here.");
2524}
2525
2526void OMXCodec::setState(State newState) {
2527 mState = newState;
2528 mAsyncCompletion.signal();
2529
2530 // This may cause some spurious wakeups but is necessary to
2531 // unblock the reader if we enter ERROR state.
2532 mBufferFilled.signal();
2533}
2534
Andreas Huberda050cf22009-09-02 14:01:43 -07002535void OMXCodec::setRawAudioFormat(
2536 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002537
2538 // port definition
2539 OMX_PARAM_PORTDEFINITIONTYPE def;
2540 InitOMXParams(&def);
2541 def.nPortIndex = portIndex;
2542 status_t err = mOMX->getParameter(
2543 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2544 CHECK_EQ(err, OK);
2545 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2546 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2547 &def, sizeof(def)), OK);
2548
2549 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002550 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002551 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002552 pcmParams.nPortIndex = portIndex;
2553
James Dongabed93a2010-04-22 17:27:04 -07002554 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002555 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2556
2557 CHECK_EQ(err, OK);
2558
2559 pcmParams.nChannels = numChannels;
2560 pcmParams.eNumData = OMX_NumericalDataSigned;
2561 pcmParams.bInterleaved = OMX_TRUE;
2562 pcmParams.nBitPerSample = 16;
2563 pcmParams.nSamplingRate = sampleRate;
2564 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2565
2566 if (numChannels == 1) {
2567 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2568 } else {
2569 CHECK_EQ(numChannels, 2);
2570
2571 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2572 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2573 }
2574
Andreas Huber784202e2009-10-15 13:46:54 -07002575 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002576 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2577
2578 CHECK_EQ(err, OK);
2579}
2580
James Dong17299ab2010-05-14 15:45:22 -07002581static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2582 if (isAMRWB) {
2583 if (bps <= 6600) {
2584 return OMX_AUDIO_AMRBandModeWB0;
2585 } else if (bps <= 8850) {
2586 return OMX_AUDIO_AMRBandModeWB1;
2587 } else if (bps <= 12650) {
2588 return OMX_AUDIO_AMRBandModeWB2;
2589 } else if (bps <= 14250) {
2590 return OMX_AUDIO_AMRBandModeWB3;
2591 } else if (bps <= 15850) {
2592 return OMX_AUDIO_AMRBandModeWB4;
2593 } else if (bps <= 18250) {
2594 return OMX_AUDIO_AMRBandModeWB5;
2595 } else if (bps <= 19850) {
2596 return OMX_AUDIO_AMRBandModeWB6;
2597 } else if (bps <= 23050) {
2598 return OMX_AUDIO_AMRBandModeWB7;
2599 }
2600
2601 // 23850 bps
2602 return OMX_AUDIO_AMRBandModeWB8;
2603 } else { // AMRNB
2604 if (bps <= 4750) {
2605 return OMX_AUDIO_AMRBandModeNB0;
2606 } else if (bps <= 5150) {
2607 return OMX_AUDIO_AMRBandModeNB1;
2608 } else if (bps <= 5900) {
2609 return OMX_AUDIO_AMRBandModeNB2;
2610 } else if (bps <= 6700) {
2611 return OMX_AUDIO_AMRBandModeNB3;
2612 } else if (bps <= 7400) {
2613 return OMX_AUDIO_AMRBandModeNB4;
2614 } else if (bps <= 7950) {
2615 return OMX_AUDIO_AMRBandModeNB5;
2616 } else if (bps <= 10200) {
2617 return OMX_AUDIO_AMRBandModeNB6;
2618 }
2619
2620 // 12200 bps
2621 return OMX_AUDIO_AMRBandModeNB7;
2622 }
2623}
2624
2625void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002626 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002627
Andreas Huber8768f2c2009-12-01 15:26:54 -08002628 OMX_AUDIO_PARAM_AMRTYPE def;
2629 InitOMXParams(&def);
2630 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002631
Andreas Huber8768f2c2009-12-01 15:26:54 -08002632 status_t err =
2633 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002634
Andreas Huber8768f2c2009-12-01 15:26:54 -08002635 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002636
Andreas Huber8768f2c2009-12-01 15:26:54 -08002637 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002638
James Dong17299ab2010-05-14 15:45:22 -07002639 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002640 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2641 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002642
2643 ////////////////////////
2644
2645 if (mIsEncoder) {
2646 sp<MetaData> format = mSource->getFormat();
2647 int32_t sampleRate;
2648 int32_t numChannels;
2649 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2650 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2651
2652 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2653 }
2654}
2655
James Dong17299ab2010-05-14 15:45:22 -07002656void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002657 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002658 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002659 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002660 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002661
2662 //////////////// output port ////////////////////
2663 // format
2664 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2665 format.nPortIndex = kPortIndexOutput;
2666 format.nIndex = 0;
2667 status_t err = OMX_ErrorNone;
2668 while (OMX_ErrorNone == err) {
2669 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2670 &format, sizeof(format)), OK);
2671 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2672 break;
2673 }
2674 format.nIndex++;
2675 }
2676 CHECK_EQ(OK, err);
2677 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2678 &format, sizeof(format)), OK);
2679
2680 // port definition
2681 OMX_PARAM_PORTDEFINITIONTYPE def;
2682 InitOMXParams(&def);
2683 def.nPortIndex = kPortIndexOutput;
2684 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2685 &def, sizeof(def)), OK);
2686 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2687 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2688 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2689 &def, sizeof(def)), OK);
2690
2691 // profile
2692 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2693 InitOMXParams(&profile);
2694 profile.nPortIndex = kPortIndexOutput;
2695 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2696 &profile, sizeof(profile)), OK);
2697 profile.nChannels = numChannels;
2698 profile.eChannelMode = (numChannels == 1?
2699 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2700 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002701 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002702 profile.nAudioBandWidth = 0;
2703 profile.nFrameLength = 0;
2704 profile.nAACtools = OMX_AUDIO_AACToolAll;
2705 profile.nAACERtools = OMX_AUDIO_AACERNone;
2706 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2707 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2708 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2709 &profile, sizeof(profile)), OK);
2710
Andreas Huberda050cf22009-09-02 14:01:43 -07002711 } else {
2712 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002713 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002714 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002715
Andreas Huber784202e2009-10-15 13:46:54 -07002716 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002717 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2718 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002719
Andreas Huberda050cf22009-09-02 14:01:43 -07002720 profile.nChannels = numChannels;
2721 profile.nSampleRate = sampleRate;
2722 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002723
Andreas Huber784202e2009-10-15 13:46:54 -07002724 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002725 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2726 CHECK_EQ(err, OK);
2727 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002728}
2729
2730void OMXCodec::setImageOutputFormat(
2731 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002732 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002733
2734#if 0
2735 OMX_INDEXTYPE index;
2736 status_t err = mOMX->get_extension_index(
2737 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2738 CHECK_EQ(err, OK);
2739
2740 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2741 CHECK_EQ(err, OK);
2742#endif
2743
2744 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002745 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002746 def.nPortIndex = kPortIndexOutput;
2747
Andreas Huber784202e2009-10-15 13:46:54 -07002748 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002749 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2750 CHECK_EQ(err, OK);
2751
2752 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2753
2754 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002755
Andreas Huberbe06d262009-08-14 14:37:10 -07002756 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2757 imageDef->eColorFormat = format;
2758 imageDef->nFrameWidth = width;
2759 imageDef->nFrameHeight = height;
2760
2761 switch (format) {
2762 case OMX_COLOR_FormatYUV420PackedPlanar:
2763 case OMX_COLOR_FormatYUV411Planar:
2764 {
2765 def.nBufferSize = (width * height * 3) / 2;
2766 break;
2767 }
2768
2769 case OMX_COLOR_FormatCbYCrY:
2770 {
2771 def.nBufferSize = width * height * 2;
2772 break;
2773 }
2774
2775 case OMX_COLOR_Format32bitARGB8888:
2776 {
2777 def.nBufferSize = width * height * 4;
2778 break;
2779 }
2780
Andreas Huber201511c2009-09-08 14:01:44 -07002781 case OMX_COLOR_Format16bitARGB4444:
2782 case OMX_COLOR_Format16bitARGB1555:
2783 case OMX_COLOR_Format16bitRGB565:
2784 case OMX_COLOR_Format16bitBGR565:
2785 {
2786 def.nBufferSize = width * height * 2;
2787 break;
2788 }
2789
Andreas Huberbe06d262009-08-14 14:37:10 -07002790 default:
2791 CHECK(!"Should not be here. Unknown color format.");
2792 break;
2793 }
2794
Andreas Huber5c0a9132009-08-20 11:16:40 -07002795 def.nBufferCountActual = def.nBufferCountMin;
2796
Andreas Huber784202e2009-10-15 13:46:54 -07002797 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002798 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2799 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002800}
Andreas Huberbe06d262009-08-14 14:37:10 -07002801
Andreas Huber5c0a9132009-08-20 11:16:40 -07002802void OMXCodec::setJPEGInputFormat(
2803 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2804 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002805 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002806 def.nPortIndex = kPortIndexInput;
2807
Andreas Huber784202e2009-10-15 13:46:54 -07002808 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002809 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2810 CHECK_EQ(err, OK);
2811
Andreas Huber5c0a9132009-08-20 11:16:40 -07002812 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2813 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2814
Andreas Huberbe06d262009-08-14 14:37:10 -07002815 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2816 imageDef->nFrameWidth = width;
2817 imageDef->nFrameHeight = height;
2818
Andreas Huber5c0a9132009-08-20 11:16:40 -07002819 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002820 def.nBufferCountActual = def.nBufferCountMin;
2821
Andreas Huber784202e2009-10-15 13:46:54 -07002822 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002823 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2824 CHECK_EQ(err, OK);
2825}
2826
2827void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2828 CodecSpecificData *specific =
2829 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2830
2831 specific->mSize = size;
2832 memcpy(specific->mData, data, size);
2833
2834 mCodecSpecificData.push(specific);
2835}
2836
2837void OMXCodec::clearCodecSpecificData() {
2838 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2839 free(mCodecSpecificData.editItemAt(i));
2840 }
2841 mCodecSpecificData.clear();
2842 mCodecSpecificDataIndex = 0;
2843}
2844
James Dong36e573b2010-06-19 09:04:18 -07002845status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07002846 Mutex::Autolock autoLock(mLock);
2847
Andreas Huberbe06d262009-08-14 14:37:10 -07002848 if (mState != LOADED) {
2849 return UNKNOWN_ERROR;
2850 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002851
Andreas Huberbe06d262009-08-14 14:37:10 -07002852 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002853 if (mQuirks & kWantsNALFragments) {
2854 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002855 }
James Dong36e573b2010-06-19 09:04:18 -07002856 if (meta) {
2857 int64_t startTimeUs = 0;
2858 int64_t timeUs;
2859 if (meta->findInt64(kKeyTime, &timeUs)) {
2860 startTimeUs = timeUs;
2861 }
2862 params->setInt64(kKeyTime, startTimeUs);
2863 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002864 status_t err = mSource->start(params.get());
2865
2866 if (err != OK) {
2867 return err;
2868 }
2869
2870 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002871 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002872 mSignalledEOS = false;
2873 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002874 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002875 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002876 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
2877 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07002878 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07002879 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002880
2881 return init();
2882}
2883
2884status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002885 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002886
2887 Mutex::Autolock autoLock(mLock);
2888
2889 while (isIntermediateState(mState)) {
2890 mAsyncCompletion.wait(mLock);
2891 }
2892
2893 switch (mState) {
2894 case LOADED:
2895 case ERROR:
2896 break;
2897
2898 case EXECUTING:
2899 {
2900 setState(EXECUTING_TO_IDLE);
2901
Andreas Huber127fcdc2009-08-26 16:27:02 -07002902 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002903 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002904 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002905
Andreas Huber127fcdc2009-08-26 16:27:02 -07002906 bool emulateInputFlushCompletion =
2907 !flushPortAsync(kPortIndexInput);
2908
2909 bool emulateOutputFlushCompletion =
2910 !flushPortAsync(kPortIndexOutput);
2911
2912 if (emulateInputFlushCompletion) {
2913 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2914 }
2915
2916 if (emulateOutputFlushCompletion) {
2917 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2918 }
2919 } else {
2920 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2921 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2922
2923 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002924 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002925 CHECK_EQ(err, OK);
2926 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002927
2928 while (mState != LOADED && mState != ERROR) {
2929 mAsyncCompletion.wait(mLock);
2930 }
2931
2932 break;
2933 }
2934
2935 default:
2936 {
2937 CHECK(!"should not be here.");
2938 break;
2939 }
2940 }
2941
Andreas Hubera4357ad2010-04-02 12:49:54 -07002942 if (mLeftOverBuffer) {
2943 mLeftOverBuffer->release();
2944 mLeftOverBuffer = NULL;
2945 }
2946
Andreas Huberbe06d262009-08-14 14:37:10 -07002947 mSource->stop();
2948
Andreas Huber4a9375e2010-02-09 11:54:33 -08002949 CODEC_LOGV("stopped");
2950
Andreas Huberbe06d262009-08-14 14:37:10 -07002951 return OK;
2952}
2953
2954sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002955 Mutex::Autolock autoLock(mLock);
2956
Andreas Huberbe06d262009-08-14 14:37:10 -07002957 return mOutputFormat;
2958}
2959
2960status_t OMXCodec::read(
2961 MediaBuffer **buffer, const ReadOptions *options) {
2962 *buffer = NULL;
2963
2964 Mutex::Autolock autoLock(mLock);
2965
Andreas Huberd06e5b82009-08-28 13:18:14 -07002966 if (mState != EXECUTING && mState != RECONFIGURING) {
2967 return UNKNOWN_ERROR;
2968 }
2969
Andreas Hubere981c332009-10-22 13:49:30 -07002970 bool seeking = false;
2971 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002972 ReadOptions::SeekMode seekMode;
2973 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07002974 seeking = true;
2975 }
James Dong53d4e0d2010-07-21 14:51:35 -07002976 int64_t skipTimeUs;
2977 if (options && options->getSkipFrame(&skipTimeUs)) {
2978 mSkipTimeUs = skipTimeUs;
2979 } else {
2980 mSkipTimeUs = -1;
2981 }
Andreas Hubere981c332009-10-22 13:49:30 -07002982
Andreas Huber42978e52009-08-27 10:08:39 -07002983 if (mInitialBufferSubmit) {
2984 mInitialBufferSubmit = false;
2985
Andreas Hubere981c332009-10-22 13:49:30 -07002986 if (seeking) {
2987 CHECK(seekTimeUs >= 0);
2988 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002989 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07002990
2991 // There's no reason to trigger the code below, there's
2992 // nothing to flush yet.
2993 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002994 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07002995 }
2996
Andreas Huber42978e52009-08-27 10:08:39 -07002997 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002998
Andreas Huberd06e5b82009-08-28 13:18:14 -07002999 if (mState == EXECUTING) {
3000 // Otherwise mState == RECONFIGURING and this code will trigger
3001 // after the output port is reenabled.
3002 fillOutputBuffers();
3003 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003004 }
3005
Andreas Hubere981c332009-10-22 13:49:30 -07003006 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07003007 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07003008
3009 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003010
3011 CHECK(seekTimeUs >= 0);
3012 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003013 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07003014
3015 mFilledBuffers.clear();
3016
3017 CHECK_EQ(mState, EXECUTING);
3018
Andreas Huber404cc412009-08-25 14:26:05 -07003019 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
3020 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
3021
3022 if (emulateInputFlushCompletion) {
3023 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3024 }
3025
3026 if (emulateOutputFlushCompletion) {
3027 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3028 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08003029
3030 while (mSeekTimeUs >= 0) {
3031 mBufferFilled.wait(mLock);
3032 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003033 }
3034
3035 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
3036 mBufferFilled.wait(mLock);
3037 }
3038
3039 if (mState == ERROR) {
3040 return UNKNOWN_ERROR;
3041 }
3042
3043 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08003044 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07003045 }
3046
Andreas Hubercfd55572009-10-09 14:11:28 -07003047 if (mOutputPortSettingsHaveChanged) {
3048 mOutputPortSettingsHaveChanged = false;
3049
3050 return INFO_FORMAT_CHANGED;
3051 }
3052
Andreas Huberbe06d262009-08-14 14:37:10 -07003053 size_t index = *mFilledBuffers.begin();
3054 mFilledBuffers.erase(mFilledBuffers.begin());
3055
3056 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
3057 info->mMediaBuffer->add_ref();
3058 *buffer = info->mMediaBuffer;
3059
3060 return OK;
3061}
3062
3063void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
3064 Mutex::Autolock autoLock(mLock);
3065
3066 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
3067 for (size_t i = 0; i < buffers->size(); ++i) {
3068 BufferInfo *info = &buffers->editItemAt(i);
3069
3070 if (info->mMediaBuffer == buffer) {
3071 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
3072 fillOutputBuffer(info);
3073 return;
3074 }
3075 }
3076
3077 CHECK(!"should not be here.");
3078}
3079
3080static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
3081 static const char *kNames[] = {
3082 "OMX_IMAGE_CodingUnused",
3083 "OMX_IMAGE_CodingAutoDetect",
3084 "OMX_IMAGE_CodingJPEG",
3085 "OMX_IMAGE_CodingJPEG2K",
3086 "OMX_IMAGE_CodingEXIF",
3087 "OMX_IMAGE_CodingTIFF",
3088 "OMX_IMAGE_CodingGIF",
3089 "OMX_IMAGE_CodingPNG",
3090 "OMX_IMAGE_CodingLZW",
3091 "OMX_IMAGE_CodingBMP",
3092 };
3093
3094 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3095
3096 if (type < 0 || (size_t)type >= numNames) {
3097 return "UNKNOWN";
3098 } else {
3099 return kNames[type];
3100 }
3101}
3102
3103static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
3104 static const char *kNames[] = {
3105 "OMX_COLOR_FormatUnused",
3106 "OMX_COLOR_FormatMonochrome",
3107 "OMX_COLOR_Format8bitRGB332",
3108 "OMX_COLOR_Format12bitRGB444",
3109 "OMX_COLOR_Format16bitARGB4444",
3110 "OMX_COLOR_Format16bitARGB1555",
3111 "OMX_COLOR_Format16bitRGB565",
3112 "OMX_COLOR_Format16bitBGR565",
3113 "OMX_COLOR_Format18bitRGB666",
3114 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003115 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07003116 "OMX_COLOR_Format24bitRGB888",
3117 "OMX_COLOR_Format24bitBGR888",
3118 "OMX_COLOR_Format24bitARGB1887",
3119 "OMX_COLOR_Format25bitARGB1888",
3120 "OMX_COLOR_Format32bitBGRA8888",
3121 "OMX_COLOR_Format32bitARGB8888",
3122 "OMX_COLOR_FormatYUV411Planar",
3123 "OMX_COLOR_FormatYUV411PackedPlanar",
3124 "OMX_COLOR_FormatYUV420Planar",
3125 "OMX_COLOR_FormatYUV420PackedPlanar",
3126 "OMX_COLOR_FormatYUV420SemiPlanar",
3127 "OMX_COLOR_FormatYUV422Planar",
3128 "OMX_COLOR_FormatYUV422PackedPlanar",
3129 "OMX_COLOR_FormatYUV422SemiPlanar",
3130 "OMX_COLOR_FormatYCbYCr",
3131 "OMX_COLOR_FormatYCrYCb",
3132 "OMX_COLOR_FormatCbYCrY",
3133 "OMX_COLOR_FormatCrYCbY",
3134 "OMX_COLOR_FormatYUV444Interleaved",
3135 "OMX_COLOR_FormatRawBayer8bit",
3136 "OMX_COLOR_FormatRawBayer10bit",
3137 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003138 "OMX_COLOR_FormatL2",
3139 "OMX_COLOR_FormatL4",
3140 "OMX_COLOR_FormatL8",
3141 "OMX_COLOR_FormatL16",
3142 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07003143 "OMX_COLOR_FormatL32",
3144 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
3145 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
3146 "OMX_COLOR_Format18BitBGR666",
3147 "OMX_COLOR_Format24BitARGB6666",
3148 "OMX_COLOR_Format24BitABGR6666",
3149 };
3150
3151 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3152
Andreas Huberbe06d262009-08-14 14:37:10 -07003153 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3154 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3155 } else if (type < 0 || (size_t)type >= numNames) {
3156 return "UNKNOWN";
3157 } else {
3158 return kNames[type];
3159 }
3160}
3161
3162static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3163 static const char *kNames[] = {
3164 "OMX_VIDEO_CodingUnused",
3165 "OMX_VIDEO_CodingAutoDetect",
3166 "OMX_VIDEO_CodingMPEG2",
3167 "OMX_VIDEO_CodingH263",
3168 "OMX_VIDEO_CodingMPEG4",
3169 "OMX_VIDEO_CodingWMV",
3170 "OMX_VIDEO_CodingRV",
3171 "OMX_VIDEO_CodingAVC",
3172 "OMX_VIDEO_CodingMJPEG",
3173 };
3174
3175 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3176
3177 if (type < 0 || (size_t)type >= numNames) {
3178 return "UNKNOWN";
3179 } else {
3180 return kNames[type];
3181 }
3182}
3183
3184static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3185 static const char *kNames[] = {
3186 "OMX_AUDIO_CodingUnused",
3187 "OMX_AUDIO_CodingAutoDetect",
3188 "OMX_AUDIO_CodingPCM",
3189 "OMX_AUDIO_CodingADPCM",
3190 "OMX_AUDIO_CodingAMR",
3191 "OMX_AUDIO_CodingGSMFR",
3192 "OMX_AUDIO_CodingGSMEFR",
3193 "OMX_AUDIO_CodingGSMHR",
3194 "OMX_AUDIO_CodingPDCFR",
3195 "OMX_AUDIO_CodingPDCEFR",
3196 "OMX_AUDIO_CodingPDCHR",
3197 "OMX_AUDIO_CodingTDMAFR",
3198 "OMX_AUDIO_CodingTDMAEFR",
3199 "OMX_AUDIO_CodingQCELP8",
3200 "OMX_AUDIO_CodingQCELP13",
3201 "OMX_AUDIO_CodingEVRC",
3202 "OMX_AUDIO_CodingSMV",
3203 "OMX_AUDIO_CodingG711",
3204 "OMX_AUDIO_CodingG723",
3205 "OMX_AUDIO_CodingG726",
3206 "OMX_AUDIO_CodingG729",
3207 "OMX_AUDIO_CodingAAC",
3208 "OMX_AUDIO_CodingMP3",
3209 "OMX_AUDIO_CodingSBC",
3210 "OMX_AUDIO_CodingVORBIS",
3211 "OMX_AUDIO_CodingWMA",
3212 "OMX_AUDIO_CodingRA",
3213 "OMX_AUDIO_CodingMIDI",
3214 };
3215
3216 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3217
3218 if (type < 0 || (size_t)type >= numNames) {
3219 return "UNKNOWN";
3220 } else {
3221 return kNames[type];
3222 }
3223}
3224
3225static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3226 static const char *kNames[] = {
3227 "OMX_AUDIO_PCMModeLinear",
3228 "OMX_AUDIO_PCMModeALaw",
3229 "OMX_AUDIO_PCMModeMULaw",
3230 };
3231
3232 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3233
3234 if (type < 0 || (size_t)type >= numNames) {
3235 return "UNKNOWN";
3236 } else {
3237 return kNames[type];
3238 }
3239}
3240
Andreas Huber7ae02c82009-09-09 16:29:47 -07003241static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3242 static const char *kNames[] = {
3243 "OMX_AUDIO_AMRBandModeUnused",
3244 "OMX_AUDIO_AMRBandModeNB0",
3245 "OMX_AUDIO_AMRBandModeNB1",
3246 "OMX_AUDIO_AMRBandModeNB2",
3247 "OMX_AUDIO_AMRBandModeNB3",
3248 "OMX_AUDIO_AMRBandModeNB4",
3249 "OMX_AUDIO_AMRBandModeNB5",
3250 "OMX_AUDIO_AMRBandModeNB6",
3251 "OMX_AUDIO_AMRBandModeNB7",
3252 "OMX_AUDIO_AMRBandModeWB0",
3253 "OMX_AUDIO_AMRBandModeWB1",
3254 "OMX_AUDIO_AMRBandModeWB2",
3255 "OMX_AUDIO_AMRBandModeWB3",
3256 "OMX_AUDIO_AMRBandModeWB4",
3257 "OMX_AUDIO_AMRBandModeWB5",
3258 "OMX_AUDIO_AMRBandModeWB6",
3259 "OMX_AUDIO_AMRBandModeWB7",
3260 "OMX_AUDIO_AMRBandModeWB8",
3261 };
3262
3263 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3264
3265 if (type < 0 || (size_t)type >= numNames) {
3266 return "UNKNOWN";
3267 } else {
3268 return kNames[type];
3269 }
3270}
3271
3272static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3273 static const char *kNames[] = {
3274 "OMX_AUDIO_AMRFrameFormatConformance",
3275 "OMX_AUDIO_AMRFrameFormatIF1",
3276 "OMX_AUDIO_AMRFrameFormatIF2",
3277 "OMX_AUDIO_AMRFrameFormatFSF",
3278 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3279 "OMX_AUDIO_AMRFrameFormatITU",
3280 };
3281
3282 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3283
3284 if (type < 0 || (size_t)type >= numNames) {
3285 return "UNKNOWN";
3286 } else {
3287 return kNames[type];
3288 }
3289}
Andreas Huberbe06d262009-08-14 14:37:10 -07003290
3291void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3292 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003293 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003294 def.nPortIndex = portIndex;
3295
Andreas Huber784202e2009-10-15 13:46:54 -07003296 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003297 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3298 CHECK_EQ(err, OK);
3299
3300 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3301
3302 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3303 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3304
3305 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3306 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3307 printf(" nBufferSize = %ld\n", def.nBufferSize);
3308
3309 switch (def.eDomain) {
3310 case OMX_PortDomainImage:
3311 {
3312 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3313
3314 printf("\n");
3315 printf(" // Image\n");
3316 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3317 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3318 printf(" nStride = %ld\n", imageDef->nStride);
3319
3320 printf(" eCompressionFormat = %s\n",
3321 imageCompressionFormatString(imageDef->eCompressionFormat));
3322
3323 printf(" eColorFormat = %s\n",
3324 colorFormatString(imageDef->eColorFormat));
3325
3326 break;
3327 }
3328
3329 case OMX_PortDomainVideo:
3330 {
3331 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3332
3333 printf("\n");
3334 printf(" // Video\n");
3335 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3336 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3337 printf(" nStride = %ld\n", videoDef->nStride);
3338
3339 printf(" eCompressionFormat = %s\n",
3340 videoCompressionFormatString(videoDef->eCompressionFormat));
3341
3342 printf(" eColorFormat = %s\n",
3343 colorFormatString(videoDef->eColorFormat));
3344
3345 break;
3346 }
3347
3348 case OMX_PortDomainAudio:
3349 {
3350 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3351
3352 printf("\n");
3353 printf(" // Audio\n");
3354 printf(" eEncoding = %s\n",
3355 audioCodingTypeString(audioDef->eEncoding));
3356
3357 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3358 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003359 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003360 params.nPortIndex = portIndex;
3361
Andreas Huber784202e2009-10-15 13:46:54 -07003362 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003363 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3364 CHECK_EQ(err, OK);
3365
3366 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3367 printf(" nChannels = %ld\n", params.nChannels);
3368 printf(" bInterleaved = %d\n", params.bInterleaved);
3369 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3370
3371 printf(" eNumData = %s\n",
3372 params.eNumData == OMX_NumericalDataSigned
3373 ? "signed" : "unsigned");
3374
3375 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003376 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3377 OMX_AUDIO_PARAM_AMRTYPE amr;
3378 InitOMXParams(&amr);
3379 amr.nPortIndex = portIndex;
3380
Andreas Huber784202e2009-10-15 13:46:54 -07003381 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003382 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3383 CHECK_EQ(err, OK);
3384
3385 printf(" nChannels = %ld\n", amr.nChannels);
3386 printf(" eAMRBandMode = %s\n",
3387 amrBandModeString(amr.eAMRBandMode));
3388 printf(" eAMRFrameFormat = %s\n",
3389 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003390 }
3391
3392 break;
3393 }
3394
3395 default:
3396 {
3397 printf(" // Unknown\n");
3398 break;
3399 }
3400 }
3401
3402 printf("}\n");
3403}
3404
3405void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3406 mOutputFormat = new MetaData;
3407 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003408 if (mIsEncoder) {
3409 int32_t timeScale;
3410 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3411 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3412 }
3413 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003414
3415 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003416 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003417 def.nPortIndex = kPortIndexOutput;
3418
Andreas Huber784202e2009-10-15 13:46:54 -07003419 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003420 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3421 CHECK_EQ(err, OK);
3422
3423 switch (def.eDomain) {
3424 case OMX_PortDomainImage:
3425 {
3426 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3427 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3428
Andreas Hubere6c40962009-09-10 14:13:30 -07003429 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003430 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3431 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3432 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3433 break;
3434 }
3435
3436 case OMX_PortDomainAudio:
3437 {
3438 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3439
Andreas Huberda050cf22009-09-02 14:01:43 -07003440 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3441 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003442 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003443 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003444
Andreas Huber784202e2009-10-15 13:46:54 -07003445 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003446 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3447 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003448
Andreas Huberda050cf22009-09-02 14:01:43 -07003449 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3450 CHECK_EQ(params.nBitPerSample, 16);
3451 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003452
Andreas Huberda050cf22009-09-02 14:01:43 -07003453 int32_t numChannels, sampleRate;
3454 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3455 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003456
Andreas Huberda050cf22009-09-02 14:01:43 -07003457 if ((OMX_U32)numChannels != params.nChannels) {
3458 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003459 "the input stream contains (contains %d channels, "
3460 "codec outputs %ld channels).",
3461 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003462 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003463
Andreas Hubere6c40962009-09-10 14:13:30 -07003464 mOutputFormat->setCString(
3465 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003466
3467 // Use the codec-advertised number of channels, as some
3468 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003469 // mono. If we know the codec lies about this information,
3470 // use the actual number of channels instead.
3471 mOutputFormat->setInt32(
3472 kKeyChannelCount,
3473 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3474 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003475
3476 // The codec-reported sampleRate is not reliable...
3477 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3478 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003479 OMX_AUDIO_PARAM_AMRTYPE amr;
3480 InitOMXParams(&amr);
3481 amr.nPortIndex = kPortIndexOutput;
3482
Andreas Huber784202e2009-10-15 13:46:54 -07003483 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003484 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3485 CHECK_EQ(err, OK);
3486
3487 CHECK_EQ(amr.nChannels, 1);
3488 mOutputFormat->setInt32(kKeyChannelCount, 1);
3489
3490 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3491 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003492 mOutputFormat->setCString(
3493 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003494 mOutputFormat->setInt32(kKeySampleRate, 8000);
3495 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3496 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003497 mOutputFormat->setCString(
3498 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003499 mOutputFormat->setInt32(kKeySampleRate, 16000);
3500 } else {
3501 CHECK(!"Unknown AMR band mode.");
3502 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003503 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003504 mOutputFormat->setCString(
3505 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003506 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003507 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3508 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003509 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003510 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3511 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003512 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003513 } else {
3514 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003515 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003516 break;
3517 }
3518
3519 case OMX_PortDomainVideo:
3520 {
3521 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3522
3523 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003524 mOutputFormat->setCString(
3525 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003526 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003527 mOutputFormat->setCString(
3528 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003529 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003530 mOutputFormat->setCString(
3531 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003532 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003533 mOutputFormat->setCString(
3534 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003535 } else {
3536 CHECK(!"Unknown compression format.");
3537 }
3538
3539 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3540 // This component appears to be lying to me.
3541 mOutputFormat->setInt32(
3542 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3543 mOutputFormat->setInt32(
3544 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3545 } else {
3546 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3547 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3548 }
3549
3550 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3551 break;
3552 }
3553
3554 default:
3555 {
3556 CHECK(!"should not be here, neither audio nor video.");
3557 break;
3558 }
3559 }
3560}
3561
Andreas Huber1f24b302010-06-10 11:12:39 -07003562status_t OMXCodec::pause() {
3563 Mutex::Autolock autoLock(mLock);
3564
3565 mPaused = true;
3566
3567 return OK;
3568}
3569
Andreas Hubere6c40962009-09-10 14:13:30 -07003570////////////////////////////////////////////////////////////////////////////////
3571
3572status_t QueryCodecs(
3573 const sp<IOMX> &omx,
3574 const char *mime, bool queryDecoders,
3575 Vector<CodecCapabilities> *results) {
3576 results->clear();
3577
3578 for (int index = 0;; ++index) {
3579 const char *componentName;
3580
3581 if (!queryDecoders) {
3582 componentName = GetCodec(
3583 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3584 mime, index);
3585 } else {
3586 componentName = GetCodec(
3587 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3588 mime, index);
3589 }
3590
3591 if (!componentName) {
3592 return OK;
3593 }
3594
Andreas Huber1a189a82010-03-24 13:49:20 -07003595 if (strncmp(componentName, "OMX.", 4)) {
3596 // Not an OpenMax component but a software codec.
3597
3598 results->push();
3599 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3600 caps->mComponentName = componentName;
3601
3602 continue;
3603 }
3604
Andreas Huber784202e2009-10-15 13:46:54 -07003605 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003606 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003607 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003608
3609 if (err != OK) {
3610 continue;
3611 }
3612
James Dong722d5912010-04-13 10:56:59 -07003613 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003614
3615 results->push();
3616 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3617 caps->mComponentName = componentName;
3618
3619 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3620 InitOMXParams(&param);
3621
3622 param.nPortIndex = queryDecoders ? 0 : 1;
3623
3624 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003625 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003626 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3627 &param, sizeof(param));
3628
3629 if (err != OK) {
3630 break;
3631 }
3632
3633 CodecProfileLevel profileLevel;
3634 profileLevel.mProfile = param.eProfile;
3635 profileLevel.mLevel = param.eLevel;
3636
3637 caps->mProfileLevels.push(profileLevel);
3638 }
3639
Andreas Huber784202e2009-10-15 13:46:54 -07003640 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003641 }
3642}
3643
Andreas Huberbe06d262009-08-14 14:37:10 -07003644} // namespace android