blob: d19fbe56c9e61d60d8f67d0e38f9f89ff840cda3 [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"
James Dong02f5b542009-12-15 16:26:55 -080029#include "include/M4vH263Decoder.h"
James Dong42ef0c72010-07-12 21:46:25 -070030#include "include/M4vH263Encoder.h"
Andreas Huber250f2432009-12-07 14:22:35 -080031#include "include/MP3Decoder.h"
Andreas Huber388379f2010-05-07 10:35:13 -070032#include "include/VorbisDecoder.h"
Andreas Huber47ba30e2010-05-24 14:38:02 -070033#include "include/VPXDecoder.h"
Andreas Huber8c7ab032009-12-07 11:23:44 -080034
Andreas Huberbd7b43b2009-10-13 10:22:55 -070035#include "include/ESDS.h"
36
Andreas Huberbe06d262009-08-14 14:37:10 -070037#include <binder/IServiceManager.h>
38#include <binder/MemoryDealer.h>
39#include <binder/ProcessState.h>
40#include <media/IMediaPlayerService.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070041#include <media/stagefright/MediaBuffer.h>
42#include <media/stagefright/MediaBufferGroup.h>
43#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070044#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070045#include <media/stagefright/MediaExtractor.h>
46#include <media/stagefright/MetaData.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070047#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070048#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070049#include <utils/Vector.h>
50
51#include <OMX_Audio.h>
52#include <OMX_Component.h>
53
54namespace android {
55
Andreas Huber8b432b12009-10-07 13:36:52 -070056static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
57
Andreas Huberbe06d262009-08-14 14:37:10 -070058struct CodecInfo {
59 const char *mime;
60 const char *codec;
61};
62
Andreas Huberfb1c2f82009-12-15 13:25:11 -080063#define FACTORY_CREATE(name) \
64static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
65 return new name(source); \
66}
67
James Dong17299ab2010-05-14 15:45:22 -070068#define FACTORY_CREATE_ENCODER(name) \
69static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \
70 return new name(source, meta); \
71}
72
Andreas Huberfb1c2f82009-12-15 13:25:11 -080073#define FACTORY_REF(name) { #name, Make##name },
74
75FACTORY_CREATE(MP3Decoder)
76FACTORY_CREATE(AMRNBDecoder)
77FACTORY_CREATE(AMRWBDecoder)
78FACTORY_CREATE(AACDecoder)
79FACTORY_CREATE(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -080080FACTORY_CREATE(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -070081FACTORY_CREATE(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -070082FACTORY_CREATE(VPXDecoder)
James Dong17299ab2010-05-14 15:45:22 -070083FACTORY_CREATE_ENCODER(AMRNBEncoder)
84FACTORY_CREATE_ENCODER(AMRWBEncoder)
85FACTORY_CREATE_ENCODER(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -070086FACTORY_CREATE_ENCODER(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -070087FACTORY_CREATE_ENCODER(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -070088
89static sp<MediaSource> InstantiateSoftwareEncoder(
90 const char *name, const sp<MediaSource> &source,
91 const sp<MetaData> &meta) {
92 struct FactoryInfo {
93 const char *name;
94 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &);
95 };
96
97 static const FactoryInfo kFactoryInfo[] = {
98 FACTORY_REF(AMRNBEncoder)
99 FACTORY_REF(AMRWBEncoder)
100 FACTORY_REF(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -0700101 FACTORY_REF(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -0700102 FACTORY_REF(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -0700103 };
104 for (size_t i = 0;
105 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
106 if (!strcmp(name, kFactoryInfo[i].name)) {
107 return (*kFactoryInfo[i].CreateFunc)(source, meta);
108 }
109 }
110
111 return NULL;
112}
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800113
114static sp<MediaSource> InstantiateSoftwareCodec(
115 const char *name, const sp<MediaSource> &source) {
116 struct FactoryInfo {
117 const char *name;
118 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
119 };
120
121 static const FactoryInfo kFactoryInfo[] = {
122 FACTORY_REF(MP3Decoder)
123 FACTORY_REF(AMRNBDecoder)
124 FACTORY_REF(AMRWBDecoder)
125 FACTORY_REF(AACDecoder)
126 FACTORY_REF(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -0800127 FACTORY_REF(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -0700128 FACTORY_REF(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -0700129 FACTORY_REF(VPXDecoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800130 };
131 for (size_t i = 0;
132 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
133 if (!strcmp(name, kFactoryInfo[i].name)) {
134 return (*kFactoryInfo[i].CreateFunc)(source);
135 }
136 }
137
138 return NULL;
139}
140
141#undef FACTORY_REF
142#undef FACTORY_CREATE
143
Andreas Huberbe06d262009-08-14 14:37:10 -0700144static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700145 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
James Dong374aee62010-04-26 10:23:30 -0700146// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800147 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700148// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700149// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800150 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700151// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700152 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800153 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700154// { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700155 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800156 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700157// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700158 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.decoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700159 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
160 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800161 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700162// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700163 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.decoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700164 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800165 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700166// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700167 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.decoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700168 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
169 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800170 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700171// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huber388379f2010-05-07 10:35:13 -0700172 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" },
Andreas Huber47ba30e2010-05-24 14:38:02 -0700173 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700174};
175
176static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700177 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800178 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700179 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700180 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700181 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700182 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700183// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
184 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.encoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700185 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
186 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700187 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700188// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
189 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.encoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700190 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
191 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700192 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700193// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
194 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.encoder.avc" },
Andreas Huber71c27d92010-03-19 11:43:15 -0700195 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700196 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
James Dong1cc31e62010-07-02 17:44:44 -0700197 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700198// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700199};
200
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800201#undef OPTIONAL
202
Andreas Hubere0873732009-09-10 09:57:53 -0700203#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700204#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber42c444a2010-02-09 10:20:00 -0800205#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700206
Andreas Huberbe06d262009-08-14 14:37:10 -0700207struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700208 OMXCodecObserver() {
209 }
210
211 void setCodec(const sp<OMXCodec> &target) {
212 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700213 }
214
215 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700216 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700217 sp<OMXCodec> codec = mTarget.promote();
218
219 if (codec.get() != NULL) {
220 codec->on_message(msg);
221 }
222 }
223
224protected:
225 virtual ~OMXCodecObserver() {}
226
227private:
228 wp<OMXCodec> mTarget;
229
230 OMXCodecObserver(const OMXCodecObserver &);
231 OMXCodecObserver &operator=(const OMXCodecObserver &);
232};
233
234static const char *GetCodec(const CodecInfo *info, size_t numInfos,
235 const char *mime, int index) {
236 CHECK(index >= 0);
237 for(size_t i = 0; i < numInfos; ++i) {
238 if (!strcasecmp(mime, info[i].mime)) {
239 if (index == 0) {
240 return info[i].codec;
241 }
242
243 --index;
244 }
245 }
246
247 return NULL;
248}
249
Andreas Huberebf66ea2009-08-19 13:32:58 -0700250enum {
251 kAVCProfileBaseline = 0x42,
252 kAVCProfileMain = 0x4d,
253 kAVCProfileExtended = 0x58,
254 kAVCProfileHigh = 0x64,
255 kAVCProfileHigh10 = 0x6e,
256 kAVCProfileHigh422 = 0x7a,
257 kAVCProfileHigh444 = 0xf4,
258 kAVCProfileCAVLC444Intra = 0x2c
259};
260
261static const char *AVCProfileToString(uint8_t profile) {
262 switch (profile) {
263 case kAVCProfileBaseline:
264 return "Baseline";
265 case kAVCProfileMain:
266 return "Main";
267 case kAVCProfileExtended:
268 return "Extended";
269 case kAVCProfileHigh:
270 return "High";
271 case kAVCProfileHigh10:
272 return "High 10";
273 case kAVCProfileHigh422:
274 return "High 422";
275 case kAVCProfileHigh444:
276 return "High 444";
277 case kAVCProfileCAVLC444Intra:
278 return "CAVLC 444 Intra";
279 default: return "Unknown";
280 }
281}
282
Andreas Huber4c483422009-09-02 16:05:36 -0700283template<class T>
284static void InitOMXParams(T *params) {
285 params->nSize = sizeof(T);
286 params->nVersion.s.nVersionMajor = 1;
287 params->nVersion.s.nVersionMinor = 0;
288 params->nVersion.s.nRevision = 0;
289 params->nVersion.s.nStep = 0;
290}
291
Andreas Hubere13526a2009-10-22 10:43:34 -0700292static bool IsSoftwareCodec(const char *componentName) {
293 if (!strncmp("OMX.PV.", componentName, 7)) {
294 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700295 }
296
Andreas Hubere13526a2009-10-22 10:43:34 -0700297 return false;
298}
299
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800300// A sort order in which non-OMX components are first,
301// followed by software codecs, i.e. OMX.PV.*, followed
302// by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700303static int CompareSoftwareCodecsFirst(
304 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800305 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
306 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
307
308 if (isNotOMX1) {
309 if (isNotOMX2) { return 0; }
310 return -1;
311 }
312 if (isNotOMX2) {
313 return 1;
314 }
315
Andreas Hubere13526a2009-10-22 10:43:34 -0700316 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
317 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
318
319 if (isSoftwareCodec1) {
320 if (isSoftwareCodec2) { return 0; }
321 return -1;
322 }
323
324 if (isSoftwareCodec2) {
325 return 1;
326 }
327
328 return 0;
329}
330
331// static
332uint32_t OMXCodec::getComponentQuirks(const char *componentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700333 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700334
Andreas Huberbe06d262009-08-14 14:37:10 -0700335 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700336 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700337 }
338 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
339 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800340 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700341 }
342 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
343 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700344 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700345 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700346 }
347 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
348 quirks |= kRequiresLoadedToIdleAfterAllocation;
349 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700350 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huberbe06d262009-08-14 14:37:10 -0700351 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700352 if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) {
353 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700354 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700355 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800356 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700357 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700358 if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) {
359 quirks |= kRequiresAllocateBufferOnInputPorts;
360 quirks |= kRequiresAllocateBufferOnOutputPorts;
361 quirks |= kDefersOutputBufferAllocation;
362 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700363
Andreas Huber2dc64d82009-09-11 12:58:53 -0700364 if (!strncmp(componentName, "OMX.TI.", 7)) {
365 // Apparently I must not use OMX_UseBuffer on either input or
366 // output ports on any of the TI components or quote:
367 // "(I) may have unexpected problem (sic) which can be timing related
368 // and hard to reproduce."
369
370 quirks |= kRequiresAllocateBufferOnInputPorts;
371 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dongdca66e12010-06-14 11:14:38 -0700372 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) {
James Dong4f501f02010-06-07 14:41:41 -0700373 quirks |= kAvoidMemcopyInputRecordingFrames;
374 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700375 }
376
Andreas Huberb8de9572010-02-22 14:58:45 -0800377 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
378 quirks |= kInputBufferSizesAreBogus;
379 }
380
Andreas Hubere13526a2009-10-22 10:43:34 -0700381 return quirks;
382}
383
384// static
385void OMXCodec::findMatchingCodecs(
386 const char *mime,
387 bool createEncoder, const char *matchComponentName,
388 uint32_t flags,
389 Vector<String8> *matchingCodecs) {
390 matchingCodecs->clear();
391
392 for (int index = 0;; ++index) {
393 const char *componentName;
394
395 if (createEncoder) {
396 componentName = GetCodec(
397 kEncoderInfo,
398 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
399 mime, index);
400 } else {
401 componentName = GetCodec(
402 kDecoderInfo,
403 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
404 mime, index);
405 }
406
407 if (!componentName) {
408 break;
409 }
410
411 // If a specific codec is requested, skip the non-matching ones.
412 if (matchComponentName && strcmp(componentName, matchComponentName)) {
413 continue;
414 }
415
416 matchingCodecs->push(String8(componentName));
417 }
418
419 if (flags & kPreferSoftwareCodecs) {
420 matchingCodecs->sort(CompareSoftwareCodecsFirst);
421 }
422}
423
424// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800425sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700426 const sp<IOMX> &omx,
427 const sp<MetaData> &meta, bool createEncoder,
428 const sp<MediaSource> &source,
429 const char *matchComponentName,
430 uint32_t flags) {
431 const char *mime;
432 bool success = meta->findCString(kKeyMIMEType, &mime);
433 CHECK(success);
434
435 Vector<String8> matchingCodecs;
436 findMatchingCodecs(
437 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
438
439 if (matchingCodecs.isEmpty()) {
440 return NULL;
441 }
442
443 sp<OMXCodecObserver> observer = new OMXCodecObserver;
444 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700445
446 const char *componentName;
447 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
448 componentName = matchingCodecs[i].string();
449
James Dong17299ab2010-05-14 15:45:22 -0700450 sp<MediaSource> softwareCodec = createEncoder?
451 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800452 InstantiateSoftwareCodec(componentName, source);
453
454 if (softwareCodec != NULL) {
455 LOGV("Successfully allocated software codec '%s'", componentName);
456
457 return softwareCodec;
458 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800459
Andreas Hubere13526a2009-10-22 10:43:34 -0700460 LOGV("Attempting to allocate OMX node '%s'", componentName);
461
462 status_t err = omx->allocateNode(componentName, observer, &node);
463 if (err == OK) {
464 LOGV("Successfully allocated OMX node '%s'", componentName);
465
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700466 sp<OMXCodec> codec = new OMXCodec(
467 omx, node, getComponentQuirks(componentName),
468 createEncoder, mime, componentName,
469 source);
470
471 observer->setCodec(codec);
472
473 err = codec->configureCodec(meta);
474
475 if (err == OK) {
476 return codec;
477 }
478
479 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700480 }
481 }
482
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700483 return NULL;
484}
Andreas Hubere13526a2009-10-22 10:43:34 -0700485
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700486status_t OMXCodec::configureCodec(const sp<MetaData> &meta) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700487 uint32_t type;
488 const void *data;
489 size_t size;
490 if (meta->findData(kKeyESDS, &type, &data, &size)) {
491 ESDS esds((const char *)data, size);
492 CHECK_EQ(esds.InitCheck(), OK);
493
494 const void *codec_specific_data;
495 size_t codec_specific_data_size;
496 esds.getCodecSpecificInfo(
497 &codec_specific_data, &codec_specific_data_size);
498
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700499 addCodecSpecificData(
Andreas Huberbe06d262009-08-14 14:37:10 -0700500 codec_specific_data, codec_specific_data_size);
501 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
Andreas Huberebf66ea2009-08-19 13:32:58 -0700502 // Parse the AVCDecoderConfigurationRecord
503
504 const uint8_t *ptr = (const uint8_t *)data;
505
506 CHECK(size >= 7);
507 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
508 uint8_t profile = ptr[1];
509 uint8_t level = ptr[3];
510
Andreas Huber44e15c42009-11-23 14:39:38 -0800511 // There is decodable content out there that fails the following
512 // assertion, let's be lenient for now...
513 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700514
515 size_t lengthSize = 1 + (ptr[4] & 3);
516
517 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
518 // violates it...
519 // CHECK((ptr[5] >> 5) == 7); // reserved
520
521 size_t numSeqParameterSets = ptr[5] & 31;
522
523 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700524 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700525
526 for (size_t i = 0; i < numSeqParameterSets; ++i) {
527 CHECK(size >= 2);
528 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700529
530 ptr += 2;
531 size -= 2;
532
Andreas Huberbe06d262009-08-14 14:37:10 -0700533 CHECK(size >= length);
534
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700535 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700536
537 ptr += length;
538 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700539 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700540
Andreas Huberebf66ea2009-08-19 13:32:58 -0700541 CHECK(size >= 1);
542 size_t numPictureParameterSets = *ptr;
543 ++ptr;
544 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700545
Andreas Huberebf66ea2009-08-19 13:32:58 -0700546 for (size_t i = 0; i < numPictureParameterSets; ++i) {
547 CHECK(size >= 2);
548 size_t length = U16_AT(ptr);
549
550 ptr += 2;
551 size -= 2;
552
553 CHECK(size >= length);
554
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700555 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700556
557 ptr += length;
558 size -= length;
559 }
560
Andreas Hubere2f85072010-06-10 09:51:27 -0700561 CODEC_LOGV(
562 "AVC profile = %d (%s), level = %d",
563 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700564
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700565 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
Andreas Hubere2f85072010-06-10 09:51:27 -0700566 && (profile != kAVCProfileBaseline || level > 30)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700567 // This stream exceeds the decoder's capabilities. The decoder
568 // does not handle this gracefully and would clobber the heap
569 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700570
571 LOGE("Profile and/or level exceed the decoder's capabilities.");
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700572 return ERROR_UNSUPPORTED;
Andreas Huberbe06d262009-08-14 14:37:10 -0700573 }
574 }
575
James Dong17299ab2010-05-14 15:45:22 -0700576 int32_t bitRate = 0;
577 if (mIsEncoder) {
578 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
579 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700580 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700581 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700582 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700583 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700584 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700585 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700586 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700587 int32_t numChannels, sampleRate;
588 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
589 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
590
James Dong17299ab2010-05-14 15:45:22 -0700591 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700592 }
James Dongabed93a2010-04-22 17:27:04 -0700593
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700594 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700595
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700596 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700597 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700598 } else {
James Dong1244eab2010-06-08 11:58:53 -0700599 int32_t width, height;
600 bool success = meta->findInt32(kKeyWidth, &width);
601 success = success && meta->findInt32(kKeyHeight, &height);
602 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700603 status_t err = setVideoOutputFormat(
604 mMIME, width, height);
605
606 if (err != OK) {
607 return err;
608 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700609 }
610 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700611
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700612 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
613 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700614 OMX_COLOR_FORMATTYPE format =
615 OMX_COLOR_Format32bitARGB8888;
616 // OMX_COLOR_FormatYUV420PackedPlanar;
617 // OMX_COLOR_FormatCbYCrY;
618 // OMX_COLOR_FormatYUV411Planar;
619
620 int32_t width, height;
621 bool success = meta->findInt32(kKeyWidth, &width);
622 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700623
624 int32_t compressedSize;
625 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700626 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700627
628 CHECK(success);
629 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700630
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700631 setImageOutputFormat(format, width, height);
632 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700633 }
634
Andreas Huberda050cf22009-09-02 14:01:43 -0700635 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800636 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700637 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700638 }
639
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700640 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700641 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
642 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700643 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700644 }
645
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700646 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700647
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700648 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700649}
650
Andreas Huberda050cf22009-09-02 14:01:43 -0700651void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
652 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700653 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700654 def.nPortIndex = portIndex;
655
Andreas Huber784202e2009-10-15 13:46:54 -0700656 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700657 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
658 CHECK_EQ(err, OK);
659
Andreas Huberb8de9572010-02-22 14:58:45 -0800660 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
661 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700662 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700663 }
664
Andreas Huber784202e2009-10-15 13:46:54 -0700665 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700666 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
667 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800668
669 err = mOMX->getParameter(
670 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
671 CHECK_EQ(err, OK);
672
673 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800674 if (portIndex == kPortIndexInput
675 && (mQuirks & kInputBufferSizesAreBogus)) {
676 CHECK_EQ(def.nBufferSize, size);
677 } else {
678 CHECK(def.nBufferSize >= size);
679 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700680}
681
Andreas Huberbe06d262009-08-14 14:37:10 -0700682status_t OMXCodec::setVideoPortFormatType(
683 OMX_U32 portIndex,
684 OMX_VIDEO_CODINGTYPE compressionFormat,
685 OMX_COLOR_FORMATTYPE colorFormat) {
686 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700687 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700688 format.nPortIndex = portIndex;
689 format.nIndex = 0;
690 bool found = false;
691
692 OMX_U32 index = 0;
693 for (;;) {
694 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700695 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700696 mNode, OMX_IndexParamVideoPortFormat,
697 &format, sizeof(format));
698
699 if (err != OK) {
700 return err;
701 }
702
703 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700704 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700705
706#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700707 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700708 portIndex,
709 index, format.eCompressionFormat, format.eColorFormat);
710#endif
711
712 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
713 if (portIndex == kPortIndexInput
714 && colorFormat == format.eColorFormat) {
715 // eCompressionFormat does not seem right.
716 found = true;
717 break;
718 }
719 if (portIndex == kPortIndexOutput
720 && compressionFormat == format.eCompressionFormat) {
721 // eColorFormat does not seem right.
722 found = true;
723 break;
724 }
725 }
726
727 if (format.eCompressionFormat == compressionFormat
728 && format.eColorFormat == colorFormat) {
729 found = true;
730 break;
731 }
732
733 ++index;
734 }
735
736 if (!found) {
737 return UNKNOWN_ERROR;
738 }
739
Andreas Huber53a76bd2009-10-06 16:20:44 -0700740 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700741 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700742 mNode, OMX_IndexParamVideoPortFormat,
743 &format, sizeof(format));
744
745 return err;
746}
747
Andreas Huberb482ce82009-10-29 12:02:48 -0700748static size_t getFrameSize(
749 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
750 switch (colorFormat) {
751 case OMX_COLOR_FormatYCbYCr:
752 case OMX_COLOR_FormatCbYCrY:
753 return width * height * 2;
754
Andreas Huber71c27d92010-03-19 11:43:15 -0700755 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700756 case OMX_COLOR_FormatYUV420SemiPlanar:
757 return (width * height * 3) / 2;
758
759 default:
760 CHECK(!"Should not be here. Unsupported color format.");
761 break;
762 }
763}
764
Andreas Huberbe06d262009-08-14 14:37:10 -0700765void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700766 const char *mime, const sp<MetaData>& meta) {
767
768 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
769 bool success = meta->findInt32(kKeyWidth, &width);
770 success = success && meta->findInt32(kKeyHeight, &height);
771 success = success && meta->findInt32(kKeySampleRate, &frameRate);
772 success = success && meta->findInt32(kKeyBitRate, &bitRate);
773 success = success && meta->findInt32(kKeyStride, &stride);
774 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
775 CHECK(success);
776 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700777
778 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700779 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700780 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700781 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700782 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700783 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700784 compressionFormat = OMX_VIDEO_CodingH263;
785 } else {
786 LOGE("Not a supported video mime type: %s", mime);
787 CHECK(!"Should not be here. Not a supported video mime type.");
788 }
789
Andreas Huberea6a38c2009-11-16 15:43:38 -0800790 OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
791 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
James Dongabed93a2010-04-22 17:27:04 -0700792 colorFormat = OMX_COLOR_FormatYCbYCr;
Andreas Huberbe06d262009-08-14 14:37:10 -0700793 }
794
James Dongb00e2462010-04-26 17:48:26 -0700795 status_t err;
796 OMX_PARAM_PORTDEFINITIONTYPE def;
797 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
798
799 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700800 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700801 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700802 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700803
James Dongb00e2462010-04-26 17:48:26 -0700804 InitOMXParams(&def);
805 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700806
James Dongb00e2462010-04-26 17:48:26 -0700807 err = mOMX->getParameter(
808 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
809 CHECK_EQ(err, OK);
810
James Dong1244eab2010-06-08 11:58:53 -0700811 def.nBufferSize = getFrameSize(colorFormat,
812 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700813
814 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
815
816 video_def->nFrameWidth = width;
817 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700818 video_def->nStride = stride;
819 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700820 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700821 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
822 video_def->eColorFormat = colorFormat;
823
James Dongb00e2462010-04-26 17:48:26 -0700824 err = mOMX->setParameter(
825 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
826 CHECK_EQ(err, OK);
827
828 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700829 CHECK_EQ(setVideoPortFormatType(
830 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
831 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700832 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700833 def.nPortIndex = kPortIndexOutput;
834
James Dongb00e2462010-04-26 17:48:26 -0700835 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700836 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
837
838 CHECK_EQ(err, OK);
839 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
840
841 video_def->nFrameWidth = width;
842 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700843 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700844 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700845 video_def->eCompressionFormat = compressionFormat;
846 video_def->eColorFormat = OMX_COLOR_FormatUnused;
847
Andreas Huber784202e2009-10-15 13:46:54 -0700848 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700849 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
850 CHECK_EQ(err, OK);
851
James Dongb00e2462010-04-26 17:48:26 -0700852 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700853 switch (compressionFormat) {
854 case OMX_VIDEO_CodingMPEG4:
855 {
James Dong1244eab2010-06-08 11:58:53 -0700856 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700857 break;
858 }
859
860 case OMX_VIDEO_CodingH263:
James Dongc0ab2a62010-06-29 16:29:19 -0700861 CHECK_EQ(setupH263EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700862 break;
863
Andreas Huberea6a38c2009-11-16 15:43:38 -0800864 case OMX_VIDEO_CodingAVC:
865 {
James Dong1244eab2010-06-08 11:58:53 -0700866 CHECK_EQ(setupAVCEncoderParameters(meta), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -0800867 break;
868 }
869
Andreas Huberb482ce82009-10-29 12:02:48 -0700870 default:
871 CHECK(!"Support for this compressionFormat to be implemented.");
872 break;
873 }
874}
875
James Dong1244eab2010-06-08 11:58:53 -0700876static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
877 if (iFramesInterval < 0) {
878 return 0xFFFFFFFF;
879 } else if (iFramesInterval == 0) {
880 return 0;
881 }
882 OMX_U32 ret = frameRate * iFramesInterval;
883 CHECK(ret > 1);
884 return ret;
885}
886
James Dongc0ab2a62010-06-29 16:29:19 -0700887status_t OMXCodec::setupErrorCorrectionParameters() {
888 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
889 InitOMXParams(&errorCorrectionType);
890 errorCorrectionType.nPortIndex = kPortIndexOutput;
891
892 status_t err = mOMX->getParameter(
893 mNode, OMX_IndexParamVideoErrorCorrection,
894 &errorCorrectionType, sizeof(errorCorrectionType));
895 CHECK_EQ(err, OK);
896
897 errorCorrectionType.bEnableHEC = OMX_FALSE;
898 errorCorrectionType.bEnableResync = OMX_TRUE;
899 errorCorrectionType.nResynchMarkerSpacing = 256;
900 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
901 errorCorrectionType.bEnableRVLC = OMX_FALSE;
902
903 err = mOMX->setParameter(
904 mNode, OMX_IndexParamVideoErrorCorrection,
905 &errorCorrectionType, sizeof(errorCorrectionType));
906 CHECK_EQ(err, OK);
907 return OK;
908}
909
910status_t OMXCodec::setupBitRate(int32_t bitRate) {
911 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
912 InitOMXParams(&bitrateType);
913 bitrateType.nPortIndex = kPortIndexOutput;
914
915 status_t err = mOMX->getParameter(
916 mNode, OMX_IndexParamVideoBitrate,
917 &bitrateType, sizeof(bitrateType));
918 CHECK_EQ(err, OK);
919
920 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
921 bitrateType.nTargetBitrate = bitRate;
922
923 err = mOMX->setParameter(
924 mNode, OMX_IndexParamVideoBitrate,
925 &bitrateType, sizeof(bitrateType));
926 CHECK_EQ(err, OK);
927 return OK;
928}
929
James Dong81c929a2010-07-01 15:02:14 -0700930status_t OMXCodec::getVideoProfileLevel(
931 const sp<MetaData>& meta,
932 const CodecProfileLevel& defaultProfileLevel,
933 CodecProfileLevel &profileLevel) {
934 CODEC_LOGV("Default profile: %ld, level %ld",
935 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
936
937 // Are the default profile and level overwriten?
938 int32_t profile, level;
939 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
940 profile = defaultProfileLevel.mProfile;
941 }
942 if (!meta->findInt32(kKeyVideoLevel, &level)) {
943 level = defaultProfileLevel.mLevel;
944 }
945 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
946
947 // Are the target profile and level supported by the encoder?
948 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
949 InitOMXParams(&param);
950 param.nPortIndex = kPortIndexOutput;
951 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
952 status_t err = mOMX->getParameter(
953 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
954 &param, sizeof(param));
955
956 if (err != OK) return err;
957
958 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
959 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -0700960 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -0700961 supportedProfile, supportedLevel);
962
963 if (profile == supportedProfile &&
964 level == supportedLevel) {
965 profileLevel.mProfile = profile;
966 profileLevel.mLevel = level;
967 return OK;
968 }
969 }
970
971 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
972 profile, level);
973 return BAD_VALUE;
974}
975
James Dongc0ab2a62010-06-29 16:29:19 -0700976status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
977 int32_t iFramesInterval, frameRate, bitRate;
978 bool success = meta->findInt32(kKeyBitRate, &bitRate);
979 success = success && meta->findInt32(kKeySampleRate, &frameRate);
980 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
981 CHECK(success);
982 OMX_VIDEO_PARAM_H263TYPE h263type;
983 InitOMXParams(&h263type);
984 h263type.nPortIndex = kPortIndexOutput;
985
986 status_t err = mOMX->getParameter(
987 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
988 CHECK_EQ(err, OK);
989
990 h263type.nAllowedPictureTypes =
991 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
992
993 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
994 if (h263type.nPFrames == 0) {
995 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
996 }
997 h263type.nBFrames = 0;
998
James Dong81c929a2010-07-01 15:02:14 -0700999 // Check profile and level parameters
1000 CodecProfileLevel defaultProfileLevel, profileLevel;
1001 defaultProfileLevel.mProfile = OMX_VIDEO_H263ProfileBaseline;
1002 defaultProfileLevel.mLevel = OMX_VIDEO_H263Level45;
1003 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1004 if (err != OK) return err;
1005 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1006 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001007
1008 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1009 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1010 h263type.nPictureHeaderRepetition = 0;
1011 h263type.nGOBHeaderInterval = 0;
1012
1013 err = mOMX->setParameter(
1014 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1015 CHECK_EQ(err, OK);
1016
1017 CHECK_EQ(setupBitRate(bitRate), OK);
1018 CHECK_EQ(setupErrorCorrectionParameters(), OK);
1019
1020 return OK;
1021}
1022
James Dong1244eab2010-06-08 11:58:53 -07001023status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1024 int32_t iFramesInterval, frameRate, bitRate;
1025 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1026 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1027 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1028 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001029 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1030 InitOMXParams(&mpeg4type);
1031 mpeg4type.nPortIndex = kPortIndexOutput;
1032
1033 status_t err = mOMX->getParameter(
1034 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1035 CHECK_EQ(err, OK);
1036
1037 mpeg4type.nSliceHeaderSpacing = 0;
1038 mpeg4type.bSVH = OMX_FALSE;
1039 mpeg4type.bGov = OMX_FALSE;
1040
1041 mpeg4type.nAllowedPictureTypes =
1042 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1043
James Dong1244eab2010-06-08 11:58:53 -07001044 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1045 if (mpeg4type.nPFrames == 0) {
1046 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1047 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001048 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001049 mpeg4type.nIDCVLCThreshold = 0;
1050 mpeg4type.bACPred = OMX_TRUE;
1051 mpeg4type.nMaxPacketSize = 256;
1052 mpeg4type.nTimeIncRes = 1000;
1053 mpeg4type.nHeaderExtension = 0;
1054 mpeg4type.bReversibleVLC = OMX_FALSE;
1055
James Dong81c929a2010-07-01 15:02:14 -07001056 // Check profile and level parameters
1057 CodecProfileLevel defaultProfileLevel, profileLevel;
1058 defaultProfileLevel.mProfile = OMX_VIDEO_MPEG4ProfileSimple;
1059 defaultProfileLevel.mLevel = OMX_VIDEO_MPEG4Level2;
1060 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1061 if (err != OK) return err;
1062 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1063 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001064
1065 err = mOMX->setParameter(
1066 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1067 CHECK_EQ(err, OK);
1068
James Dongc0ab2a62010-06-29 16:29:19 -07001069 CHECK_EQ(setupBitRate(bitRate), OK);
1070 CHECK_EQ(setupErrorCorrectionParameters(), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001071
1072 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001073}
1074
James Dong1244eab2010-06-08 11:58:53 -07001075status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1076 int32_t iFramesInterval, frameRate, bitRate;
1077 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1078 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1079 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1080 CHECK(success);
1081
Andreas Huberea6a38c2009-11-16 15:43:38 -08001082 OMX_VIDEO_PARAM_AVCTYPE h264type;
1083 InitOMXParams(&h264type);
1084 h264type.nPortIndex = kPortIndexOutput;
1085
1086 status_t err = mOMX->getParameter(
1087 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1088 CHECK_EQ(err, OK);
1089
1090 h264type.nAllowedPictureTypes =
1091 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1092
1093 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001094 h264type.nBFrames = 0; // No B frames support yet
1095 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1096 if (h264type.nPFrames == 0) {
1097 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1098 }
James Dong81c929a2010-07-01 15:02:14 -07001099
1100 // Check profile and level parameters
1101 CodecProfileLevel defaultProfileLevel, profileLevel;
1102 defaultProfileLevel.mProfile = h264type.eProfile;
1103 defaultProfileLevel.mLevel = h264type.eLevel;
1104 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1105 if (err != OK) return err;
1106 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1107 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1108
1109 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1110 h264type.bUseHadamard = OMX_TRUE;
1111 h264type.nRefFrames = 1;
1112 h264type.nRefIdx10ActiveMinus1 = 0;
1113 h264type.nRefIdx11ActiveMinus1 = 0;
1114 h264type.bEntropyCodingCABAC = OMX_FALSE;
1115 h264type.bWeightedPPrediction = OMX_FALSE;
1116 h264type.bconstIpred = OMX_FALSE;
1117 h264type.bDirect8x8Inference = OMX_FALSE;
1118 h264type.bDirectSpatialTemporal = OMX_FALSE;
1119 h264type.nCabacInitIdc = 0;
1120 }
1121
1122 if (h264type.nBFrames != 0) {
1123 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1124 }
1125
Andreas Huberea6a38c2009-11-16 15:43:38 -08001126 h264type.bEnableUEP = OMX_FALSE;
1127 h264type.bEnableFMO = OMX_FALSE;
1128 h264type.bEnableASO = OMX_FALSE;
1129 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001130 h264type.bFrameMBsOnly = OMX_TRUE;
1131 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001132 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1133
1134 err = mOMX->setParameter(
1135 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1136 CHECK_EQ(err, OK);
1137
James Dongc0ab2a62010-06-29 16:29:19 -07001138 CHECK_EQ(setupBitRate(bitRate), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001139
1140 return OK;
1141}
1142
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001143status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001144 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001145 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001146
Andreas Huberbe06d262009-08-14 14:37:10 -07001147 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001148 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001149 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001150 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001151 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001152 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001153 compressionFormat = OMX_VIDEO_CodingH263;
1154 } else {
1155 LOGE("Not a supported video mime type: %s", mime);
1156 CHECK(!"Should not be here. Not a supported video mime type.");
1157 }
1158
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001159 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001160 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1161
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001162 if (err != OK) {
1163 return err;
1164 }
1165
Andreas Huberbe06d262009-08-14 14:37:10 -07001166#if 1
1167 {
1168 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001169 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001170 format.nPortIndex = kPortIndexOutput;
1171 format.nIndex = 0;
1172
Andreas Huber784202e2009-10-15 13:46:54 -07001173 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001174 mNode, OMX_IndexParamVideoPortFormat,
1175 &format, sizeof(format));
1176 CHECK_EQ(err, OK);
1177 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1178
1179 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1180
1181 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1182 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1183 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1184 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1185
Andreas Huber784202e2009-10-15 13:46:54 -07001186 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001187 mNode, OMX_IndexParamVideoPortFormat,
1188 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001189
1190 if (err != OK) {
1191 return err;
1192 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001193 }
1194#endif
1195
1196 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001197 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001198 def.nPortIndex = kPortIndexInput;
1199
Andreas Huber4c483422009-09-02 16:05:36 -07001200 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1201
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001202 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001203 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1204
1205 CHECK_EQ(err, OK);
1206
1207#if 1
1208 // XXX Need a (much) better heuristic to compute input buffer sizes.
1209 const size_t X = 64 * 1024;
1210 if (def.nBufferSize < X) {
1211 def.nBufferSize = X;
1212 }
1213#endif
1214
1215 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1216
1217 video_def->nFrameWidth = width;
1218 video_def->nFrameHeight = height;
1219
Andreas Huberb482ce82009-10-29 12:02:48 -07001220 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001221 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1222
Andreas Huber784202e2009-10-15 13:46:54 -07001223 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001224 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001225
1226 if (err != OK) {
1227 return err;
1228 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001229
1230 ////////////////////////////////////////////////////////////////////////////
1231
Andreas Huber4c483422009-09-02 16:05:36 -07001232 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001233 def.nPortIndex = kPortIndexOutput;
1234
Andreas Huber784202e2009-10-15 13:46:54 -07001235 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001236 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1237 CHECK_EQ(err, OK);
1238 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1239
1240#if 0
1241 def.nBufferSize =
1242 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1243#endif
1244
1245 video_def->nFrameWidth = width;
1246 video_def->nFrameHeight = height;
1247
Andreas Huber784202e2009-10-15 13:46:54 -07001248 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001249 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001250
1251 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001252}
1253
Andreas Huberbe06d262009-08-14 14:37:10 -07001254OMXCodec::OMXCodec(
1255 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001256 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001257 const char *mime,
1258 const char *componentName,
1259 const sp<MediaSource> &source)
1260 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001261 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001262 mNode(node),
1263 mQuirks(quirks),
1264 mIsEncoder(isEncoder),
1265 mMIME(strdup(mime)),
1266 mComponentName(strdup(componentName)),
1267 mSource(source),
1268 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001269 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001270 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001271 mSignalledEOS(false),
1272 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001273 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001274 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001275 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1276 mTargetTimeUs(-1),
James Dong53d4e0d2010-07-21 14:51:35 -07001277 mSkipTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001278 mLeftOverBuffer(NULL),
1279 mPaused(false) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001280 mPortStatus[kPortIndexInput] = ENABLED;
1281 mPortStatus[kPortIndexOutput] = ENABLED;
1282
Andreas Huber4c483422009-09-02 16:05:36 -07001283 setComponentRole();
1284}
1285
Andreas Hubere6c40962009-09-10 14:13:30 -07001286// static
1287void OMXCodec::setComponentRole(
1288 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1289 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001290 struct MimeToRole {
1291 const char *mime;
1292 const char *decoderRole;
1293 const char *encoderRole;
1294 };
1295
1296 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001297 { MEDIA_MIMETYPE_AUDIO_MPEG,
1298 "audio_decoder.mp3", "audio_encoder.mp3" },
1299 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1300 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1301 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1302 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1303 { MEDIA_MIMETYPE_AUDIO_AAC,
1304 "audio_decoder.aac", "audio_encoder.aac" },
1305 { MEDIA_MIMETYPE_VIDEO_AVC,
1306 "video_decoder.avc", "video_encoder.avc" },
1307 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1308 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1309 { MEDIA_MIMETYPE_VIDEO_H263,
1310 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001311 };
1312
1313 static const size_t kNumMimeToRole =
1314 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1315
1316 size_t i;
1317 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001318 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001319 break;
1320 }
1321 }
1322
1323 if (i == kNumMimeToRole) {
1324 return;
1325 }
1326
1327 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001328 isEncoder ? kMimeToRole[i].encoderRole
1329 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001330
1331 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001332 OMX_PARAM_COMPONENTROLETYPE roleParams;
1333 InitOMXParams(&roleParams);
1334
1335 strncpy((char *)roleParams.cRole,
1336 role, OMX_MAX_STRINGNAME_SIZE - 1);
1337
1338 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1339
Andreas Huber784202e2009-10-15 13:46:54 -07001340 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001341 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001342 &roleParams, sizeof(roleParams));
1343
1344 if (err != OK) {
1345 LOGW("Failed to set standard component role '%s'.", role);
1346 }
1347 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001348}
1349
Andreas Hubere6c40962009-09-10 14:13:30 -07001350void OMXCodec::setComponentRole() {
1351 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1352}
1353
Andreas Huberbe06d262009-08-14 14:37:10 -07001354OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001355 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001356
Andreas Huber784202e2009-10-15 13:46:54 -07001357 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001358 CHECK_EQ(err, OK);
1359
1360 mNode = NULL;
1361 setState(DEAD);
1362
1363 clearCodecSpecificData();
1364
1365 free(mComponentName);
1366 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001367
Andreas Huberbe06d262009-08-14 14:37:10 -07001368 free(mMIME);
1369 mMIME = NULL;
1370}
1371
1372status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001373 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001374
1375 CHECK_EQ(mState, LOADED);
1376
1377 status_t err;
1378 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001379 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001380 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001381 setState(LOADED_TO_IDLE);
1382 }
1383
1384 err = allocateBuffers();
1385 CHECK_EQ(err, OK);
1386
1387 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001388 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001389 CHECK_EQ(err, OK);
1390
1391 setState(LOADED_TO_IDLE);
1392 }
1393
1394 while (mState != EXECUTING && mState != ERROR) {
1395 mAsyncCompletion.wait(mLock);
1396 }
1397
1398 return mState == ERROR ? UNKNOWN_ERROR : OK;
1399}
1400
1401// static
1402bool OMXCodec::isIntermediateState(State state) {
1403 return state == LOADED_TO_IDLE
1404 || state == IDLE_TO_EXECUTING
1405 || state == EXECUTING_TO_IDLE
1406 || state == IDLE_TO_LOADED
1407 || state == RECONFIGURING;
1408}
1409
1410status_t OMXCodec::allocateBuffers() {
1411 status_t err = allocateBuffersOnPort(kPortIndexInput);
1412
1413 if (err != OK) {
1414 return err;
1415 }
1416
1417 return allocateBuffersOnPort(kPortIndexOutput);
1418}
1419
1420status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1421 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001422 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001423 def.nPortIndex = portIndex;
1424
Andreas Huber784202e2009-10-15 13:46:54 -07001425 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001426 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1427
1428 if (err != OK) {
1429 return err;
1430 }
1431
Andreas Huber57648e42010-08-04 10:14:30 -07001432 CODEC_LOGI("allocating %lu buffers of size %lu on %s port",
1433 def.nBufferCountActual, def.nBufferSize,
1434 portIndex == kPortIndexInput ? "input" : "output");
1435
Andreas Huber5c0a9132009-08-20 11:16:40 -07001436 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001437 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001438
Andreas Huberbe06d262009-08-14 14:37:10 -07001439 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001440 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001441 CHECK(mem.get() != NULL);
1442
Andreas Huberc712b9f2010-01-20 15:05:46 -08001443 BufferInfo info;
1444 info.mData = NULL;
1445 info.mSize = def.nBufferSize;
1446
Andreas Huberbe06d262009-08-14 14:37:10 -07001447 IOMX::buffer_id buffer;
1448 if (portIndex == kPortIndexInput
1449 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001450 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001451 mem.clear();
1452
Andreas Huberf1fe0642010-01-15 15:28:19 -08001453 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001454 mNode, portIndex, def.nBufferSize, &buffer,
1455 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001456 } else {
1457 err = mOMX->allocateBufferWithBackup(
1458 mNode, portIndex, mem, &buffer);
1459 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001460 } else if (portIndex == kPortIndexOutput
1461 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001462 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001463 mem.clear();
1464
Andreas Huberf1fe0642010-01-15 15:28:19 -08001465 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001466 mNode, portIndex, def.nBufferSize, &buffer,
1467 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001468 } else {
1469 err = mOMX->allocateBufferWithBackup(
1470 mNode, portIndex, mem, &buffer);
1471 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001472 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001473 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001474 }
1475
1476 if (err != OK) {
1477 LOGE("allocate_buffer_with_backup failed");
1478 return err;
1479 }
1480
Andreas Huberc712b9f2010-01-20 15:05:46 -08001481 if (mem != NULL) {
1482 info.mData = mem->pointer();
1483 }
1484
Andreas Huberbe06d262009-08-14 14:37:10 -07001485 info.mBuffer = buffer;
1486 info.mOwnedByComponent = false;
1487 info.mMem = mem;
1488 info.mMediaBuffer = NULL;
1489
1490 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001491 if (!(mOMXLivesLocally
1492 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1493 && (mQuirks & kDefersOutputBufferAllocation))) {
1494 // If the node does not fill in the buffer ptr at this time,
1495 // we will defer creating the MediaBuffer until receiving
1496 // the first FILL_BUFFER_DONE notification instead.
1497 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1498 info.mMediaBuffer->setObserver(this);
1499 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001500 }
1501
1502 mPortBuffers[portIndex].push(info);
1503
Andreas Huber4c483422009-09-02 16:05:36 -07001504 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001505 portIndex == kPortIndexInput ? "input" : "output");
1506 }
1507
Andreas Huber2ea14e22009-12-16 09:30:55 -08001508 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001509
1510 return OK;
1511}
1512
1513void OMXCodec::on_message(const omx_message &msg) {
1514 Mutex::Autolock autoLock(mLock);
1515
1516 switch (msg.type) {
1517 case omx_message::EVENT:
1518 {
1519 onEvent(
1520 msg.u.event_data.event, msg.u.event_data.data1,
1521 msg.u.event_data.data2);
1522
1523 break;
1524 }
1525
1526 case omx_message::EMPTY_BUFFER_DONE:
1527 {
1528 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1529
Andreas Huber4c483422009-09-02 16:05:36 -07001530 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001531
1532 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1533 size_t i = 0;
1534 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1535 ++i;
1536 }
1537
1538 CHECK(i < buffers->size());
1539 if (!(*buffers)[i].mOwnedByComponent) {
1540 LOGW("We already own input buffer %p, yet received "
1541 "an EMPTY_BUFFER_DONE.", buffer);
1542 }
1543
1544 buffers->editItemAt(i).mOwnedByComponent = false;
1545
1546 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001547 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001548
1549 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001550 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001551 CHECK_EQ(err, OK);
1552
1553 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001554 } else if (mState != ERROR
1555 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001556 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1557 drainInputBuffer(&buffers->editItemAt(i));
1558 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001559 break;
1560 }
1561
1562 case omx_message::FILL_BUFFER_DONE:
1563 {
1564 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1565 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1566
Andreas Huber2ea14e22009-12-16 09:30:55 -08001567 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001568 buffer,
1569 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001570 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001571 msg.u.extended_buffer_data.timestamp,
1572 msg.u.extended_buffer_data.timestamp / 1E6);
1573
1574 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1575 size_t i = 0;
1576 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1577 ++i;
1578 }
1579
1580 CHECK(i < buffers->size());
1581 BufferInfo *info = &buffers->editItemAt(i);
1582
1583 if (!info->mOwnedByComponent) {
1584 LOGW("We already own output buffer %p, yet received "
1585 "a FILL_BUFFER_DONE.", buffer);
1586 }
1587
1588 info->mOwnedByComponent = false;
1589
1590 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001591 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001592
1593 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001594 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001595 CHECK_EQ(err, OK);
1596
1597 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001598#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001599 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1600 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001601 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001602 mNoMoreOutputData = true;
1603 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001604#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001605 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1606 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001607
Andreas Huber52733b82010-01-25 10:41:35 -08001608 if (info->mMediaBuffer == NULL) {
1609 CHECK(mOMXLivesLocally);
1610 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1611 CHECK(mQuirks & kDefersOutputBufferAllocation);
1612
1613 // The qcom video decoders on Nexus don't actually allocate
1614 // output buffer memory on a call to OMX_AllocateBuffer
1615 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1616 // structure is only filled in later.
1617
1618 info->mMediaBuffer = new MediaBuffer(
1619 msg.u.extended_buffer_data.data_ptr,
1620 info->mSize);
1621 info->mMediaBuffer->setObserver(this);
1622 }
1623
Andreas Huberbe06d262009-08-14 14:37:10 -07001624 MediaBuffer *buffer = info->mMediaBuffer;
1625
1626 buffer->set_range(
1627 msg.u.extended_buffer_data.range_offset,
1628 msg.u.extended_buffer_data.range_length);
1629
1630 buffer->meta_data()->clear();
1631
Andreas Huberfa8de752009-10-08 10:07:49 -07001632 buffer->meta_data()->setInt64(
1633 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001634
1635 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1636 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1637 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001638 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1639 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1640 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001641
1642 buffer->meta_data()->setPointer(
1643 kKeyPlatformPrivate,
1644 msg.u.extended_buffer_data.platform_private);
1645
1646 buffer->meta_data()->setPointer(
1647 kKeyBufferID,
1648 msg.u.extended_buffer_data.buffer);
1649
Andreas Huber2ea14e22009-12-16 09:30:55 -08001650 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1651 CODEC_LOGV("No more output data.");
1652 mNoMoreOutputData = true;
1653 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07001654
1655 if (mTargetTimeUs >= 0) {
1656 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
1657
1658 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
1659 CODEC_LOGV(
1660 "skipping output buffer at timestamp %lld us",
1661 msg.u.extended_buffer_data.timestamp);
1662
1663 fillOutputBuffer(info);
1664 break;
1665 }
1666
1667 CODEC_LOGV(
1668 "returning output buffer at target timestamp "
1669 "%lld us",
1670 msg.u.extended_buffer_data.timestamp);
1671
1672 mTargetTimeUs = -1;
1673 }
1674
1675 mFilledBuffers.push_back(i);
1676 mBufferFilled.signal();
Andreas Huberbe06d262009-08-14 14:37:10 -07001677 }
1678
1679 break;
1680 }
1681
1682 default:
1683 {
1684 CHECK(!"should not be here.");
1685 break;
1686 }
1687 }
1688}
1689
1690void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1691 switch (event) {
1692 case OMX_EventCmdComplete:
1693 {
1694 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1695 break;
1696 }
1697
1698 case OMX_EventError:
1699 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001700 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001701
1702 setState(ERROR);
1703 break;
1704 }
1705
1706 case OMX_EventPortSettingsChanged:
1707 {
1708 onPortSettingsChanged(data1);
1709 break;
1710 }
1711
Andreas Huber2ea14e22009-12-16 09:30:55 -08001712#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001713 case OMX_EventBufferFlag:
1714 {
Andreas Huber4c483422009-09-02 16:05:36 -07001715 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001716
1717 if (data1 == kPortIndexOutput) {
1718 mNoMoreOutputData = true;
1719 }
1720 break;
1721 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001722#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001723
1724 default:
1725 {
Andreas Huber4c483422009-09-02 16:05:36 -07001726 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001727 break;
1728 }
1729 }
1730}
1731
Andreas Huberb1678602009-10-19 13:06:40 -07001732// Has the format changed in any way that the client would have to be aware of?
1733static bool formatHasNotablyChanged(
1734 const sp<MetaData> &from, const sp<MetaData> &to) {
1735 if (from.get() == NULL && to.get() == NULL) {
1736 return false;
1737 }
1738
Andreas Huberf68c1682009-10-21 14:01:30 -07001739 if ((from.get() == NULL && to.get() != NULL)
1740 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001741 return true;
1742 }
1743
1744 const char *mime_from, *mime_to;
1745 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1746 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1747
1748 if (strcasecmp(mime_from, mime_to)) {
1749 return true;
1750 }
1751
1752 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1753 int32_t colorFormat_from, colorFormat_to;
1754 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1755 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1756
1757 if (colorFormat_from != colorFormat_to) {
1758 return true;
1759 }
1760
1761 int32_t width_from, width_to;
1762 CHECK(from->findInt32(kKeyWidth, &width_from));
1763 CHECK(to->findInt32(kKeyWidth, &width_to));
1764
1765 if (width_from != width_to) {
1766 return true;
1767 }
1768
1769 int32_t height_from, height_to;
1770 CHECK(from->findInt32(kKeyHeight, &height_from));
1771 CHECK(to->findInt32(kKeyHeight, &height_to));
1772
1773 if (height_from != height_to) {
1774 return true;
1775 }
1776 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1777 int32_t numChannels_from, numChannels_to;
1778 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1779 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1780
1781 if (numChannels_from != numChannels_to) {
1782 return true;
1783 }
1784
1785 int32_t sampleRate_from, sampleRate_to;
1786 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1787 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1788
1789 if (sampleRate_from != sampleRate_to) {
1790 return true;
1791 }
1792 }
1793
1794 return false;
1795}
1796
Andreas Huberbe06d262009-08-14 14:37:10 -07001797void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1798 switch (cmd) {
1799 case OMX_CommandStateSet:
1800 {
1801 onStateChange((OMX_STATETYPE)data);
1802 break;
1803 }
1804
1805 case OMX_CommandPortDisable:
1806 {
1807 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001808 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001809
1810 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1811 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1812 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1813
1814 mPortStatus[portIndex] = DISABLED;
1815
1816 if (mState == RECONFIGURING) {
1817 CHECK_EQ(portIndex, kPortIndexOutput);
1818
Andreas Huberb1678602009-10-19 13:06:40 -07001819 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001820 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001821
1822 // Don't notify clients if the output port settings change
1823 // wasn't of importance to them, i.e. it may be that just the
1824 // number of buffers has changed and nothing else.
1825 mOutputPortSettingsHaveChanged =
1826 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001827
Andreas Huberbe06d262009-08-14 14:37:10 -07001828 enablePortAsync(portIndex);
1829
1830 status_t err = allocateBuffersOnPort(portIndex);
1831 CHECK_EQ(err, OK);
1832 }
1833 break;
1834 }
1835
1836 case OMX_CommandPortEnable:
1837 {
1838 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001839 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001840
1841 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1842 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1843
1844 mPortStatus[portIndex] = ENABLED;
1845
1846 if (mState == RECONFIGURING) {
1847 CHECK_EQ(portIndex, kPortIndexOutput);
1848
1849 setState(EXECUTING);
1850
1851 fillOutputBuffers();
1852 }
1853 break;
1854 }
1855
1856 case OMX_CommandFlush:
1857 {
1858 OMX_U32 portIndex = data;
1859
Andreas Huber4c483422009-09-02 16:05:36 -07001860 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001861
1862 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1863 mPortStatus[portIndex] = ENABLED;
1864
1865 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1866 mPortBuffers[portIndex].size());
1867
1868 if (mState == RECONFIGURING) {
1869 CHECK_EQ(portIndex, kPortIndexOutput);
1870
1871 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001872 } else if (mState == EXECUTING_TO_IDLE) {
1873 if (mPortStatus[kPortIndexInput] == ENABLED
1874 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001875 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001876 "transition from EXECUTING to IDLE.");
1877
1878 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1879 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1880
1881 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001882 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001883 CHECK_EQ(err, OK);
1884 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001885 } else {
1886 // We're flushing both ports in preparation for seeking.
1887
1888 if (mPortStatus[kPortIndexInput] == ENABLED
1889 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001890 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001891 " seek-time.");
1892
Andreas Huber1f24b302010-06-10 11:12:39 -07001893 // We implicitly resume pulling on our upstream source.
1894 mPaused = false;
1895
Andreas Huberbe06d262009-08-14 14:37:10 -07001896 drainInputBuffers();
1897 fillOutputBuffers();
1898 }
1899 }
1900
1901 break;
1902 }
1903
1904 default:
1905 {
Andreas Huber4c483422009-09-02 16:05:36 -07001906 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001907 break;
1908 }
1909 }
1910}
1911
1912void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001913 CODEC_LOGV("onStateChange %d", newState);
1914
Andreas Huberbe06d262009-08-14 14:37:10 -07001915 switch (newState) {
1916 case OMX_StateIdle:
1917 {
Andreas Huber4c483422009-09-02 16:05:36 -07001918 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001919 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001920 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001921 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1922
1923 CHECK_EQ(err, OK);
1924
1925 setState(IDLE_TO_EXECUTING);
1926 } else {
1927 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1928
1929 CHECK_EQ(
1930 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1931 mPortBuffers[kPortIndexInput].size());
1932
1933 CHECK_EQ(
1934 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1935 mPortBuffers[kPortIndexOutput].size());
1936
Andreas Huber784202e2009-10-15 13:46:54 -07001937 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001938 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1939
1940 CHECK_EQ(err, OK);
1941
1942 err = freeBuffersOnPort(kPortIndexInput);
1943 CHECK_EQ(err, OK);
1944
1945 err = freeBuffersOnPort(kPortIndexOutput);
1946 CHECK_EQ(err, OK);
1947
1948 mPortStatus[kPortIndexInput] = ENABLED;
1949 mPortStatus[kPortIndexOutput] = ENABLED;
1950
1951 setState(IDLE_TO_LOADED);
1952 }
1953 break;
1954 }
1955
1956 case OMX_StateExecuting:
1957 {
1958 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1959
Andreas Huber4c483422009-09-02 16:05:36 -07001960 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001961
1962 setState(EXECUTING);
1963
Andreas Huber42978e52009-08-27 10:08:39 -07001964 // Buffers will be submitted to the component in the first
1965 // call to OMXCodec::read as mInitialBufferSubmit is true at
1966 // this point. This ensures that this on_message call returns,
1967 // releases the lock and ::init can notice the state change and
1968 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001969 break;
1970 }
1971
1972 case OMX_StateLoaded:
1973 {
1974 CHECK_EQ(mState, IDLE_TO_LOADED);
1975
Andreas Huber4c483422009-09-02 16:05:36 -07001976 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001977
1978 setState(LOADED);
1979 break;
1980 }
1981
Andreas Huberc712b9f2010-01-20 15:05:46 -08001982 case OMX_StateInvalid:
1983 {
1984 setState(ERROR);
1985 break;
1986 }
1987
Andreas Huberbe06d262009-08-14 14:37:10 -07001988 default:
1989 {
1990 CHECK(!"should not be here.");
1991 break;
1992 }
1993 }
1994}
1995
1996// static
1997size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1998 size_t n = 0;
1999 for (size_t i = 0; i < buffers.size(); ++i) {
2000 if (!buffers[i].mOwnedByComponent) {
2001 ++n;
2002 }
2003 }
2004
2005 return n;
2006}
2007
2008status_t OMXCodec::freeBuffersOnPort(
2009 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2010 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2011
2012 status_t stickyErr = OK;
2013
2014 for (size_t i = buffers->size(); i-- > 0;) {
2015 BufferInfo *info = &buffers->editItemAt(i);
2016
2017 if (onlyThoseWeOwn && info->mOwnedByComponent) {
2018 continue;
2019 }
2020
2021 CHECK_EQ(info->mOwnedByComponent, false);
2022
Andreas Huber92022852009-09-14 15:24:14 -07002023 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2024
Andreas Huberbe06d262009-08-14 14:37:10 -07002025 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002026 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07002027
2028 if (err != OK) {
2029 stickyErr = err;
2030 }
2031
2032 if (info->mMediaBuffer != NULL) {
2033 info->mMediaBuffer->setObserver(NULL);
2034
2035 // Make sure nobody but us owns this buffer at this point.
2036 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2037
2038 info->mMediaBuffer->release();
2039 }
2040
2041 buffers->removeAt(i);
2042 }
2043
2044 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2045
2046 return stickyErr;
2047}
2048
2049void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002050 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002051
2052 CHECK_EQ(mState, EXECUTING);
2053 CHECK_EQ(portIndex, kPortIndexOutput);
2054 setState(RECONFIGURING);
2055
2056 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002057 if (!flushPortAsync(portIndex)) {
2058 onCmdComplete(OMX_CommandFlush, portIndex);
2059 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002060 } else {
2061 disablePortAsync(portIndex);
2062 }
2063}
2064
Andreas Huber404cc412009-08-25 14:26:05 -07002065bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002066 CHECK(mState == EXECUTING || mState == RECONFIGURING
2067 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002068
Andreas Huber4c483422009-09-02 16:05:36 -07002069 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002070 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2071 mPortBuffers[portIndex].size());
2072
Andreas Huberbe06d262009-08-14 14:37:10 -07002073 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2074 mPortStatus[portIndex] = SHUTTING_DOWN;
2075
Andreas Huber404cc412009-08-25 14:26:05 -07002076 if ((mQuirks & kRequiresFlushCompleteEmulation)
2077 && countBuffersWeOwn(mPortBuffers[portIndex])
2078 == mPortBuffers[portIndex].size()) {
2079 // No flush is necessary and this component fails to send a
2080 // flush-complete event in this case.
2081
2082 return false;
2083 }
2084
Andreas Huberbe06d262009-08-14 14:37:10 -07002085 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002086 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002087 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002088
2089 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002090}
2091
2092void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2093 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2094
2095 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2096 mPortStatus[portIndex] = DISABLING;
2097
2098 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002099 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002100 CHECK_EQ(err, OK);
2101
2102 freeBuffersOnPort(portIndex, true);
2103}
2104
2105void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2106 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2107
2108 CHECK_EQ(mPortStatus[portIndex], DISABLED);
2109 mPortStatus[portIndex] = ENABLING;
2110
2111 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002112 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002113 CHECK_EQ(err, OK);
2114}
2115
2116void OMXCodec::fillOutputBuffers() {
2117 CHECK_EQ(mState, EXECUTING);
2118
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002119 // This is a workaround for some decoders not properly reporting
2120 // end-of-output-stream. If we own all input buffers and also own
2121 // all output buffers and we already signalled end-of-input-stream,
2122 // the end-of-output-stream is implied.
2123 if (mSignalledEOS
2124 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2125 == mPortBuffers[kPortIndexInput].size()
2126 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2127 == mPortBuffers[kPortIndexOutput].size()) {
2128 mNoMoreOutputData = true;
2129 mBufferFilled.signal();
2130
2131 return;
2132 }
2133
Andreas Huberbe06d262009-08-14 14:37:10 -07002134 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2135 for (size_t i = 0; i < buffers->size(); ++i) {
2136 fillOutputBuffer(&buffers->editItemAt(i));
2137 }
2138}
2139
2140void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002141 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002142
2143 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2144 for (size_t i = 0; i < buffers->size(); ++i) {
2145 drainInputBuffer(&buffers->editItemAt(i));
2146 }
2147}
2148
2149void OMXCodec::drainInputBuffer(BufferInfo *info) {
2150 CHECK_EQ(info->mOwnedByComponent, false);
2151
2152 if (mSignalledEOS) {
2153 return;
2154 }
2155
2156 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2157 const CodecSpecificData *specific =
2158 mCodecSpecificData[mCodecSpecificDataIndex];
2159
2160 size_t size = specific->mSize;
2161
Andreas Hubere6c40962009-09-10 14:13:30 -07002162 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002163 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002164 static const uint8_t kNALStartCode[4] =
2165 { 0x00, 0x00, 0x00, 0x01 };
2166
Andreas Huberc712b9f2010-01-20 15:05:46 -08002167 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002168
2169 size += 4;
2170
Andreas Huberc712b9f2010-01-20 15:05:46 -08002171 memcpy(info->mData, kNALStartCode, 4);
2172 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002173 specific->mData, specific->mSize);
2174 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002175 CHECK(info->mSize >= specific->mSize);
2176 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002177 }
2178
Andreas Huber2ea14e22009-12-16 09:30:55 -08002179 mNoMoreOutputData = false;
2180
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002181 CODEC_LOGV("calling emptyBuffer with codec specific data");
2182
Andreas Huber784202e2009-10-15 13:46:54 -07002183 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002184 mNode, info->mBuffer, 0, size,
2185 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2186 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002187 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002188
2189 info->mOwnedByComponent = true;
2190
2191 ++mCodecSpecificDataIndex;
2192 return;
2193 }
2194
Andreas Huber1f24b302010-06-10 11:12:39 -07002195 if (mPaused) {
2196 return;
2197 }
2198
Andreas Huberbe06d262009-08-14 14:37:10 -07002199 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002200
Andreas Hubera4357ad2010-04-02 12:49:54 -07002201 bool signalEOS = false;
2202 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002203
Andreas Hubera4357ad2010-04-02 12:49:54 -07002204 size_t offset = 0;
2205 int32_t n = 0;
2206 for (;;) {
2207 MediaBuffer *srcBuffer;
James Dong53d4e0d2010-07-21 14:51:35 -07002208 MediaSource::ReadOptions options;
2209 if (mSkipTimeUs >= 0) {
2210 options.setSkipFrame(mSkipTimeUs);
2211 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002212 if (mSeekTimeUs >= 0) {
2213 if (mLeftOverBuffer) {
2214 mLeftOverBuffer->release();
2215 mLeftOverBuffer = NULL;
2216 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002217 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002218
2219 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002220 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002221 mBufferFilled.signal();
2222
2223 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002224
2225 if (err == OK) {
2226 int64_t targetTimeUs;
2227 if (srcBuffer->meta_data()->findInt64(
2228 kKeyTargetTime, &targetTimeUs)
2229 && targetTimeUs >= 0) {
2230 mTargetTimeUs = targetTimeUs;
2231 } else {
2232 mTargetTimeUs = -1;
2233 }
2234 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002235 } else if (mLeftOverBuffer) {
2236 srcBuffer = mLeftOverBuffer;
2237 mLeftOverBuffer = NULL;
2238
2239 err = OK;
2240 } else {
James Dong53d4e0d2010-07-21 14:51:35 -07002241 err = mSource->read(&srcBuffer, &options);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002242 }
2243
2244 if (err != OK) {
2245 signalEOS = true;
2246 mFinalStatus = err;
2247 mSignalledEOS = true;
2248 break;
2249 }
2250
2251 size_t remainingBytes = info->mSize - offset;
2252
2253 if (srcBuffer->range_length() > remainingBytes) {
2254 if (offset == 0) {
2255 CODEC_LOGE(
2256 "Codec's input buffers are too small to accomodate "
2257 "buffer read from source (info->mSize = %d, srcLength = %d)",
2258 info->mSize, srcBuffer->range_length());
2259
2260 srcBuffer->release();
2261 srcBuffer = NULL;
2262
2263 setState(ERROR);
2264 return;
2265 }
2266
2267 mLeftOverBuffer = srcBuffer;
2268 break;
2269 }
2270
James Dong4f501f02010-06-07 14:41:41 -07002271 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2272 CHECK(mOMXLivesLocally && offset == 0);
2273 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2274 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2275 } else {
2276 memcpy((uint8_t *)info->mData + offset,
2277 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2278 srcBuffer->range_length());
2279 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002280
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002281 int64_t lastBufferTimeUs;
2282 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002283 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002284
Andreas Hubera4357ad2010-04-02 12:49:54 -07002285 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002286 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002287 }
2288
2289 offset += srcBuffer->range_length();
2290
2291 srcBuffer->release();
2292 srcBuffer = NULL;
2293
2294 ++n;
2295
2296 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2297 break;
2298 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002299
2300 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2301
2302 if (coalescedDurationUs > 250000ll) {
2303 // Don't coalesce more than 250ms worth of encoded data at once.
2304 break;
2305 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002306 }
2307
2308 if (n > 1) {
2309 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002310 }
2311
2312 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002313
Andreas Hubera4357ad2010-04-02 12:49:54 -07002314 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002315 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002316 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002317 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002318 }
2319
Andreas Hubera4357ad2010-04-02 12:49:54 -07002320 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2321 "timestamp %lld us (%.2f secs)",
2322 info->mBuffer, offset,
2323 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002324
Andreas Huber784202e2009-10-15 13:46:54 -07002325 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002326 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002327 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002328
2329 if (err != OK) {
2330 setState(ERROR);
2331 return;
2332 }
2333
2334 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002335
2336 // This component does not ever signal the EOS flag on output buffers,
2337 // Thanks for nothing.
2338 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2339 mNoMoreOutputData = true;
2340 mBufferFilled.signal();
2341 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002342}
2343
2344void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2345 CHECK_EQ(info->mOwnedByComponent, false);
2346
Andreas Huber404cc412009-08-25 14:26:05 -07002347 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002348 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002349 "calling fillOutputBuffer");
2350 return;
2351 }
2352
Andreas Huber4c483422009-09-02 16:05:36 -07002353 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002354 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002355
2356 if (err != OK) {
2357 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2358
2359 setState(ERROR);
2360 return;
2361 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002362
2363 info->mOwnedByComponent = true;
2364}
2365
2366void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2367 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2368 for (size_t i = 0; i < buffers->size(); ++i) {
2369 if ((*buffers)[i].mBuffer == buffer) {
2370 drainInputBuffer(&buffers->editItemAt(i));
2371 return;
2372 }
2373 }
2374
2375 CHECK(!"should not be here.");
2376}
2377
2378void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2379 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2380 for (size_t i = 0; i < buffers->size(); ++i) {
2381 if ((*buffers)[i].mBuffer == buffer) {
2382 fillOutputBuffer(&buffers->editItemAt(i));
2383 return;
2384 }
2385 }
2386
2387 CHECK(!"should not be here.");
2388}
2389
2390void OMXCodec::setState(State newState) {
2391 mState = newState;
2392 mAsyncCompletion.signal();
2393
2394 // This may cause some spurious wakeups but is necessary to
2395 // unblock the reader if we enter ERROR state.
2396 mBufferFilled.signal();
2397}
2398
Andreas Huberda050cf22009-09-02 14:01:43 -07002399void OMXCodec::setRawAudioFormat(
2400 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002401
2402 // port definition
2403 OMX_PARAM_PORTDEFINITIONTYPE def;
2404 InitOMXParams(&def);
2405 def.nPortIndex = portIndex;
2406 status_t err = mOMX->getParameter(
2407 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2408 CHECK_EQ(err, OK);
2409 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2410 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2411 &def, sizeof(def)), OK);
2412
2413 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002414 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002415 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002416 pcmParams.nPortIndex = portIndex;
2417
James Dongabed93a2010-04-22 17:27:04 -07002418 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002419 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2420
2421 CHECK_EQ(err, OK);
2422
2423 pcmParams.nChannels = numChannels;
2424 pcmParams.eNumData = OMX_NumericalDataSigned;
2425 pcmParams.bInterleaved = OMX_TRUE;
2426 pcmParams.nBitPerSample = 16;
2427 pcmParams.nSamplingRate = sampleRate;
2428 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2429
2430 if (numChannels == 1) {
2431 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2432 } else {
2433 CHECK_EQ(numChannels, 2);
2434
2435 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2436 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2437 }
2438
Andreas Huber784202e2009-10-15 13:46:54 -07002439 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002440 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2441
2442 CHECK_EQ(err, OK);
2443}
2444
James Dong17299ab2010-05-14 15:45:22 -07002445static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2446 if (isAMRWB) {
2447 if (bps <= 6600) {
2448 return OMX_AUDIO_AMRBandModeWB0;
2449 } else if (bps <= 8850) {
2450 return OMX_AUDIO_AMRBandModeWB1;
2451 } else if (bps <= 12650) {
2452 return OMX_AUDIO_AMRBandModeWB2;
2453 } else if (bps <= 14250) {
2454 return OMX_AUDIO_AMRBandModeWB3;
2455 } else if (bps <= 15850) {
2456 return OMX_AUDIO_AMRBandModeWB4;
2457 } else if (bps <= 18250) {
2458 return OMX_AUDIO_AMRBandModeWB5;
2459 } else if (bps <= 19850) {
2460 return OMX_AUDIO_AMRBandModeWB6;
2461 } else if (bps <= 23050) {
2462 return OMX_AUDIO_AMRBandModeWB7;
2463 }
2464
2465 // 23850 bps
2466 return OMX_AUDIO_AMRBandModeWB8;
2467 } else { // AMRNB
2468 if (bps <= 4750) {
2469 return OMX_AUDIO_AMRBandModeNB0;
2470 } else if (bps <= 5150) {
2471 return OMX_AUDIO_AMRBandModeNB1;
2472 } else if (bps <= 5900) {
2473 return OMX_AUDIO_AMRBandModeNB2;
2474 } else if (bps <= 6700) {
2475 return OMX_AUDIO_AMRBandModeNB3;
2476 } else if (bps <= 7400) {
2477 return OMX_AUDIO_AMRBandModeNB4;
2478 } else if (bps <= 7950) {
2479 return OMX_AUDIO_AMRBandModeNB5;
2480 } else if (bps <= 10200) {
2481 return OMX_AUDIO_AMRBandModeNB6;
2482 }
2483
2484 // 12200 bps
2485 return OMX_AUDIO_AMRBandModeNB7;
2486 }
2487}
2488
2489void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002490 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002491
Andreas Huber8768f2c2009-12-01 15:26:54 -08002492 OMX_AUDIO_PARAM_AMRTYPE def;
2493 InitOMXParams(&def);
2494 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002495
Andreas Huber8768f2c2009-12-01 15:26:54 -08002496 status_t err =
2497 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002498
Andreas Huber8768f2c2009-12-01 15:26:54 -08002499 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002500
Andreas Huber8768f2c2009-12-01 15:26:54 -08002501 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002502
James Dong17299ab2010-05-14 15:45:22 -07002503 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002504 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2505 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002506
2507 ////////////////////////
2508
2509 if (mIsEncoder) {
2510 sp<MetaData> format = mSource->getFormat();
2511 int32_t sampleRate;
2512 int32_t numChannels;
2513 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2514 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2515
2516 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2517 }
2518}
2519
James Dong17299ab2010-05-14 15:45:22 -07002520void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002521 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002522 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002523 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002524 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002525
2526 //////////////// output port ////////////////////
2527 // format
2528 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2529 format.nPortIndex = kPortIndexOutput;
2530 format.nIndex = 0;
2531 status_t err = OMX_ErrorNone;
2532 while (OMX_ErrorNone == err) {
2533 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2534 &format, sizeof(format)), OK);
2535 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2536 break;
2537 }
2538 format.nIndex++;
2539 }
2540 CHECK_EQ(OK, err);
2541 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2542 &format, sizeof(format)), OK);
2543
2544 // port definition
2545 OMX_PARAM_PORTDEFINITIONTYPE def;
2546 InitOMXParams(&def);
2547 def.nPortIndex = kPortIndexOutput;
2548 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2549 &def, sizeof(def)), OK);
2550 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2551 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2552 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2553 &def, sizeof(def)), OK);
2554
2555 // profile
2556 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2557 InitOMXParams(&profile);
2558 profile.nPortIndex = kPortIndexOutput;
2559 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2560 &profile, sizeof(profile)), OK);
2561 profile.nChannels = numChannels;
2562 profile.eChannelMode = (numChannels == 1?
2563 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2564 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002565 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002566 profile.nAudioBandWidth = 0;
2567 profile.nFrameLength = 0;
2568 profile.nAACtools = OMX_AUDIO_AACToolAll;
2569 profile.nAACERtools = OMX_AUDIO_AACERNone;
2570 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2571 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2572 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2573 &profile, sizeof(profile)), OK);
2574
Andreas Huberda050cf22009-09-02 14:01:43 -07002575 } else {
2576 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002577 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002578 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002579
Andreas Huber784202e2009-10-15 13:46:54 -07002580 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002581 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2582 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002583
Andreas Huberda050cf22009-09-02 14:01:43 -07002584 profile.nChannels = numChannels;
2585 profile.nSampleRate = sampleRate;
2586 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002587
Andreas Huber784202e2009-10-15 13:46:54 -07002588 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002589 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2590 CHECK_EQ(err, OK);
2591 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002592}
2593
2594void OMXCodec::setImageOutputFormat(
2595 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002596 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002597
2598#if 0
2599 OMX_INDEXTYPE index;
2600 status_t err = mOMX->get_extension_index(
2601 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2602 CHECK_EQ(err, OK);
2603
2604 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2605 CHECK_EQ(err, OK);
2606#endif
2607
2608 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002609 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002610 def.nPortIndex = kPortIndexOutput;
2611
Andreas Huber784202e2009-10-15 13:46:54 -07002612 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002613 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2614 CHECK_EQ(err, OK);
2615
2616 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2617
2618 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002619
Andreas Huberbe06d262009-08-14 14:37:10 -07002620 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2621 imageDef->eColorFormat = format;
2622 imageDef->nFrameWidth = width;
2623 imageDef->nFrameHeight = height;
2624
2625 switch (format) {
2626 case OMX_COLOR_FormatYUV420PackedPlanar:
2627 case OMX_COLOR_FormatYUV411Planar:
2628 {
2629 def.nBufferSize = (width * height * 3) / 2;
2630 break;
2631 }
2632
2633 case OMX_COLOR_FormatCbYCrY:
2634 {
2635 def.nBufferSize = width * height * 2;
2636 break;
2637 }
2638
2639 case OMX_COLOR_Format32bitARGB8888:
2640 {
2641 def.nBufferSize = width * height * 4;
2642 break;
2643 }
2644
Andreas Huber201511c2009-09-08 14:01:44 -07002645 case OMX_COLOR_Format16bitARGB4444:
2646 case OMX_COLOR_Format16bitARGB1555:
2647 case OMX_COLOR_Format16bitRGB565:
2648 case OMX_COLOR_Format16bitBGR565:
2649 {
2650 def.nBufferSize = width * height * 2;
2651 break;
2652 }
2653
Andreas Huberbe06d262009-08-14 14:37:10 -07002654 default:
2655 CHECK(!"Should not be here. Unknown color format.");
2656 break;
2657 }
2658
Andreas Huber5c0a9132009-08-20 11:16:40 -07002659 def.nBufferCountActual = def.nBufferCountMin;
2660
Andreas Huber784202e2009-10-15 13:46:54 -07002661 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002662 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2663 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002664}
Andreas Huberbe06d262009-08-14 14:37:10 -07002665
Andreas Huber5c0a9132009-08-20 11:16:40 -07002666void OMXCodec::setJPEGInputFormat(
2667 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2668 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002669 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002670 def.nPortIndex = kPortIndexInput;
2671
Andreas Huber784202e2009-10-15 13:46:54 -07002672 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002673 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2674 CHECK_EQ(err, OK);
2675
Andreas Huber5c0a9132009-08-20 11:16:40 -07002676 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2677 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2678
Andreas Huberbe06d262009-08-14 14:37:10 -07002679 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2680 imageDef->nFrameWidth = width;
2681 imageDef->nFrameHeight = height;
2682
Andreas Huber5c0a9132009-08-20 11:16:40 -07002683 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002684 def.nBufferCountActual = def.nBufferCountMin;
2685
Andreas Huber784202e2009-10-15 13:46:54 -07002686 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002687 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2688 CHECK_EQ(err, OK);
2689}
2690
2691void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2692 CodecSpecificData *specific =
2693 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2694
2695 specific->mSize = size;
2696 memcpy(specific->mData, data, size);
2697
2698 mCodecSpecificData.push(specific);
2699}
2700
2701void OMXCodec::clearCodecSpecificData() {
2702 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2703 free(mCodecSpecificData.editItemAt(i));
2704 }
2705 mCodecSpecificData.clear();
2706 mCodecSpecificDataIndex = 0;
2707}
2708
James Dong36e573b2010-06-19 09:04:18 -07002709status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07002710 Mutex::Autolock autoLock(mLock);
2711
Andreas Huberbe06d262009-08-14 14:37:10 -07002712 if (mState != LOADED) {
2713 return UNKNOWN_ERROR;
2714 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002715
Andreas Huberbe06d262009-08-14 14:37:10 -07002716 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002717 if (mQuirks & kWantsNALFragments) {
2718 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002719 }
James Dong36e573b2010-06-19 09:04:18 -07002720 if (meta) {
2721 int64_t startTimeUs = 0;
2722 int64_t timeUs;
2723 if (meta->findInt64(kKeyTime, &timeUs)) {
2724 startTimeUs = timeUs;
2725 }
2726 params->setInt64(kKeyTime, startTimeUs);
2727 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002728 status_t err = mSource->start(params.get());
2729
2730 if (err != OK) {
2731 return err;
2732 }
2733
2734 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002735 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002736 mSignalledEOS = false;
2737 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002738 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002739 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002740 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
2741 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07002742 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07002743 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002744
2745 return init();
2746}
2747
2748status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002749 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002750
2751 Mutex::Autolock autoLock(mLock);
2752
2753 while (isIntermediateState(mState)) {
2754 mAsyncCompletion.wait(mLock);
2755 }
2756
2757 switch (mState) {
2758 case LOADED:
2759 case ERROR:
2760 break;
2761
2762 case EXECUTING:
2763 {
2764 setState(EXECUTING_TO_IDLE);
2765
Andreas Huber127fcdc2009-08-26 16:27:02 -07002766 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002767 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002768 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002769
Andreas Huber127fcdc2009-08-26 16:27:02 -07002770 bool emulateInputFlushCompletion =
2771 !flushPortAsync(kPortIndexInput);
2772
2773 bool emulateOutputFlushCompletion =
2774 !flushPortAsync(kPortIndexOutput);
2775
2776 if (emulateInputFlushCompletion) {
2777 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2778 }
2779
2780 if (emulateOutputFlushCompletion) {
2781 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2782 }
2783 } else {
2784 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2785 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2786
2787 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002788 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002789 CHECK_EQ(err, OK);
2790 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002791
2792 while (mState != LOADED && mState != ERROR) {
2793 mAsyncCompletion.wait(mLock);
2794 }
2795
2796 break;
2797 }
2798
2799 default:
2800 {
2801 CHECK(!"should not be here.");
2802 break;
2803 }
2804 }
2805
Andreas Hubera4357ad2010-04-02 12:49:54 -07002806 if (mLeftOverBuffer) {
2807 mLeftOverBuffer->release();
2808 mLeftOverBuffer = NULL;
2809 }
2810
Andreas Huberbe06d262009-08-14 14:37:10 -07002811 mSource->stop();
2812
Andreas Huber4a9375e2010-02-09 11:54:33 -08002813 CODEC_LOGV("stopped");
2814
Andreas Huberbe06d262009-08-14 14:37:10 -07002815 return OK;
2816}
2817
2818sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002819 Mutex::Autolock autoLock(mLock);
2820
Andreas Huberbe06d262009-08-14 14:37:10 -07002821 return mOutputFormat;
2822}
2823
2824status_t OMXCodec::read(
2825 MediaBuffer **buffer, const ReadOptions *options) {
2826 *buffer = NULL;
2827
2828 Mutex::Autolock autoLock(mLock);
2829
Andreas Huberd06e5b82009-08-28 13:18:14 -07002830 if (mState != EXECUTING && mState != RECONFIGURING) {
2831 return UNKNOWN_ERROR;
2832 }
2833
Andreas Hubere981c332009-10-22 13:49:30 -07002834 bool seeking = false;
2835 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002836 ReadOptions::SeekMode seekMode;
2837 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07002838 seeking = true;
2839 }
James Dong53d4e0d2010-07-21 14:51:35 -07002840 int64_t skipTimeUs;
2841 if (options && options->getSkipFrame(&skipTimeUs)) {
2842 mSkipTimeUs = skipTimeUs;
2843 } else {
2844 mSkipTimeUs = -1;
2845 }
Andreas Hubere981c332009-10-22 13:49:30 -07002846
Andreas Huber42978e52009-08-27 10:08:39 -07002847 if (mInitialBufferSubmit) {
2848 mInitialBufferSubmit = false;
2849
Andreas Hubere981c332009-10-22 13:49:30 -07002850 if (seeking) {
2851 CHECK(seekTimeUs >= 0);
2852 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002853 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07002854
2855 // There's no reason to trigger the code below, there's
2856 // nothing to flush yet.
2857 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002858 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07002859 }
2860
Andreas Huber42978e52009-08-27 10:08:39 -07002861 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002862
Andreas Huberd06e5b82009-08-28 13:18:14 -07002863 if (mState == EXECUTING) {
2864 // Otherwise mState == RECONFIGURING and this code will trigger
2865 // after the output port is reenabled.
2866 fillOutputBuffers();
2867 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002868 }
2869
Andreas Hubere981c332009-10-22 13:49:30 -07002870 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002871 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002872
2873 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002874
2875 CHECK(seekTimeUs >= 0);
2876 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002877 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07002878
2879 mFilledBuffers.clear();
2880
2881 CHECK_EQ(mState, EXECUTING);
2882
Andreas Huber404cc412009-08-25 14:26:05 -07002883 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2884 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2885
2886 if (emulateInputFlushCompletion) {
2887 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2888 }
2889
2890 if (emulateOutputFlushCompletion) {
2891 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2892 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002893
2894 while (mSeekTimeUs >= 0) {
2895 mBufferFilled.wait(mLock);
2896 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002897 }
2898
2899 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2900 mBufferFilled.wait(mLock);
2901 }
2902
2903 if (mState == ERROR) {
2904 return UNKNOWN_ERROR;
2905 }
2906
2907 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08002908 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07002909 }
2910
Andreas Hubercfd55572009-10-09 14:11:28 -07002911 if (mOutputPortSettingsHaveChanged) {
2912 mOutputPortSettingsHaveChanged = false;
2913
2914 return INFO_FORMAT_CHANGED;
2915 }
2916
Andreas Huberbe06d262009-08-14 14:37:10 -07002917 size_t index = *mFilledBuffers.begin();
2918 mFilledBuffers.erase(mFilledBuffers.begin());
2919
2920 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2921 info->mMediaBuffer->add_ref();
2922 *buffer = info->mMediaBuffer;
2923
2924 return OK;
2925}
2926
2927void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2928 Mutex::Autolock autoLock(mLock);
2929
2930 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2931 for (size_t i = 0; i < buffers->size(); ++i) {
2932 BufferInfo *info = &buffers->editItemAt(i);
2933
2934 if (info->mMediaBuffer == buffer) {
2935 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2936 fillOutputBuffer(info);
2937 return;
2938 }
2939 }
2940
2941 CHECK(!"should not be here.");
2942}
2943
2944static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2945 static const char *kNames[] = {
2946 "OMX_IMAGE_CodingUnused",
2947 "OMX_IMAGE_CodingAutoDetect",
2948 "OMX_IMAGE_CodingJPEG",
2949 "OMX_IMAGE_CodingJPEG2K",
2950 "OMX_IMAGE_CodingEXIF",
2951 "OMX_IMAGE_CodingTIFF",
2952 "OMX_IMAGE_CodingGIF",
2953 "OMX_IMAGE_CodingPNG",
2954 "OMX_IMAGE_CodingLZW",
2955 "OMX_IMAGE_CodingBMP",
2956 };
2957
2958 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2959
2960 if (type < 0 || (size_t)type >= numNames) {
2961 return "UNKNOWN";
2962 } else {
2963 return kNames[type];
2964 }
2965}
2966
2967static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2968 static const char *kNames[] = {
2969 "OMX_COLOR_FormatUnused",
2970 "OMX_COLOR_FormatMonochrome",
2971 "OMX_COLOR_Format8bitRGB332",
2972 "OMX_COLOR_Format12bitRGB444",
2973 "OMX_COLOR_Format16bitARGB4444",
2974 "OMX_COLOR_Format16bitARGB1555",
2975 "OMX_COLOR_Format16bitRGB565",
2976 "OMX_COLOR_Format16bitBGR565",
2977 "OMX_COLOR_Format18bitRGB666",
2978 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002979 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002980 "OMX_COLOR_Format24bitRGB888",
2981 "OMX_COLOR_Format24bitBGR888",
2982 "OMX_COLOR_Format24bitARGB1887",
2983 "OMX_COLOR_Format25bitARGB1888",
2984 "OMX_COLOR_Format32bitBGRA8888",
2985 "OMX_COLOR_Format32bitARGB8888",
2986 "OMX_COLOR_FormatYUV411Planar",
2987 "OMX_COLOR_FormatYUV411PackedPlanar",
2988 "OMX_COLOR_FormatYUV420Planar",
2989 "OMX_COLOR_FormatYUV420PackedPlanar",
2990 "OMX_COLOR_FormatYUV420SemiPlanar",
2991 "OMX_COLOR_FormatYUV422Planar",
2992 "OMX_COLOR_FormatYUV422PackedPlanar",
2993 "OMX_COLOR_FormatYUV422SemiPlanar",
2994 "OMX_COLOR_FormatYCbYCr",
2995 "OMX_COLOR_FormatYCrYCb",
2996 "OMX_COLOR_FormatCbYCrY",
2997 "OMX_COLOR_FormatCrYCbY",
2998 "OMX_COLOR_FormatYUV444Interleaved",
2999 "OMX_COLOR_FormatRawBayer8bit",
3000 "OMX_COLOR_FormatRawBayer10bit",
3001 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003002 "OMX_COLOR_FormatL2",
3003 "OMX_COLOR_FormatL4",
3004 "OMX_COLOR_FormatL8",
3005 "OMX_COLOR_FormatL16",
3006 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07003007 "OMX_COLOR_FormatL32",
3008 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
3009 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
3010 "OMX_COLOR_Format18BitBGR666",
3011 "OMX_COLOR_Format24BitARGB6666",
3012 "OMX_COLOR_Format24BitABGR6666",
3013 };
3014
3015 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3016
Andreas Huberbe06d262009-08-14 14:37:10 -07003017 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3018 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3019 } else if (type < 0 || (size_t)type >= numNames) {
3020 return "UNKNOWN";
3021 } else {
3022 return kNames[type];
3023 }
3024}
3025
3026static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3027 static const char *kNames[] = {
3028 "OMX_VIDEO_CodingUnused",
3029 "OMX_VIDEO_CodingAutoDetect",
3030 "OMX_VIDEO_CodingMPEG2",
3031 "OMX_VIDEO_CodingH263",
3032 "OMX_VIDEO_CodingMPEG4",
3033 "OMX_VIDEO_CodingWMV",
3034 "OMX_VIDEO_CodingRV",
3035 "OMX_VIDEO_CodingAVC",
3036 "OMX_VIDEO_CodingMJPEG",
3037 };
3038
3039 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3040
3041 if (type < 0 || (size_t)type >= numNames) {
3042 return "UNKNOWN";
3043 } else {
3044 return kNames[type];
3045 }
3046}
3047
3048static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3049 static const char *kNames[] = {
3050 "OMX_AUDIO_CodingUnused",
3051 "OMX_AUDIO_CodingAutoDetect",
3052 "OMX_AUDIO_CodingPCM",
3053 "OMX_AUDIO_CodingADPCM",
3054 "OMX_AUDIO_CodingAMR",
3055 "OMX_AUDIO_CodingGSMFR",
3056 "OMX_AUDIO_CodingGSMEFR",
3057 "OMX_AUDIO_CodingGSMHR",
3058 "OMX_AUDIO_CodingPDCFR",
3059 "OMX_AUDIO_CodingPDCEFR",
3060 "OMX_AUDIO_CodingPDCHR",
3061 "OMX_AUDIO_CodingTDMAFR",
3062 "OMX_AUDIO_CodingTDMAEFR",
3063 "OMX_AUDIO_CodingQCELP8",
3064 "OMX_AUDIO_CodingQCELP13",
3065 "OMX_AUDIO_CodingEVRC",
3066 "OMX_AUDIO_CodingSMV",
3067 "OMX_AUDIO_CodingG711",
3068 "OMX_AUDIO_CodingG723",
3069 "OMX_AUDIO_CodingG726",
3070 "OMX_AUDIO_CodingG729",
3071 "OMX_AUDIO_CodingAAC",
3072 "OMX_AUDIO_CodingMP3",
3073 "OMX_AUDIO_CodingSBC",
3074 "OMX_AUDIO_CodingVORBIS",
3075 "OMX_AUDIO_CodingWMA",
3076 "OMX_AUDIO_CodingRA",
3077 "OMX_AUDIO_CodingMIDI",
3078 };
3079
3080 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3081
3082 if (type < 0 || (size_t)type >= numNames) {
3083 return "UNKNOWN";
3084 } else {
3085 return kNames[type];
3086 }
3087}
3088
3089static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3090 static const char *kNames[] = {
3091 "OMX_AUDIO_PCMModeLinear",
3092 "OMX_AUDIO_PCMModeALaw",
3093 "OMX_AUDIO_PCMModeMULaw",
3094 };
3095
3096 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3097
3098 if (type < 0 || (size_t)type >= numNames) {
3099 return "UNKNOWN";
3100 } else {
3101 return kNames[type];
3102 }
3103}
3104
Andreas Huber7ae02c82009-09-09 16:29:47 -07003105static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3106 static const char *kNames[] = {
3107 "OMX_AUDIO_AMRBandModeUnused",
3108 "OMX_AUDIO_AMRBandModeNB0",
3109 "OMX_AUDIO_AMRBandModeNB1",
3110 "OMX_AUDIO_AMRBandModeNB2",
3111 "OMX_AUDIO_AMRBandModeNB3",
3112 "OMX_AUDIO_AMRBandModeNB4",
3113 "OMX_AUDIO_AMRBandModeNB5",
3114 "OMX_AUDIO_AMRBandModeNB6",
3115 "OMX_AUDIO_AMRBandModeNB7",
3116 "OMX_AUDIO_AMRBandModeWB0",
3117 "OMX_AUDIO_AMRBandModeWB1",
3118 "OMX_AUDIO_AMRBandModeWB2",
3119 "OMX_AUDIO_AMRBandModeWB3",
3120 "OMX_AUDIO_AMRBandModeWB4",
3121 "OMX_AUDIO_AMRBandModeWB5",
3122 "OMX_AUDIO_AMRBandModeWB6",
3123 "OMX_AUDIO_AMRBandModeWB7",
3124 "OMX_AUDIO_AMRBandModeWB8",
3125 };
3126
3127 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3128
3129 if (type < 0 || (size_t)type >= numNames) {
3130 return "UNKNOWN";
3131 } else {
3132 return kNames[type];
3133 }
3134}
3135
3136static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3137 static const char *kNames[] = {
3138 "OMX_AUDIO_AMRFrameFormatConformance",
3139 "OMX_AUDIO_AMRFrameFormatIF1",
3140 "OMX_AUDIO_AMRFrameFormatIF2",
3141 "OMX_AUDIO_AMRFrameFormatFSF",
3142 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3143 "OMX_AUDIO_AMRFrameFormatITU",
3144 };
3145
3146 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3147
3148 if (type < 0 || (size_t)type >= numNames) {
3149 return "UNKNOWN";
3150 } else {
3151 return kNames[type];
3152 }
3153}
Andreas Huberbe06d262009-08-14 14:37:10 -07003154
3155void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3156 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003157 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003158 def.nPortIndex = portIndex;
3159
Andreas Huber784202e2009-10-15 13:46:54 -07003160 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003161 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3162 CHECK_EQ(err, OK);
3163
3164 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3165
3166 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3167 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3168
3169 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3170 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3171 printf(" nBufferSize = %ld\n", def.nBufferSize);
3172
3173 switch (def.eDomain) {
3174 case OMX_PortDomainImage:
3175 {
3176 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3177
3178 printf("\n");
3179 printf(" // Image\n");
3180 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3181 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3182 printf(" nStride = %ld\n", imageDef->nStride);
3183
3184 printf(" eCompressionFormat = %s\n",
3185 imageCompressionFormatString(imageDef->eCompressionFormat));
3186
3187 printf(" eColorFormat = %s\n",
3188 colorFormatString(imageDef->eColorFormat));
3189
3190 break;
3191 }
3192
3193 case OMX_PortDomainVideo:
3194 {
3195 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3196
3197 printf("\n");
3198 printf(" // Video\n");
3199 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3200 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3201 printf(" nStride = %ld\n", videoDef->nStride);
3202
3203 printf(" eCompressionFormat = %s\n",
3204 videoCompressionFormatString(videoDef->eCompressionFormat));
3205
3206 printf(" eColorFormat = %s\n",
3207 colorFormatString(videoDef->eColorFormat));
3208
3209 break;
3210 }
3211
3212 case OMX_PortDomainAudio:
3213 {
3214 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3215
3216 printf("\n");
3217 printf(" // Audio\n");
3218 printf(" eEncoding = %s\n",
3219 audioCodingTypeString(audioDef->eEncoding));
3220
3221 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3222 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003223 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003224 params.nPortIndex = portIndex;
3225
Andreas Huber784202e2009-10-15 13:46:54 -07003226 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003227 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3228 CHECK_EQ(err, OK);
3229
3230 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3231 printf(" nChannels = %ld\n", params.nChannels);
3232 printf(" bInterleaved = %d\n", params.bInterleaved);
3233 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3234
3235 printf(" eNumData = %s\n",
3236 params.eNumData == OMX_NumericalDataSigned
3237 ? "signed" : "unsigned");
3238
3239 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003240 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3241 OMX_AUDIO_PARAM_AMRTYPE amr;
3242 InitOMXParams(&amr);
3243 amr.nPortIndex = portIndex;
3244
Andreas Huber784202e2009-10-15 13:46:54 -07003245 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003246 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3247 CHECK_EQ(err, OK);
3248
3249 printf(" nChannels = %ld\n", amr.nChannels);
3250 printf(" eAMRBandMode = %s\n",
3251 amrBandModeString(amr.eAMRBandMode));
3252 printf(" eAMRFrameFormat = %s\n",
3253 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003254 }
3255
3256 break;
3257 }
3258
3259 default:
3260 {
3261 printf(" // Unknown\n");
3262 break;
3263 }
3264 }
3265
3266 printf("}\n");
3267}
3268
3269void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3270 mOutputFormat = new MetaData;
3271 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003272 if (mIsEncoder) {
3273 int32_t timeScale;
3274 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3275 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3276 }
3277 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003278
3279 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003280 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003281 def.nPortIndex = kPortIndexOutput;
3282
Andreas Huber784202e2009-10-15 13:46:54 -07003283 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003284 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3285 CHECK_EQ(err, OK);
3286
3287 switch (def.eDomain) {
3288 case OMX_PortDomainImage:
3289 {
3290 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3291 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3292
Andreas Hubere6c40962009-09-10 14:13:30 -07003293 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003294 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3295 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3296 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3297 break;
3298 }
3299
3300 case OMX_PortDomainAudio:
3301 {
3302 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3303
Andreas Huberda050cf22009-09-02 14:01:43 -07003304 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3305 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003306 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003307 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003308
Andreas Huber784202e2009-10-15 13:46:54 -07003309 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003310 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3311 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003312
Andreas Huberda050cf22009-09-02 14:01:43 -07003313 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3314 CHECK_EQ(params.nBitPerSample, 16);
3315 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003316
Andreas Huberda050cf22009-09-02 14:01:43 -07003317 int32_t numChannels, sampleRate;
3318 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3319 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003320
Andreas Huberda050cf22009-09-02 14:01:43 -07003321 if ((OMX_U32)numChannels != params.nChannels) {
3322 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003323 "the input stream contains (contains %d channels, "
3324 "codec outputs %ld channels).",
3325 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003326 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003327
Andreas Hubere6c40962009-09-10 14:13:30 -07003328 mOutputFormat->setCString(
3329 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003330
3331 // Use the codec-advertised number of channels, as some
3332 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003333 // mono. If we know the codec lies about this information,
3334 // use the actual number of channels instead.
3335 mOutputFormat->setInt32(
3336 kKeyChannelCount,
3337 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3338 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003339
3340 // The codec-reported sampleRate is not reliable...
3341 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3342 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003343 OMX_AUDIO_PARAM_AMRTYPE amr;
3344 InitOMXParams(&amr);
3345 amr.nPortIndex = kPortIndexOutput;
3346
Andreas Huber784202e2009-10-15 13:46:54 -07003347 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003348 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3349 CHECK_EQ(err, OK);
3350
3351 CHECK_EQ(amr.nChannels, 1);
3352 mOutputFormat->setInt32(kKeyChannelCount, 1);
3353
3354 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3355 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003356 mOutputFormat->setCString(
3357 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003358 mOutputFormat->setInt32(kKeySampleRate, 8000);
3359 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3360 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003361 mOutputFormat->setCString(
3362 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003363 mOutputFormat->setInt32(kKeySampleRate, 16000);
3364 } else {
3365 CHECK(!"Unknown AMR band mode.");
3366 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003367 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003368 mOutputFormat->setCString(
3369 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003370 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003371 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3372 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003373 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003374 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3375 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003376 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003377 } else {
3378 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003379 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003380 break;
3381 }
3382
3383 case OMX_PortDomainVideo:
3384 {
3385 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3386
3387 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003388 mOutputFormat->setCString(
3389 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003390 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003391 mOutputFormat->setCString(
3392 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003393 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003394 mOutputFormat->setCString(
3395 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003396 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003397 mOutputFormat->setCString(
3398 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003399 } else {
3400 CHECK(!"Unknown compression format.");
3401 }
3402
3403 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3404 // This component appears to be lying to me.
3405 mOutputFormat->setInt32(
3406 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3407 mOutputFormat->setInt32(
3408 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3409 } else {
3410 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3411 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3412 }
3413
3414 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3415 break;
3416 }
3417
3418 default:
3419 {
3420 CHECK(!"should not be here, neither audio nor video.");
3421 break;
3422 }
3423 }
3424}
3425
Andreas Huber1f24b302010-06-10 11:12:39 -07003426status_t OMXCodec::pause() {
3427 Mutex::Autolock autoLock(mLock);
3428
3429 mPaused = true;
3430
3431 return OK;
3432}
3433
Andreas Hubere6c40962009-09-10 14:13:30 -07003434////////////////////////////////////////////////////////////////////////////////
3435
3436status_t QueryCodecs(
3437 const sp<IOMX> &omx,
3438 const char *mime, bool queryDecoders,
3439 Vector<CodecCapabilities> *results) {
3440 results->clear();
3441
3442 for (int index = 0;; ++index) {
3443 const char *componentName;
3444
3445 if (!queryDecoders) {
3446 componentName = GetCodec(
3447 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3448 mime, index);
3449 } else {
3450 componentName = GetCodec(
3451 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3452 mime, index);
3453 }
3454
3455 if (!componentName) {
3456 return OK;
3457 }
3458
Andreas Huber1a189a82010-03-24 13:49:20 -07003459 if (strncmp(componentName, "OMX.", 4)) {
3460 // Not an OpenMax component but a software codec.
3461
3462 results->push();
3463 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3464 caps->mComponentName = componentName;
3465
3466 continue;
3467 }
3468
Andreas Huber784202e2009-10-15 13:46:54 -07003469 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003470 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003471 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003472
3473 if (err != OK) {
3474 continue;
3475 }
3476
James Dong722d5912010-04-13 10:56:59 -07003477 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003478
3479 results->push();
3480 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3481 caps->mComponentName = componentName;
3482
3483 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3484 InitOMXParams(&param);
3485
3486 param.nPortIndex = queryDecoders ? 0 : 1;
3487
3488 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003489 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003490 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3491 &param, sizeof(param));
3492
3493 if (err != OK) {
3494 break;
3495 }
3496
3497 CodecProfileLevel profileLevel;
3498 profileLevel.mProfile = param.eProfile;
3499 profileLevel.mLevel = param.eLevel;
3500
3501 caps->mProfileLevels.push(profileLevel);
3502 }
3503
Andreas Huber784202e2009-10-15 13:46:54 -07003504 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003505 }
3506}
3507
Andreas Huberbe06d262009-08-14 14:37:10 -07003508} // namespace android