blob: 7762bf4206917fca985fb10367efebc56950fc49 [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 Huber1f24b302010-06-10 11:12:39 -07001275 mLeftOverBuffer(NULL),
1276 mPaused(false) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001277 mPortStatus[kPortIndexInput] = ENABLED;
1278 mPortStatus[kPortIndexOutput] = ENABLED;
1279
Andreas Huber4c483422009-09-02 16:05:36 -07001280 setComponentRole();
1281}
1282
Andreas Hubere6c40962009-09-10 14:13:30 -07001283// static
1284void OMXCodec::setComponentRole(
1285 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1286 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001287 struct MimeToRole {
1288 const char *mime;
1289 const char *decoderRole;
1290 const char *encoderRole;
1291 };
1292
1293 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001294 { MEDIA_MIMETYPE_AUDIO_MPEG,
1295 "audio_decoder.mp3", "audio_encoder.mp3" },
1296 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1297 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1298 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1299 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1300 { MEDIA_MIMETYPE_AUDIO_AAC,
1301 "audio_decoder.aac", "audio_encoder.aac" },
1302 { MEDIA_MIMETYPE_VIDEO_AVC,
1303 "video_decoder.avc", "video_encoder.avc" },
1304 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1305 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1306 { MEDIA_MIMETYPE_VIDEO_H263,
1307 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001308 };
1309
1310 static const size_t kNumMimeToRole =
1311 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1312
1313 size_t i;
1314 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001315 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001316 break;
1317 }
1318 }
1319
1320 if (i == kNumMimeToRole) {
1321 return;
1322 }
1323
1324 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001325 isEncoder ? kMimeToRole[i].encoderRole
1326 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001327
1328 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001329 OMX_PARAM_COMPONENTROLETYPE roleParams;
1330 InitOMXParams(&roleParams);
1331
1332 strncpy((char *)roleParams.cRole,
1333 role, OMX_MAX_STRINGNAME_SIZE - 1);
1334
1335 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1336
Andreas Huber784202e2009-10-15 13:46:54 -07001337 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001338 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001339 &roleParams, sizeof(roleParams));
1340
1341 if (err != OK) {
1342 LOGW("Failed to set standard component role '%s'.", role);
1343 }
1344 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001345}
1346
Andreas Hubere6c40962009-09-10 14:13:30 -07001347void OMXCodec::setComponentRole() {
1348 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1349}
1350
Andreas Huberbe06d262009-08-14 14:37:10 -07001351OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001352 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001353
Andreas Huber784202e2009-10-15 13:46:54 -07001354 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001355 CHECK_EQ(err, OK);
1356
1357 mNode = NULL;
1358 setState(DEAD);
1359
1360 clearCodecSpecificData();
1361
1362 free(mComponentName);
1363 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001364
Andreas Huberbe06d262009-08-14 14:37:10 -07001365 free(mMIME);
1366 mMIME = NULL;
1367}
1368
1369status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001370 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001371
1372 CHECK_EQ(mState, LOADED);
1373
1374 status_t err;
1375 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001376 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001377 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001378 setState(LOADED_TO_IDLE);
1379 }
1380
1381 err = allocateBuffers();
1382 CHECK_EQ(err, OK);
1383
1384 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001385 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001386 CHECK_EQ(err, OK);
1387
1388 setState(LOADED_TO_IDLE);
1389 }
1390
1391 while (mState != EXECUTING && mState != ERROR) {
1392 mAsyncCompletion.wait(mLock);
1393 }
1394
1395 return mState == ERROR ? UNKNOWN_ERROR : OK;
1396}
1397
1398// static
1399bool OMXCodec::isIntermediateState(State state) {
1400 return state == LOADED_TO_IDLE
1401 || state == IDLE_TO_EXECUTING
1402 || state == EXECUTING_TO_IDLE
1403 || state == IDLE_TO_LOADED
1404 || state == RECONFIGURING;
1405}
1406
1407status_t OMXCodec::allocateBuffers() {
1408 status_t err = allocateBuffersOnPort(kPortIndexInput);
1409
1410 if (err != OK) {
1411 return err;
1412 }
1413
1414 return allocateBuffersOnPort(kPortIndexOutput);
1415}
1416
1417status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1418 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001419 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001420 def.nPortIndex = portIndex;
1421
Andreas Huber784202e2009-10-15 13:46:54 -07001422 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001423 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1424
1425 if (err != OK) {
1426 return err;
1427 }
1428
Andreas Huber5c0a9132009-08-20 11:16:40 -07001429 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001430 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001431
Andreas Huberbe06d262009-08-14 14:37:10 -07001432 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001433 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001434 CHECK(mem.get() != NULL);
1435
Andreas Huberc712b9f2010-01-20 15:05:46 -08001436 BufferInfo info;
1437 info.mData = NULL;
1438 info.mSize = def.nBufferSize;
1439
Andreas Huberbe06d262009-08-14 14:37:10 -07001440 IOMX::buffer_id buffer;
1441 if (portIndex == kPortIndexInput
1442 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001443 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001444 mem.clear();
1445
Andreas Huberf1fe0642010-01-15 15:28:19 -08001446 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001447 mNode, portIndex, def.nBufferSize, &buffer,
1448 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001449 } else {
1450 err = mOMX->allocateBufferWithBackup(
1451 mNode, portIndex, mem, &buffer);
1452 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001453 } else if (portIndex == kPortIndexOutput
1454 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001455 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001456 mem.clear();
1457
Andreas Huberf1fe0642010-01-15 15:28:19 -08001458 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001459 mNode, portIndex, def.nBufferSize, &buffer,
1460 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001461 } else {
1462 err = mOMX->allocateBufferWithBackup(
1463 mNode, portIndex, mem, &buffer);
1464 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001465 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001466 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001467 }
1468
1469 if (err != OK) {
1470 LOGE("allocate_buffer_with_backup failed");
1471 return err;
1472 }
1473
Andreas Huberc712b9f2010-01-20 15:05:46 -08001474 if (mem != NULL) {
1475 info.mData = mem->pointer();
1476 }
1477
Andreas Huberbe06d262009-08-14 14:37:10 -07001478 info.mBuffer = buffer;
1479 info.mOwnedByComponent = false;
1480 info.mMem = mem;
1481 info.mMediaBuffer = NULL;
1482
1483 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001484 if (!(mOMXLivesLocally
1485 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1486 && (mQuirks & kDefersOutputBufferAllocation))) {
1487 // If the node does not fill in the buffer ptr at this time,
1488 // we will defer creating the MediaBuffer until receiving
1489 // the first FILL_BUFFER_DONE notification instead.
1490 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1491 info.mMediaBuffer->setObserver(this);
1492 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001493 }
1494
1495 mPortBuffers[portIndex].push(info);
1496
Andreas Huber4c483422009-09-02 16:05:36 -07001497 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001498 portIndex == kPortIndexInput ? "input" : "output");
1499 }
1500
Andreas Huber2ea14e22009-12-16 09:30:55 -08001501 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001502
1503 return OK;
1504}
1505
1506void OMXCodec::on_message(const omx_message &msg) {
1507 Mutex::Autolock autoLock(mLock);
1508
1509 switch (msg.type) {
1510 case omx_message::EVENT:
1511 {
1512 onEvent(
1513 msg.u.event_data.event, msg.u.event_data.data1,
1514 msg.u.event_data.data2);
1515
1516 break;
1517 }
1518
1519 case omx_message::EMPTY_BUFFER_DONE:
1520 {
1521 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1522
Andreas Huber4c483422009-09-02 16:05:36 -07001523 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001524
1525 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1526 size_t i = 0;
1527 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1528 ++i;
1529 }
1530
1531 CHECK(i < buffers->size());
1532 if (!(*buffers)[i].mOwnedByComponent) {
1533 LOGW("We already own input buffer %p, yet received "
1534 "an EMPTY_BUFFER_DONE.", buffer);
1535 }
1536
1537 buffers->editItemAt(i).mOwnedByComponent = false;
1538
1539 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001540 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001541
1542 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001543 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001544 CHECK_EQ(err, OK);
1545
1546 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001547 } else if (mState != ERROR
1548 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001549 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1550 drainInputBuffer(&buffers->editItemAt(i));
1551 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001552 break;
1553 }
1554
1555 case omx_message::FILL_BUFFER_DONE:
1556 {
1557 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1558 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1559
Andreas Huber2ea14e22009-12-16 09:30:55 -08001560 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001561 buffer,
1562 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001563 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001564 msg.u.extended_buffer_data.timestamp,
1565 msg.u.extended_buffer_data.timestamp / 1E6);
1566
1567 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1568 size_t i = 0;
1569 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1570 ++i;
1571 }
1572
1573 CHECK(i < buffers->size());
1574 BufferInfo *info = &buffers->editItemAt(i);
1575
1576 if (!info->mOwnedByComponent) {
1577 LOGW("We already own output buffer %p, yet received "
1578 "a FILL_BUFFER_DONE.", buffer);
1579 }
1580
1581 info->mOwnedByComponent = false;
1582
1583 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001584 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001585
1586 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001587 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001588 CHECK_EQ(err, OK);
1589
1590 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001591#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001592 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1593 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001594 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001595 mNoMoreOutputData = true;
1596 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001597#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001598 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1599 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001600
Andreas Huber52733b82010-01-25 10:41:35 -08001601 if (info->mMediaBuffer == NULL) {
1602 CHECK(mOMXLivesLocally);
1603 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1604 CHECK(mQuirks & kDefersOutputBufferAllocation);
1605
1606 // The qcom video decoders on Nexus don't actually allocate
1607 // output buffer memory on a call to OMX_AllocateBuffer
1608 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1609 // structure is only filled in later.
1610
1611 info->mMediaBuffer = new MediaBuffer(
1612 msg.u.extended_buffer_data.data_ptr,
1613 info->mSize);
1614 info->mMediaBuffer->setObserver(this);
1615 }
1616
Andreas Huberbe06d262009-08-14 14:37:10 -07001617 MediaBuffer *buffer = info->mMediaBuffer;
1618
1619 buffer->set_range(
1620 msg.u.extended_buffer_data.range_offset,
1621 msg.u.extended_buffer_data.range_length);
1622
1623 buffer->meta_data()->clear();
1624
Andreas Huberfa8de752009-10-08 10:07:49 -07001625 buffer->meta_data()->setInt64(
1626 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001627
1628 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1629 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1630 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001631 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1632 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1633 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001634
1635 buffer->meta_data()->setPointer(
1636 kKeyPlatformPrivate,
1637 msg.u.extended_buffer_data.platform_private);
1638
1639 buffer->meta_data()->setPointer(
1640 kKeyBufferID,
1641 msg.u.extended_buffer_data.buffer);
1642
1643 mFilledBuffers.push_back(i);
1644 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001645
1646 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1647 CODEC_LOGV("No more output data.");
1648 mNoMoreOutputData = true;
1649 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001650 }
1651
1652 break;
1653 }
1654
1655 default:
1656 {
1657 CHECK(!"should not be here.");
1658 break;
1659 }
1660 }
1661}
1662
1663void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1664 switch (event) {
1665 case OMX_EventCmdComplete:
1666 {
1667 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1668 break;
1669 }
1670
1671 case OMX_EventError:
1672 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001673 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001674
1675 setState(ERROR);
1676 break;
1677 }
1678
1679 case OMX_EventPortSettingsChanged:
1680 {
1681 onPortSettingsChanged(data1);
1682 break;
1683 }
1684
Andreas Huber2ea14e22009-12-16 09:30:55 -08001685#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001686 case OMX_EventBufferFlag:
1687 {
Andreas Huber4c483422009-09-02 16:05:36 -07001688 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001689
1690 if (data1 == kPortIndexOutput) {
1691 mNoMoreOutputData = true;
1692 }
1693 break;
1694 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001695#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001696
1697 default:
1698 {
Andreas Huber4c483422009-09-02 16:05:36 -07001699 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001700 break;
1701 }
1702 }
1703}
1704
Andreas Huberb1678602009-10-19 13:06:40 -07001705// Has the format changed in any way that the client would have to be aware of?
1706static bool formatHasNotablyChanged(
1707 const sp<MetaData> &from, const sp<MetaData> &to) {
1708 if (from.get() == NULL && to.get() == NULL) {
1709 return false;
1710 }
1711
Andreas Huberf68c1682009-10-21 14:01:30 -07001712 if ((from.get() == NULL && to.get() != NULL)
1713 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001714 return true;
1715 }
1716
1717 const char *mime_from, *mime_to;
1718 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1719 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1720
1721 if (strcasecmp(mime_from, mime_to)) {
1722 return true;
1723 }
1724
1725 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1726 int32_t colorFormat_from, colorFormat_to;
1727 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1728 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1729
1730 if (colorFormat_from != colorFormat_to) {
1731 return true;
1732 }
1733
1734 int32_t width_from, width_to;
1735 CHECK(from->findInt32(kKeyWidth, &width_from));
1736 CHECK(to->findInt32(kKeyWidth, &width_to));
1737
1738 if (width_from != width_to) {
1739 return true;
1740 }
1741
1742 int32_t height_from, height_to;
1743 CHECK(from->findInt32(kKeyHeight, &height_from));
1744 CHECK(to->findInt32(kKeyHeight, &height_to));
1745
1746 if (height_from != height_to) {
1747 return true;
1748 }
1749 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1750 int32_t numChannels_from, numChannels_to;
1751 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1752 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1753
1754 if (numChannels_from != numChannels_to) {
1755 return true;
1756 }
1757
1758 int32_t sampleRate_from, sampleRate_to;
1759 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1760 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1761
1762 if (sampleRate_from != sampleRate_to) {
1763 return true;
1764 }
1765 }
1766
1767 return false;
1768}
1769
Andreas Huberbe06d262009-08-14 14:37:10 -07001770void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1771 switch (cmd) {
1772 case OMX_CommandStateSet:
1773 {
1774 onStateChange((OMX_STATETYPE)data);
1775 break;
1776 }
1777
1778 case OMX_CommandPortDisable:
1779 {
1780 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001781 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001782
1783 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1784 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1785 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1786
1787 mPortStatus[portIndex] = DISABLED;
1788
1789 if (mState == RECONFIGURING) {
1790 CHECK_EQ(portIndex, kPortIndexOutput);
1791
Andreas Huberb1678602009-10-19 13:06:40 -07001792 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001793 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001794
1795 // Don't notify clients if the output port settings change
1796 // wasn't of importance to them, i.e. it may be that just the
1797 // number of buffers has changed and nothing else.
1798 mOutputPortSettingsHaveChanged =
1799 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001800
Andreas Huberbe06d262009-08-14 14:37:10 -07001801 enablePortAsync(portIndex);
1802
1803 status_t err = allocateBuffersOnPort(portIndex);
1804 CHECK_EQ(err, OK);
1805 }
1806 break;
1807 }
1808
1809 case OMX_CommandPortEnable:
1810 {
1811 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001812 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001813
1814 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1815 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1816
1817 mPortStatus[portIndex] = ENABLED;
1818
1819 if (mState == RECONFIGURING) {
1820 CHECK_EQ(portIndex, kPortIndexOutput);
1821
1822 setState(EXECUTING);
1823
1824 fillOutputBuffers();
1825 }
1826 break;
1827 }
1828
1829 case OMX_CommandFlush:
1830 {
1831 OMX_U32 portIndex = data;
1832
Andreas Huber4c483422009-09-02 16:05:36 -07001833 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001834
1835 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1836 mPortStatus[portIndex] = ENABLED;
1837
1838 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1839 mPortBuffers[portIndex].size());
1840
1841 if (mState == RECONFIGURING) {
1842 CHECK_EQ(portIndex, kPortIndexOutput);
1843
1844 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001845 } else if (mState == EXECUTING_TO_IDLE) {
1846 if (mPortStatus[kPortIndexInput] == ENABLED
1847 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001848 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001849 "transition from EXECUTING to IDLE.");
1850
1851 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1852 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1853
1854 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001855 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001856 CHECK_EQ(err, OK);
1857 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001858 } else {
1859 // We're flushing both ports in preparation for seeking.
1860
1861 if (mPortStatus[kPortIndexInput] == ENABLED
1862 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001863 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001864 " seek-time.");
1865
Andreas Huber1f24b302010-06-10 11:12:39 -07001866 // We implicitly resume pulling on our upstream source.
1867 mPaused = false;
1868
Andreas Huberbe06d262009-08-14 14:37:10 -07001869 drainInputBuffers();
1870 fillOutputBuffers();
1871 }
1872 }
1873
1874 break;
1875 }
1876
1877 default:
1878 {
Andreas Huber4c483422009-09-02 16:05:36 -07001879 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001880 break;
1881 }
1882 }
1883}
1884
1885void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001886 CODEC_LOGV("onStateChange %d", newState);
1887
Andreas Huberbe06d262009-08-14 14:37:10 -07001888 switch (newState) {
1889 case OMX_StateIdle:
1890 {
Andreas Huber4c483422009-09-02 16:05:36 -07001891 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001892 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001893 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001894 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1895
1896 CHECK_EQ(err, OK);
1897
1898 setState(IDLE_TO_EXECUTING);
1899 } else {
1900 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1901
1902 CHECK_EQ(
1903 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1904 mPortBuffers[kPortIndexInput].size());
1905
1906 CHECK_EQ(
1907 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1908 mPortBuffers[kPortIndexOutput].size());
1909
Andreas Huber784202e2009-10-15 13:46:54 -07001910 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001911 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1912
1913 CHECK_EQ(err, OK);
1914
1915 err = freeBuffersOnPort(kPortIndexInput);
1916 CHECK_EQ(err, OK);
1917
1918 err = freeBuffersOnPort(kPortIndexOutput);
1919 CHECK_EQ(err, OK);
1920
1921 mPortStatus[kPortIndexInput] = ENABLED;
1922 mPortStatus[kPortIndexOutput] = ENABLED;
1923
1924 setState(IDLE_TO_LOADED);
1925 }
1926 break;
1927 }
1928
1929 case OMX_StateExecuting:
1930 {
1931 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1932
Andreas Huber4c483422009-09-02 16:05:36 -07001933 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001934
1935 setState(EXECUTING);
1936
Andreas Huber42978e52009-08-27 10:08:39 -07001937 // Buffers will be submitted to the component in the first
1938 // call to OMXCodec::read as mInitialBufferSubmit is true at
1939 // this point. This ensures that this on_message call returns,
1940 // releases the lock and ::init can notice the state change and
1941 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001942 break;
1943 }
1944
1945 case OMX_StateLoaded:
1946 {
1947 CHECK_EQ(mState, IDLE_TO_LOADED);
1948
Andreas Huber4c483422009-09-02 16:05:36 -07001949 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001950
1951 setState(LOADED);
1952 break;
1953 }
1954
Andreas Huberc712b9f2010-01-20 15:05:46 -08001955 case OMX_StateInvalid:
1956 {
1957 setState(ERROR);
1958 break;
1959 }
1960
Andreas Huberbe06d262009-08-14 14:37:10 -07001961 default:
1962 {
1963 CHECK(!"should not be here.");
1964 break;
1965 }
1966 }
1967}
1968
1969// static
1970size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1971 size_t n = 0;
1972 for (size_t i = 0; i < buffers.size(); ++i) {
1973 if (!buffers[i].mOwnedByComponent) {
1974 ++n;
1975 }
1976 }
1977
1978 return n;
1979}
1980
1981status_t OMXCodec::freeBuffersOnPort(
1982 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1983 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1984
1985 status_t stickyErr = OK;
1986
1987 for (size_t i = buffers->size(); i-- > 0;) {
1988 BufferInfo *info = &buffers->editItemAt(i);
1989
1990 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1991 continue;
1992 }
1993
1994 CHECK_EQ(info->mOwnedByComponent, false);
1995
Andreas Huber92022852009-09-14 15:24:14 -07001996 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1997
Andreas Huberbe06d262009-08-14 14:37:10 -07001998 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001999 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07002000
2001 if (err != OK) {
2002 stickyErr = err;
2003 }
2004
2005 if (info->mMediaBuffer != NULL) {
2006 info->mMediaBuffer->setObserver(NULL);
2007
2008 // Make sure nobody but us owns this buffer at this point.
2009 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2010
2011 info->mMediaBuffer->release();
2012 }
2013
2014 buffers->removeAt(i);
2015 }
2016
2017 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2018
2019 return stickyErr;
2020}
2021
2022void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002023 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002024
2025 CHECK_EQ(mState, EXECUTING);
2026 CHECK_EQ(portIndex, kPortIndexOutput);
2027 setState(RECONFIGURING);
2028
2029 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002030 if (!flushPortAsync(portIndex)) {
2031 onCmdComplete(OMX_CommandFlush, portIndex);
2032 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002033 } else {
2034 disablePortAsync(portIndex);
2035 }
2036}
2037
Andreas Huber404cc412009-08-25 14:26:05 -07002038bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002039 CHECK(mState == EXECUTING || mState == RECONFIGURING
2040 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002041
Andreas Huber4c483422009-09-02 16:05:36 -07002042 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002043 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2044 mPortBuffers[portIndex].size());
2045
Andreas Huberbe06d262009-08-14 14:37:10 -07002046 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2047 mPortStatus[portIndex] = SHUTTING_DOWN;
2048
Andreas Huber404cc412009-08-25 14:26:05 -07002049 if ((mQuirks & kRequiresFlushCompleteEmulation)
2050 && countBuffersWeOwn(mPortBuffers[portIndex])
2051 == mPortBuffers[portIndex].size()) {
2052 // No flush is necessary and this component fails to send a
2053 // flush-complete event in this case.
2054
2055 return false;
2056 }
2057
Andreas Huberbe06d262009-08-14 14:37:10 -07002058 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002059 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002060 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002061
2062 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002063}
2064
2065void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2066 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2067
2068 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2069 mPortStatus[portIndex] = DISABLING;
2070
2071 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002072 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002073 CHECK_EQ(err, OK);
2074
2075 freeBuffersOnPort(portIndex, true);
2076}
2077
2078void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2079 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2080
2081 CHECK_EQ(mPortStatus[portIndex], DISABLED);
2082 mPortStatus[portIndex] = ENABLING;
2083
2084 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002085 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002086 CHECK_EQ(err, OK);
2087}
2088
2089void OMXCodec::fillOutputBuffers() {
2090 CHECK_EQ(mState, EXECUTING);
2091
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002092 // This is a workaround for some decoders not properly reporting
2093 // end-of-output-stream. If we own all input buffers and also own
2094 // all output buffers and we already signalled end-of-input-stream,
2095 // the end-of-output-stream is implied.
2096 if (mSignalledEOS
2097 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2098 == mPortBuffers[kPortIndexInput].size()
2099 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2100 == mPortBuffers[kPortIndexOutput].size()) {
2101 mNoMoreOutputData = true;
2102 mBufferFilled.signal();
2103
2104 return;
2105 }
2106
Andreas Huberbe06d262009-08-14 14:37:10 -07002107 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2108 for (size_t i = 0; i < buffers->size(); ++i) {
2109 fillOutputBuffer(&buffers->editItemAt(i));
2110 }
2111}
2112
2113void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002114 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002115
2116 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2117 for (size_t i = 0; i < buffers->size(); ++i) {
2118 drainInputBuffer(&buffers->editItemAt(i));
2119 }
2120}
2121
2122void OMXCodec::drainInputBuffer(BufferInfo *info) {
2123 CHECK_EQ(info->mOwnedByComponent, false);
2124
2125 if (mSignalledEOS) {
2126 return;
2127 }
2128
2129 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2130 const CodecSpecificData *specific =
2131 mCodecSpecificData[mCodecSpecificDataIndex];
2132
2133 size_t size = specific->mSize;
2134
Andreas Hubere6c40962009-09-10 14:13:30 -07002135 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002136 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002137 static const uint8_t kNALStartCode[4] =
2138 { 0x00, 0x00, 0x00, 0x01 };
2139
Andreas Huberc712b9f2010-01-20 15:05:46 -08002140 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002141
2142 size += 4;
2143
Andreas Huberc712b9f2010-01-20 15:05:46 -08002144 memcpy(info->mData, kNALStartCode, 4);
2145 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002146 specific->mData, specific->mSize);
2147 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002148 CHECK(info->mSize >= specific->mSize);
2149 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002150 }
2151
Andreas Huber2ea14e22009-12-16 09:30:55 -08002152 mNoMoreOutputData = false;
2153
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002154 CODEC_LOGV("calling emptyBuffer with codec specific data");
2155
Andreas Huber784202e2009-10-15 13:46:54 -07002156 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002157 mNode, info->mBuffer, 0, size,
2158 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2159 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002160 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002161
2162 info->mOwnedByComponent = true;
2163
2164 ++mCodecSpecificDataIndex;
2165 return;
2166 }
2167
Andreas Huber1f24b302010-06-10 11:12:39 -07002168 if (mPaused) {
2169 return;
2170 }
2171
Andreas Huberbe06d262009-08-14 14:37:10 -07002172 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002173
Andreas Hubera4357ad2010-04-02 12:49:54 -07002174 bool signalEOS = false;
2175 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002176
Andreas Hubera4357ad2010-04-02 12:49:54 -07002177 size_t offset = 0;
2178 int32_t n = 0;
2179 for (;;) {
2180 MediaBuffer *srcBuffer;
2181 if (mSeekTimeUs >= 0) {
2182 if (mLeftOverBuffer) {
2183 mLeftOverBuffer->release();
2184 mLeftOverBuffer = NULL;
2185 }
2186
2187 MediaSource::ReadOptions options;
2188 options.setSeekTo(mSeekTimeUs);
2189
2190 mSeekTimeUs = -1;
2191 mBufferFilled.signal();
2192
2193 err = mSource->read(&srcBuffer, &options);
2194 } else if (mLeftOverBuffer) {
2195 srcBuffer = mLeftOverBuffer;
2196 mLeftOverBuffer = NULL;
2197
2198 err = OK;
2199 } else {
2200 err = mSource->read(&srcBuffer);
2201 }
2202
2203 if (err != OK) {
2204 signalEOS = true;
2205 mFinalStatus = err;
2206 mSignalledEOS = true;
2207 break;
2208 }
2209
2210 size_t remainingBytes = info->mSize - offset;
2211
2212 if (srcBuffer->range_length() > remainingBytes) {
2213 if (offset == 0) {
2214 CODEC_LOGE(
2215 "Codec's input buffers are too small to accomodate "
2216 "buffer read from source (info->mSize = %d, srcLength = %d)",
2217 info->mSize, srcBuffer->range_length());
2218
2219 srcBuffer->release();
2220 srcBuffer = NULL;
2221
2222 setState(ERROR);
2223 return;
2224 }
2225
2226 mLeftOverBuffer = srcBuffer;
2227 break;
2228 }
2229
James Dong4f501f02010-06-07 14:41:41 -07002230 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2231 CHECK(mOMXLivesLocally && offset == 0);
2232 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2233 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2234 } else {
2235 memcpy((uint8_t *)info->mData + offset,
2236 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2237 srcBuffer->range_length());
2238 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002239
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002240 int64_t lastBufferTimeUs;
2241 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
2242 CHECK(timestampUs >= 0);
2243
Andreas Hubera4357ad2010-04-02 12:49:54 -07002244 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002245 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002246 }
2247
2248 offset += srcBuffer->range_length();
2249
2250 srcBuffer->release();
2251 srcBuffer = NULL;
2252
2253 ++n;
2254
2255 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2256 break;
2257 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002258
2259 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2260
2261 if (coalescedDurationUs > 250000ll) {
2262 // Don't coalesce more than 250ms worth of encoded data at once.
2263 break;
2264 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002265 }
2266
2267 if (n > 1) {
2268 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002269 }
2270
2271 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002272
Andreas Hubera4357ad2010-04-02 12:49:54 -07002273 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002274 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002275 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002276 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002277 }
2278
Andreas Hubera4357ad2010-04-02 12:49:54 -07002279 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2280 "timestamp %lld us (%.2f secs)",
2281 info->mBuffer, offset,
2282 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002283
Andreas Huber784202e2009-10-15 13:46:54 -07002284 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002285 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002286 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002287
2288 if (err != OK) {
2289 setState(ERROR);
2290 return;
2291 }
2292
2293 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002294
2295 // This component does not ever signal the EOS flag on output buffers,
2296 // Thanks for nothing.
2297 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2298 mNoMoreOutputData = true;
2299 mBufferFilled.signal();
2300 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002301}
2302
2303void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2304 CHECK_EQ(info->mOwnedByComponent, false);
2305
Andreas Huber404cc412009-08-25 14:26:05 -07002306 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002307 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002308 "calling fillOutputBuffer");
2309 return;
2310 }
2311
Andreas Huber4c483422009-09-02 16:05:36 -07002312 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002313 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002314
2315 if (err != OK) {
2316 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2317
2318 setState(ERROR);
2319 return;
2320 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002321
2322 info->mOwnedByComponent = true;
2323}
2324
2325void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2326 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2327 for (size_t i = 0; i < buffers->size(); ++i) {
2328 if ((*buffers)[i].mBuffer == buffer) {
2329 drainInputBuffer(&buffers->editItemAt(i));
2330 return;
2331 }
2332 }
2333
2334 CHECK(!"should not be here.");
2335}
2336
2337void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2338 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2339 for (size_t i = 0; i < buffers->size(); ++i) {
2340 if ((*buffers)[i].mBuffer == buffer) {
2341 fillOutputBuffer(&buffers->editItemAt(i));
2342 return;
2343 }
2344 }
2345
2346 CHECK(!"should not be here.");
2347}
2348
2349void OMXCodec::setState(State newState) {
2350 mState = newState;
2351 mAsyncCompletion.signal();
2352
2353 // This may cause some spurious wakeups but is necessary to
2354 // unblock the reader if we enter ERROR state.
2355 mBufferFilled.signal();
2356}
2357
Andreas Huberda050cf22009-09-02 14:01:43 -07002358void OMXCodec::setRawAudioFormat(
2359 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002360
2361 // port definition
2362 OMX_PARAM_PORTDEFINITIONTYPE def;
2363 InitOMXParams(&def);
2364 def.nPortIndex = portIndex;
2365 status_t err = mOMX->getParameter(
2366 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2367 CHECK_EQ(err, OK);
2368 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2369 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2370 &def, sizeof(def)), OK);
2371
2372 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002373 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002374 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002375 pcmParams.nPortIndex = portIndex;
2376
James Dongabed93a2010-04-22 17:27:04 -07002377 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002378 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2379
2380 CHECK_EQ(err, OK);
2381
2382 pcmParams.nChannels = numChannels;
2383 pcmParams.eNumData = OMX_NumericalDataSigned;
2384 pcmParams.bInterleaved = OMX_TRUE;
2385 pcmParams.nBitPerSample = 16;
2386 pcmParams.nSamplingRate = sampleRate;
2387 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2388
2389 if (numChannels == 1) {
2390 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2391 } else {
2392 CHECK_EQ(numChannels, 2);
2393
2394 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2395 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2396 }
2397
Andreas Huber784202e2009-10-15 13:46:54 -07002398 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002399 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2400
2401 CHECK_EQ(err, OK);
2402}
2403
James Dong17299ab2010-05-14 15:45:22 -07002404static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2405 if (isAMRWB) {
2406 if (bps <= 6600) {
2407 return OMX_AUDIO_AMRBandModeWB0;
2408 } else if (bps <= 8850) {
2409 return OMX_AUDIO_AMRBandModeWB1;
2410 } else if (bps <= 12650) {
2411 return OMX_AUDIO_AMRBandModeWB2;
2412 } else if (bps <= 14250) {
2413 return OMX_AUDIO_AMRBandModeWB3;
2414 } else if (bps <= 15850) {
2415 return OMX_AUDIO_AMRBandModeWB4;
2416 } else if (bps <= 18250) {
2417 return OMX_AUDIO_AMRBandModeWB5;
2418 } else if (bps <= 19850) {
2419 return OMX_AUDIO_AMRBandModeWB6;
2420 } else if (bps <= 23050) {
2421 return OMX_AUDIO_AMRBandModeWB7;
2422 }
2423
2424 // 23850 bps
2425 return OMX_AUDIO_AMRBandModeWB8;
2426 } else { // AMRNB
2427 if (bps <= 4750) {
2428 return OMX_AUDIO_AMRBandModeNB0;
2429 } else if (bps <= 5150) {
2430 return OMX_AUDIO_AMRBandModeNB1;
2431 } else if (bps <= 5900) {
2432 return OMX_AUDIO_AMRBandModeNB2;
2433 } else if (bps <= 6700) {
2434 return OMX_AUDIO_AMRBandModeNB3;
2435 } else if (bps <= 7400) {
2436 return OMX_AUDIO_AMRBandModeNB4;
2437 } else if (bps <= 7950) {
2438 return OMX_AUDIO_AMRBandModeNB5;
2439 } else if (bps <= 10200) {
2440 return OMX_AUDIO_AMRBandModeNB6;
2441 }
2442
2443 // 12200 bps
2444 return OMX_AUDIO_AMRBandModeNB7;
2445 }
2446}
2447
2448void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002449 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002450
Andreas Huber8768f2c2009-12-01 15:26:54 -08002451 OMX_AUDIO_PARAM_AMRTYPE def;
2452 InitOMXParams(&def);
2453 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002454
Andreas Huber8768f2c2009-12-01 15:26:54 -08002455 status_t err =
2456 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002457
Andreas Huber8768f2c2009-12-01 15:26:54 -08002458 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002459
Andreas Huber8768f2c2009-12-01 15:26:54 -08002460 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002461
James Dong17299ab2010-05-14 15:45:22 -07002462 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002463 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2464 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002465
2466 ////////////////////////
2467
2468 if (mIsEncoder) {
2469 sp<MetaData> format = mSource->getFormat();
2470 int32_t sampleRate;
2471 int32_t numChannels;
2472 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2473 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2474
2475 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2476 }
2477}
2478
James Dong17299ab2010-05-14 15:45:22 -07002479void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002480 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002481 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002482 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002483 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002484
2485 //////////////// output port ////////////////////
2486 // format
2487 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2488 format.nPortIndex = kPortIndexOutput;
2489 format.nIndex = 0;
2490 status_t err = OMX_ErrorNone;
2491 while (OMX_ErrorNone == err) {
2492 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2493 &format, sizeof(format)), OK);
2494 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2495 break;
2496 }
2497 format.nIndex++;
2498 }
2499 CHECK_EQ(OK, err);
2500 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2501 &format, sizeof(format)), OK);
2502
2503 // port definition
2504 OMX_PARAM_PORTDEFINITIONTYPE def;
2505 InitOMXParams(&def);
2506 def.nPortIndex = kPortIndexOutput;
2507 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2508 &def, sizeof(def)), OK);
2509 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2510 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2511 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2512 &def, sizeof(def)), OK);
2513
2514 // profile
2515 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2516 InitOMXParams(&profile);
2517 profile.nPortIndex = kPortIndexOutput;
2518 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2519 &profile, sizeof(profile)), OK);
2520 profile.nChannels = numChannels;
2521 profile.eChannelMode = (numChannels == 1?
2522 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2523 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002524 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002525 profile.nAudioBandWidth = 0;
2526 profile.nFrameLength = 0;
2527 profile.nAACtools = OMX_AUDIO_AACToolAll;
2528 profile.nAACERtools = OMX_AUDIO_AACERNone;
2529 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2530 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2531 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2532 &profile, sizeof(profile)), OK);
2533
Andreas Huberda050cf22009-09-02 14:01:43 -07002534 } else {
2535 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002536 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002537 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002538
Andreas Huber784202e2009-10-15 13:46:54 -07002539 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002540 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2541 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002542
Andreas Huberda050cf22009-09-02 14:01:43 -07002543 profile.nChannels = numChannels;
2544 profile.nSampleRate = sampleRate;
2545 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002546
Andreas Huber784202e2009-10-15 13:46:54 -07002547 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002548 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2549 CHECK_EQ(err, OK);
2550 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002551}
2552
2553void OMXCodec::setImageOutputFormat(
2554 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002555 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002556
2557#if 0
2558 OMX_INDEXTYPE index;
2559 status_t err = mOMX->get_extension_index(
2560 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2561 CHECK_EQ(err, OK);
2562
2563 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2564 CHECK_EQ(err, OK);
2565#endif
2566
2567 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002568 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002569 def.nPortIndex = kPortIndexOutput;
2570
Andreas Huber784202e2009-10-15 13:46:54 -07002571 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002572 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2573 CHECK_EQ(err, OK);
2574
2575 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2576
2577 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002578
Andreas Huberbe06d262009-08-14 14:37:10 -07002579 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2580 imageDef->eColorFormat = format;
2581 imageDef->nFrameWidth = width;
2582 imageDef->nFrameHeight = height;
2583
2584 switch (format) {
2585 case OMX_COLOR_FormatYUV420PackedPlanar:
2586 case OMX_COLOR_FormatYUV411Planar:
2587 {
2588 def.nBufferSize = (width * height * 3) / 2;
2589 break;
2590 }
2591
2592 case OMX_COLOR_FormatCbYCrY:
2593 {
2594 def.nBufferSize = width * height * 2;
2595 break;
2596 }
2597
2598 case OMX_COLOR_Format32bitARGB8888:
2599 {
2600 def.nBufferSize = width * height * 4;
2601 break;
2602 }
2603
Andreas Huber201511c2009-09-08 14:01:44 -07002604 case OMX_COLOR_Format16bitARGB4444:
2605 case OMX_COLOR_Format16bitARGB1555:
2606 case OMX_COLOR_Format16bitRGB565:
2607 case OMX_COLOR_Format16bitBGR565:
2608 {
2609 def.nBufferSize = width * height * 2;
2610 break;
2611 }
2612
Andreas Huberbe06d262009-08-14 14:37:10 -07002613 default:
2614 CHECK(!"Should not be here. Unknown color format.");
2615 break;
2616 }
2617
Andreas Huber5c0a9132009-08-20 11:16:40 -07002618 def.nBufferCountActual = def.nBufferCountMin;
2619
Andreas Huber784202e2009-10-15 13:46:54 -07002620 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002621 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2622 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002623}
Andreas Huberbe06d262009-08-14 14:37:10 -07002624
Andreas Huber5c0a9132009-08-20 11:16:40 -07002625void OMXCodec::setJPEGInputFormat(
2626 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2627 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002628 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002629 def.nPortIndex = kPortIndexInput;
2630
Andreas Huber784202e2009-10-15 13:46:54 -07002631 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002632 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2633 CHECK_EQ(err, OK);
2634
Andreas Huber5c0a9132009-08-20 11:16:40 -07002635 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2636 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2637
Andreas Huberbe06d262009-08-14 14:37:10 -07002638 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2639 imageDef->nFrameWidth = width;
2640 imageDef->nFrameHeight = height;
2641
Andreas Huber5c0a9132009-08-20 11:16:40 -07002642 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002643 def.nBufferCountActual = def.nBufferCountMin;
2644
Andreas Huber784202e2009-10-15 13:46:54 -07002645 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002646 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2647 CHECK_EQ(err, OK);
2648}
2649
2650void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2651 CodecSpecificData *specific =
2652 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2653
2654 specific->mSize = size;
2655 memcpy(specific->mData, data, size);
2656
2657 mCodecSpecificData.push(specific);
2658}
2659
2660void OMXCodec::clearCodecSpecificData() {
2661 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2662 free(mCodecSpecificData.editItemAt(i));
2663 }
2664 mCodecSpecificData.clear();
2665 mCodecSpecificDataIndex = 0;
2666}
2667
James Dong36e573b2010-06-19 09:04:18 -07002668status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07002669 Mutex::Autolock autoLock(mLock);
2670
Andreas Huberbe06d262009-08-14 14:37:10 -07002671 if (mState != LOADED) {
2672 return UNKNOWN_ERROR;
2673 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002674
Andreas Huberbe06d262009-08-14 14:37:10 -07002675 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002676 if (mQuirks & kWantsNALFragments) {
2677 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002678 }
James Dong36e573b2010-06-19 09:04:18 -07002679 if (meta) {
2680 int64_t startTimeUs = 0;
2681 int64_t timeUs;
2682 if (meta->findInt64(kKeyTime, &timeUs)) {
2683 startTimeUs = timeUs;
2684 }
2685 params->setInt64(kKeyTime, startTimeUs);
2686 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002687 status_t err = mSource->start(params.get());
2688
2689 if (err != OK) {
2690 return err;
2691 }
2692
2693 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002694 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002695 mSignalledEOS = false;
2696 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002697 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002698 mSeekTimeUs = -1;
2699 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07002700 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002701
2702 return init();
2703}
2704
2705status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002706 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002707
2708 Mutex::Autolock autoLock(mLock);
2709
2710 while (isIntermediateState(mState)) {
2711 mAsyncCompletion.wait(mLock);
2712 }
2713
2714 switch (mState) {
2715 case LOADED:
2716 case ERROR:
2717 break;
2718
2719 case EXECUTING:
2720 {
2721 setState(EXECUTING_TO_IDLE);
2722
Andreas Huber127fcdc2009-08-26 16:27:02 -07002723 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002724 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002725 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002726
Andreas Huber127fcdc2009-08-26 16:27:02 -07002727 bool emulateInputFlushCompletion =
2728 !flushPortAsync(kPortIndexInput);
2729
2730 bool emulateOutputFlushCompletion =
2731 !flushPortAsync(kPortIndexOutput);
2732
2733 if (emulateInputFlushCompletion) {
2734 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2735 }
2736
2737 if (emulateOutputFlushCompletion) {
2738 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2739 }
2740 } else {
2741 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2742 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2743
2744 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002745 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002746 CHECK_EQ(err, OK);
2747 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002748
2749 while (mState != LOADED && mState != ERROR) {
2750 mAsyncCompletion.wait(mLock);
2751 }
2752
2753 break;
2754 }
2755
2756 default:
2757 {
2758 CHECK(!"should not be here.");
2759 break;
2760 }
2761 }
2762
Andreas Hubera4357ad2010-04-02 12:49:54 -07002763 if (mLeftOverBuffer) {
2764 mLeftOverBuffer->release();
2765 mLeftOverBuffer = NULL;
2766 }
2767
Andreas Huberbe06d262009-08-14 14:37:10 -07002768 mSource->stop();
2769
Andreas Huber4a9375e2010-02-09 11:54:33 -08002770 CODEC_LOGV("stopped");
2771
Andreas Huberbe06d262009-08-14 14:37:10 -07002772 return OK;
2773}
2774
2775sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002776 Mutex::Autolock autoLock(mLock);
2777
Andreas Huberbe06d262009-08-14 14:37:10 -07002778 return mOutputFormat;
2779}
2780
2781status_t OMXCodec::read(
2782 MediaBuffer **buffer, const ReadOptions *options) {
2783 *buffer = NULL;
2784
2785 Mutex::Autolock autoLock(mLock);
2786
Andreas Huberd06e5b82009-08-28 13:18:14 -07002787 if (mState != EXECUTING && mState != RECONFIGURING) {
2788 return UNKNOWN_ERROR;
2789 }
2790
Andreas Hubere981c332009-10-22 13:49:30 -07002791 bool seeking = false;
2792 int64_t seekTimeUs;
2793 if (options && options->getSeekTo(&seekTimeUs)) {
2794 seeking = true;
2795 }
2796
Andreas Huber42978e52009-08-27 10:08:39 -07002797 if (mInitialBufferSubmit) {
2798 mInitialBufferSubmit = false;
2799
Andreas Hubere981c332009-10-22 13:49:30 -07002800 if (seeking) {
2801 CHECK(seekTimeUs >= 0);
2802 mSeekTimeUs = seekTimeUs;
2803
2804 // There's no reason to trigger the code below, there's
2805 // nothing to flush yet.
2806 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002807 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07002808 }
2809
Andreas Huber42978e52009-08-27 10:08:39 -07002810 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002811
Andreas Huberd06e5b82009-08-28 13:18:14 -07002812 if (mState == EXECUTING) {
2813 // Otherwise mState == RECONFIGURING and this code will trigger
2814 // after the output port is reenabled.
2815 fillOutputBuffers();
2816 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002817 }
2818
Andreas Hubere981c332009-10-22 13:49:30 -07002819 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002820 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002821
2822 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002823
2824 CHECK(seekTimeUs >= 0);
2825 mSeekTimeUs = seekTimeUs;
2826
2827 mFilledBuffers.clear();
2828
2829 CHECK_EQ(mState, EXECUTING);
2830
Andreas Huber404cc412009-08-25 14:26:05 -07002831 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2832 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2833
2834 if (emulateInputFlushCompletion) {
2835 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2836 }
2837
2838 if (emulateOutputFlushCompletion) {
2839 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2840 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002841
2842 while (mSeekTimeUs >= 0) {
2843 mBufferFilled.wait(mLock);
2844 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002845 }
2846
2847 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2848 mBufferFilled.wait(mLock);
2849 }
2850
2851 if (mState == ERROR) {
2852 return UNKNOWN_ERROR;
2853 }
2854
2855 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08002856 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07002857 }
2858
Andreas Hubercfd55572009-10-09 14:11:28 -07002859 if (mOutputPortSettingsHaveChanged) {
2860 mOutputPortSettingsHaveChanged = false;
2861
2862 return INFO_FORMAT_CHANGED;
2863 }
2864
Andreas Huberbe06d262009-08-14 14:37:10 -07002865 size_t index = *mFilledBuffers.begin();
2866 mFilledBuffers.erase(mFilledBuffers.begin());
2867
2868 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2869 info->mMediaBuffer->add_ref();
2870 *buffer = info->mMediaBuffer;
2871
2872 return OK;
2873}
2874
2875void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2876 Mutex::Autolock autoLock(mLock);
2877
2878 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2879 for (size_t i = 0; i < buffers->size(); ++i) {
2880 BufferInfo *info = &buffers->editItemAt(i);
2881
2882 if (info->mMediaBuffer == buffer) {
2883 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2884 fillOutputBuffer(info);
2885 return;
2886 }
2887 }
2888
2889 CHECK(!"should not be here.");
2890}
2891
2892static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2893 static const char *kNames[] = {
2894 "OMX_IMAGE_CodingUnused",
2895 "OMX_IMAGE_CodingAutoDetect",
2896 "OMX_IMAGE_CodingJPEG",
2897 "OMX_IMAGE_CodingJPEG2K",
2898 "OMX_IMAGE_CodingEXIF",
2899 "OMX_IMAGE_CodingTIFF",
2900 "OMX_IMAGE_CodingGIF",
2901 "OMX_IMAGE_CodingPNG",
2902 "OMX_IMAGE_CodingLZW",
2903 "OMX_IMAGE_CodingBMP",
2904 };
2905
2906 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2907
2908 if (type < 0 || (size_t)type >= numNames) {
2909 return "UNKNOWN";
2910 } else {
2911 return kNames[type];
2912 }
2913}
2914
2915static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2916 static const char *kNames[] = {
2917 "OMX_COLOR_FormatUnused",
2918 "OMX_COLOR_FormatMonochrome",
2919 "OMX_COLOR_Format8bitRGB332",
2920 "OMX_COLOR_Format12bitRGB444",
2921 "OMX_COLOR_Format16bitARGB4444",
2922 "OMX_COLOR_Format16bitARGB1555",
2923 "OMX_COLOR_Format16bitRGB565",
2924 "OMX_COLOR_Format16bitBGR565",
2925 "OMX_COLOR_Format18bitRGB666",
2926 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002927 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002928 "OMX_COLOR_Format24bitRGB888",
2929 "OMX_COLOR_Format24bitBGR888",
2930 "OMX_COLOR_Format24bitARGB1887",
2931 "OMX_COLOR_Format25bitARGB1888",
2932 "OMX_COLOR_Format32bitBGRA8888",
2933 "OMX_COLOR_Format32bitARGB8888",
2934 "OMX_COLOR_FormatYUV411Planar",
2935 "OMX_COLOR_FormatYUV411PackedPlanar",
2936 "OMX_COLOR_FormatYUV420Planar",
2937 "OMX_COLOR_FormatYUV420PackedPlanar",
2938 "OMX_COLOR_FormatYUV420SemiPlanar",
2939 "OMX_COLOR_FormatYUV422Planar",
2940 "OMX_COLOR_FormatYUV422PackedPlanar",
2941 "OMX_COLOR_FormatYUV422SemiPlanar",
2942 "OMX_COLOR_FormatYCbYCr",
2943 "OMX_COLOR_FormatYCrYCb",
2944 "OMX_COLOR_FormatCbYCrY",
2945 "OMX_COLOR_FormatCrYCbY",
2946 "OMX_COLOR_FormatYUV444Interleaved",
2947 "OMX_COLOR_FormatRawBayer8bit",
2948 "OMX_COLOR_FormatRawBayer10bit",
2949 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002950 "OMX_COLOR_FormatL2",
2951 "OMX_COLOR_FormatL4",
2952 "OMX_COLOR_FormatL8",
2953 "OMX_COLOR_FormatL16",
2954 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002955 "OMX_COLOR_FormatL32",
2956 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2957 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2958 "OMX_COLOR_Format18BitBGR666",
2959 "OMX_COLOR_Format24BitARGB6666",
2960 "OMX_COLOR_Format24BitABGR6666",
2961 };
2962
2963 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2964
Andreas Huberbe06d262009-08-14 14:37:10 -07002965 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2966 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2967 } else if (type < 0 || (size_t)type >= numNames) {
2968 return "UNKNOWN";
2969 } else {
2970 return kNames[type];
2971 }
2972}
2973
2974static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2975 static const char *kNames[] = {
2976 "OMX_VIDEO_CodingUnused",
2977 "OMX_VIDEO_CodingAutoDetect",
2978 "OMX_VIDEO_CodingMPEG2",
2979 "OMX_VIDEO_CodingH263",
2980 "OMX_VIDEO_CodingMPEG4",
2981 "OMX_VIDEO_CodingWMV",
2982 "OMX_VIDEO_CodingRV",
2983 "OMX_VIDEO_CodingAVC",
2984 "OMX_VIDEO_CodingMJPEG",
2985 };
2986
2987 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2988
2989 if (type < 0 || (size_t)type >= numNames) {
2990 return "UNKNOWN";
2991 } else {
2992 return kNames[type];
2993 }
2994}
2995
2996static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2997 static const char *kNames[] = {
2998 "OMX_AUDIO_CodingUnused",
2999 "OMX_AUDIO_CodingAutoDetect",
3000 "OMX_AUDIO_CodingPCM",
3001 "OMX_AUDIO_CodingADPCM",
3002 "OMX_AUDIO_CodingAMR",
3003 "OMX_AUDIO_CodingGSMFR",
3004 "OMX_AUDIO_CodingGSMEFR",
3005 "OMX_AUDIO_CodingGSMHR",
3006 "OMX_AUDIO_CodingPDCFR",
3007 "OMX_AUDIO_CodingPDCEFR",
3008 "OMX_AUDIO_CodingPDCHR",
3009 "OMX_AUDIO_CodingTDMAFR",
3010 "OMX_AUDIO_CodingTDMAEFR",
3011 "OMX_AUDIO_CodingQCELP8",
3012 "OMX_AUDIO_CodingQCELP13",
3013 "OMX_AUDIO_CodingEVRC",
3014 "OMX_AUDIO_CodingSMV",
3015 "OMX_AUDIO_CodingG711",
3016 "OMX_AUDIO_CodingG723",
3017 "OMX_AUDIO_CodingG726",
3018 "OMX_AUDIO_CodingG729",
3019 "OMX_AUDIO_CodingAAC",
3020 "OMX_AUDIO_CodingMP3",
3021 "OMX_AUDIO_CodingSBC",
3022 "OMX_AUDIO_CodingVORBIS",
3023 "OMX_AUDIO_CodingWMA",
3024 "OMX_AUDIO_CodingRA",
3025 "OMX_AUDIO_CodingMIDI",
3026 };
3027
3028 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3029
3030 if (type < 0 || (size_t)type >= numNames) {
3031 return "UNKNOWN";
3032 } else {
3033 return kNames[type];
3034 }
3035}
3036
3037static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3038 static const char *kNames[] = {
3039 "OMX_AUDIO_PCMModeLinear",
3040 "OMX_AUDIO_PCMModeALaw",
3041 "OMX_AUDIO_PCMModeMULaw",
3042 };
3043
3044 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3045
3046 if (type < 0 || (size_t)type >= numNames) {
3047 return "UNKNOWN";
3048 } else {
3049 return kNames[type];
3050 }
3051}
3052
Andreas Huber7ae02c82009-09-09 16:29:47 -07003053static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3054 static const char *kNames[] = {
3055 "OMX_AUDIO_AMRBandModeUnused",
3056 "OMX_AUDIO_AMRBandModeNB0",
3057 "OMX_AUDIO_AMRBandModeNB1",
3058 "OMX_AUDIO_AMRBandModeNB2",
3059 "OMX_AUDIO_AMRBandModeNB3",
3060 "OMX_AUDIO_AMRBandModeNB4",
3061 "OMX_AUDIO_AMRBandModeNB5",
3062 "OMX_AUDIO_AMRBandModeNB6",
3063 "OMX_AUDIO_AMRBandModeNB7",
3064 "OMX_AUDIO_AMRBandModeWB0",
3065 "OMX_AUDIO_AMRBandModeWB1",
3066 "OMX_AUDIO_AMRBandModeWB2",
3067 "OMX_AUDIO_AMRBandModeWB3",
3068 "OMX_AUDIO_AMRBandModeWB4",
3069 "OMX_AUDIO_AMRBandModeWB5",
3070 "OMX_AUDIO_AMRBandModeWB6",
3071 "OMX_AUDIO_AMRBandModeWB7",
3072 "OMX_AUDIO_AMRBandModeWB8",
3073 };
3074
3075 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3076
3077 if (type < 0 || (size_t)type >= numNames) {
3078 return "UNKNOWN";
3079 } else {
3080 return kNames[type];
3081 }
3082}
3083
3084static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3085 static const char *kNames[] = {
3086 "OMX_AUDIO_AMRFrameFormatConformance",
3087 "OMX_AUDIO_AMRFrameFormatIF1",
3088 "OMX_AUDIO_AMRFrameFormatIF2",
3089 "OMX_AUDIO_AMRFrameFormatFSF",
3090 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3091 "OMX_AUDIO_AMRFrameFormatITU",
3092 };
3093
3094 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3095
3096 if (type < 0 || (size_t)type >= numNames) {
3097 return "UNKNOWN";
3098 } else {
3099 return kNames[type];
3100 }
3101}
Andreas Huberbe06d262009-08-14 14:37:10 -07003102
3103void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3104 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003105 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003106 def.nPortIndex = portIndex;
3107
Andreas Huber784202e2009-10-15 13:46:54 -07003108 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003109 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3110 CHECK_EQ(err, OK);
3111
3112 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3113
3114 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3115 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3116
3117 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3118 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3119 printf(" nBufferSize = %ld\n", def.nBufferSize);
3120
3121 switch (def.eDomain) {
3122 case OMX_PortDomainImage:
3123 {
3124 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3125
3126 printf("\n");
3127 printf(" // Image\n");
3128 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3129 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3130 printf(" nStride = %ld\n", imageDef->nStride);
3131
3132 printf(" eCompressionFormat = %s\n",
3133 imageCompressionFormatString(imageDef->eCompressionFormat));
3134
3135 printf(" eColorFormat = %s\n",
3136 colorFormatString(imageDef->eColorFormat));
3137
3138 break;
3139 }
3140
3141 case OMX_PortDomainVideo:
3142 {
3143 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3144
3145 printf("\n");
3146 printf(" // Video\n");
3147 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3148 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3149 printf(" nStride = %ld\n", videoDef->nStride);
3150
3151 printf(" eCompressionFormat = %s\n",
3152 videoCompressionFormatString(videoDef->eCompressionFormat));
3153
3154 printf(" eColorFormat = %s\n",
3155 colorFormatString(videoDef->eColorFormat));
3156
3157 break;
3158 }
3159
3160 case OMX_PortDomainAudio:
3161 {
3162 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3163
3164 printf("\n");
3165 printf(" // Audio\n");
3166 printf(" eEncoding = %s\n",
3167 audioCodingTypeString(audioDef->eEncoding));
3168
3169 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3170 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003171 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003172 params.nPortIndex = portIndex;
3173
Andreas Huber784202e2009-10-15 13:46:54 -07003174 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003175 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3176 CHECK_EQ(err, OK);
3177
3178 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3179 printf(" nChannels = %ld\n", params.nChannels);
3180 printf(" bInterleaved = %d\n", params.bInterleaved);
3181 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3182
3183 printf(" eNumData = %s\n",
3184 params.eNumData == OMX_NumericalDataSigned
3185 ? "signed" : "unsigned");
3186
3187 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003188 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3189 OMX_AUDIO_PARAM_AMRTYPE amr;
3190 InitOMXParams(&amr);
3191 amr.nPortIndex = portIndex;
3192
Andreas Huber784202e2009-10-15 13:46:54 -07003193 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003194 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3195 CHECK_EQ(err, OK);
3196
3197 printf(" nChannels = %ld\n", amr.nChannels);
3198 printf(" eAMRBandMode = %s\n",
3199 amrBandModeString(amr.eAMRBandMode));
3200 printf(" eAMRFrameFormat = %s\n",
3201 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003202 }
3203
3204 break;
3205 }
3206
3207 default:
3208 {
3209 printf(" // Unknown\n");
3210 break;
3211 }
3212 }
3213
3214 printf("}\n");
3215}
3216
3217void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3218 mOutputFormat = new MetaData;
3219 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003220 if (mIsEncoder) {
3221 int32_t timeScale;
3222 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3223 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3224 }
3225 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003226
3227 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003228 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003229 def.nPortIndex = kPortIndexOutput;
3230
Andreas Huber784202e2009-10-15 13:46:54 -07003231 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003232 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3233 CHECK_EQ(err, OK);
3234
3235 switch (def.eDomain) {
3236 case OMX_PortDomainImage:
3237 {
3238 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3239 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3240
Andreas Hubere6c40962009-09-10 14:13:30 -07003241 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003242 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3243 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3244 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3245 break;
3246 }
3247
3248 case OMX_PortDomainAudio:
3249 {
3250 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3251
Andreas Huberda050cf22009-09-02 14:01:43 -07003252 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3253 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003254 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003255 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003256
Andreas Huber784202e2009-10-15 13:46:54 -07003257 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003258 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3259 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003260
Andreas Huberda050cf22009-09-02 14:01:43 -07003261 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3262 CHECK_EQ(params.nBitPerSample, 16);
3263 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003264
Andreas Huberda050cf22009-09-02 14:01:43 -07003265 int32_t numChannels, sampleRate;
3266 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3267 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003268
Andreas Huberda050cf22009-09-02 14:01:43 -07003269 if ((OMX_U32)numChannels != params.nChannels) {
3270 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003271 "the input stream contains (contains %d channels, "
3272 "codec outputs %ld channels).",
3273 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003274 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003275
Andreas Hubere6c40962009-09-10 14:13:30 -07003276 mOutputFormat->setCString(
3277 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003278
3279 // Use the codec-advertised number of channels, as some
3280 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003281 // mono. If we know the codec lies about this information,
3282 // use the actual number of channels instead.
3283 mOutputFormat->setInt32(
3284 kKeyChannelCount,
3285 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3286 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003287
3288 // The codec-reported sampleRate is not reliable...
3289 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3290 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003291 OMX_AUDIO_PARAM_AMRTYPE amr;
3292 InitOMXParams(&amr);
3293 amr.nPortIndex = kPortIndexOutput;
3294
Andreas Huber784202e2009-10-15 13:46:54 -07003295 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003296 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3297 CHECK_EQ(err, OK);
3298
3299 CHECK_EQ(amr.nChannels, 1);
3300 mOutputFormat->setInt32(kKeyChannelCount, 1);
3301
3302 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3303 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003304 mOutputFormat->setCString(
3305 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003306 mOutputFormat->setInt32(kKeySampleRate, 8000);
3307 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3308 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003309 mOutputFormat->setCString(
3310 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003311 mOutputFormat->setInt32(kKeySampleRate, 16000);
3312 } else {
3313 CHECK(!"Unknown AMR band mode.");
3314 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003315 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003316 mOutputFormat->setCString(
3317 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003318 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003319 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3320 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003321 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003322 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3323 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003324 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003325 } else {
3326 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003327 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003328 break;
3329 }
3330
3331 case OMX_PortDomainVideo:
3332 {
3333 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3334
3335 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003336 mOutputFormat->setCString(
3337 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003338 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003339 mOutputFormat->setCString(
3340 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003341 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003342 mOutputFormat->setCString(
3343 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003344 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003345 mOutputFormat->setCString(
3346 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003347 } else {
3348 CHECK(!"Unknown compression format.");
3349 }
3350
3351 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3352 // This component appears to be lying to me.
3353 mOutputFormat->setInt32(
3354 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3355 mOutputFormat->setInt32(
3356 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3357 } else {
3358 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3359 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3360 }
3361
3362 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3363 break;
3364 }
3365
3366 default:
3367 {
3368 CHECK(!"should not be here, neither audio nor video.");
3369 break;
3370 }
3371 }
3372}
3373
Andreas Huber1f24b302010-06-10 11:12:39 -07003374status_t OMXCodec::pause() {
3375 Mutex::Autolock autoLock(mLock);
3376
3377 mPaused = true;
3378
3379 return OK;
3380}
3381
Andreas Hubere6c40962009-09-10 14:13:30 -07003382////////////////////////////////////////////////////////////////////////////////
3383
3384status_t QueryCodecs(
3385 const sp<IOMX> &omx,
3386 const char *mime, bool queryDecoders,
3387 Vector<CodecCapabilities> *results) {
3388 results->clear();
3389
3390 for (int index = 0;; ++index) {
3391 const char *componentName;
3392
3393 if (!queryDecoders) {
3394 componentName = GetCodec(
3395 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3396 mime, index);
3397 } else {
3398 componentName = GetCodec(
3399 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3400 mime, index);
3401 }
3402
3403 if (!componentName) {
3404 return OK;
3405 }
3406
Andreas Huber1a189a82010-03-24 13:49:20 -07003407 if (strncmp(componentName, "OMX.", 4)) {
3408 // Not an OpenMax component but a software codec.
3409
3410 results->push();
3411 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3412 caps->mComponentName = componentName;
3413
3414 continue;
3415 }
3416
Andreas Huber784202e2009-10-15 13:46:54 -07003417 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003418 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003419 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003420
3421 if (err != OK) {
3422 continue;
3423 }
3424
James Dong722d5912010-04-13 10:56:59 -07003425 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003426
3427 results->push();
3428 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3429 caps->mComponentName = componentName;
3430
3431 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3432 InitOMXParams(&param);
3433
3434 param.nPortIndex = queryDecoders ? 0 : 1;
3435
3436 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003437 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003438 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3439 &param, sizeof(param));
3440
3441 if (err != OK) {
3442 break;
3443 }
3444
3445 CodecProfileLevel profileLevel;
3446 profileLevel.mProfile = param.eProfile;
3447 profileLevel.mLevel = param.eLevel;
3448
3449 caps->mProfileLevels.push(profileLevel);
3450 }
3451
Andreas Huber784202e2009-10-15 13:46:54 -07003452 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003453 }
3454}
3455
Andreas Huberbe06d262009-08-14 14:37:10 -07003456} // namespace android