blob: 1b6308361869c24792e59f25f9016114ec26fd40 [file] [log] [blame]
Andreas Huberbe06d262009-08-14 14:37:10 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "OMXCodec"
19#include <utils/Log.h>
20
Andreas Huberdacaa732009-12-07 09:56:32 -080021#include "include/AACDecoder.h"
James Dong17299ab2010-05-14 15:45:22 -070022#include "include/AACEncoder.h"
Andreas Hubera30d4002009-12-08 15:40:06 -080023#include "include/AMRNBDecoder.h"
Andreas Huberd49b526dd2009-12-11 15:07:25 -080024#include "include/AMRNBEncoder.h"
Andreas Hubera30d4002009-12-08 15:40:06 -080025#include "include/AMRWBDecoder.h"
James Dong17299ab2010-05-14 15:45:22 -070026#include "include/AMRWBEncoder.h"
Andreas Huber4a0ec3f2009-12-10 09:44:29 -080027#include "include/AVCDecoder.h"
James Dong1cc31e62010-07-02 17:44:44 -070028#include "include/AVCEncoder.h"
James Dong02f5b542009-12-15 16:26:55 -080029#include "include/M4vH263Decoder.h"
James Dong42ef0c72010-07-12 21:46:25 -070030#include "include/M4vH263Encoder.h"
Andreas Huber250f2432009-12-07 14:22:35 -080031#include "include/MP3Decoder.h"
Andreas Huber388379f2010-05-07 10:35:13 -070032#include "include/VorbisDecoder.h"
Andreas Huber47ba30e2010-05-24 14:38:02 -070033#include "include/VPXDecoder.h"
Andreas Huber8c7ab032009-12-07 11:23:44 -080034
Andreas Huberbd7b43b2009-10-13 10:22:55 -070035#include "include/ESDS.h"
36
Andreas Huberbe06d262009-08-14 14:37:10 -070037#include <binder/IServiceManager.h>
38#include <binder/MemoryDealer.h>
39#include <binder/ProcessState.h>
40#include <media/IMediaPlayerService.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070041#include <media/stagefright/MediaBuffer.h>
42#include <media/stagefright/MediaBufferGroup.h>
43#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070044#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070045#include <media/stagefright/MediaExtractor.h>
46#include <media/stagefright/MetaData.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070047#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070048#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070049#include <utils/Vector.h>
50
51#include <OMX_Audio.h>
52#include <OMX_Component.h>
53
54namespace android {
55
Andreas Huber8b432b12009-10-07 13:36:52 -070056static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
57
Andreas Huberbe06d262009-08-14 14:37:10 -070058struct CodecInfo {
59 const char *mime;
60 const char *codec;
61};
62
Andreas Huberfb1c2f82009-12-15 13:25:11 -080063#define FACTORY_CREATE(name) \
64static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
65 return new name(source); \
66}
67
James Dong17299ab2010-05-14 15:45:22 -070068#define FACTORY_CREATE_ENCODER(name) \
69static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \
70 return new name(source, meta); \
71}
72
Andreas Huberfb1c2f82009-12-15 13:25:11 -080073#define FACTORY_REF(name) { #name, Make##name },
74
75FACTORY_CREATE(MP3Decoder)
76FACTORY_CREATE(AMRNBDecoder)
77FACTORY_CREATE(AMRWBDecoder)
78FACTORY_CREATE(AACDecoder)
79FACTORY_CREATE(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -080080FACTORY_CREATE(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -070081FACTORY_CREATE(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -070082FACTORY_CREATE(VPXDecoder)
James Dong17299ab2010-05-14 15:45:22 -070083FACTORY_CREATE_ENCODER(AMRNBEncoder)
84FACTORY_CREATE_ENCODER(AMRWBEncoder)
85FACTORY_CREATE_ENCODER(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -070086FACTORY_CREATE_ENCODER(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -070087FACTORY_CREATE_ENCODER(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -070088
89static sp<MediaSource> InstantiateSoftwareEncoder(
90 const char *name, const sp<MediaSource> &source,
91 const sp<MetaData> &meta) {
92 struct FactoryInfo {
93 const char *name;
94 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &);
95 };
96
97 static const FactoryInfo kFactoryInfo[] = {
98 FACTORY_REF(AMRNBEncoder)
99 FACTORY_REF(AMRWBEncoder)
100 FACTORY_REF(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -0700101 FACTORY_REF(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -0700102 FACTORY_REF(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -0700103 };
104 for (size_t i = 0;
105 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
106 if (!strcmp(name, kFactoryInfo[i].name)) {
107 return (*kFactoryInfo[i].CreateFunc)(source, meta);
108 }
109 }
110
111 return NULL;
112}
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800113
114static sp<MediaSource> InstantiateSoftwareCodec(
115 const char *name, const sp<MediaSource> &source) {
116 struct FactoryInfo {
117 const char *name;
118 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
119 };
120
121 static const FactoryInfo kFactoryInfo[] = {
122 FACTORY_REF(MP3Decoder)
123 FACTORY_REF(AMRNBDecoder)
124 FACTORY_REF(AMRWBDecoder)
125 FACTORY_REF(AACDecoder)
126 FACTORY_REF(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -0800127 FACTORY_REF(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -0700128 FACTORY_REF(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -0700129 FACTORY_REF(VPXDecoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800130 };
131 for (size_t i = 0;
132 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
133 if (!strcmp(name, kFactoryInfo[i].name)) {
134 return (*kFactoryInfo[i].CreateFunc)(source);
135 }
136 }
137
138 return NULL;
139}
140
141#undef FACTORY_REF
142#undef FACTORY_CREATE
143
Andreas Huberbe06d262009-08-14 14:37:10 -0700144static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700145 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
James Dong374aee62010-04-26 10:23:30 -0700146// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800147 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700148// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700149// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800150 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700151// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700152 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800153 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700154// { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700155 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800156 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700157// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700158 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.decoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700159 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
160 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800161 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700162// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700163 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.decoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700164 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800165 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700166// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700167 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.decoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700168 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
169 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800170 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700171// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huber388379f2010-05-07 10:35:13 -0700172 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" },
Andreas Huber47ba30e2010-05-24 14:38:02 -0700173 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700174};
175
176static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700177 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800178 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700179 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700180 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700181 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700182 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700183// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
184 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.encoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700185 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
186 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700187 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700188// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
189 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.encoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700190 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
191 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700192 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700193// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
194 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.encoder.avc" },
Andreas Huber71c27d92010-03-19 11:43:15 -0700195 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700196 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
James Dong1cc31e62010-07-02 17:44:44 -0700197 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700198// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700199};
200
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800201#undef OPTIONAL
202
Andreas Hubere0873732009-09-10 09:57:53 -0700203#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700204#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber42c444a2010-02-09 10:20:00 -0800205#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700206
Andreas Huberbe06d262009-08-14 14:37:10 -0700207struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700208 OMXCodecObserver() {
209 }
210
211 void setCodec(const sp<OMXCodec> &target) {
212 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700213 }
214
215 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700216 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700217 sp<OMXCodec> codec = mTarget.promote();
218
219 if (codec.get() != NULL) {
220 codec->on_message(msg);
221 }
222 }
223
224protected:
225 virtual ~OMXCodecObserver() {}
226
227private:
228 wp<OMXCodec> mTarget;
229
230 OMXCodecObserver(const OMXCodecObserver &);
231 OMXCodecObserver &operator=(const OMXCodecObserver &);
232};
233
234static const char *GetCodec(const CodecInfo *info, size_t numInfos,
235 const char *mime, int index) {
236 CHECK(index >= 0);
237 for(size_t i = 0; i < numInfos; ++i) {
238 if (!strcasecmp(mime, info[i].mime)) {
239 if (index == 0) {
240 return info[i].codec;
241 }
242
243 --index;
244 }
245 }
246
247 return NULL;
248}
249
Andreas Huberebf66ea2009-08-19 13:32:58 -0700250enum {
251 kAVCProfileBaseline = 0x42,
252 kAVCProfileMain = 0x4d,
253 kAVCProfileExtended = 0x58,
254 kAVCProfileHigh = 0x64,
255 kAVCProfileHigh10 = 0x6e,
256 kAVCProfileHigh422 = 0x7a,
257 kAVCProfileHigh444 = 0xf4,
258 kAVCProfileCAVLC444Intra = 0x2c
259};
260
261static const char *AVCProfileToString(uint8_t profile) {
262 switch (profile) {
263 case kAVCProfileBaseline:
264 return "Baseline";
265 case kAVCProfileMain:
266 return "Main";
267 case kAVCProfileExtended:
268 return "Extended";
269 case kAVCProfileHigh:
270 return "High";
271 case kAVCProfileHigh10:
272 return "High 10";
273 case kAVCProfileHigh422:
274 return "High 422";
275 case kAVCProfileHigh444:
276 return "High 444";
277 case kAVCProfileCAVLC444Intra:
278 return "CAVLC 444 Intra";
279 default: return "Unknown";
280 }
281}
282
Andreas Huber4c483422009-09-02 16:05:36 -0700283template<class T>
284static void InitOMXParams(T *params) {
285 params->nSize = sizeof(T);
286 params->nVersion.s.nVersionMajor = 1;
287 params->nVersion.s.nVersionMinor = 0;
288 params->nVersion.s.nRevision = 0;
289 params->nVersion.s.nStep = 0;
290}
291
Andreas Hubere13526a2009-10-22 10:43:34 -0700292static bool IsSoftwareCodec(const char *componentName) {
293 if (!strncmp("OMX.PV.", componentName, 7)) {
294 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700295 }
296
Andreas Hubere13526a2009-10-22 10:43:34 -0700297 return false;
298}
299
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800300// A sort order in which non-OMX components are first,
301// followed by software codecs, i.e. OMX.PV.*, followed
302// by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700303static int CompareSoftwareCodecsFirst(
304 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800305 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
306 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
307
308 if (isNotOMX1) {
309 if (isNotOMX2) { return 0; }
310 return -1;
311 }
312 if (isNotOMX2) {
313 return 1;
314 }
315
Andreas Hubere13526a2009-10-22 10:43:34 -0700316 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
317 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
318
319 if (isSoftwareCodec1) {
320 if (isSoftwareCodec2) { return 0; }
321 return -1;
322 }
323
324 if (isSoftwareCodec2) {
325 return 1;
326 }
327
328 return 0;
329}
330
331// static
332uint32_t OMXCodec::getComponentQuirks(const char *componentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700333 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700334
Andreas Huberbe06d262009-08-14 14:37:10 -0700335 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700336 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700337 }
338 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
339 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800340 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700341 }
342 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
343 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700344 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700345 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700346 }
347 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
348 quirks |= kRequiresLoadedToIdleAfterAllocation;
349 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700350 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huberbe06d262009-08-14 14:37:10 -0700351 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700352 if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) {
353 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700354 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700355 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800356 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700357 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700358 if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) {
359 quirks |= kRequiresAllocateBufferOnInputPorts;
360 quirks |= kRequiresAllocateBufferOnOutputPorts;
361 quirks |= kDefersOutputBufferAllocation;
362 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700363
Andreas Huber2dc64d82009-09-11 12:58:53 -0700364 if (!strncmp(componentName, "OMX.TI.", 7)) {
365 // Apparently I must not use OMX_UseBuffer on either input or
366 // output ports on any of the TI components or quote:
367 // "(I) may have unexpected problem (sic) which can be timing related
368 // and hard to reproduce."
369
370 quirks |= kRequiresAllocateBufferOnInputPorts;
371 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dongdca66e12010-06-14 11:14:38 -0700372 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) {
James Dong4f501f02010-06-07 14:41:41 -0700373 quirks |= kAvoidMemcopyInputRecordingFrames;
374 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700375 }
376
Andreas Huberb8de9572010-02-22 14:58:45 -0800377 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
378 quirks |= kInputBufferSizesAreBogus;
379 }
380
Andreas Hubere13526a2009-10-22 10:43:34 -0700381 return quirks;
382}
383
384// static
385void OMXCodec::findMatchingCodecs(
386 const char *mime,
387 bool createEncoder, const char *matchComponentName,
388 uint32_t flags,
389 Vector<String8> *matchingCodecs) {
390 matchingCodecs->clear();
391
392 for (int index = 0;; ++index) {
393 const char *componentName;
394
395 if (createEncoder) {
396 componentName = GetCodec(
397 kEncoderInfo,
398 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
399 mime, index);
400 } else {
401 componentName = GetCodec(
402 kDecoderInfo,
403 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
404 mime, index);
405 }
406
407 if (!componentName) {
408 break;
409 }
410
411 // If a specific codec is requested, skip the non-matching ones.
412 if (matchComponentName && strcmp(componentName, matchComponentName)) {
413 continue;
414 }
415
416 matchingCodecs->push(String8(componentName));
417 }
418
419 if (flags & kPreferSoftwareCodecs) {
420 matchingCodecs->sort(CompareSoftwareCodecsFirst);
421 }
422}
423
424// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800425sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700426 const sp<IOMX> &omx,
427 const sp<MetaData> &meta, bool createEncoder,
428 const sp<MediaSource> &source,
429 const char *matchComponentName,
430 uint32_t flags) {
431 const char *mime;
432 bool success = meta->findCString(kKeyMIMEType, &mime);
433 CHECK(success);
434
435 Vector<String8> matchingCodecs;
436 findMatchingCodecs(
437 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
438
439 if (matchingCodecs.isEmpty()) {
440 return NULL;
441 }
442
443 sp<OMXCodecObserver> observer = new OMXCodecObserver;
444 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700445
446 const char *componentName;
447 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
448 componentName = matchingCodecs[i].string();
449
James Dong17299ab2010-05-14 15:45:22 -0700450 sp<MediaSource> softwareCodec = createEncoder?
451 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800452 InstantiateSoftwareCodec(componentName, source);
453
454 if (softwareCodec != NULL) {
455 LOGV("Successfully allocated software codec '%s'", componentName);
456
457 return softwareCodec;
458 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800459
Andreas Hubere13526a2009-10-22 10:43:34 -0700460 LOGV("Attempting to allocate OMX node '%s'", componentName);
461
462 status_t err = omx->allocateNode(componentName, observer, &node);
463 if (err == OK) {
464 LOGV("Successfully allocated OMX node '%s'", componentName);
465
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700466 sp<OMXCodec> codec = new OMXCodec(
467 omx, node, getComponentQuirks(componentName),
468 createEncoder, mime, componentName,
469 source);
470
471 observer->setCodec(codec);
472
473 err = codec->configureCodec(meta);
474
475 if (err == OK) {
476 return codec;
477 }
478
479 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700480 }
481 }
482
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700483 return NULL;
484}
Andreas Hubere13526a2009-10-22 10:43:34 -0700485
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700486status_t OMXCodec::configureCodec(const sp<MetaData> &meta) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700487 uint32_t type;
488 const void *data;
489 size_t size;
490 if (meta->findData(kKeyESDS, &type, &data, &size)) {
491 ESDS esds((const char *)data, size);
492 CHECK_EQ(esds.InitCheck(), OK);
493
494 const void *codec_specific_data;
495 size_t codec_specific_data_size;
496 esds.getCodecSpecificInfo(
497 &codec_specific_data, &codec_specific_data_size);
498
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700499 addCodecSpecificData(
Andreas Huberbe06d262009-08-14 14:37:10 -0700500 codec_specific_data, codec_specific_data_size);
501 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
Andreas Huberebf66ea2009-08-19 13:32:58 -0700502 // Parse the AVCDecoderConfigurationRecord
503
504 const uint8_t *ptr = (const uint8_t *)data;
505
506 CHECK(size >= 7);
507 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
508 uint8_t profile = ptr[1];
509 uint8_t level = ptr[3];
510
Andreas Huber44e15c42009-11-23 14:39:38 -0800511 // There is decodable content out there that fails the following
512 // assertion, let's be lenient for now...
513 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700514
515 size_t lengthSize = 1 + (ptr[4] & 3);
516
517 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
518 // violates it...
519 // CHECK((ptr[5] >> 5) == 7); // reserved
520
521 size_t numSeqParameterSets = ptr[5] & 31;
522
523 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700524 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700525
526 for (size_t i = 0; i < numSeqParameterSets; ++i) {
527 CHECK(size >= 2);
528 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700529
530 ptr += 2;
531 size -= 2;
532
Andreas Huberbe06d262009-08-14 14:37:10 -0700533 CHECK(size >= length);
534
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700535 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700536
537 ptr += length;
538 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700539 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700540
Andreas Huberebf66ea2009-08-19 13:32:58 -0700541 CHECK(size >= 1);
542 size_t numPictureParameterSets = *ptr;
543 ++ptr;
544 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700545
Andreas Huberebf66ea2009-08-19 13:32:58 -0700546 for (size_t i = 0; i < numPictureParameterSets; ++i) {
547 CHECK(size >= 2);
548 size_t length = U16_AT(ptr);
549
550 ptr += 2;
551 size -= 2;
552
553 CHECK(size >= length);
554
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700555 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700556
557 ptr += length;
558 size -= length;
559 }
560
Andreas Hubere2f85072010-06-10 09:51:27 -0700561 CODEC_LOGV(
562 "AVC profile = %d (%s), level = %d",
563 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700564
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700565 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
Andreas Hubere2f85072010-06-10 09:51:27 -0700566 && (profile != kAVCProfileBaseline || level > 30)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700567 // This stream exceeds the decoder's capabilities. The decoder
568 // does not handle this gracefully and would clobber the heap
569 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700570
571 LOGE("Profile and/or level exceed the decoder's capabilities.");
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700572 return ERROR_UNSUPPORTED;
Andreas Huberbe06d262009-08-14 14:37:10 -0700573 }
574 }
575
James Dong17299ab2010-05-14 15:45:22 -0700576 int32_t bitRate = 0;
577 if (mIsEncoder) {
578 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
579 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700580 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700581 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700582 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700583 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700584 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700585 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700586 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700587 int32_t numChannels, sampleRate;
588 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
589 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
590
James Dong17299ab2010-05-14 15:45:22 -0700591 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700592 }
James Dongabed93a2010-04-22 17:27:04 -0700593
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700594 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700595
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700596 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700597 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700598 } else {
James Dong1244eab2010-06-08 11:58:53 -0700599 int32_t width, height;
600 bool success = meta->findInt32(kKeyWidth, &width);
601 success = success && meta->findInt32(kKeyHeight, &height);
602 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700603 status_t err = setVideoOutputFormat(
604 mMIME, width, height);
605
606 if (err != OK) {
607 return err;
608 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700609 }
610 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700611
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700612 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
613 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700614 OMX_COLOR_FORMATTYPE format =
615 OMX_COLOR_Format32bitARGB8888;
616 // OMX_COLOR_FormatYUV420PackedPlanar;
617 // OMX_COLOR_FormatCbYCrY;
618 // OMX_COLOR_FormatYUV411Planar;
619
620 int32_t width, height;
621 bool success = meta->findInt32(kKeyWidth, &width);
622 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700623
624 int32_t compressedSize;
625 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700626 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700627
628 CHECK(success);
629 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700630
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700631 setImageOutputFormat(format, width, height);
632 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700633 }
634
Andreas Huberda050cf22009-09-02 14:01:43 -0700635 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800636 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700637 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700638 }
639
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700640 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700641 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
642 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700643 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700644 }
645
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700646 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700647
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700648 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700649}
650
Andreas Huberda050cf22009-09-02 14:01:43 -0700651void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
652 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700653 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700654 def.nPortIndex = portIndex;
655
Andreas Huber784202e2009-10-15 13:46:54 -0700656 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700657 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
658 CHECK_EQ(err, OK);
659
Andreas Huberb8de9572010-02-22 14:58:45 -0800660 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
661 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700662 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700663 }
664
Andreas Huber784202e2009-10-15 13:46:54 -0700665 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700666 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
667 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800668
669 err = mOMX->getParameter(
670 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
671 CHECK_EQ(err, OK);
672
673 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800674 if (portIndex == kPortIndexInput
675 && (mQuirks & kInputBufferSizesAreBogus)) {
676 CHECK_EQ(def.nBufferSize, size);
677 } else {
678 CHECK(def.nBufferSize >= size);
679 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700680}
681
Andreas Huberbe06d262009-08-14 14:37:10 -0700682status_t OMXCodec::setVideoPortFormatType(
683 OMX_U32 portIndex,
684 OMX_VIDEO_CODINGTYPE compressionFormat,
685 OMX_COLOR_FORMATTYPE colorFormat) {
686 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700687 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700688 format.nPortIndex = portIndex;
689 format.nIndex = 0;
690 bool found = false;
691
692 OMX_U32 index = 0;
693 for (;;) {
694 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700695 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700696 mNode, OMX_IndexParamVideoPortFormat,
697 &format, sizeof(format));
698
699 if (err != OK) {
700 return err;
701 }
702
703 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700704 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700705
706#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700707 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700708 portIndex,
709 index, format.eCompressionFormat, format.eColorFormat);
710#endif
711
712 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
713 if (portIndex == kPortIndexInput
714 && colorFormat == format.eColorFormat) {
715 // eCompressionFormat does not seem right.
716 found = true;
717 break;
718 }
719 if (portIndex == kPortIndexOutput
720 && compressionFormat == format.eCompressionFormat) {
721 // eColorFormat does not seem right.
722 found = true;
723 break;
724 }
725 }
726
727 if (format.eCompressionFormat == compressionFormat
728 && format.eColorFormat == colorFormat) {
729 found = true;
730 break;
731 }
732
733 ++index;
734 }
735
736 if (!found) {
737 return UNKNOWN_ERROR;
738 }
739
Andreas Huber53a76bd2009-10-06 16:20:44 -0700740 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700741 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700742 mNode, OMX_IndexParamVideoPortFormat,
743 &format, sizeof(format));
744
745 return err;
746}
747
Andreas Huberb482ce82009-10-29 12:02:48 -0700748static size_t getFrameSize(
749 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
750 switch (colorFormat) {
751 case OMX_COLOR_FormatYCbYCr:
752 case OMX_COLOR_FormatCbYCrY:
753 return width * height * 2;
754
Andreas Huber71c27d92010-03-19 11:43:15 -0700755 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700756 case OMX_COLOR_FormatYUV420SemiPlanar:
757 return (width * height * 3) / 2;
758
759 default:
760 CHECK(!"Should not be here. Unsupported color format.");
761 break;
762 }
763}
764
Andreas Huberbe06d262009-08-14 14:37:10 -0700765void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700766 const char *mime, const sp<MetaData>& meta) {
767
768 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
769 bool success = meta->findInt32(kKeyWidth, &width);
770 success = success && meta->findInt32(kKeyHeight, &height);
771 success = success && meta->findInt32(kKeySampleRate, &frameRate);
772 success = success && meta->findInt32(kKeyBitRate, &bitRate);
773 success = success && meta->findInt32(kKeyStride, &stride);
774 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
775 CHECK(success);
776 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700777
778 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700779 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700780 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700781 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700782 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700783 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700784 compressionFormat = OMX_VIDEO_CodingH263;
785 } else {
786 LOGE("Not a supported video mime type: %s", mime);
787 CHECK(!"Should not be here. Not a supported video mime type.");
788 }
789
Andreas Huberea6a38c2009-11-16 15:43:38 -0800790 OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
791 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
James Dongabed93a2010-04-22 17:27:04 -0700792 colorFormat = OMX_COLOR_FormatYCbYCr;
Andreas Huberbe06d262009-08-14 14:37:10 -0700793 }
794
James Dongb00e2462010-04-26 17:48:26 -0700795 status_t err;
796 OMX_PARAM_PORTDEFINITIONTYPE def;
797 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
798
799 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700800 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700801 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700802 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700803
James Dongb00e2462010-04-26 17:48:26 -0700804 InitOMXParams(&def);
805 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700806
James Dongb00e2462010-04-26 17:48:26 -0700807 err = mOMX->getParameter(
808 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
809 CHECK_EQ(err, OK);
810
James Dong1244eab2010-06-08 11:58:53 -0700811 def.nBufferSize = getFrameSize(colorFormat,
812 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700813
814 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
815
816 video_def->nFrameWidth = width;
817 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700818 video_def->nStride = stride;
819 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700820 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700821 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
822 video_def->eColorFormat = colorFormat;
823
James Dongb00e2462010-04-26 17:48:26 -0700824 err = mOMX->setParameter(
825 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
826 CHECK_EQ(err, OK);
827
828 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700829 CHECK_EQ(setVideoPortFormatType(
830 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
831 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700832 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700833 def.nPortIndex = kPortIndexOutput;
834
James Dongb00e2462010-04-26 17:48:26 -0700835 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700836 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
837
838 CHECK_EQ(err, OK);
839 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
840
841 video_def->nFrameWidth = width;
842 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700843 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700844 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700845 video_def->eCompressionFormat = compressionFormat;
846 video_def->eColorFormat = OMX_COLOR_FormatUnused;
847
Andreas Huber784202e2009-10-15 13:46:54 -0700848 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700849 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
850 CHECK_EQ(err, OK);
851
James Dongb00e2462010-04-26 17:48:26 -0700852 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700853 switch (compressionFormat) {
854 case OMX_VIDEO_CodingMPEG4:
855 {
James Dong1244eab2010-06-08 11:58:53 -0700856 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700857 break;
858 }
859
860 case OMX_VIDEO_CodingH263:
James Dongc0ab2a62010-06-29 16:29:19 -0700861 CHECK_EQ(setupH263EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700862 break;
863
Andreas Huberea6a38c2009-11-16 15:43:38 -0800864 case OMX_VIDEO_CodingAVC:
865 {
James Dong1244eab2010-06-08 11:58:53 -0700866 CHECK_EQ(setupAVCEncoderParameters(meta), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -0800867 break;
868 }
869
Andreas Huberb482ce82009-10-29 12:02:48 -0700870 default:
871 CHECK(!"Support for this compressionFormat to be implemented.");
872 break;
873 }
874}
875
James Dong1244eab2010-06-08 11:58:53 -0700876static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
877 if (iFramesInterval < 0) {
878 return 0xFFFFFFFF;
879 } else if (iFramesInterval == 0) {
880 return 0;
881 }
882 OMX_U32 ret = frameRate * iFramesInterval;
883 CHECK(ret > 1);
884 return ret;
885}
886
James Dongc0ab2a62010-06-29 16:29:19 -0700887status_t OMXCodec::setupErrorCorrectionParameters() {
888 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
889 InitOMXParams(&errorCorrectionType);
890 errorCorrectionType.nPortIndex = kPortIndexOutput;
891
892 status_t err = mOMX->getParameter(
893 mNode, OMX_IndexParamVideoErrorCorrection,
894 &errorCorrectionType, sizeof(errorCorrectionType));
895 CHECK_EQ(err, OK);
896
897 errorCorrectionType.bEnableHEC = OMX_FALSE;
898 errorCorrectionType.bEnableResync = OMX_TRUE;
899 errorCorrectionType.nResynchMarkerSpacing = 256;
900 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
901 errorCorrectionType.bEnableRVLC = OMX_FALSE;
902
903 err = mOMX->setParameter(
904 mNode, OMX_IndexParamVideoErrorCorrection,
905 &errorCorrectionType, sizeof(errorCorrectionType));
906 CHECK_EQ(err, OK);
907 return OK;
908}
909
910status_t OMXCodec::setupBitRate(int32_t bitRate) {
911 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
912 InitOMXParams(&bitrateType);
913 bitrateType.nPortIndex = kPortIndexOutput;
914
915 status_t err = mOMX->getParameter(
916 mNode, OMX_IndexParamVideoBitrate,
917 &bitrateType, sizeof(bitrateType));
918 CHECK_EQ(err, OK);
919
920 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
921 bitrateType.nTargetBitrate = bitRate;
922
923 err = mOMX->setParameter(
924 mNode, OMX_IndexParamVideoBitrate,
925 &bitrateType, sizeof(bitrateType));
926 CHECK_EQ(err, OK);
927 return OK;
928}
929
James Dong81c929a2010-07-01 15:02:14 -0700930status_t OMXCodec::getVideoProfileLevel(
931 const sp<MetaData>& meta,
932 const CodecProfileLevel& defaultProfileLevel,
933 CodecProfileLevel &profileLevel) {
934 CODEC_LOGV("Default profile: %ld, level %ld",
935 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
936
937 // Are the default profile and level overwriten?
938 int32_t profile, level;
939 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
940 profile = defaultProfileLevel.mProfile;
941 }
942 if (!meta->findInt32(kKeyVideoLevel, &level)) {
943 level = defaultProfileLevel.mLevel;
944 }
945 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
946
947 // Are the target profile and level supported by the encoder?
948 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
949 InitOMXParams(&param);
950 param.nPortIndex = kPortIndexOutput;
951 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
952 status_t err = mOMX->getParameter(
953 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
954 &param, sizeof(param));
955
956 if (err != OK) return err;
957
958 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
959 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -0700960 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -0700961 supportedProfile, supportedLevel);
962
963 if (profile == supportedProfile &&
964 level == supportedLevel) {
965 profileLevel.mProfile = profile;
966 profileLevel.mLevel = level;
967 return OK;
968 }
969 }
970
971 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
972 profile, level);
973 return BAD_VALUE;
974}
975
James Dongc0ab2a62010-06-29 16:29:19 -0700976status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
977 int32_t iFramesInterval, frameRate, bitRate;
978 bool success = meta->findInt32(kKeyBitRate, &bitRate);
979 success = success && meta->findInt32(kKeySampleRate, &frameRate);
980 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
981 CHECK(success);
982 OMX_VIDEO_PARAM_H263TYPE h263type;
983 InitOMXParams(&h263type);
984 h263type.nPortIndex = kPortIndexOutput;
985
986 status_t err = mOMX->getParameter(
987 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
988 CHECK_EQ(err, OK);
989
990 h263type.nAllowedPictureTypes =
991 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
992
993 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
994 if (h263type.nPFrames == 0) {
995 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
996 }
997 h263type.nBFrames = 0;
998
James Dong81c929a2010-07-01 15:02:14 -0700999 // Check profile and level parameters
1000 CodecProfileLevel defaultProfileLevel, profileLevel;
1001 defaultProfileLevel.mProfile = OMX_VIDEO_H263ProfileBaseline;
1002 defaultProfileLevel.mLevel = OMX_VIDEO_H263Level45;
1003 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1004 if (err != OK) return err;
1005 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1006 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001007
1008 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1009 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1010 h263type.nPictureHeaderRepetition = 0;
1011 h263type.nGOBHeaderInterval = 0;
1012
1013 err = mOMX->setParameter(
1014 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1015 CHECK_EQ(err, OK);
1016
1017 CHECK_EQ(setupBitRate(bitRate), OK);
1018 CHECK_EQ(setupErrorCorrectionParameters(), OK);
1019
1020 return OK;
1021}
1022
James Dong1244eab2010-06-08 11:58:53 -07001023status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1024 int32_t iFramesInterval, frameRate, bitRate;
1025 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1026 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1027 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1028 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001029 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1030 InitOMXParams(&mpeg4type);
1031 mpeg4type.nPortIndex = kPortIndexOutput;
1032
1033 status_t err = mOMX->getParameter(
1034 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1035 CHECK_EQ(err, OK);
1036
1037 mpeg4type.nSliceHeaderSpacing = 0;
1038 mpeg4type.bSVH = OMX_FALSE;
1039 mpeg4type.bGov = OMX_FALSE;
1040
1041 mpeg4type.nAllowedPictureTypes =
1042 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1043
James Dong1244eab2010-06-08 11:58:53 -07001044 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1045 if (mpeg4type.nPFrames == 0) {
1046 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1047 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001048 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001049 mpeg4type.nIDCVLCThreshold = 0;
1050 mpeg4type.bACPred = OMX_TRUE;
1051 mpeg4type.nMaxPacketSize = 256;
1052 mpeg4type.nTimeIncRes = 1000;
1053 mpeg4type.nHeaderExtension = 0;
1054 mpeg4type.bReversibleVLC = OMX_FALSE;
1055
James Dong81c929a2010-07-01 15:02:14 -07001056 // Check profile and level parameters
1057 CodecProfileLevel defaultProfileLevel, profileLevel;
1058 defaultProfileLevel.mProfile = OMX_VIDEO_MPEG4ProfileSimple;
1059 defaultProfileLevel.mLevel = OMX_VIDEO_MPEG4Level2;
1060 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1061 if (err != OK) return err;
1062 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1063 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001064
1065 err = mOMX->setParameter(
1066 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1067 CHECK_EQ(err, OK);
1068
James Dongc0ab2a62010-06-29 16:29:19 -07001069 CHECK_EQ(setupBitRate(bitRate), OK);
1070 CHECK_EQ(setupErrorCorrectionParameters(), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001071
1072 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001073}
1074
James Dong1244eab2010-06-08 11:58:53 -07001075status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1076 int32_t iFramesInterval, frameRate, bitRate;
1077 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1078 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1079 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1080 CHECK(success);
1081
Andreas Huberea6a38c2009-11-16 15:43:38 -08001082 OMX_VIDEO_PARAM_AVCTYPE h264type;
1083 InitOMXParams(&h264type);
1084 h264type.nPortIndex = kPortIndexOutput;
1085
1086 status_t err = mOMX->getParameter(
1087 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1088 CHECK_EQ(err, OK);
1089
1090 h264type.nAllowedPictureTypes =
1091 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1092
1093 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001094 h264type.nBFrames = 0; // No B frames support yet
1095 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1096 if (h264type.nPFrames == 0) {
1097 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1098 }
James Dong81c929a2010-07-01 15:02:14 -07001099
1100 // Check profile and level parameters
1101 CodecProfileLevel defaultProfileLevel, profileLevel;
1102 defaultProfileLevel.mProfile = h264type.eProfile;
1103 defaultProfileLevel.mLevel = h264type.eLevel;
1104 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1105 if (err != OK) return err;
1106 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1107 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1108
1109 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1110 h264type.bUseHadamard = OMX_TRUE;
1111 h264type.nRefFrames = 1;
1112 h264type.nRefIdx10ActiveMinus1 = 0;
1113 h264type.nRefIdx11ActiveMinus1 = 0;
1114 h264type.bEntropyCodingCABAC = OMX_FALSE;
1115 h264type.bWeightedPPrediction = OMX_FALSE;
1116 h264type.bconstIpred = OMX_FALSE;
1117 h264type.bDirect8x8Inference = OMX_FALSE;
1118 h264type.bDirectSpatialTemporal = OMX_FALSE;
1119 h264type.nCabacInitIdc = 0;
1120 }
1121
1122 if (h264type.nBFrames != 0) {
1123 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1124 }
1125
Andreas Huberea6a38c2009-11-16 15:43:38 -08001126 h264type.bEnableUEP = OMX_FALSE;
1127 h264type.bEnableFMO = OMX_FALSE;
1128 h264type.bEnableASO = OMX_FALSE;
1129 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001130 h264type.bFrameMBsOnly = OMX_TRUE;
1131 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001132 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1133
1134 err = mOMX->setParameter(
1135 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1136 CHECK_EQ(err, OK);
1137
James Dongc0ab2a62010-06-29 16:29:19 -07001138 CHECK_EQ(setupBitRate(bitRate), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001139
1140 return OK;
1141}
1142
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001143status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001144 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001145 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001146
Andreas Huberbe06d262009-08-14 14:37:10 -07001147 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001148 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001149 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001150 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001151 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001152 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001153 compressionFormat = OMX_VIDEO_CodingH263;
1154 } else {
1155 LOGE("Not a supported video mime type: %s", mime);
1156 CHECK(!"Should not be here. Not a supported video mime type.");
1157 }
1158
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001159 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001160 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1161
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001162 if (err != OK) {
1163 return err;
1164 }
1165
Andreas Huberbe06d262009-08-14 14:37:10 -07001166#if 1
1167 {
1168 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001169 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001170 format.nPortIndex = kPortIndexOutput;
1171 format.nIndex = 0;
1172
Andreas Huber784202e2009-10-15 13:46:54 -07001173 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001174 mNode, OMX_IndexParamVideoPortFormat,
1175 &format, sizeof(format));
1176 CHECK_EQ(err, OK);
1177 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1178
1179 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1180
1181 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1182 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1183 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1184 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1185
Andreas Huber784202e2009-10-15 13:46:54 -07001186 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001187 mNode, OMX_IndexParamVideoPortFormat,
1188 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001189
1190 if (err != OK) {
1191 return err;
1192 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001193 }
1194#endif
1195
1196 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001197 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001198 def.nPortIndex = kPortIndexInput;
1199
Andreas Huber4c483422009-09-02 16:05:36 -07001200 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1201
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001202 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001203 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1204
1205 CHECK_EQ(err, OK);
1206
1207#if 1
1208 // XXX Need a (much) better heuristic to compute input buffer sizes.
1209 const size_t X = 64 * 1024;
1210 if (def.nBufferSize < X) {
1211 def.nBufferSize = X;
1212 }
1213#endif
1214
1215 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1216
1217 video_def->nFrameWidth = width;
1218 video_def->nFrameHeight = height;
1219
Andreas Huberb482ce82009-10-29 12:02:48 -07001220 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001221 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1222
Andreas Huber784202e2009-10-15 13:46:54 -07001223 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001224 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001225
1226 if (err != OK) {
1227 return err;
1228 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001229
1230 ////////////////////////////////////////////////////////////////////////////
1231
Andreas Huber4c483422009-09-02 16:05:36 -07001232 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001233 def.nPortIndex = kPortIndexOutput;
1234
Andreas Huber784202e2009-10-15 13:46:54 -07001235 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001236 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1237 CHECK_EQ(err, OK);
1238 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1239
1240#if 0
1241 def.nBufferSize =
1242 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1243#endif
1244
1245 video_def->nFrameWidth = width;
1246 video_def->nFrameHeight = height;
1247
Andreas Huber784202e2009-10-15 13:46:54 -07001248 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001249 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001250
1251 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001252}
1253
Andreas Huberbe06d262009-08-14 14:37:10 -07001254OMXCodec::OMXCodec(
1255 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001256 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001257 const char *mime,
1258 const char *componentName,
1259 const sp<MediaSource> &source)
1260 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001261 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001262 mNode(node),
1263 mQuirks(quirks),
1264 mIsEncoder(isEncoder),
1265 mMIME(strdup(mime)),
1266 mComponentName(strdup(componentName)),
1267 mSource(source),
1268 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001269 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001270 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001271 mSignalledEOS(false),
1272 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001273 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001274 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001275 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1276 mTargetTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001277 mLeftOverBuffer(NULL),
1278 mPaused(false) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001279 mPortStatus[kPortIndexInput] = ENABLED;
1280 mPortStatus[kPortIndexOutput] = ENABLED;
1281
Andreas Huber4c483422009-09-02 16:05:36 -07001282 setComponentRole();
1283}
1284
Andreas Hubere6c40962009-09-10 14:13:30 -07001285// static
1286void OMXCodec::setComponentRole(
1287 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1288 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001289 struct MimeToRole {
1290 const char *mime;
1291 const char *decoderRole;
1292 const char *encoderRole;
1293 };
1294
1295 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001296 { MEDIA_MIMETYPE_AUDIO_MPEG,
1297 "audio_decoder.mp3", "audio_encoder.mp3" },
1298 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1299 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1300 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1301 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1302 { MEDIA_MIMETYPE_AUDIO_AAC,
1303 "audio_decoder.aac", "audio_encoder.aac" },
1304 { MEDIA_MIMETYPE_VIDEO_AVC,
1305 "video_decoder.avc", "video_encoder.avc" },
1306 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1307 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1308 { MEDIA_MIMETYPE_VIDEO_H263,
1309 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001310 };
1311
1312 static const size_t kNumMimeToRole =
1313 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1314
1315 size_t i;
1316 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001317 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001318 break;
1319 }
1320 }
1321
1322 if (i == kNumMimeToRole) {
1323 return;
1324 }
1325
1326 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001327 isEncoder ? kMimeToRole[i].encoderRole
1328 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001329
1330 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001331 OMX_PARAM_COMPONENTROLETYPE roleParams;
1332 InitOMXParams(&roleParams);
1333
1334 strncpy((char *)roleParams.cRole,
1335 role, OMX_MAX_STRINGNAME_SIZE - 1);
1336
1337 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1338
Andreas Huber784202e2009-10-15 13:46:54 -07001339 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001340 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001341 &roleParams, sizeof(roleParams));
1342
1343 if (err != OK) {
1344 LOGW("Failed to set standard component role '%s'.", role);
1345 }
1346 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001347}
1348
Andreas Hubere6c40962009-09-10 14:13:30 -07001349void OMXCodec::setComponentRole() {
1350 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1351}
1352
Andreas Huberbe06d262009-08-14 14:37:10 -07001353OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001354 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001355
Andreas Huber784202e2009-10-15 13:46:54 -07001356 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001357 CHECK_EQ(err, OK);
1358
1359 mNode = NULL;
1360 setState(DEAD);
1361
1362 clearCodecSpecificData();
1363
1364 free(mComponentName);
1365 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001366
Andreas Huberbe06d262009-08-14 14:37:10 -07001367 free(mMIME);
1368 mMIME = NULL;
1369}
1370
1371status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001372 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001373
1374 CHECK_EQ(mState, LOADED);
1375
1376 status_t err;
1377 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001378 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001379 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001380 setState(LOADED_TO_IDLE);
1381 }
1382
1383 err = allocateBuffers();
1384 CHECK_EQ(err, OK);
1385
1386 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001387 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001388 CHECK_EQ(err, OK);
1389
1390 setState(LOADED_TO_IDLE);
1391 }
1392
1393 while (mState != EXECUTING && mState != ERROR) {
1394 mAsyncCompletion.wait(mLock);
1395 }
1396
1397 return mState == ERROR ? UNKNOWN_ERROR : OK;
1398}
1399
1400// static
1401bool OMXCodec::isIntermediateState(State state) {
1402 return state == LOADED_TO_IDLE
1403 || state == IDLE_TO_EXECUTING
1404 || state == EXECUTING_TO_IDLE
1405 || state == IDLE_TO_LOADED
1406 || state == RECONFIGURING;
1407}
1408
1409status_t OMXCodec::allocateBuffers() {
1410 status_t err = allocateBuffersOnPort(kPortIndexInput);
1411
1412 if (err != OK) {
1413 return err;
1414 }
1415
1416 return allocateBuffersOnPort(kPortIndexOutput);
1417}
1418
1419status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1420 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001421 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001422 def.nPortIndex = portIndex;
1423
Andreas Huber784202e2009-10-15 13:46:54 -07001424 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001425 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1426
1427 if (err != OK) {
1428 return err;
1429 }
1430
Andreas Huber5c0a9132009-08-20 11:16:40 -07001431 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001432 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001433
Andreas Huberbe06d262009-08-14 14:37:10 -07001434 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001435 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001436 CHECK(mem.get() != NULL);
1437
Andreas Huberc712b9f2010-01-20 15:05:46 -08001438 BufferInfo info;
1439 info.mData = NULL;
1440 info.mSize = def.nBufferSize;
1441
Andreas Huberbe06d262009-08-14 14:37:10 -07001442 IOMX::buffer_id buffer;
1443 if (portIndex == kPortIndexInput
1444 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001445 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001446 mem.clear();
1447
Andreas Huberf1fe0642010-01-15 15:28:19 -08001448 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001449 mNode, portIndex, def.nBufferSize, &buffer,
1450 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001451 } else {
1452 err = mOMX->allocateBufferWithBackup(
1453 mNode, portIndex, mem, &buffer);
1454 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001455 } else if (portIndex == kPortIndexOutput
1456 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001457 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001458 mem.clear();
1459
Andreas Huberf1fe0642010-01-15 15:28:19 -08001460 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001461 mNode, portIndex, def.nBufferSize, &buffer,
1462 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001463 } else {
1464 err = mOMX->allocateBufferWithBackup(
1465 mNode, portIndex, mem, &buffer);
1466 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001467 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001468 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001469 }
1470
1471 if (err != OK) {
1472 LOGE("allocate_buffer_with_backup failed");
1473 return err;
1474 }
1475
Andreas Huberc712b9f2010-01-20 15:05:46 -08001476 if (mem != NULL) {
1477 info.mData = mem->pointer();
1478 }
1479
Andreas Huberbe06d262009-08-14 14:37:10 -07001480 info.mBuffer = buffer;
1481 info.mOwnedByComponent = false;
1482 info.mMem = mem;
1483 info.mMediaBuffer = NULL;
1484
1485 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001486 if (!(mOMXLivesLocally
1487 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1488 && (mQuirks & kDefersOutputBufferAllocation))) {
1489 // If the node does not fill in the buffer ptr at this time,
1490 // we will defer creating the MediaBuffer until receiving
1491 // the first FILL_BUFFER_DONE notification instead.
1492 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1493 info.mMediaBuffer->setObserver(this);
1494 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001495 }
1496
1497 mPortBuffers[portIndex].push(info);
1498
Andreas Huber4c483422009-09-02 16:05:36 -07001499 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001500 portIndex == kPortIndexInput ? "input" : "output");
1501 }
1502
Andreas Huber2ea14e22009-12-16 09:30:55 -08001503 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001504
1505 return OK;
1506}
1507
1508void OMXCodec::on_message(const omx_message &msg) {
1509 Mutex::Autolock autoLock(mLock);
1510
1511 switch (msg.type) {
1512 case omx_message::EVENT:
1513 {
1514 onEvent(
1515 msg.u.event_data.event, msg.u.event_data.data1,
1516 msg.u.event_data.data2);
1517
1518 break;
1519 }
1520
1521 case omx_message::EMPTY_BUFFER_DONE:
1522 {
1523 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1524
Andreas Huber4c483422009-09-02 16:05:36 -07001525 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001526
1527 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1528 size_t i = 0;
1529 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1530 ++i;
1531 }
1532
1533 CHECK(i < buffers->size());
1534 if (!(*buffers)[i].mOwnedByComponent) {
1535 LOGW("We already own input buffer %p, yet received "
1536 "an EMPTY_BUFFER_DONE.", buffer);
1537 }
1538
1539 buffers->editItemAt(i).mOwnedByComponent = false;
1540
1541 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001542 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001543
1544 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001545 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001546 CHECK_EQ(err, OK);
1547
1548 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001549 } else if (mState != ERROR
1550 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001551 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1552 drainInputBuffer(&buffers->editItemAt(i));
1553 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001554 break;
1555 }
1556
1557 case omx_message::FILL_BUFFER_DONE:
1558 {
1559 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1560 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1561
Andreas Huber2ea14e22009-12-16 09:30:55 -08001562 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001563 buffer,
1564 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001565 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001566 msg.u.extended_buffer_data.timestamp,
1567 msg.u.extended_buffer_data.timestamp / 1E6);
1568
1569 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1570 size_t i = 0;
1571 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1572 ++i;
1573 }
1574
1575 CHECK(i < buffers->size());
1576 BufferInfo *info = &buffers->editItemAt(i);
1577
1578 if (!info->mOwnedByComponent) {
1579 LOGW("We already own output buffer %p, yet received "
1580 "a FILL_BUFFER_DONE.", buffer);
1581 }
1582
1583 info->mOwnedByComponent = false;
1584
1585 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001586 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001587
1588 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001589 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001590 CHECK_EQ(err, OK);
1591
1592 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001593#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001594 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1595 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001596 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001597 mNoMoreOutputData = true;
1598 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001599#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001600 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1601 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001602
Andreas Huber52733b82010-01-25 10:41:35 -08001603 if (info->mMediaBuffer == NULL) {
1604 CHECK(mOMXLivesLocally);
1605 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1606 CHECK(mQuirks & kDefersOutputBufferAllocation);
1607
1608 // The qcom video decoders on Nexus don't actually allocate
1609 // output buffer memory on a call to OMX_AllocateBuffer
1610 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1611 // structure is only filled in later.
1612
1613 info->mMediaBuffer = new MediaBuffer(
1614 msg.u.extended_buffer_data.data_ptr,
1615 info->mSize);
1616 info->mMediaBuffer->setObserver(this);
1617 }
1618
Andreas Huberbe06d262009-08-14 14:37:10 -07001619 MediaBuffer *buffer = info->mMediaBuffer;
1620
1621 buffer->set_range(
1622 msg.u.extended_buffer_data.range_offset,
1623 msg.u.extended_buffer_data.range_length);
1624
1625 buffer->meta_data()->clear();
1626
Andreas Huberfa8de752009-10-08 10:07:49 -07001627 buffer->meta_data()->setInt64(
1628 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001629
1630 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1631 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1632 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001633 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1634 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1635 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001636
1637 buffer->meta_data()->setPointer(
1638 kKeyPlatformPrivate,
1639 msg.u.extended_buffer_data.platform_private);
1640
1641 buffer->meta_data()->setPointer(
1642 kKeyBufferID,
1643 msg.u.extended_buffer_data.buffer);
1644
Andreas Huber2ea14e22009-12-16 09:30:55 -08001645 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1646 CODEC_LOGV("No more output data.");
1647 mNoMoreOutputData = true;
1648 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07001649
1650 if (mTargetTimeUs >= 0) {
1651 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
1652
1653 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
1654 CODEC_LOGV(
1655 "skipping output buffer at timestamp %lld us",
1656 msg.u.extended_buffer_data.timestamp);
1657
1658 fillOutputBuffer(info);
1659 break;
1660 }
1661
1662 CODEC_LOGV(
1663 "returning output buffer at target timestamp "
1664 "%lld us",
1665 msg.u.extended_buffer_data.timestamp);
1666
1667 mTargetTimeUs = -1;
1668 }
1669
1670 mFilledBuffers.push_back(i);
1671 mBufferFilled.signal();
Andreas Huberbe06d262009-08-14 14:37:10 -07001672 }
1673
1674 break;
1675 }
1676
1677 default:
1678 {
1679 CHECK(!"should not be here.");
1680 break;
1681 }
1682 }
1683}
1684
1685void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1686 switch (event) {
1687 case OMX_EventCmdComplete:
1688 {
1689 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1690 break;
1691 }
1692
1693 case OMX_EventError:
1694 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001695 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001696
1697 setState(ERROR);
1698 break;
1699 }
1700
1701 case OMX_EventPortSettingsChanged:
1702 {
1703 onPortSettingsChanged(data1);
1704 break;
1705 }
1706
Andreas Huber2ea14e22009-12-16 09:30:55 -08001707#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001708 case OMX_EventBufferFlag:
1709 {
Andreas Huber4c483422009-09-02 16:05:36 -07001710 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001711
1712 if (data1 == kPortIndexOutput) {
1713 mNoMoreOutputData = true;
1714 }
1715 break;
1716 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001717#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001718
1719 default:
1720 {
Andreas Huber4c483422009-09-02 16:05:36 -07001721 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001722 break;
1723 }
1724 }
1725}
1726
Andreas Huberb1678602009-10-19 13:06:40 -07001727// Has the format changed in any way that the client would have to be aware of?
1728static bool formatHasNotablyChanged(
1729 const sp<MetaData> &from, const sp<MetaData> &to) {
1730 if (from.get() == NULL && to.get() == NULL) {
1731 return false;
1732 }
1733
Andreas Huberf68c1682009-10-21 14:01:30 -07001734 if ((from.get() == NULL && to.get() != NULL)
1735 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001736 return true;
1737 }
1738
1739 const char *mime_from, *mime_to;
1740 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1741 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1742
1743 if (strcasecmp(mime_from, mime_to)) {
1744 return true;
1745 }
1746
1747 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1748 int32_t colorFormat_from, colorFormat_to;
1749 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1750 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1751
1752 if (colorFormat_from != colorFormat_to) {
1753 return true;
1754 }
1755
1756 int32_t width_from, width_to;
1757 CHECK(from->findInt32(kKeyWidth, &width_from));
1758 CHECK(to->findInt32(kKeyWidth, &width_to));
1759
1760 if (width_from != width_to) {
1761 return true;
1762 }
1763
1764 int32_t height_from, height_to;
1765 CHECK(from->findInt32(kKeyHeight, &height_from));
1766 CHECK(to->findInt32(kKeyHeight, &height_to));
1767
1768 if (height_from != height_to) {
1769 return true;
1770 }
1771 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1772 int32_t numChannels_from, numChannels_to;
1773 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1774 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1775
1776 if (numChannels_from != numChannels_to) {
1777 return true;
1778 }
1779
1780 int32_t sampleRate_from, sampleRate_to;
1781 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1782 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1783
1784 if (sampleRate_from != sampleRate_to) {
1785 return true;
1786 }
1787 }
1788
1789 return false;
1790}
1791
Andreas Huberbe06d262009-08-14 14:37:10 -07001792void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1793 switch (cmd) {
1794 case OMX_CommandStateSet:
1795 {
1796 onStateChange((OMX_STATETYPE)data);
1797 break;
1798 }
1799
1800 case OMX_CommandPortDisable:
1801 {
1802 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001803 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001804
1805 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1806 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1807 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1808
1809 mPortStatus[portIndex] = DISABLED;
1810
1811 if (mState == RECONFIGURING) {
1812 CHECK_EQ(portIndex, kPortIndexOutput);
1813
Andreas Huberb1678602009-10-19 13:06:40 -07001814 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001815 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001816
1817 // Don't notify clients if the output port settings change
1818 // wasn't of importance to them, i.e. it may be that just the
1819 // number of buffers has changed and nothing else.
1820 mOutputPortSettingsHaveChanged =
1821 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001822
Andreas Huberbe06d262009-08-14 14:37:10 -07001823 enablePortAsync(portIndex);
1824
1825 status_t err = allocateBuffersOnPort(portIndex);
1826 CHECK_EQ(err, OK);
1827 }
1828 break;
1829 }
1830
1831 case OMX_CommandPortEnable:
1832 {
1833 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001834 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001835
1836 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1837 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1838
1839 mPortStatus[portIndex] = ENABLED;
1840
1841 if (mState == RECONFIGURING) {
1842 CHECK_EQ(portIndex, kPortIndexOutput);
1843
1844 setState(EXECUTING);
1845
1846 fillOutputBuffers();
1847 }
1848 break;
1849 }
1850
1851 case OMX_CommandFlush:
1852 {
1853 OMX_U32 portIndex = data;
1854
Andreas Huber4c483422009-09-02 16:05:36 -07001855 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001856
1857 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1858 mPortStatus[portIndex] = ENABLED;
1859
1860 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1861 mPortBuffers[portIndex].size());
1862
1863 if (mState == RECONFIGURING) {
1864 CHECK_EQ(portIndex, kPortIndexOutput);
1865
1866 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001867 } else if (mState == EXECUTING_TO_IDLE) {
1868 if (mPortStatus[kPortIndexInput] == ENABLED
1869 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001870 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001871 "transition from EXECUTING to IDLE.");
1872
1873 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1874 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1875
1876 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001877 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001878 CHECK_EQ(err, OK);
1879 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001880 } else {
1881 // We're flushing both ports in preparation for seeking.
1882
1883 if (mPortStatus[kPortIndexInput] == ENABLED
1884 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001885 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001886 " seek-time.");
1887
Andreas Huber1f24b302010-06-10 11:12:39 -07001888 // We implicitly resume pulling on our upstream source.
1889 mPaused = false;
1890
Andreas Huberbe06d262009-08-14 14:37:10 -07001891 drainInputBuffers();
1892 fillOutputBuffers();
1893 }
1894 }
1895
1896 break;
1897 }
1898
1899 default:
1900 {
Andreas Huber4c483422009-09-02 16:05:36 -07001901 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001902 break;
1903 }
1904 }
1905}
1906
1907void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001908 CODEC_LOGV("onStateChange %d", newState);
1909
Andreas Huberbe06d262009-08-14 14:37:10 -07001910 switch (newState) {
1911 case OMX_StateIdle:
1912 {
Andreas Huber4c483422009-09-02 16:05:36 -07001913 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001914 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001915 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001916 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1917
1918 CHECK_EQ(err, OK);
1919
1920 setState(IDLE_TO_EXECUTING);
1921 } else {
1922 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1923
1924 CHECK_EQ(
1925 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1926 mPortBuffers[kPortIndexInput].size());
1927
1928 CHECK_EQ(
1929 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1930 mPortBuffers[kPortIndexOutput].size());
1931
Andreas Huber784202e2009-10-15 13:46:54 -07001932 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001933 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1934
1935 CHECK_EQ(err, OK);
1936
1937 err = freeBuffersOnPort(kPortIndexInput);
1938 CHECK_EQ(err, OK);
1939
1940 err = freeBuffersOnPort(kPortIndexOutput);
1941 CHECK_EQ(err, OK);
1942
1943 mPortStatus[kPortIndexInput] = ENABLED;
1944 mPortStatus[kPortIndexOutput] = ENABLED;
1945
1946 setState(IDLE_TO_LOADED);
1947 }
1948 break;
1949 }
1950
1951 case OMX_StateExecuting:
1952 {
1953 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1954
Andreas Huber4c483422009-09-02 16:05:36 -07001955 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001956
1957 setState(EXECUTING);
1958
Andreas Huber42978e52009-08-27 10:08:39 -07001959 // Buffers will be submitted to the component in the first
1960 // call to OMXCodec::read as mInitialBufferSubmit is true at
1961 // this point. This ensures that this on_message call returns,
1962 // releases the lock and ::init can notice the state change and
1963 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001964 break;
1965 }
1966
1967 case OMX_StateLoaded:
1968 {
1969 CHECK_EQ(mState, IDLE_TO_LOADED);
1970
Andreas Huber4c483422009-09-02 16:05:36 -07001971 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001972
1973 setState(LOADED);
1974 break;
1975 }
1976
Andreas Huberc712b9f2010-01-20 15:05:46 -08001977 case OMX_StateInvalid:
1978 {
1979 setState(ERROR);
1980 break;
1981 }
1982
Andreas Huberbe06d262009-08-14 14:37:10 -07001983 default:
1984 {
1985 CHECK(!"should not be here.");
1986 break;
1987 }
1988 }
1989}
1990
1991// static
1992size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1993 size_t n = 0;
1994 for (size_t i = 0; i < buffers.size(); ++i) {
1995 if (!buffers[i].mOwnedByComponent) {
1996 ++n;
1997 }
1998 }
1999
2000 return n;
2001}
2002
2003status_t OMXCodec::freeBuffersOnPort(
2004 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2005 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2006
2007 status_t stickyErr = OK;
2008
2009 for (size_t i = buffers->size(); i-- > 0;) {
2010 BufferInfo *info = &buffers->editItemAt(i);
2011
2012 if (onlyThoseWeOwn && info->mOwnedByComponent) {
2013 continue;
2014 }
2015
2016 CHECK_EQ(info->mOwnedByComponent, false);
2017
Andreas Huber92022852009-09-14 15:24:14 -07002018 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2019
Andreas Huberbe06d262009-08-14 14:37:10 -07002020 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002021 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07002022
2023 if (err != OK) {
2024 stickyErr = err;
2025 }
2026
2027 if (info->mMediaBuffer != NULL) {
2028 info->mMediaBuffer->setObserver(NULL);
2029
2030 // Make sure nobody but us owns this buffer at this point.
2031 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2032
2033 info->mMediaBuffer->release();
2034 }
2035
2036 buffers->removeAt(i);
2037 }
2038
2039 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2040
2041 return stickyErr;
2042}
2043
2044void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002045 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002046
2047 CHECK_EQ(mState, EXECUTING);
2048 CHECK_EQ(portIndex, kPortIndexOutput);
2049 setState(RECONFIGURING);
2050
2051 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002052 if (!flushPortAsync(portIndex)) {
2053 onCmdComplete(OMX_CommandFlush, portIndex);
2054 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002055 } else {
2056 disablePortAsync(portIndex);
2057 }
2058}
2059
Andreas Huber404cc412009-08-25 14:26:05 -07002060bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002061 CHECK(mState == EXECUTING || mState == RECONFIGURING
2062 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002063
Andreas Huber4c483422009-09-02 16:05:36 -07002064 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002065 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2066 mPortBuffers[portIndex].size());
2067
Andreas Huberbe06d262009-08-14 14:37:10 -07002068 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2069 mPortStatus[portIndex] = SHUTTING_DOWN;
2070
Andreas Huber404cc412009-08-25 14:26:05 -07002071 if ((mQuirks & kRequiresFlushCompleteEmulation)
2072 && countBuffersWeOwn(mPortBuffers[portIndex])
2073 == mPortBuffers[portIndex].size()) {
2074 // No flush is necessary and this component fails to send a
2075 // flush-complete event in this case.
2076
2077 return false;
2078 }
2079
Andreas Huberbe06d262009-08-14 14:37:10 -07002080 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002081 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002082 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002083
2084 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002085}
2086
2087void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2088 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2089
2090 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2091 mPortStatus[portIndex] = DISABLING;
2092
2093 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002094 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002095 CHECK_EQ(err, OK);
2096
2097 freeBuffersOnPort(portIndex, true);
2098}
2099
2100void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2101 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2102
2103 CHECK_EQ(mPortStatus[portIndex], DISABLED);
2104 mPortStatus[portIndex] = ENABLING;
2105
2106 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002107 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002108 CHECK_EQ(err, OK);
2109}
2110
2111void OMXCodec::fillOutputBuffers() {
2112 CHECK_EQ(mState, EXECUTING);
2113
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002114 // This is a workaround for some decoders not properly reporting
2115 // end-of-output-stream. If we own all input buffers and also own
2116 // all output buffers and we already signalled end-of-input-stream,
2117 // the end-of-output-stream is implied.
2118 if (mSignalledEOS
2119 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2120 == mPortBuffers[kPortIndexInput].size()
2121 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2122 == mPortBuffers[kPortIndexOutput].size()) {
2123 mNoMoreOutputData = true;
2124 mBufferFilled.signal();
2125
2126 return;
2127 }
2128
Andreas Huberbe06d262009-08-14 14:37:10 -07002129 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2130 for (size_t i = 0; i < buffers->size(); ++i) {
2131 fillOutputBuffer(&buffers->editItemAt(i));
2132 }
2133}
2134
2135void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002136 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002137
2138 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2139 for (size_t i = 0; i < buffers->size(); ++i) {
2140 drainInputBuffer(&buffers->editItemAt(i));
2141 }
2142}
2143
2144void OMXCodec::drainInputBuffer(BufferInfo *info) {
2145 CHECK_EQ(info->mOwnedByComponent, false);
2146
2147 if (mSignalledEOS) {
2148 return;
2149 }
2150
2151 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2152 const CodecSpecificData *specific =
2153 mCodecSpecificData[mCodecSpecificDataIndex];
2154
2155 size_t size = specific->mSize;
2156
Andreas Hubere6c40962009-09-10 14:13:30 -07002157 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002158 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002159 static const uint8_t kNALStartCode[4] =
2160 { 0x00, 0x00, 0x00, 0x01 };
2161
Andreas Huberc712b9f2010-01-20 15:05:46 -08002162 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002163
2164 size += 4;
2165
Andreas Huberc712b9f2010-01-20 15:05:46 -08002166 memcpy(info->mData, kNALStartCode, 4);
2167 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002168 specific->mData, specific->mSize);
2169 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002170 CHECK(info->mSize >= specific->mSize);
2171 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002172 }
2173
Andreas Huber2ea14e22009-12-16 09:30:55 -08002174 mNoMoreOutputData = false;
2175
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002176 CODEC_LOGV("calling emptyBuffer with codec specific data");
2177
Andreas Huber784202e2009-10-15 13:46:54 -07002178 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002179 mNode, info->mBuffer, 0, size,
2180 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2181 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002182 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002183
2184 info->mOwnedByComponent = true;
2185
2186 ++mCodecSpecificDataIndex;
2187 return;
2188 }
2189
Andreas Huber1f24b302010-06-10 11:12:39 -07002190 if (mPaused) {
2191 return;
2192 }
2193
Andreas Huberbe06d262009-08-14 14:37:10 -07002194 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002195
Andreas Hubera4357ad2010-04-02 12:49:54 -07002196 bool signalEOS = false;
2197 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002198
Andreas Hubera4357ad2010-04-02 12:49:54 -07002199 size_t offset = 0;
2200 int32_t n = 0;
2201 for (;;) {
2202 MediaBuffer *srcBuffer;
2203 if (mSeekTimeUs >= 0) {
2204 if (mLeftOverBuffer) {
2205 mLeftOverBuffer->release();
2206 mLeftOverBuffer = NULL;
2207 }
2208
2209 MediaSource::ReadOptions options;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002210 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002211
2212 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002213 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002214 mBufferFilled.signal();
2215
2216 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002217
2218 if (err == OK) {
2219 int64_t targetTimeUs;
2220 if (srcBuffer->meta_data()->findInt64(
2221 kKeyTargetTime, &targetTimeUs)
2222 && targetTimeUs >= 0) {
2223 mTargetTimeUs = targetTimeUs;
2224 } else {
2225 mTargetTimeUs = -1;
2226 }
2227 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002228 } else if (mLeftOverBuffer) {
2229 srcBuffer = mLeftOverBuffer;
2230 mLeftOverBuffer = NULL;
2231
2232 err = OK;
2233 } else {
2234 err = mSource->read(&srcBuffer);
2235 }
2236
2237 if (err != OK) {
2238 signalEOS = true;
2239 mFinalStatus = err;
2240 mSignalledEOS = true;
2241 break;
2242 }
2243
2244 size_t remainingBytes = info->mSize - offset;
2245
2246 if (srcBuffer->range_length() > remainingBytes) {
2247 if (offset == 0) {
2248 CODEC_LOGE(
2249 "Codec's input buffers are too small to accomodate "
2250 "buffer read from source (info->mSize = %d, srcLength = %d)",
2251 info->mSize, srcBuffer->range_length());
2252
2253 srcBuffer->release();
2254 srcBuffer = NULL;
2255
2256 setState(ERROR);
2257 return;
2258 }
2259
2260 mLeftOverBuffer = srcBuffer;
2261 break;
2262 }
2263
James Dong4f501f02010-06-07 14:41:41 -07002264 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2265 CHECK(mOMXLivesLocally && offset == 0);
2266 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2267 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2268 } else {
2269 memcpy((uint8_t *)info->mData + offset,
2270 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2271 srcBuffer->range_length());
2272 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002273
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002274 int64_t lastBufferTimeUs;
2275 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002276 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002277
Andreas Hubera4357ad2010-04-02 12:49:54 -07002278 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002279 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002280 }
2281
2282 offset += srcBuffer->range_length();
2283
2284 srcBuffer->release();
2285 srcBuffer = NULL;
2286
2287 ++n;
2288
2289 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2290 break;
2291 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002292
2293 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2294
2295 if (coalescedDurationUs > 250000ll) {
2296 // Don't coalesce more than 250ms worth of encoded data at once.
2297 break;
2298 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002299 }
2300
2301 if (n > 1) {
2302 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002303 }
2304
2305 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002306
Andreas Hubera4357ad2010-04-02 12:49:54 -07002307 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002308 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002309 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002310 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002311 }
2312
Andreas Hubera4357ad2010-04-02 12:49:54 -07002313 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2314 "timestamp %lld us (%.2f secs)",
2315 info->mBuffer, offset,
2316 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002317
Andreas Huber784202e2009-10-15 13:46:54 -07002318 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002319 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002320 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002321
2322 if (err != OK) {
2323 setState(ERROR);
2324 return;
2325 }
2326
2327 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002328
2329 // This component does not ever signal the EOS flag on output buffers,
2330 // Thanks for nothing.
2331 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2332 mNoMoreOutputData = true;
2333 mBufferFilled.signal();
2334 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002335}
2336
2337void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2338 CHECK_EQ(info->mOwnedByComponent, false);
2339
Andreas Huber404cc412009-08-25 14:26:05 -07002340 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002341 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002342 "calling fillOutputBuffer");
2343 return;
2344 }
2345
Andreas Huber4c483422009-09-02 16:05:36 -07002346 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002347 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002348
2349 if (err != OK) {
2350 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2351
2352 setState(ERROR);
2353 return;
2354 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002355
2356 info->mOwnedByComponent = true;
2357}
2358
2359void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2360 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2361 for (size_t i = 0; i < buffers->size(); ++i) {
2362 if ((*buffers)[i].mBuffer == buffer) {
2363 drainInputBuffer(&buffers->editItemAt(i));
2364 return;
2365 }
2366 }
2367
2368 CHECK(!"should not be here.");
2369}
2370
2371void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2372 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2373 for (size_t i = 0; i < buffers->size(); ++i) {
2374 if ((*buffers)[i].mBuffer == buffer) {
2375 fillOutputBuffer(&buffers->editItemAt(i));
2376 return;
2377 }
2378 }
2379
2380 CHECK(!"should not be here.");
2381}
2382
2383void OMXCodec::setState(State newState) {
2384 mState = newState;
2385 mAsyncCompletion.signal();
2386
2387 // This may cause some spurious wakeups but is necessary to
2388 // unblock the reader if we enter ERROR state.
2389 mBufferFilled.signal();
2390}
2391
Andreas Huberda050cf22009-09-02 14:01:43 -07002392void OMXCodec::setRawAudioFormat(
2393 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002394
2395 // port definition
2396 OMX_PARAM_PORTDEFINITIONTYPE def;
2397 InitOMXParams(&def);
2398 def.nPortIndex = portIndex;
2399 status_t err = mOMX->getParameter(
2400 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2401 CHECK_EQ(err, OK);
2402 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2403 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2404 &def, sizeof(def)), OK);
2405
2406 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002407 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002408 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002409 pcmParams.nPortIndex = portIndex;
2410
James Dongabed93a2010-04-22 17:27:04 -07002411 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002412 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2413
2414 CHECK_EQ(err, OK);
2415
2416 pcmParams.nChannels = numChannels;
2417 pcmParams.eNumData = OMX_NumericalDataSigned;
2418 pcmParams.bInterleaved = OMX_TRUE;
2419 pcmParams.nBitPerSample = 16;
2420 pcmParams.nSamplingRate = sampleRate;
2421 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2422
2423 if (numChannels == 1) {
2424 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2425 } else {
2426 CHECK_EQ(numChannels, 2);
2427
2428 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2429 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2430 }
2431
Andreas Huber784202e2009-10-15 13:46:54 -07002432 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002433 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2434
2435 CHECK_EQ(err, OK);
2436}
2437
James Dong17299ab2010-05-14 15:45:22 -07002438static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2439 if (isAMRWB) {
2440 if (bps <= 6600) {
2441 return OMX_AUDIO_AMRBandModeWB0;
2442 } else if (bps <= 8850) {
2443 return OMX_AUDIO_AMRBandModeWB1;
2444 } else if (bps <= 12650) {
2445 return OMX_AUDIO_AMRBandModeWB2;
2446 } else if (bps <= 14250) {
2447 return OMX_AUDIO_AMRBandModeWB3;
2448 } else if (bps <= 15850) {
2449 return OMX_AUDIO_AMRBandModeWB4;
2450 } else if (bps <= 18250) {
2451 return OMX_AUDIO_AMRBandModeWB5;
2452 } else if (bps <= 19850) {
2453 return OMX_AUDIO_AMRBandModeWB6;
2454 } else if (bps <= 23050) {
2455 return OMX_AUDIO_AMRBandModeWB7;
2456 }
2457
2458 // 23850 bps
2459 return OMX_AUDIO_AMRBandModeWB8;
2460 } else { // AMRNB
2461 if (bps <= 4750) {
2462 return OMX_AUDIO_AMRBandModeNB0;
2463 } else if (bps <= 5150) {
2464 return OMX_AUDIO_AMRBandModeNB1;
2465 } else if (bps <= 5900) {
2466 return OMX_AUDIO_AMRBandModeNB2;
2467 } else if (bps <= 6700) {
2468 return OMX_AUDIO_AMRBandModeNB3;
2469 } else if (bps <= 7400) {
2470 return OMX_AUDIO_AMRBandModeNB4;
2471 } else if (bps <= 7950) {
2472 return OMX_AUDIO_AMRBandModeNB5;
2473 } else if (bps <= 10200) {
2474 return OMX_AUDIO_AMRBandModeNB6;
2475 }
2476
2477 // 12200 bps
2478 return OMX_AUDIO_AMRBandModeNB7;
2479 }
2480}
2481
2482void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002483 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002484
Andreas Huber8768f2c2009-12-01 15:26:54 -08002485 OMX_AUDIO_PARAM_AMRTYPE def;
2486 InitOMXParams(&def);
2487 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002488
Andreas Huber8768f2c2009-12-01 15:26:54 -08002489 status_t err =
2490 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002491
Andreas Huber8768f2c2009-12-01 15:26:54 -08002492 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002493
Andreas Huber8768f2c2009-12-01 15:26:54 -08002494 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002495
James Dong17299ab2010-05-14 15:45:22 -07002496 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002497 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2498 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002499
2500 ////////////////////////
2501
2502 if (mIsEncoder) {
2503 sp<MetaData> format = mSource->getFormat();
2504 int32_t sampleRate;
2505 int32_t numChannels;
2506 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2507 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2508
2509 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2510 }
2511}
2512
James Dong17299ab2010-05-14 15:45:22 -07002513void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002514 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002515 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002516 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002517 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002518
2519 //////////////// output port ////////////////////
2520 // format
2521 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2522 format.nPortIndex = kPortIndexOutput;
2523 format.nIndex = 0;
2524 status_t err = OMX_ErrorNone;
2525 while (OMX_ErrorNone == err) {
2526 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2527 &format, sizeof(format)), OK);
2528 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2529 break;
2530 }
2531 format.nIndex++;
2532 }
2533 CHECK_EQ(OK, err);
2534 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2535 &format, sizeof(format)), OK);
2536
2537 // port definition
2538 OMX_PARAM_PORTDEFINITIONTYPE def;
2539 InitOMXParams(&def);
2540 def.nPortIndex = kPortIndexOutput;
2541 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2542 &def, sizeof(def)), OK);
2543 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2544 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2545 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2546 &def, sizeof(def)), OK);
2547
2548 // profile
2549 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2550 InitOMXParams(&profile);
2551 profile.nPortIndex = kPortIndexOutput;
2552 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2553 &profile, sizeof(profile)), OK);
2554 profile.nChannels = numChannels;
2555 profile.eChannelMode = (numChannels == 1?
2556 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2557 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002558 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002559 profile.nAudioBandWidth = 0;
2560 profile.nFrameLength = 0;
2561 profile.nAACtools = OMX_AUDIO_AACToolAll;
2562 profile.nAACERtools = OMX_AUDIO_AACERNone;
2563 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2564 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2565 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2566 &profile, sizeof(profile)), OK);
2567
Andreas Huberda050cf22009-09-02 14:01:43 -07002568 } else {
2569 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002570 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002571 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002572
Andreas Huber784202e2009-10-15 13:46:54 -07002573 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002574 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2575 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002576
Andreas Huberda050cf22009-09-02 14:01:43 -07002577 profile.nChannels = numChannels;
2578 profile.nSampleRate = sampleRate;
2579 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002580
Andreas Huber784202e2009-10-15 13:46:54 -07002581 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002582 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2583 CHECK_EQ(err, OK);
2584 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002585}
2586
2587void OMXCodec::setImageOutputFormat(
2588 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002589 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002590
2591#if 0
2592 OMX_INDEXTYPE index;
2593 status_t err = mOMX->get_extension_index(
2594 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2595 CHECK_EQ(err, OK);
2596
2597 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2598 CHECK_EQ(err, OK);
2599#endif
2600
2601 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002602 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002603 def.nPortIndex = kPortIndexOutput;
2604
Andreas Huber784202e2009-10-15 13:46:54 -07002605 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002606 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2607 CHECK_EQ(err, OK);
2608
2609 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2610
2611 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002612
Andreas Huberbe06d262009-08-14 14:37:10 -07002613 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2614 imageDef->eColorFormat = format;
2615 imageDef->nFrameWidth = width;
2616 imageDef->nFrameHeight = height;
2617
2618 switch (format) {
2619 case OMX_COLOR_FormatYUV420PackedPlanar:
2620 case OMX_COLOR_FormatYUV411Planar:
2621 {
2622 def.nBufferSize = (width * height * 3) / 2;
2623 break;
2624 }
2625
2626 case OMX_COLOR_FormatCbYCrY:
2627 {
2628 def.nBufferSize = width * height * 2;
2629 break;
2630 }
2631
2632 case OMX_COLOR_Format32bitARGB8888:
2633 {
2634 def.nBufferSize = width * height * 4;
2635 break;
2636 }
2637
Andreas Huber201511c2009-09-08 14:01:44 -07002638 case OMX_COLOR_Format16bitARGB4444:
2639 case OMX_COLOR_Format16bitARGB1555:
2640 case OMX_COLOR_Format16bitRGB565:
2641 case OMX_COLOR_Format16bitBGR565:
2642 {
2643 def.nBufferSize = width * height * 2;
2644 break;
2645 }
2646
Andreas Huberbe06d262009-08-14 14:37:10 -07002647 default:
2648 CHECK(!"Should not be here. Unknown color format.");
2649 break;
2650 }
2651
Andreas Huber5c0a9132009-08-20 11:16:40 -07002652 def.nBufferCountActual = def.nBufferCountMin;
2653
Andreas Huber784202e2009-10-15 13:46:54 -07002654 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002655 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2656 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002657}
Andreas Huberbe06d262009-08-14 14:37:10 -07002658
Andreas Huber5c0a9132009-08-20 11:16:40 -07002659void OMXCodec::setJPEGInputFormat(
2660 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2661 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002662 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002663 def.nPortIndex = kPortIndexInput;
2664
Andreas Huber784202e2009-10-15 13:46:54 -07002665 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002666 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2667 CHECK_EQ(err, OK);
2668
Andreas Huber5c0a9132009-08-20 11:16:40 -07002669 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2670 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2671
Andreas Huberbe06d262009-08-14 14:37:10 -07002672 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2673 imageDef->nFrameWidth = width;
2674 imageDef->nFrameHeight = height;
2675
Andreas Huber5c0a9132009-08-20 11:16:40 -07002676 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002677 def.nBufferCountActual = def.nBufferCountMin;
2678
Andreas Huber784202e2009-10-15 13:46:54 -07002679 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002680 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2681 CHECK_EQ(err, OK);
2682}
2683
2684void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2685 CodecSpecificData *specific =
2686 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2687
2688 specific->mSize = size;
2689 memcpy(specific->mData, data, size);
2690
2691 mCodecSpecificData.push(specific);
2692}
2693
2694void OMXCodec::clearCodecSpecificData() {
2695 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2696 free(mCodecSpecificData.editItemAt(i));
2697 }
2698 mCodecSpecificData.clear();
2699 mCodecSpecificDataIndex = 0;
2700}
2701
James Dong36e573b2010-06-19 09:04:18 -07002702status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07002703 Mutex::Autolock autoLock(mLock);
2704
Andreas Huberbe06d262009-08-14 14:37:10 -07002705 if (mState != LOADED) {
2706 return UNKNOWN_ERROR;
2707 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002708
Andreas Huberbe06d262009-08-14 14:37:10 -07002709 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002710 if (mQuirks & kWantsNALFragments) {
2711 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002712 }
James Dong36e573b2010-06-19 09:04:18 -07002713 if (meta) {
2714 int64_t startTimeUs = 0;
2715 int64_t timeUs;
2716 if (meta->findInt64(kKeyTime, &timeUs)) {
2717 startTimeUs = timeUs;
2718 }
2719 params->setInt64(kKeyTime, startTimeUs);
2720 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002721 status_t err = mSource->start(params.get());
2722
2723 if (err != OK) {
2724 return err;
2725 }
2726
2727 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002728 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002729 mSignalledEOS = false;
2730 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002731 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002732 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002733 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
2734 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07002735 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07002736 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002737
2738 return init();
2739}
2740
2741status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002742 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002743
2744 Mutex::Autolock autoLock(mLock);
2745
2746 while (isIntermediateState(mState)) {
2747 mAsyncCompletion.wait(mLock);
2748 }
2749
2750 switch (mState) {
2751 case LOADED:
2752 case ERROR:
2753 break;
2754
2755 case EXECUTING:
2756 {
2757 setState(EXECUTING_TO_IDLE);
2758
Andreas Huber127fcdc2009-08-26 16:27:02 -07002759 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002760 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002761 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002762
Andreas Huber127fcdc2009-08-26 16:27:02 -07002763 bool emulateInputFlushCompletion =
2764 !flushPortAsync(kPortIndexInput);
2765
2766 bool emulateOutputFlushCompletion =
2767 !flushPortAsync(kPortIndexOutput);
2768
2769 if (emulateInputFlushCompletion) {
2770 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2771 }
2772
2773 if (emulateOutputFlushCompletion) {
2774 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2775 }
2776 } else {
2777 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2778 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2779
2780 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002781 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002782 CHECK_EQ(err, OK);
2783 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002784
2785 while (mState != LOADED && mState != ERROR) {
2786 mAsyncCompletion.wait(mLock);
2787 }
2788
2789 break;
2790 }
2791
2792 default:
2793 {
2794 CHECK(!"should not be here.");
2795 break;
2796 }
2797 }
2798
Andreas Hubera4357ad2010-04-02 12:49:54 -07002799 if (mLeftOverBuffer) {
2800 mLeftOverBuffer->release();
2801 mLeftOverBuffer = NULL;
2802 }
2803
Andreas Huberbe06d262009-08-14 14:37:10 -07002804 mSource->stop();
2805
Andreas Huber4a9375e2010-02-09 11:54:33 -08002806 CODEC_LOGV("stopped");
2807
Andreas Huberbe06d262009-08-14 14:37:10 -07002808 return OK;
2809}
2810
2811sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002812 Mutex::Autolock autoLock(mLock);
2813
Andreas Huberbe06d262009-08-14 14:37:10 -07002814 return mOutputFormat;
2815}
2816
2817status_t OMXCodec::read(
2818 MediaBuffer **buffer, const ReadOptions *options) {
2819 *buffer = NULL;
2820
2821 Mutex::Autolock autoLock(mLock);
2822
Andreas Huberd06e5b82009-08-28 13:18:14 -07002823 if (mState != EXECUTING && mState != RECONFIGURING) {
2824 return UNKNOWN_ERROR;
2825 }
2826
Andreas Hubere981c332009-10-22 13:49:30 -07002827 bool seeking = false;
2828 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002829 ReadOptions::SeekMode seekMode;
2830 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07002831 seeking = true;
2832 }
2833
Andreas Huber42978e52009-08-27 10:08:39 -07002834 if (mInitialBufferSubmit) {
2835 mInitialBufferSubmit = false;
2836
Andreas Hubere981c332009-10-22 13:49:30 -07002837 if (seeking) {
2838 CHECK(seekTimeUs >= 0);
2839 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002840 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07002841
2842 // There's no reason to trigger the code below, there's
2843 // nothing to flush yet.
2844 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002845 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07002846 }
2847
Andreas Huber42978e52009-08-27 10:08:39 -07002848 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002849
Andreas Huberd06e5b82009-08-28 13:18:14 -07002850 if (mState == EXECUTING) {
2851 // Otherwise mState == RECONFIGURING and this code will trigger
2852 // after the output port is reenabled.
2853 fillOutputBuffers();
2854 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002855 }
2856
Andreas Hubere981c332009-10-22 13:49:30 -07002857 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002858 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002859
2860 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002861
2862 CHECK(seekTimeUs >= 0);
2863 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002864 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07002865
2866 mFilledBuffers.clear();
2867
2868 CHECK_EQ(mState, EXECUTING);
2869
Andreas Huber404cc412009-08-25 14:26:05 -07002870 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2871 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2872
2873 if (emulateInputFlushCompletion) {
2874 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2875 }
2876
2877 if (emulateOutputFlushCompletion) {
2878 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2879 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002880
2881 while (mSeekTimeUs >= 0) {
2882 mBufferFilled.wait(mLock);
2883 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002884 }
2885
2886 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2887 mBufferFilled.wait(mLock);
2888 }
2889
2890 if (mState == ERROR) {
2891 return UNKNOWN_ERROR;
2892 }
2893
2894 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08002895 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07002896 }
2897
Andreas Hubercfd55572009-10-09 14:11:28 -07002898 if (mOutputPortSettingsHaveChanged) {
2899 mOutputPortSettingsHaveChanged = false;
2900
2901 return INFO_FORMAT_CHANGED;
2902 }
2903
Andreas Huberbe06d262009-08-14 14:37:10 -07002904 size_t index = *mFilledBuffers.begin();
2905 mFilledBuffers.erase(mFilledBuffers.begin());
2906
2907 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2908 info->mMediaBuffer->add_ref();
2909 *buffer = info->mMediaBuffer;
2910
2911 return OK;
2912}
2913
2914void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2915 Mutex::Autolock autoLock(mLock);
2916
2917 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2918 for (size_t i = 0; i < buffers->size(); ++i) {
2919 BufferInfo *info = &buffers->editItemAt(i);
2920
2921 if (info->mMediaBuffer == buffer) {
2922 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2923 fillOutputBuffer(info);
2924 return;
2925 }
2926 }
2927
2928 CHECK(!"should not be here.");
2929}
2930
2931static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2932 static const char *kNames[] = {
2933 "OMX_IMAGE_CodingUnused",
2934 "OMX_IMAGE_CodingAutoDetect",
2935 "OMX_IMAGE_CodingJPEG",
2936 "OMX_IMAGE_CodingJPEG2K",
2937 "OMX_IMAGE_CodingEXIF",
2938 "OMX_IMAGE_CodingTIFF",
2939 "OMX_IMAGE_CodingGIF",
2940 "OMX_IMAGE_CodingPNG",
2941 "OMX_IMAGE_CodingLZW",
2942 "OMX_IMAGE_CodingBMP",
2943 };
2944
2945 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2946
2947 if (type < 0 || (size_t)type >= numNames) {
2948 return "UNKNOWN";
2949 } else {
2950 return kNames[type];
2951 }
2952}
2953
2954static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2955 static const char *kNames[] = {
2956 "OMX_COLOR_FormatUnused",
2957 "OMX_COLOR_FormatMonochrome",
2958 "OMX_COLOR_Format8bitRGB332",
2959 "OMX_COLOR_Format12bitRGB444",
2960 "OMX_COLOR_Format16bitARGB4444",
2961 "OMX_COLOR_Format16bitARGB1555",
2962 "OMX_COLOR_Format16bitRGB565",
2963 "OMX_COLOR_Format16bitBGR565",
2964 "OMX_COLOR_Format18bitRGB666",
2965 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002966 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002967 "OMX_COLOR_Format24bitRGB888",
2968 "OMX_COLOR_Format24bitBGR888",
2969 "OMX_COLOR_Format24bitARGB1887",
2970 "OMX_COLOR_Format25bitARGB1888",
2971 "OMX_COLOR_Format32bitBGRA8888",
2972 "OMX_COLOR_Format32bitARGB8888",
2973 "OMX_COLOR_FormatYUV411Planar",
2974 "OMX_COLOR_FormatYUV411PackedPlanar",
2975 "OMX_COLOR_FormatYUV420Planar",
2976 "OMX_COLOR_FormatYUV420PackedPlanar",
2977 "OMX_COLOR_FormatYUV420SemiPlanar",
2978 "OMX_COLOR_FormatYUV422Planar",
2979 "OMX_COLOR_FormatYUV422PackedPlanar",
2980 "OMX_COLOR_FormatYUV422SemiPlanar",
2981 "OMX_COLOR_FormatYCbYCr",
2982 "OMX_COLOR_FormatYCrYCb",
2983 "OMX_COLOR_FormatCbYCrY",
2984 "OMX_COLOR_FormatCrYCbY",
2985 "OMX_COLOR_FormatYUV444Interleaved",
2986 "OMX_COLOR_FormatRawBayer8bit",
2987 "OMX_COLOR_FormatRawBayer10bit",
2988 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002989 "OMX_COLOR_FormatL2",
2990 "OMX_COLOR_FormatL4",
2991 "OMX_COLOR_FormatL8",
2992 "OMX_COLOR_FormatL16",
2993 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002994 "OMX_COLOR_FormatL32",
2995 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2996 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2997 "OMX_COLOR_Format18BitBGR666",
2998 "OMX_COLOR_Format24BitARGB6666",
2999 "OMX_COLOR_Format24BitABGR6666",
3000 };
3001
3002 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3003
Andreas Huberbe06d262009-08-14 14:37:10 -07003004 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3005 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3006 } else if (type < 0 || (size_t)type >= numNames) {
3007 return "UNKNOWN";
3008 } else {
3009 return kNames[type];
3010 }
3011}
3012
3013static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3014 static const char *kNames[] = {
3015 "OMX_VIDEO_CodingUnused",
3016 "OMX_VIDEO_CodingAutoDetect",
3017 "OMX_VIDEO_CodingMPEG2",
3018 "OMX_VIDEO_CodingH263",
3019 "OMX_VIDEO_CodingMPEG4",
3020 "OMX_VIDEO_CodingWMV",
3021 "OMX_VIDEO_CodingRV",
3022 "OMX_VIDEO_CodingAVC",
3023 "OMX_VIDEO_CodingMJPEG",
3024 };
3025
3026 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3027
3028 if (type < 0 || (size_t)type >= numNames) {
3029 return "UNKNOWN";
3030 } else {
3031 return kNames[type];
3032 }
3033}
3034
3035static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3036 static const char *kNames[] = {
3037 "OMX_AUDIO_CodingUnused",
3038 "OMX_AUDIO_CodingAutoDetect",
3039 "OMX_AUDIO_CodingPCM",
3040 "OMX_AUDIO_CodingADPCM",
3041 "OMX_AUDIO_CodingAMR",
3042 "OMX_AUDIO_CodingGSMFR",
3043 "OMX_AUDIO_CodingGSMEFR",
3044 "OMX_AUDIO_CodingGSMHR",
3045 "OMX_AUDIO_CodingPDCFR",
3046 "OMX_AUDIO_CodingPDCEFR",
3047 "OMX_AUDIO_CodingPDCHR",
3048 "OMX_AUDIO_CodingTDMAFR",
3049 "OMX_AUDIO_CodingTDMAEFR",
3050 "OMX_AUDIO_CodingQCELP8",
3051 "OMX_AUDIO_CodingQCELP13",
3052 "OMX_AUDIO_CodingEVRC",
3053 "OMX_AUDIO_CodingSMV",
3054 "OMX_AUDIO_CodingG711",
3055 "OMX_AUDIO_CodingG723",
3056 "OMX_AUDIO_CodingG726",
3057 "OMX_AUDIO_CodingG729",
3058 "OMX_AUDIO_CodingAAC",
3059 "OMX_AUDIO_CodingMP3",
3060 "OMX_AUDIO_CodingSBC",
3061 "OMX_AUDIO_CodingVORBIS",
3062 "OMX_AUDIO_CodingWMA",
3063 "OMX_AUDIO_CodingRA",
3064 "OMX_AUDIO_CodingMIDI",
3065 };
3066
3067 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3068
3069 if (type < 0 || (size_t)type >= numNames) {
3070 return "UNKNOWN";
3071 } else {
3072 return kNames[type];
3073 }
3074}
3075
3076static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3077 static const char *kNames[] = {
3078 "OMX_AUDIO_PCMModeLinear",
3079 "OMX_AUDIO_PCMModeALaw",
3080 "OMX_AUDIO_PCMModeMULaw",
3081 };
3082
3083 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3084
3085 if (type < 0 || (size_t)type >= numNames) {
3086 return "UNKNOWN";
3087 } else {
3088 return kNames[type];
3089 }
3090}
3091
Andreas Huber7ae02c82009-09-09 16:29:47 -07003092static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3093 static const char *kNames[] = {
3094 "OMX_AUDIO_AMRBandModeUnused",
3095 "OMX_AUDIO_AMRBandModeNB0",
3096 "OMX_AUDIO_AMRBandModeNB1",
3097 "OMX_AUDIO_AMRBandModeNB2",
3098 "OMX_AUDIO_AMRBandModeNB3",
3099 "OMX_AUDIO_AMRBandModeNB4",
3100 "OMX_AUDIO_AMRBandModeNB5",
3101 "OMX_AUDIO_AMRBandModeNB6",
3102 "OMX_AUDIO_AMRBandModeNB7",
3103 "OMX_AUDIO_AMRBandModeWB0",
3104 "OMX_AUDIO_AMRBandModeWB1",
3105 "OMX_AUDIO_AMRBandModeWB2",
3106 "OMX_AUDIO_AMRBandModeWB3",
3107 "OMX_AUDIO_AMRBandModeWB4",
3108 "OMX_AUDIO_AMRBandModeWB5",
3109 "OMX_AUDIO_AMRBandModeWB6",
3110 "OMX_AUDIO_AMRBandModeWB7",
3111 "OMX_AUDIO_AMRBandModeWB8",
3112 };
3113
3114 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3115
3116 if (type < 0 || (size_t)type >= numNames) {
3117 return "UNKNOWN";
3118 } else {
3119 return kNames[type];
3120 }
3121}
3122
3123static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3124 static const char *kNames[] = {
3125 "OMX_AUDIO_AMRFrameFormatConformance",
3126 "OMX_AUDIO_AMRFrameFormatIF1",
3127 "OMX_AUDIO_AMRFrameFormatIF2",
3128 "OMX_AUDIO_AMRFrameFormatFSF",
3129 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3130 "OMX_AUDIO_AMRFrameFormatITU",
3131 };
3132
3133 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3134
3135 if (type < 0 || (size_t)type >= numNames) {
3136 return "UNKNOWN";
3137 } else {
3138 return kNames[type];
3139 }
3140}
Andreas Huberbe06d262009-08-14 14:37:10 -07003141
3142void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3143 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003144 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003145 def.nPortIndex = portIndex;
3146
Andreas Huber784202e2009-10-15 13:46:54 -07003147 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003148 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3149 CHECK_EQ(err, OK);
3150
3151 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3152
3153 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3154 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3155
3156 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3157 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3158 printf(" nBufferSize = %ld\n", def.nBufferSize);
3159
3160 switch (def.eDomain) {
3161 case OMX_PortDomainImage:
3162 {
3163 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3164
3165 printf("\n");
3166 printf(" // Image\n");
3167 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3168 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3169 printf(" nStride = %ld\n", imageDef->nStride);
3170
3171 printf(" eCompressionFormat = %s\n",
3172 imageCompressionFormatString(imageDef->eCompressionFormat));
3173
3174 printf(" eColorFormat = %s\n",
3175 colorFormatString(imageDef->eColorFormat));
3176
3177 break;
3178 }
3179
3180 case OMX_PortDomainVideo:
3181 {
3182 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3183
3184 printf("\n");
3185 printf(" // Video\n");
3186 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3187 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3188 printf(" nStride = %ld\n", videoDef->nStride);
3189
3190 printf(" eCompressionFormat = %s\n",
3191 videoCompressionFormatString(videoDef->eCompressionFormat));
3192
3193 printf(" eColorFormat = %s\n",
3194 colorFormatString(videoDef->eColorFormat));
3195
3196 break;
3197 }
3198
3199 case OMX_PortDomainAudio:
3200 {
3201 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3202
3203 printf("\n");
3204 printf(" // Audio\n");
3205 printf(" eEncoding = %s\n",
3206 audioCodingTypeString(audioDef->eEncoding));
3207
3208 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3209 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003210 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003211 params.nPortIndex = portIndex;
3212
Andreas Huber784202e2009-10-15 13:46:54 -07003213 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003214 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3215 CHECK_EQ(err, OK);
3216
3217 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3218 printf(" nChannels = %ld\n", params.nChannels);
3219 printf(" bInterleaved = %d\n", params.bInterleaved);
3220 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3221
3222 printf(" eNumData = %s\n",
3223 params.eNumData == OMX_NumericalDataSigned
3224 ? "signed" : "unsigned");
3225
3226 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003227 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3228 OMX_AUDIO_PARAM_AMRTYPE amr;
3229 InitOMXParams(&amr);
3230 amr.nPortIndex = portIndex;
3231
Andreas Huber784202e2009-10-15 13:46:54 -07003232 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003233 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3234 CHECK_EQ(err, OK);
3235
3236 printf(" nChannels = %ld\n", amr.nChannels);
3237 printf(" eAMRBandMode = %s\n",
3238 amrBandModeString(amr.eAMRBandMode));
3239 printf(" eAMRFrameFormat = %s\n",
3240 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003241 }
3242
3243 break;
3244 }
3245
3246 default:
3247 {
3248 printf(" // Unknown\n");
3249 break;
3250 }
3251 }
3252
3253 printf("}\n");
3254}
3255
3256void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3257 mOutputFormat = new MetaData;
3258 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003259 if (mIsEncoder) {
3260 int32_t timeScale;
3261 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3262 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3263 }
3264 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003265
3266 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003267 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003268 def.nPortIndex = kPortIndexOutput;
3269
Andreas Huber784202e2009-10-15 13:46:54 -07003270 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003271 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3272 CHECK_EQ(err, OK);
3273
3274 switch (def.eDomain) {
3275 case OMX_PortDomainImage:
3276 {
3277 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3278 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3279
Andreas Hubere6c40962009-09-10 14:13:30 -07003280 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003281 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3282 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3283 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3284 break;
3285 }
3286
3287 case OMX_PortDomainAudio:
3288 {
3289 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3290
Andreas Huberda050cf22009-09-02 14:01:43 -07003291 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3292 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003293 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003294 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003295
Andreas Huber784202e2009-10-15 13:46:54 -07003296 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003297 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3298 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003299
Andreas Huberda050cf22009-09-02 14:01:43 -07003300 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3301 CHECK_EQ(params.nBitPerSample, 16);
3302 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003303
Andreas Huberda050cf22009-09-02 14:01:43 -07003304 int32_t numChannels, sampleRate;
3305 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3306 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003307
Andreas Huberda050cf22009-09-02 14:01:43 -07003308 if ((OMX_U32)numChannels != params.nChannels) {
3309 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003310 "the input stream contains (contains %d channels, "
3311 "codec outputs %ld channels).",
3312 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003313 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003314
Andreas Hubere6c40962009-09-10 14:13:30 -07003315 mOutputFormat->setCString(
3316 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003317
3318 // Use the codec-advertised number of channels, as some
3319 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003320 // mono. If we know the codec lies about this information,
3321 // use the actual number of channels instead.
3322 mOutputFormat->setInt32(
3323 kKeyChannelCount,
3324 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3325 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003326
3327 // The codec-reported sampleRate is not reliable...
3328 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3329 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003330 OMX_AUDIO_PARAM_AMRTYPE amr;
3331 InitOMXParams(&amr);
3332 amr.nPortIndex = kPortIndexOutput;
3333
Andreas Huber784202e2009-10-15 13:46:54 -07003334 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003335 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3336 CHECK_EQ(err, OK);
3337
3338 CHECK_EQ(amr.nChannels, 1);
3339 mOutputFormat->setInt32(kKeyChannelCount, 1);
3340
3341 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3342 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003343 mOutputFormat->setCString(
3344 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003345 mOutputFormat->setInt32(kKeySampleRate, 8000);
3346 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3347 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003348 mOutputFormat->setCString(
3349 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003350 mOutputFormat->setInt32(kKeySampleRate, 16000);
3351 } else {
3352 CHECK(!"Unknown AMR band mode.");
3353 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003354 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003355 mOutputFormat->setCString(
3356 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003357 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003358 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3359 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003360 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003361 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3362 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003363 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003364 } else {
3365 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003366 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003367 break;
3368 }
3369
3370 case OMX_PortDomainVideo:
3371 {
3372 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3373
3374 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003375 mOutputFormat->setCString(
3376 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003377 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003378 mOutputFormat->setCString(
3379 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003380 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003381 mOutputFormat->setCString(
3382 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003383 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003384 mOutputFormat->setCString(
3385 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003386 } else {
3387 CHECK(!"Unknown compression format.");
3388 }
3389
3390 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3391 // This component appears to be lying to me.
3392 mOutputFormat->setInt32(
3393 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3394 mOutputFormat->setInt32(
3395 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3396 } else {
3397 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3398 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3399 }
3400
3401 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3402 break;
3403 }
3404
3405 default:
3406 {
3407 CHECK(!"should not be here, neither audio nor video.");
3408 break;
3409 }
3410 }
3411}
3412
Andreas Huber1f24b302010-06-10 11:12:39 -07003413status_t OMXCodec::pause() {
3414 Mutex::Autolock autoLock(mLock);
3415
3416 mPaused = true;
3417
3418 return OK;
3419}
3420
Andreas Hubere6c40962009-09-10 14:13:30 -07003421////////////////////////////////////////////////////////////////////////////////
3422
3423status_t QueryCodecs(
3424 const sp<IOMX> &omx,
3425 const char *mime, bool queryDecoders,
3426 Vector<CodecCapabilities> *results) {
3427 results->clear();
3428
3429 for (int index = 0;; ++index) {
3430 const char *componentName;
3431
3432 if (!queryDecoders) {
3433 componentName = GetCodec(
3434 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3435 mime, index);
3436 } else {
3437 componentName = GetCodec(
3438 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3439 mime, index);
3440 }
3441
3442 if (!componentName) {
3443 return OK;
3444 }
3445
Andreas Huber1a189a82010-03-24 13:49:20 -07003446 if (strncmp(componentName, "OMX.", 4)) {
3447 // Not an OpenMax component but a software codec.
3448
3449 results->push();
3450 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3451 caps->mComponentName = componentName;
3452
3453 continue;
3454 }
3455
Andreas Huber784202e2009-10-15 13:46:54 -07003456 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003457 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003458 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003459
3460 if (err != OK) {
3461 continue;
3462 }
3463
James Dong722d5912010-04-13 10:56:59 -07003464 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003465
3466 results->push();
3467 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3468 caps->mComponentName = componentName;
3469
3470 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3471 InitOMXParams(&param);
3472
3473 param.nPortIndex = queryDecoders ? 0 : 1;
3474
3475 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003476 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003477 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3478 &param, sizeof(param));
3479
3480 if (err != OK) {
3481 break;
3482 }
3483
3484 CodecProfileLevel profileLevel;
3485 profileLevel.mProfile = param.eProfile;
3486 profileLevel.mLevel = param.eLevel;
3487
3488 caps->mProfileLevels.push(profileLevel);
3489 }
3490
Andreas Huber784202e2009-10-15 13:46:54 -07003491 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003492 }
3493}
3494
Andreas Huberbe06d262009-08-14 14:37:10 -07003495} // namespace android