blob: 83f7040200dc607053a2f646782c13b5d30d2ab6 [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 Dong02f5b542009-12-15 16:26:55 -080028#include "include/M4vH263Decoder.h"
Andreas Huber250f2432009-12-07 14:22:35 -080029#include "include/MP3Decoder.h"
Andreas Huber388379f2010-05-07 10:35:13 -070030#include "include/VorbisDecoder.h"
Andreas Huber47ba30e2010-05-24 14:38:02 -070031#include "include/VPXDecoder.h"
Andreas Huber8c7ab032009-12-07 11:23:44 -080032
Andreas Huberbd7b43b2009-10-13 10:22:55 -070033#include "include/ESDS.h"
34
Andreas Huberbe06d262009-08-14 14:37:10 -070035#include <binder/IServiceManager.h>
36#include <binder/MemoryDealer.h>
37#include <binder/ProcessState.h>
38#include <media/IMediaPlayerService.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070039#include <media/stagefright/MediaBuffer.h>
40#include <media/stagefright/MediaBufferGroup.h>
41#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070042#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070043#include <media/stagefright/MediaExtractor.h>
44#include <media/stagefright/MetaData.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070045#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070046#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070047#include <utils/Vector.h>
48
49#include <OMX_Audio.h>
50#include <OMX_Component.h>
51
52namespace android {
53
Andreas Huber8b432b12009-10-07 13:36:52 -070054static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
55
Andreas Huberbe06d262009-08-14 14:37:10 -070056struct CodecInfo {
57 const char *mime;
58 const char *codec;
59};
60
Andreas Huberfb1c2f82009-12-15 13:25:11 -080061#define FACTORY_CREATE(name) \
62static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
63 return new name(source); \
64}
65
James Dong17299ab2010-05-14 15:45:22 -070066#define FACTORY_CREATE_ENCODER(name) \
67static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \
68 return new name(source, meta); \
69}
70
Andreas Huberfb1c2f82009-12-15 13:25:11 -080071#define FACTORY_REF(name) { #name, Make##name },
72
73FACTORY_CREATE(MP3Decoder)
74FACTORY_CREATE(AMRNBDecoder)
75FACTORY_CREATE(AMRWBDecoder)
76FACTORY_CREATE(AACDecoder)
77FACTORY_CREATE(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -080078FACTORY_CREATE(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -070079FACTORY_CREATE(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -070080FACTORY_CREATE(VPXDecoder)
James Dong17299ab2010-05-14 15:45:22 -070081FACTORY_CREATE_ENCODER(AMRNBEncoder)
82FACTORY_CREATE_ENCODER(AMRWBEncoder)
83FACTORY_CREATE_ENCODER(AACEncoder)
84
85static sp<MediaSource> InstantiateSoftwareEncoder(
86 const char *name, const sp<MediaSource> &source,
87 const sp<MetaData> &meta) {
88 struct FactoryInfo {
89 const char *name;
90 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &);
91 };
92
93 static const FactoryInfo kFactoryInfo[] = {
94 FACTORY_REF(AMRNBEncoder)
95 FACTORY_REF(AMRWBEncoder)
96 FACTORY_REF(AACEncoder)
97 };
98 for (size_t i = 0;
99 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
100 if (!strcmp(name, kFactoryInfo[i].name)) {
101 return (*kFactoryInfo[i].CreateFunc)(source, meta);
102 }
103 }
104
105 return NULL;
106}
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800107
108static sp<MediaSource> InstantiateSoftwareCodec(
109 const char *name, const sp<MediaSource> &source) {
110 struct FactoryInfo {
111 const char *name;
112 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
113 };
114
115 static const FactoryInfo kFactoryInfo[] = {
116 FACTORY_REF(MP3Decoder)
117 FACTORY_REF(AMRNBDecoder)
118 FACTORY_REF(AMRWBDecoder)
119 FACTORY_REF(AACDecoder)
120 FACTORY_REF(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -0800121 FACTORY_REF(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -0700122 FACTORY_REF(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -0700123 FACTORY_REF(VPXDecoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800124 };
125 for (size_t i = 0;
126 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
127 if (!strcmp(name, kFactoryInfo[i].name)) {
128 return (*kFactoryInfo[i].CreateFunc)(source);
129 }
130 }
131
132 return NULL;
133}
134
135#undef FACTORY_REF
136#undef FACTORY_CREATE
137
Andreas Huberbe06d262009-08-14 14:37:10 -0700138static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700139 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
James Dong374aee62010-04-26 10:23:30 -0700140// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800141 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700142// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700143// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800144 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700145// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700146 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800147 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700148// { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700149 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800150 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700151// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700152 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.decoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700153 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
154 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800155 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700156// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700157 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.decoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700158 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800159 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700160// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700161 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.decoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700162 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
163 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800164 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700165// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huber388379f2010-05-07 10:35:13 -0700166 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" },
Andreas Huber47ba30e2010-05-24 14:38:02 -0700167 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700168};
169
170static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700171 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800172 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700173 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700174 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700175 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700176 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700177// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
178 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.encoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700179 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
180 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700181// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
182 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.encoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700183 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
184 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700185// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
186 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.encoder.avc" },
Andreas Huber71c27d92010-03-19 11:43:15 -0700187 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700188 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700189// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700190};
191
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800192#undef OPTIONAL
193
Andreas Hubere0873732009-09-10 09:57:53 -0700194#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700195#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber42c444a2010-02-09 10:20:00 -0800196#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700197
Andreas Huberbe06d262009-08-14 14:37:10 -0700198struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700199 OMXCodecObserver() {
200 }
201
202 void setCodec(const sp<OMXCodec> &target) {
203 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700204 }
205
206 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700207 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700208 sp<OMXCodec> codec = mTarget.promote();
209
210 if (codec.get() != NULL) {
211 codec->on_message(msg);
212 }
213 }
214
215protected:
216 virtual ~OMXCodecObserver() {}
217
218private:
219 wp<OMXCodec> mTarget;
220
221 OMXCodecObserver(const OMXCodecObserver &);
222 OMXCodecObserver &operator=(const OMXCodecObserver &);
223};
224
225static const char *GetCodec(const CodecInfo *info, size_t numInfos,
226 const char *mime, int index) {
227 CHECK(index >= 0);
228 for(size_t i = 0; i < numInfos; ++i) {
229 if (!strcasecmp(mime, info[i].mime)) {
230 if (index == 0) {
231 return info[i].codec;
232 }
233
234 --index;
235 }
236 }
237
238 return NULL;
239}
240
Andreas Huberebf66ea2009-08-19 13:32:58 -0700241enum {
242 kAVCProfileBaseline = 0x42,
243 kAVCProfileMain = 0x4d,
244 kAVCProfileExtended = 0x58,
245 kAVCProfileHigh = 0x64,
246 kAVCProfileHigh10 = 0x6e,
247 kAVCProfileHigh422 = 0x7a,
248 kAVCProfileHigh444 = 0xf4,
249 kAVCProfileCAVLC444Intra = 0x2c
250};
251
252static const char *AVCProfileToString(uint8_t profile) {
253 switch (profile) {
254 case kAVCProfileBaseline:
255 return "Baseline";
256 case kAVCProfileMain:
257 return "Main";
258 case kAVCProfileExtended:
259 return "Extended";
260 case kAVCProfileHigh:
261 return "High";
262 case kAVCProfileHigh10:
263 return "High 10";
264 case kAVCProfileHigh422:
265 return "High 422";
266 case kAVCProfileHigh444:
267 return "High 444";
268 case kAVCProfileCAVLC444Intra:
269 return "CAVLC 444 Intra";
270 default: return "Unknown";
271 }
272}
273
Andreas Huber4c483422009-09-02 16:05:36 -0700274template<class T>
275static void InitOMXParams(T *params) {
276 params->nSize = sizeof(T);
277 params->nVersion.s.nVersionMajor = 1;
278 params->nVersion.s.nVersionMinor = 0;
279 params->nVersion.s.nRevision = 0;
280 params->nVersion.s.nStep = 0;
281}
282
Andreas Hubere13526a2009-10-22 10:43:34 -0700283static bool IsSoftwareCodec(const char *componentName) {
284 if (!strncmp("OMX.PV.", componentName, 7)) {
285 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700286 }
287
Andreas Hubere13526a2009-10-22 10:43:34 -0700288 return false;
289}
290
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800291// A sort order in which non-OMX components are first,
292// followed by software codecs, i.e. OMX.PV.*, followed
293// by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700294static int CompareSoftwareCodecsFirst(
295 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800296 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
297 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
298
299 if (isNotOMX1) {
300 if (isNotOMX2) { return 0; }
301 return -1;
302 }
303 if (isNotOMX2) {
304 return 1;
305 }
306
Andreas Hubere13526a2009-10-22 10:43:34 -0700307 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
308 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
309
310 if (isSoftwareCodec1) {
311 if (isSoftwareCodec2) { return 0; }
312 return -1;
313 }
314
315 if (isSoftwareCodec2) {
316 return 1;
317 }
318
319 return 0;
320}
321
322// static
323uint32_t OMXCodec::getComponentQuirks(const char *componentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700324 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700325
Andreas Huberbe06d262009-08-14 14:37:10 -0700326 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700327 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700328 }
329 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
330 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800331 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700332 }
333 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
334 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700335 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700336 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700337 }
338 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
339 quirks |= kRequiresLoadedToIdleAfterAllocation;
340 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700341 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huberbe06d262009-08-14 14:37:10 -0700342 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700343 if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) {
344 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700345 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700346 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800347 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700348 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700349 if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) {
350 quirks |= kRequiresAllocateBufferOnInputPorts;
351 quirks |= kRequiresAllocateBufferOnOutputPorts;
352 quirks |= kDefersOutputBufferAllocation;
353 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700354
Andreas Huber2dc64d82009-09-11 12:58:53 -0700355 if (!strncmp(componentName, "OMX.TI.", 7)) {
356 // Apparently I must not use OMX_UseBuffer on either input or
357 // output ports on any of the TI components or quote:
358 // "(I) may have unexpected problem (sic) which can be timing related
359 // and hard to reproduce."
360
361 quirks |= kRequiresAllocateBufferOnInputPorts;
362 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dongdca66e12010-06-14 11:14:38 -0700363 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) {
James Dong4f501f02010-06-07 14:41:41 -0700364 quirks |= kAvoidMemcopyInputRecordingFrames;
365 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700366 }
367
Andreas Huberb8de9572010-02-22 14:58:45 -0800368 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
369 quirks |= kInputBufferSizesAreBogus;
370 }
371
Andreas Hubere13526a2009-10-22 10:43:34 -0700372 return quirks;
373}
374
375// static
376void OMXCodec::findMatchingCodecs(
377 const char *mime,
378 bool createEncoder, const char *matchComponentName,
379 uint32_t flags,
380 Vector<String8> *matchingCodecs) {
381 matchingCodecs->clear();
382
383 for (int index = 0;; ++index) {
384 const char *componentName;
385
386 if (createEncoder) {
387 componentName = GetCodec(
388 kEncoderInfo,
389 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
390 mime, index);
391 } else {
392 componentName = GetCodec(
393 kDecoderInfo,
394 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
395 mime, index);
396 }
397
398 if (!componentName) {
399 break;
400 }
401
402 // If a specific codec is requested, skip the non-matching ones.
403 if (matchComponentName && strcmp(componentName, matchComponentName)) {
404 continue;
405 }
406
407 matchingCodecs->push(String8(componentName));
408 }
409
410 if (flags & kPreferSoftwareCodecs) {
411 matchingCodecs->sort(CompareSoftwareCodecsFirst);
412 }
413}
414
415// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800416sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700417 const sp<IOMX> &omx,
418 const sp<MetaData> &meta, bool createEncoder,
419 const sp<MediaSource> &source,
420 const char *matchComponentName,
421 uint32_t flags) {
422 const char *mime;
423 bool success = meta->findCString(kKeyMIMEType, &mime);
424 CHECK(success);
425
426 Vector<String8> matchingCodecs;
427 findMatchingCodecs(
428 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
429
430 if (matchingCodecs.isEmpty()) {
431 return NULL;
432 }
433
434 sp<OMXCodecObserver> observer = new OMXCodecObserver;
435 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700436
437 const char *componentName;
438 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
439 componentName = matchingCodecs[i].string();
440
James Dong17299ab2010-05-14 15:45:22 -0700441 sp<MediaSource> softwareCodec = createEncoder?
442 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800443 InstantiateSoftwareCodec(componentName, source);
444
445 if (softwareCodec != NULL) {
446 LOGV("Successfully allocated software codec '%s'", componentName);
447
448 return softwareCodec;
449 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800450
Andreas Hubere13526a2009-10-22 10:43:34 -0700451 LOGV("Attempting to allocate OMX node '%s'", componentName);
452
453 status_t err = omx->allocateNode(componentName, observer, &node);
454 if (err == OK) {
455 LOGV("Successfully allocated OMX node '%s'", componentName);
456
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700457 sp<OMXCodec> codec = new OMXCodec(
458 omx, node, getComponentQuirks(componentName),
459 createEncoder, mime, componentName,
460 source);
461
462 observer->setCodec(codec);
463
464 err = codec->configureCodec(meta);
465
466 if (err == OK) {
467 return codec;
468 }
469
470 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700471 }
472 }
473
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700474 return NULL;
475}
Andreas Hubere13526a2009-10-22 10:43:34 -0700476
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700477status_t OMXCodec::configureCodec(const sp<MetaData> &meta) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700478 uint32_t type;
479 const void *data;
480 size_t size;
481 if (meta->findData(kKeyESDS, &type, &data, &size)) {
482 ESDS esds((const char *)data, size);
483 CHECK_EQ(esds.InitCheck(), OK);
484
485 const void *codec_specific_data;
486 size_t codec_specific_data_size;
487 esds.getCodecSpecificInfo(
488 &codec_specific_data, &codec_specific_data_size);
489
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700490 addCodecSpecificData(
Andreas Huberbe06d262009-08-14 14:37:10 -0700491 codec_specific_data, codec_specific_data_size);
492 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
Andreas Huberebf66ea2009-08-19 13:32:58 -0700493 // Parse the AVCDecoderConfigurationRecord
494
495 const uint8_t *ptr = (const uint8_t *)data;
496
497 CHECK(size >= 7);
498 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
499 uint8_t profile = ptr[1];
500 uint8_t level = ptr[3];
501
Andreas Huber44e15c42009-11-23 14:39:38 -0800502 // There is decodable content out there that fails the following
503 // assertion, let's be lenient for now...
504 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700505
506 size_t lengthSize = 1 + (ptr[4] & 3);
507
508 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
509 // violates it...
510 // CHECK((ptr[5] >> 5) == 7); // reserved
511
512 size_t numSeqParameterSets = ptr[5] & 31;
513
514 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700515 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700516
517 for (size_t i = 0; i < numSeqParameterSets; ++i) {
518 CHECK(size >= 2);
519 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700520
521 ptr += 2;
522 size -= 2;
523
Andreas Huberbe06d262009-08-14 14:37:10 -0700524 CHECK(size >= length);
525
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700526 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700527
528 ptr += length;
529 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700530 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700531
Andreas Huberebf66ea2009-08-19 13:32:58 -0700532 CHECK(size >= 1);
533 size_t numPictureParameterSets = *ptr;
534 ++ptr;
535 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700536
Andreas Huberebf66ea2009-08-19 13:32:58 -0700537 for (size_t i = 0; i < numPictureParameterSets; ++i) {
538 CHECK(size >= 2);
539 size_t length = U16_AT(ptr);
540
541 ptr += 2;
542 size -= 2;
543
544 CHECK(size >= length);
545
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700546 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700547
548 ptr += length;
549 size -= length;
550 }
551
Andreas Hubere2f85072010-06-10 09:51:27 -0700552 CODEC_LOGV(
553 "AVC profile = %d (%s), level = %d",
554 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700555
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700556 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
Andreas Hubere2f85072010-06-10 09:51:27 -0700557 && (profile != kAVCProfileBaseline || level > 30)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700558 // This stream exceeds the decoder's capabilities. The decoder
559 // does not handle this gracefully and would clobber the heap
560 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700561
562 LOGE("Profile and/or level exceed the decoder's capabilities.");
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700563 return ERROR_UNSUPPORTED;
Andreas Huberbe06d262009-08-14 14:37:10 -0700564 }
565 }
566
James Dong17299ab2010-05-14 15:45:22 -0700567 int32_t bitRate = 0;
568 if (mIsEncoder) {
569 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
570 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700571 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700572 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700573 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700574 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700575 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700576 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700577 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700578 int32_t numChannels, sampleRate;
579 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
580 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
581
James Dong17299ab2010-05-14 15:45:22 -0700582 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700583 }
James Dongabed93a2010-04-22 17:27:04 -0700584
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700585 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700586
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700587 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700588 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700589 } else {
James Dong1244eab2010-06-08 11:58:53 -0700590 int32_t width, height;
591 bool success = meta->findInt32(kKeyWidth, &width);
592 success = success && meta->findInt32(kKeyHeight, &height);
593 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700594 status_t err = setVideoOutputFormat(
595 mMIME, width, height);
596
597 if (err != OK) {
598 return err;
599 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700600 }
601 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700602
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700603 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
604 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700605 OMX_COLOR_FORMATTYPE format =
606 OMX_COLOR_Format32bitARGB8888;
607 // OMX_COLOR_FormatYUV420PackedPlanar;
608 // OMX_COLOR_FormatCbYCrY;
609 // OMX_COLOR_FormatYUV411Planar;
610
611 int32_t width, height;
612 bool success = meta->findInt32(kKeyWidth, &width);
613 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700614
615 int32_t compressedSize;
616 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700617 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700618
619 CHECK(success);
620 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700621
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700622 setImageOutputFormat(format, width, height);
623 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700624 }
625
Andreas Huberda050cf22009-09-02 14:01:43 -0700626 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800627 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700628 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700629 }
630
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700631 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700632 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
633 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700634 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700635 }
636
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700637 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700638
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700639 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700640}
641
Andreas Huberda050cf22009-09-02 14:01:43 -0700642void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
643 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700644 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700645 def.nPortIndex = portIndex;
646
Andreas Huber784202e2009-10-15 13:46:54 -0700647 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700648 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
649 CHECK_EQ(err, OK);
650
Andreas Huberb8de9572010-02-22 14:58:45 -0800651 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
652 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700653 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700654 }
655
Andreas Huber784202e2009-10-15 13:46:54 -0700656 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700657 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
658 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800659
660 err = mOMX->getParameter(
661 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
662 CHECK_EQ(err, OK);
663
664 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800665 if (portIndex == kPortIndexInput
666 && (mQuirks & kInputBufferSizesAreBogus)) {
667 CHECK_EQ(def.nBufferSize, size);
668 } else {
669 CHECK(def.nBufferSize >= size);
670 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700671}
672
Andreas Huberbe06d262009-08-14 14:37:10 -0700673status_t OMXCodec::setVideoPortFormatType(
674 OMX_U32 portIndex,
675 OMX_VIDEO_CODINGTYPE compressionFormat,
676 OMX_COLOR_FORMATTYPE colorFormat) {
677 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700678 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700679 format.nPortIndex = portIndex;
680 format.nIndex = 0;
681 bool found = false;
682
683 OMX_U32 index = 0;
684 for (;;) {
685 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700686 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700687 mNode, OMX_IndexParamVideoPortFormat,
688 &format, sizeof(format));
689
690 if (err != OK) {
691 return err;
692 }
693
694 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700695 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700696
697#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700698 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700699 portIndex,
700 index, format.eCompressionFormat, format.eColorFormat);
701#endif
702
703 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
704 if (portIndex == kPortIndexInput
705 && colorFormat == format.eColorFormat) {
706 // eCompressionFormat does not seem right.
707 found = true;
708 break;
709 }
710 if (portIndex == kPortIndexOutput
711 && compressionFormat == format.eCompressionFormat) {
712 // eColorFormat does not seem right.
713 found = true;
714 break;
715 }
716 }
717
718 if (format.eCompressionFormat == compressionFormat
719 && format.eColorFormat == colorFormat) {
720 found = true;
721 break;
722 }
723
724 ++index;
725 }
726
727 if (!found) {
728 return UNKNOWN_ERROR;
729 }
730
Andreas Huber53a76bd2009-10-06 16:20:44 -0700731 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700732 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700733 mNode, OMX_IndexParamVideoPortFormat,
734 &format, sizeof(format));
735
736 return err;
737}
738
Andreas Huberb482ce82009-10-29 12:02:48 -0700739static size_t getFrameSize(
740 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
741 switch (colorFormat) {
742 case OMX_COLOR_FormatYCbYCr:
743 case OMX_COLOR_FormatCbYCrY:
744 return width * height * 2;
745
Andreas Huber71c27d92010-03-19 11:43:15 -0700746 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700747 case OMX_COLOR_FormatYUV420SemiPlanar:
748 return (width * height * 3) / 2;
749
750 default:
751 CHECK(!"Should not be here. Unsupported color format.");
752 break;
753 }
754}
755
Andreas Huberbe06d262009-08-14 14:37:10 -0700756void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700757 const char *mime, const sp<MetaData>& meta) {
758
759 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
760 bool success = meta->findInt32(kKeyWidth, &width);
761 success = success && meta->findInt32(kKeyHeight, &height);
762 success = success && meta->findInt32(kKeySampleRate, &frameRate);
763 success = success && meta->findInt32(kKeyBitRate, &bitRate);
764 success = success && meta->findInt32(kKeyStride, &stride);
765 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
766 CHECK(success);
767 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700768
769 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700770 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700771 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700772 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700773 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700774 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700775 compressionFormat = OMX_VIDEO_CodingH263;
776 } else {
777 LOGE("Not a supported video mime type: %s", mime);
778 CHECK(!"Should not be here. Not a supported video mime type.");
779 }
780
Andreas Huberea6a38c2009-11-16 15:43:38 -0800781 OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
782 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
James Dongabed93a2010-04-22 17:27:04 -0700783 colorFormat = OMX_COLOR_FormatYCbYCr;
Andreas Huberbe06d262009-08-14 14:37:10 -0700784 }
785
James Dongb00e2462010-04-26 17:48:26 -0700786 status_t err;
787 OMX_PARAM_PORTDEFINITIONTYPE def;
788 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
789
790 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700791 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700792 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700793 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700794
James Dongb00e2462010-04-26 17:48:26 -0700795 InitOMXParams(&def);
796 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700797
James Dongb00e2462010-04-26 17:48:26 -0700798 err = mOMX->getParameter(
799 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
800 CHECK_EQ(err, OK);
801
James Dong1244eab2010-06-08 11:58:53 -0700802 def.nBufferSize = getFrameSize(colorFormat,
803 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700804
805 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
806
807 video_def->nFrameWidth = width;
808 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700809 video_def->nStride = stride;
810 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700811 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700812 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
813 video_def->eColorFormat = colorFormat;
814
James Dongb00e2462010-04-26 17:48:26 -0700815 err = mOMX->setParameter(
816 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
817 CHECK_EQ(err, OK);
818
819 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700820 CHECK_EQ(setVideoPortFormatType(
821 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
822 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700823 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700824 def.nPortIndex = kPortIndexOutput;
825
James Dongb00e2462010-04-26 17:48:26 -0700826 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700827 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
828
829 CHECK_EQ(err, OK);
830 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
831
832 video_def->nFrameWidth = width;
833 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700834 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700835 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700836 video_def->eCompressionFormat = compressionFormat;
837 video_def->eColorFormat = OMX_COLOR_FormatUnused;
838
Andreas Huber784202e2009-10-15 13:46:54 -0700839 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700840 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
841 CHECK_EQ(err, OK);
842
James Dongb00e2462010-04-26 17:48:26 -0700843 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700844 switch (compressionFormat) {
845 case OMX_VIDEO_CodingMPEG4:
846 {
James Dong1244eab2010-06-08 11:58:53 -0700847 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700848 break;
849 }
850
851 case OMX_VIDEO_CodingH263:
James Dongc0ab2a62010-06-29 16:29:19 -0700852 CHECK_EQ(setupH263EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700853 break;
854
Andreas Huberea6a38c2009-11-16 15:43:38 -0800855 case OMX_VIDEO_CodingAVC:
856 {
James Dong1244eab2010-06-08 11:58:53 -0700857 CHECK_EQ(setupAVCEncoderParameters(meta), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -0800858 break;
859 }
860
Andreas Huberb482ce82009-10-29 12:02:48 -0700861 default:
862 CHECK(!"Support for this compressionFormat to be implemented.");
863 break;
864 }
865}
866
James Dong1244eab2010-06-08 11:58:53 -0700867static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
868 if (iFramesInterval < 0) {
869 return 0xFFFFFFFF;
870 } else if (iFramesInterval == 0) {
871 return 0;
872 }
873 OMX_U32 ret = frameRate * iFramesInterval;
874 CHECK(ret > 1);
875 return ret;
876}
877
James Dongc0ab2a62010-06-29 16:29:19 -0700878status_t OMXCodec::setupErrorCorrectionParameters() {
879 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
880 InitOMXParams(&errorCorrectionType);
881 errorCorrectionType.nPortIndex = kPortIndexOutput;
882
883 status_t err = mOMX->getParameter(
884 mNode, OMX_IndexParamVideoErrorCorrection,
885 &errorCorrectionType, sizeof(errorCorrectionType));
886 CHECK_EQ(err, OK);
887
888 errorCorrectionType.bEnableHEC = OMX_FALSE;
889 errorCorrectionType.bEnableResync = OMX_TRUE;
890 errorCorrectionType.nResynchMarkerSpacing = 256;
891 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
892 errorCorrectionType.bEnableRVLC = OMX_FALSE;
893
894 err = mOMX->setParameter(
895 mNode, OMX_IndexParamVideoErrorCorrection,
896 &errorCorrectionType, sizeof(errorCorrectionType));
897 CHECK_EQ(err, OK);
898 return OK;
899}
900
901status_t OMXCodec::setupBitRate(int32_t bitRate) {
902 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
903 InitOMXParams(&bitrateType);
904 bitrateType.nPortIndex = kPortIndexOutput;
905
906 status_t err = mOMX->getParameter(
907 mNode, OMX_IndexParamVideoBitrate,
908 &bitrateType, sizeof(bitrateType));
909 CHECK_EQ(err, OK);
910
911 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
912 bitrateType.nTargetBitrate = bitRate;
913
914 err = mOMX->setParameter(
915 mNode, OMX_IndexParamVideoBitrate,
916 &bitrateType, sizeof(bitrateType));
917 CHECK_EQ(err, OK);
918 return OK;
919}
920
James Dong81c929a2010-07-01 15:02:14 -0700921status_t OMXCodec::getVideoProfileLevel(
922 const sp<MetaData>& meta,
923 const CodecProfileLevel& defaultProfileLevel,
924 CodecProfileLevel &profileLevel) {
925 CODEC_LOGV("Default profile: %ld, level %ld",
926 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
927
928 // Are the default profile and level overwriten?
929 int32_t profile, level;
930 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
931 profile = defaultProfileLevel.mProfile;
932 }
933 if (!meta->findInt32(kKeyVideoLevel, &level)) {
934 level = defaultProfileLevel.mLevel;
935 }
936 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
937
938 // Are the target profile and level supported by the encoder?
939 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
940 InitOMXParams(&param);
941 param.nPortIndex = kPortIndexOutput;
942 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
943 status_t err = mOMX->getParameter(
944 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
945 &param, sizeof(param));
946
947 if (err != OK) return err;
948
949 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
950 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
951 CODEC_LOGV("Supported profile: %ld, level %ld",
952 supportedProfile, supportedLevel);
953
954 if (profile == supportedProfile &&
955 level == supportedLevel) {
956 profileLevel.mProfile = profile;
957 profileLevel.mLevel = level;
958 return OK;
959 }
960 }
961
962 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
963 profile, level);
964 return BAD_VALUE;
965}
966
James Dongc0ab2a62010-06-29 16:29:19 -0700967status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
968 int32_t iFramesInterval, frameRate, bitRate;
969 bool success = meta->findInt32(kKeyBitRate, &bitRate);
970 success = success && meta->findInt32(kKeySampleRate, &frameRate);
971 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
972 CHECK(success);
973 OMX_VIDEO_PARAM_H263TYPE h263type;
974 InitOMXParams(&h263type);
975 h263type.nPortIndex = kPortIndexOutput;
976
977 status_t err = mOMX->getParameter(
978 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
979 CHECK_EQ(err, OK);
980
981 h263type.nAllowedPictureTypes =
982 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
983
984 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
985 if (h263type.nPFrames == 0) {
986 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
987 }
988 h263type.nBFrames = 0;
989
James Dong81c929a2010-07-01 15:02:14 -0700990 // Check profile and level parameters
991 CodecProfileLevel defaultProfileLevel, profileLevel;
992 defaultProfileLevel.mProfile = OMX_VIDEO_H263ProfileBaseline;
993 defaultProfileLevel.mLevel = OMX_VIDEO_H263Level45;
994 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
995 if (err != OK) return err;
996 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
997 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -0700998
999 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1000 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1001 h263type.nPictureHeaderRepetition = 0;
1002 h263type.nGOBHeaderInterval = 0;
1003
1004 err = mOMX->setParameter(
1005 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1006 CHECK_EQ(err, OK);
1007
1008 CHECK_EQ(setupBitRate(bitRate), OK);
1009 CHECK_EQ(setupErrorCorrectionParameters(), OK);
1010
1011 return OK;
1012}
1013
James Dong1244eab2010-06-08 11:58:53 -07001014status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1015 int32_t iFramesInterval, frameRate, bitRate;
1016 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1017 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1018 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1019 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001020 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1021 InitOMXParams(&mpeg4type);
1022 mpeg4type.nPortIndex = kPortIndexOutput;
1023
1024 status_t err = mOMX->getParameter(
1025 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1026 CHECK_EQ(err, OK);
1027
1028 mpeg4type.nSliceHeaderSpacing = 0;
1029 mpeg4type.bSVH = OMX_FALSE;
1030 mpeg4type.bGov = OMX_FALSE;
1031
1032 mpeg4type.nAllowedPictureTypes =
1033 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1034
James Dong1244eab2010-06-08 11:58:53 -07001035 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1036 if (mpeg4type.nPFrames == 0) {
1037 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1038 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001039 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001040 mpeg4type.nIDCVLCThreshold = 0;
1041 mpeg4type.bACPred = OMX_TRUE;
1042 mpeg4type.nMaxPacketSize = 256;
1043 mpeg4type.nTimeIncRes = 1000;
1044 mpeg4type.nHeaderExtension = 0;
1045 mpeg4type.bReversibleVLC = OMX_FALSE;
1046
James Dong81c929a2010-07-01 15:02:14 -07001047 // Check profile and level parameters
1048 CodecProfileLevel defaultProfileLevel, profileLevel;
1049 defaultProfileLevel.mProfile = OMX_VIDEO_MPEG4ProfileSimple;
1050 defaultProfileLevel.mLevel = OMX_VIDEO_MPEG4Level2;
1051 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1052 if (err != OK) return err;
1053 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1054 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001055
1056 err = mOMX->setParameter(
1057 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1058 CHECK_EQ(err, OK);
1059
James Dongc0ab2a62010-06-29 16:29:19 -07001060 CHECK_EQ(setupBitRate(bitRate), OK);
1061 CHECK_EQ(setupErrorCorrectionParameters(), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001062
1063 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001064}
1065
James Dong1244eab2010-06-08 11:58:53 -07001066status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1067 int32_t iFramesInterval, frameRate, bitRate;
1068 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1069 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1070 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1071 CHECK(success);
1072
Andreas Huberea6a38c2009-11-16 15:43:38 -08001073 OMX_VIDEO_PARAM_AVCTYPE h264type;
1074 InitOMXParams(&h264type);
1075 h264type.nPortIndex = kPortIndexOutput;
1076
1077 status_t err = mOMX->getParameter(
1078 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1079 CHECK_EQ(err, OK);
1080
1081 h264type.nAllowedPictureTypes =
1082 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1083
1084 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001085 h264type.nBFrames = 0; // No B frames support yet
1086 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1087 if (h264type.nPFrames == 0) {
1088 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1089 }
James Dong81c929a2010-07-01 15:02:14 -07001090
1091 // Check profile and level parameters
1092 CodecProfileLevel defaultProfileLevel, profileLevel;
1093 defaultProfileLevel.mProfile = h264type.eProfile;
1094 defaultProfileLevel.mLevel = h264type.eLevel;
1095 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1096 if (err != OK) return err;
1097 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1098 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1099
1100 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1101 h264type.bUseHadamard = OMX_TRUE;
1102 h264type.nRefFrames = 1;
1103 h264type.nRefIdx10ActiveMinus1 = 0;
1104 h264type.nRefIdx11ActiveMinus1 = 0;
1105 h264type.bEntropyCodingCABAC = OMX_FALSE;
1106 h264type.bWeightedPPrediction = OMX_FALSE;
1107 h264type.bconstIpred = OMX_FALSE;
1108 h264type.bDirect8x8Inference = OMX_FALSE;
1109 h264type.bDirectSpatialTemporal = OMX_FALSE;
1110 h264type.nCabacInitIdc = 0;
1111 }
1112
1113 if (h264type.nBFrames != 0) {
1114 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1115 }
1116
Andreas Huberea6a38c2009-11-16 15:43:38 -08001117 h264type.bEnableUEP = OMX_FALSE;
1118 h264type.bEnableFMO = OMX_FALSE;
1119 h264type.bEnableASO = OMX_FALSE;
1120 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001121 h264type.bFrameMBsOnly = OMX_TRUE;
1122 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001123 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1124
1125 err = mOMX->setParameter(
1126 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1127 CHECK_EQ(err, OK);
1128
James Dongc0ab2a62010-06-29 16:29:19 -07001129 CHECK_EQ(setupBitRate(bitRate), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001130
1131 return OK;
1132}
1133
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001134status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001135 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001136 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001137
Andreas Huberbe06d262009-08-14 14:37:10 -07001138 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001139 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001140 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001141 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001142 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001143 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001144 compressionFormat = OMX_VIDEO_CodingH263;
1145 } else {
1146 LOGE("Not a supported video mime type: %s", mime);
1147 CHECK(!"Should not be here. Not a supported video mime type.");
1148 }
1149
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001150 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001151 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1152
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001153 if (err != OK) {
1154 return err;
1155 }
1156
Andreas Huberbe06d262009-08-14 14:37:10 -07001157#if 1
1158 {
1159 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001160 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001161 format.nPortIndex = kPortIndexOutput;
1162 format.nIndex = 0;
1163
Andreas Huber784202e2009-10-15 13:46:54 -07001164 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001165 mNode, OMX_IndexParamVideoPortFormat,
1166 &format, sizeof(format));
1167 CHECK_EQ(err, OK);
1168 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1169
1170 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1171
1172 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1173 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1174 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1175 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1176
Andreas Huber784202e2009-10-15 13:46:54 -07001177 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001178 mNode, OMX_IndexParamVideoPortFormat,
1179 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001180
1181 if (err != OK) {
1182 return err;
1183 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001184 }
1185#endif
1186
1187 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001188 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001189 def.nPortIndex = kPortIndexInput;
1190
Andreas Huber4c483422009-09-02 16:05:36 -07001191 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1192
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001193 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001194 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1195
1196 CHECK_EQ(err, OK);
1197
1198#if 1
1199 // XXX Need a (much) better heuristic to compute input buffer sizes.
1200 const size_t X = 64 * 1024;
1201 if (def.nBufferSize < X) {
1202 def.nBufferSize = X;
1203 }
1204#endif
1205
1206 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1207
1208 video_def->nFrameWidth = width;
1209 video_def->nFrameHeight = height;
1210
Andreas Huberb482ce82009-10-29 12:02:48 -07001211 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001212 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1213
Andreas Huber784202e2009-10-15 13:46:54 -07001214 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001215 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001216
1217 if (err != OK) {
1218 return err;
1219 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001220
1221 ////////////////////////////////////////////////////////////////////////////
1222
Andreas Huber4c483422009-09-02 16:05:36 -07001223 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001224 def.nPortIndex = kPortIndexOutput;
1225
Andreas Huber784202e2009-10-15 13:46:54 -07001226 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001227 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1228 CHECK_EQ(err, OK);
1229 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1230
1231#if 0
1232 def.nBufferSize =
1233 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1234#endif
1235
1236 video_def->nFrameWidth = width;
1237 video_def->nFrameHeight = height;
1238
Andreas Huber784202e2009-10-15 13:46:54 -07001239 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001240 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001241
1242 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001243}
1244
Andreas Huberbe06d262009-08-14 14:37:10 -07001245OMXCodec::OMXCodec(
1246 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001247 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001248 const char *mime,
1249 const char *componentName,
1250 const sp<MediaSource> &source)
1251 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001252 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001253 mNode(node),
1254 mQuirks(quirks),
1255 mIsEncoder(isEncoder),
1256 mMIME(strdup(mime)),
1257 mComponentName(strdup(componentName)),
1258 mSource(source),
1259 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001260 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001261 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001262 mSignalledEOS(false),
1263 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001264 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001265 mSeekTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001266 mLeftOverBuffer(NULL),
1267 mPaused(false) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001268 mPortStatus[kPortIndexInput] = ENABLED;
1269 mPortStatus[kPortIndexOutput] = ENABLED;
1270
Andreas Huber4c483422009-09-02 16:05:36 -07001271 setComponentRole();
1272}
1273
Andreas Hubere6c40962009-09-10 14:13:30 -07001274// static
1275void OMXCodec::setComponentRole(
1276 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1277 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001278 struct MimeToRole {
1279 const char *mime;
1280 const char *decoderRole;
1281 const char *encoderRole;
1282 };
1283
1284 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001285 { MEDIA_MIMETYPE_AUDIO_MPEG,
1286 "audio_decoder.mp3", "audio_encoder.mp3" },
1287 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1288 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1289 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1290 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1291 { MEDIA_MIMETYPE_AUDIO_AAC,
1292 "audio_decoder.aac", "audio_encoder.aac" },
1293 { MEDIA_MIMETYPE_VIDEO_AVC,
1294 "video_decoder.avc", "video_encoder.avc" },
1295 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1296 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1297 { MEDIA_MIMETYPE_VIDEO_H263,
1298 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001299 };
1300
1301 static const size_t kNumMimeToRole =
1302 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1303
1304 size_t i;
1305 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001306 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001307 break;
1308 }
1309 }
1310
1311 if (i == kNumMimeToRole) {
1312 return;
1313 }
1314
1315 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001316 isEncoder ? kMimeToRole[i].encoderRole
1317 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001318
1319 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001320 OMX_PARAM_COMPONENTROLETYPE roleParams;
1321 InitOMXParams(&roleParams);
1322
1323 strncpy((char *)roleParams.cRole,
1324 role, OMX_MAX_STRINGNAME_SIZE - 1);
1325
1326 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1327
Andreas Huber784202e2009-10-15 13:46:54 -07001328 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001329 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001330 &roleParams, sizeof(roleParams));
1331
1332 if (err != OK) {
1333 LOGW("Failed to set standard component role '%s'.", role);
1334 }
1335 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001336}
1337
Andreas Hubere6c40962009-09-10 14:13:30 -07001338void OMXCodec::setComponentRole() {
1339 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1340}
1341
Andreas Huberbe06d262009-08-14 14:37:10 -07001342OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001343 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001344
Andreas Huber784202e2009-10-15 13:46:54 -07001345 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001346 CHECK_EQ(err, OK);
1347
1348 mNode = NULL;
1349 setState(DEAD);
1350
1351 clearCodecSpecificData();
1352
1353 free(mComponentName);
1354 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001355
Andreas Huberbe06d262009-08-14 14:37:10 -07001356 free(mMIME);
1357 mMIME = NULL;
1358}
1359
1360status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001361 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001362
1363 CHECK_EQ(mState, LOADED);
1364
1365 status_t err;
1366 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001367 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001368 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001369 setState(LOADED_TO_IDLE);
1370 }
1371
1372 err = allocateBuffers();
1373 CHECK_EQ(err, OK);
1374
1375 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001376 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001377 CHECK_EQ(err, OK);
1378
1379 setState(LOADED_TO_IDLE);
1380 }
1381
1382 while (mState != EXECUTING && mState != ERROR) {
1383 mAsyncCompletion.wait(mLock);
1384 }
1385
1386 return mState == ERROR ? UNKNOWN_ERROR : OK;
1387}
1388
1389// static
1390bool OMXCodec::isIntermediateState(State state) {
1391 return state == LOADED_TO_IDLE
1392 || state == IDLE_TO_EXECUTING
1393 || state == EXECUTING_TO_IDLE
1394 || state == IDLE_TO_LOADED
1395 || state == RECONFIGURING;
1396}
1397
1398status_t OMXCodec::allocateBuffers() {
1399 status_t err = allocateBuffersOnPort(kPortIndexInput);
1400
1401 if (err != OK) {
1402 return err;
1403 }
1404
1405 return allocateBuffersOnPort(kPortIndexOutput);
1406}
1407
1408status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1409 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001410 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001411 def.nPortIndex = portIndex;
1412
Andreas Huber784202e2009-10-15 13:46:54 -07001413 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001414 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1415
1416 if (err != OK) {
1417 return err;
1418 }
1419
Andreas Huber5c0a9132009-08-20 11:16:40 -07001420 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001421 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001422
Andreas Huberbe06d262009-08-14 14:37:10 -07001423 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001424 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001425 CHECK(mem.get() != NULL);
1426
Andreas Huberc712b9f2010-01-20 15:05:46 -08001427 BufferInfo info;
1428 info.mData = NULL;
1429 info.mSize = def.nBufferSize;
1430
Andreas Huberbe06d262009-08-14 14:37:10 -07001431 IOMX::buffer_id buffer;
1432 if (portIndex == kPortIndexInput
1433 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001434 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001435 mem.clear();
1436
Andreas Huberf1fe0642010-01-15 15:28:19 -08001437 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001438 mNode, portIndex, def.nBufferSize, &buffer,
1439 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001440 } else {
1441 err = mOMX->allocateBufferWithBackup(
1442 mNode, portIndex, mem, &buffer);
1443 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001444 } else if (portIndex == kPortIndexOutput
1445 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001446 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001447 mem.clear();
1448
Andreas Huberf1fe0642010-01-15 15:28:19 -08001449 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001450 mNode, portIndex, def.nBufferSize, &buffer,
1451 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001452 } else {
1453 err = mOMX->allocateBufferWithBackup(
1454 mNode, portIndex, mem, &buffer);
1455 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001456 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001457 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001458 }
1459
1460 if (err != OK) {
1461 LOGE("allocate_buffer_with_backup failed");
1462 return err;
1463 }
1464
Andreas Huberc712b9f2010-01-20 15:05:46 -08001465 if (mem != NULL) {
1466 info.mData = mem->pointer();
1467 }
1468
Andreas Huberbe06d262009-08-14 14:37:10 -07001469 info.mBuffer = buffer;
1470 info.mOwnedByComponent = false;
1471 info.mMem = mem;
1472 info.mMediaBuffer = NULL;
1473
1474 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001475 if (!(mOMXLivesLocally
1476 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1477 && (mQuirks & kDefersOutputBufferAllocation))) {
1478 // If the node does not fill in the buffer ptr at this time,
1479 // we will defer creating the MediaBuffer until receiving
1480 // the first FILL_BUFFER_DONE notification instead.
1481 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1482 info.mMediaBuffer->setObserver(this);
1483 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001484 }
1485
1486 mPortBuffers[portIndex].push(info);
1487
Andreas Huber4c483422009-09-02 16:05:36 -07001488 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001489 portIndex == kPortIndexInput ? "input" : "output");
1490 }
1491
Andreas Huber2ea14e22009-12-16 09:30:55 -08001492 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001493
1494 return OK;
1495}
1496
1497void OMXCodec::on_message(const omx_message &msg) {
1498 Mutex::Autolock autoLock(mLock);
1499
1500 switch (msg.type) {
1501 case omx_message::EVENT:
1502 {
1503 onEvent(
1504 msg.u.event_data.event, msg.u.event_data.data1,
1505 msg.u.event_data.data2);
1506
1507 break;
1508 }
1509
1510 case omx_message::EMPTY_BUFFER_DONE:
1511 {
1512 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1513
Andreas Huber4c483422009-09-02 16:05:36 -07001514 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001515
1516 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1517 size_t i = 0;
1518 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1519 ++i;
1520 }
1521
1522 CHECK(i < buffers->size());
1523 if (!(*buffers)[i].mOwnedByComponent) {
1524 LOGW("We already own input buffer %p, yet received "
1525 "an EMPTY_BUFFER_DONE.", buffer);
1526 }
1527
1528 buffers->editItemAt(i).mOwnedByComponent = false;
1529
1530 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001531 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001532
1533 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001534 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001535 CHECK_EQ(err, OK);
1536
1537 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001538 } else if (mState != ERROR
1539 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001540 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1541 drainInputBuffer(&buffers->editItemAt(i));
1542 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001543 break;
1544 }
1545
1546 case omx_message::FILL_BUFFER_DONE:
1547 {
1548 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1549 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1550
Andreas Huber2ea14e22009-12-16 09:30:55 -08001551 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001552 buffer,
1553 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001554 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001555 msg.u.extended_buffer_data.timestamp,
1556 msg.u.extended_buffer_data.timestamp / 1E6);
1557
1558 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1559 size_t i = 0;
1560 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1561 ++i;
1562 }
1563
1564 CHECK(i < buffers->size());
1565 BufferInfo *info = &buffers->editItemAt(i);
1566
1567 if (!info->mOwnedByComponent) {
1568 LOGW("We already own output buffer %p, yet received "
1569 "a FILL_BUFFER_DONE.", buffer);
1570 }
1571
1572 info->mOwnedByComponent = false;
1573
1574 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001575 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001576
1577 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001578 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001579 CHECK_EQ(err, OK);
1580
1581 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001582#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001583 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1584 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001585 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001586 mNoMoreOutputData = true;
1587 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001588#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001589 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1590 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001591
Andreas Huber52733b82010-01-25 10:41:35 -08001592 if (info->mMediaBuffer == NULL) {
1593 CHECK(mOMXLivesLocally);
1594 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1595 CHECK(mQuirks & kDefersOutputBufferAllocation);
1596
1597 // The qcom video decoders on Nexus don't actually allocate
1598 // output buffer memory on a call to OMX_AllocateBuffer
1599 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1600 // structure is only filled in later.
1601
1602 info->mMediaBuffer = new MediaBuffer(
1603 msg.u.extended_buffer_data.data_ptr,
1604 info->mSize);
1605 info->mMediaBuffer->setObserver(this);
1606 }
1607
Andreas Huberbe06d262009-08-14 14:37:10 -07001608 MediaBuffer *buffer = info->mMediaBuffer;
1609
1610 buffer->set_range(
1611 msg.u.extended_buffer_data.range_offset,
1612 msg.u.extended_buffer_data.range_length);
1613
1614 buffer->meta_data()->clear();
1615
Andreas Huberfa8de752009-10-08 10:07:49 -07001616 buffer->meta_data()->setInt64(
1617 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001618
1619 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1620 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1621 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001622 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1623 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1624 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001625
1626 buffer->meta_data()->setPointer(
1627 kKeyPlatformPrivate,
1628 msg.u.extended_buffer_data.platform_private);
1629
1630 buffer->meta_data()->setPointer(
1631 kKeyBufferID,
1632 msg.u.extended_buffer_data.buffer);
1633
1634 mFilledBuffers.push_back(i);
1635 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001636
1637 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1638 CODEC_LOGV("No more output data.");
1639 mNoMoreOutputData = true;
1640 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001641 }
1642
1643 break;
1644 }
1645
1646 default:
1647 {
1648 CHECK(!"should not be here.");
1649 break;
1650 }
1651 }
1652}
1653
1654void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1655 switch (event) {
1656 case OMX_EventCmdComplete:
1657 {
1658 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1659 break;
1660 }
1661
1662 case OMX_EventError:
1663 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001664 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001665
1666 setState(ERROR);
1667 break;
1668 }
1669
1670 case OMX_EventPortSettingsChanged:
1671 {
1672 onPortSettingsChanged(data1);
1673 break;
1674 }
1675
Andreas Huber2ea14e22009-12-16 09:30:55 -08001676#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001677 case OMX_EventBufferFlag:
1678 {
Andreas Huber4c483422009-09-02 16:05:36 -07001679 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001680
1681 if (data1 == kPortIndexOutput) {
1682 mNoMoreOutputData = true;
1683 }
1684 break;
1685 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001686#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001687
1688 default:
1689 {
Andreas Huber4c483422009-09-02 16:05:36 -07001690 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001691 break;
1692 }
1693 }
1694}
1695
Andreas Huberb1678602009-10-19 13:06:40 -07001696// Has the format changed in any way that the client would have to be aware of?
1697static bool formatHasNotablyChanged(
1698 const sp<MetaData> &from, const sp<MetaData> &to) {
1699 if (from.get() == NULL && to.get() == NULL) {
1700 return false;
1701 }
1702
Andreas Huberf68c1682009-10-21 14:01:30 -07001703 if ((from.get() == NULL && to.get() != NULL)
1704 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001705 return true;
1706 }
1707
1708 const char *mime_from, *mime_to;
1709 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1710 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1711
1712 if (strcasecmp(mime_from, mime_to)) {
1713 return true;
1714 }
1715
1716 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1717 int32_t colorFormat_from, colorFormat_to;
1718 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1719 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1720
1721 if (colorFormat_from != colorFormat_to) {
1722 return true;
1723 }
1724
1725 int32_t width_from, width_to;
1726 CHECK(from->findInt32(kKeyWidth, &width_from));
1727 CHECK(to->findInt32(kKeyWidth, &width_to));
1728
1729 if (width_from != width_to) {
1730 return true;
1731 }
1732
1733 int32_t height_from, height_to;
1734 CHECK(from->findInt32(kKeyHeight, &height_from));
1735 CHECK(to->findInt32(kKeyHeight, &height_to));
1736
1737 if (height_from != height_to) {
1738 return true;
1739 }
1740 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1741 int32_t numChannels_from, numChannels_to;
1742 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1743 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1744
1745 if (numChannels_from != numChannels_to) {
1746 return true;
1747 }
1748
1749 int32_t sampleRate_from, sampleRate_to;
1750 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1751 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1752
1753 if (sampleRate_from != sampleRate_to) {
1754 return true;
1755 }
1756 }
1757
1758 return false;
1759}
1760
Andreas Huberbe06d262009-08-14 14:37:10 -07001761void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1762 switch (cmd) {
1763 case OMX_CommandStateSet:
1764 {
1765 onStateChange((OMX_STATETYPE)data);
1766 break;
1767 }
1768
1769 case OMX_CommandPortDisable:
1770 {
1771 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001772 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001773
1774 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1775 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1776 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1777
1778 mPortStatus[portIndex] = DISABLED;
1779
1780 if (mState == RECONFIGURING) {
1781 CHECK_EQ(portIndex, kPortIndexOutput);
1782
Andreas Huberb1678602009-10-19 13:06:40 -07001783 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001784 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001785
1786 // Don't notify clients if the output port settings change
1787 // wasn't of importance to them, i.e. it may be that just the
1788 // number of buffers has changed and nothing else.
1789 mOutputPortSettingsHaveChanged =
1790 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001791
Andreas Huberbe06d262009-08-14 14:37:10 -07001792 enablePortAsync(portIndex);
1793
1794 status_t err = allocateBuffersOnPort(portIndex);
1795 CHECK_EQ(err, OK);
1796 }
1797 break;
1798 }
1799
1800 case OMX_CommandPortEnable:
1801 {
1802 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001803 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001804
1805 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1806 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1807
1808 mPortStatus[portIndex] = ENABLED;
1809
1810 if (mState == RECONFIGURING) {
1811 CHECK_EQ(portIndex, kPortIndexOutput);
1812
1813 setState(EXECUTING);
1814
1815 fillOutputBuffers();
1816 }
1817 break;
1818 }
1819
1820 case OMX_CommandFlush:
1821 {
1822 OMX_U32 portIndex = data;
1823
Andreas Huber4c483422009-09-02 16:05:36 -07001824 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001825
1826 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1827 mPortStatus[portIndex] = ENABLED;
1828
1829 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1830 mPortBuffers[portIndex].size());
1831
1832 if (mState == RECONFIGURING) {
1833 CHECK_EQ(portIndex, kPortIndexOutput);
1834
1835 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001836 } else if (mState == EXECUTING_TO_IDLE) {
1837 if (mPortStatus[kPortIndexInput] == ENABLED
1838 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001839 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001840 "transition from EXECUTING to IDLE.");
1841
1842 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1843 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1844
1845 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001846 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001847 CHECK_EQ(err, OK);
1848 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001849 } else {
1850 // We're flushing both ports in preparation for seeking.
1851
1852 if (mPortStatus[kPortIndexInput] == ENABLED
1853 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001854 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001855 " seek-time.");
1856
Andreas Huber1f24b302010-06-10 11:12:39 -07001857 // We implicitly resume pulling on our upstream source.
1858 mPaused = false;
1859
Andreas Huberbe06d262009-08-14 14:37:10 -07001860 drainInputBuffers();
1861 fillOutputBuffers();
1862 }
1863 }
1864
1865 break;
1866 }
1867
1868 default:
1869 {
Andreas Huber4c483422009-09-02 16:05:36 -07001870 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001871 break;
1872 }
1873 }
1874}
1875
1876void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001877 CODEC_LOGV("onStateChange %d", newState);
1878
Andreas Huberbe06d262009-08-14 14:37:10 -07001879 switch (newState) {
1880 case OMX_StateIdle:
1881 {
Andreas Huber4c483422009-09-02 16:05:36 -07001882 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001883 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001884 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001885 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1886
1887 CHECK_EQ(err, OK);
1888
1889 setState(IDLE_TO_EXECUTING);
1890 } else {
1891 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1892
1893 CHECK_EQ(
1894 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1895 mPortBuffers[kPortIndexInput].size());
1896
1897 CHECK_EQ(
1898 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1899 mPortBuffers[kPortIndexOutput].size());
1900
Andreas Huber784202e2009-10-15 13:46:54 -07001901 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001902 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1903
1904 CHECK_EQ(err, OK);
1905
1906 err = freeBuffersOnPort(kPortIndexInput);
1907 CHECK_EQ(err, OK);
1908
1909 err = freeBuffersOnPort(kPortIndexOutput);
1910 CHECK_EQ(err, OK);
1911
1912 mPortStatus[kPortIndexInput] = ENABLED;
1913 mPortStatus[kPortIndexOutput] = ENABLED;
1914
1915 setState(IDLE_TO_LOADED);
1916 }
1917 break;
1918 }
1919
1920 case OMX_StateExecuting:
1921 {
1922 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1923
Andreas Huber4c483422009-09-02 16:05:36 -07001924 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001925
1926 setState(EXECUTING);
1927
Andreas Huber42978e52009-08-27 10:08:39 -07001928 // Buffers will be submitted to the component in the first
1929 // call to OMXCodec::read as mInitialBufferSubmit is true at
1930 // this point. This ensures that this on_message call returns,
1931 // releases the lock and ::init can notice the state change and
1932 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001933 break;
1934 }
1935
1936 case OMX_StateLoaded:
1937 {
1938 CHECK_EQ(mState, IDLE_TO_LOADED);
1939
Andreas Huber4c483422009-09-02 16:05:36 -07001940 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001941
1942 setState(LOADED);
1943 break;
1944 }
1945
Andreas Huberc712b9f2010-01-20 15:05:46 -08001946 case OMX_StateInvalid:
1947 {
1948 setState(ERROR);
1949 break;
1950 }
1951
Andreas Huberbe06d262009-08-14 14:37:10 -07001952 default:
1953 {
1954 CHECK(!"should not be here.");
1955 break;
1956 }
1957 }
1958}
1959
1960// static
1961size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1962 size_t n = 0;
1963 for (size_t i = 0; i < buffers.size(); ++i) {
1964 if (!buffers[i].mOwnedByComponent) {
1965 ++n;
1966 }
1967 }
1968
1969 return n;
1970}
1971
1972status_t OMXCodec::freeBuffersOnPort(
1973 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1974 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1975
1976 status_t stickyErr = OK;
1977
1978 for (size_t i = buffers->size(); i-- > 0;) {
1979 BufferInfo *info = &buffers->editItemAt(i);
1980
1981 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1982 continue;
1983 }
1984
1985 CHECK_EQ(info->mOwnedByComponent, false);
1986
Andreas Huber92022852009-09-14 15:24:14 -07001987 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1988
Andreas Huberbe06d262009-08-14 14:37:10 -07001989 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001990 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001991
1992 if (err != OK) {
1993 stickyErr = err;
1994 }
1995
1996 if (info->mMediaBuffer != NULL) {
1997 info->mMediaBuffer->setObserver(NULL);
1998
1999 // Make sure nobody but us owns this buffer at this point.
2000 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2001
2002 info->mMediaBuffer->release();
2003 }
2004
2005 buffers->removeAt(i);
2006 }
2007
2008 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2009
2010 return stickyErr;
2011}
2012
2013void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002014 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002015
2016 CHECK_EQ(mState, EXECUTING);
2017 CHECK_EQ(portIndex, kPortIndexOutput);
2018 setState(RECONFIGURING);
2019
2020 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002021 if (!flushPortAsync(portIndex)) {
2022 onCmdComplete(OMX_CommandFlush, portIndex);
2023 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002024 } else {
2025 disablePortAsync(portIndex);
2026 }
2027}
2028
Andreas Huber404cc412009-08-25 14:26:05 -07002029bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002030 CHECK(mState == EXECUTING || mState == RECONFIGURING
2031 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002032
Andreas Huber4c483422009-09-02 16:05:36 -07002033 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002034 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2035 mPortBuffers[portIndex].size());
2036
Andreas Huberbe06d262009-08-14 14:37:10 -07002037 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2038 mPortStatus[portIndex] = SHUTTING_DOWN;
2039
Andreas Huber404cc412009-08-25 14:26:05 -07002040 if ((mQuirks & kRequiresFlushCompleteEmulation)
2041 && countBuffersWeOwn(mPortBuffers[portIndex])
2042 == mPortBuffers[portIndex].size()) {
2043 // No flush is necessary and this component fails to send a
2044 // flush-complete event in this case.
2045
2046 return false;
2047 }
2048
Andreas Huberbe06d262009-08-14 14:37:10 -07002049 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002050 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002051 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002052
2053 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002054}
2055
2056void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2057 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2058
2059 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2060 mPortStatus[portIndex] = DISABLING;
2061
2062 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002063 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002064 CHECK_EQ(err, OK);
2065
2066 freeBuffersOnPort(portIndex, true);
2067}
2068
2069void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2070 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2071
2072 CHECK_EQ(mPortStatus[portIndex], DISABLED);
2073 mPortStatus[portIndex] = ENABLING;
2074
2075 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002076 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002077 CHECK_EQ(err, OK);
2078}
2079
2080void OMXCodec::fillOutputBuffers() {
2081 CHECK_EQ(mState, EXECUTING);
2082
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002083 // This is a workaround for some decoders not properly reporting
2084 // end-of-output-stream. If we own all input buffers and also own
2085 // all output buffers and we already signalled end-of-input-stream,
2086 // the end-of-output-stream is implied.
2087 if (mSignalledEOS
2088 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2089 == mPortBuffers[kPortIndexInput].size()
2090 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2091 == mPortBuffers[kPortIndexOutput].size()) {
2092 mNoMoreOutputData = true;
2093 mBufferFilled.signal();
2094
2095 return;
2096 }
2097
Andreas Huberbe06d262009-08-14 14:37:10 -07002098 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2099 for (size_t i = 0; i < buffers->size(); ++i) {
2100 fillOutputBuffer(&buffers->editItemAt(i));
2101 }
2102}
2103
2104void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002105 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002106
2107 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2108 for (size_t i = 0; i < buffers->size(); ++i) {
2109 drainInputBuffer(&buffers->editItemAt(i));
2110 }
2111}
2112
2113void OMXCodec::drainInputBuffer(BufferInfo *info) {
2114 CHECK_EQ(info->mOwnedByComponent, false);
2115
2116 if (mSignalledEOS) {
2117 return;
2118 }
2119
2120 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2121 const CodecSpecificData *specific =
2122 mCodecSpecificData[mCodecSpecificDataIndex];
2123
2124 size_t size = specific->mSize;
2125
Andreas Hubere6c40962009-09-10 14:13:30 -07002126 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002127 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002128 static const uint8_t kNALStartCode[4] =
2129 { 0x00, 0x00, 0x00, 0x01 };
2130
Andreas Huberc712b9f2010-01-20 15:05:46 -08002131 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002132
2133 size += 4;
2134
Andreas Huberc712b9f2010-01-20 15:05:46 -08002135 memcpy(info->mData, kNALStartCode, 4);
2136 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002137 specific->mData, specific->mSize);
2138 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002139 CHECK(info->mSize >= specific->mSize);
2140 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002141 }
2142
Andreas Huber2ea14e22009-12-16 09:30:55 -08002143 mNoMoreOutputData = false;
2144
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002145 CODEC_LOGV("calling emptyBuffer with codec specific data");
2146
Andreas Huber784202e2009-10-15 13:46:54 -07002147 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002148 mNode, info->mBuffer, 0, size,
2149 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2150 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002151 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002152
2153 info->mOwnedByComponent = true;
2154
2155 ++mCodecSpecificDataIndex;
2156 return;
2157 }
2158
Andreas Huber1f24b302010-06-10 11:12:39 -07002159 if (mPaused) {
2160 return;
2161 }
2162
Andreas Huberbe06d262009-08-14 14:37:10 -07002163 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002164
Andreas Hubera4357ad2010-04-02 12:49:54 -07002165 bool signalEOS = false;
2166 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002167
Andreas Hubera4357ad2010-04-02 12:49:54 -07002168 size_t offset = 0;
2169 int32_t n = 0;
2170 for (;;) {
2171 MediaBuffer *srcBuffer;
2172 if (mSeekTimeUs >= 0) {
2173 if (mLeftOverBuffer) {
2174 mLeftOverBuffer->release();
2175 mLeftOverBuffer = NULL;
2176 }
2177
2178 MediaSource::ReadOptions options;
2179 options.setSeekTo(mSeekTimeUs);
2180
2181 mSeekTimeUs = -1;
2182 mBufferFilled.signal();
2183
2184 err = mSource->read(&srcBuffer, &options);
2185 } else if (mLeftOverBuffer) {
2186 srcBuffer = mLeftOverBuffer;
2187 mLeftOverBuffer = NULL;
2188
2189 err = OK;
2190 } else {
2191 err = mSource->read(&srcBuffer);
2192 }
2193
2194 if (err != OK) {
2195 signalEOS = true;
2196 mFinalStatus = err;
2197 mSignalledEOS = true;
2198 break;
2199 }
2200
2201 size_t remainingBytes = info->mSize - offset;
2202
2203 if (srcBuffer->range_length() > remainingBytes) {
2204 if (offset == 0) {
2205 CODEC_LOGE(
2206 "Codec's input buffers are too small to accomodate "
2207 "buffer read from source (info->mSize = %d, srcLength = %d)",
2208 info->mSize, srcBuffer->range_length());
2209
2210 srcBuffer->release();
2211 srcBuffer = NULL;
2212
2213 setState(ERROR);
2214 return;
2215 }
2216
2217 mLeftOverBuffer = srcBuffer;
2218 break;
2219 }
2220
James Dong4f501f02010-06-07 14:41:41 -07002221 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2222 CHECK(mOMXLivesLocally && offset == 0);
2223 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2224 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2225 } else {
2226 memcpy((uint8_t *)info->mData + offset,
2227 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2228 srcBuffer->range_length());
2229 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002230
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002231 int64_t lastBufferTimeUs;
2232 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
2233 CHECK(timestampUs >= 0);
2234
Andreas Hubera4357ad2010-04-02 12:49:54 -07002235 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002236 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002237 }
2238
2239 offset += srcBuffer->range_length();
2240
2241 srcBuffer->release();
2242 srcBuffer = NULL;
2243
2244 ++n;
2245
2246 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2247 break;
2248 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002249
2250 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2251
2252 if (coalescedDurationUs > 250000ll) {
2253 // Don't coalesce more than 250ms worth of encoded data at once.
2254 break;
2255 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002256 }
2257
2258 if (n > 1) {
2259 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002260 }
2261
2262 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002263
Andreas Hubera4357ad2010-04-02 12:49:54 -07002264 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002265 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002266 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002267 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002268 }
2269
Andreas Hubera4357ad2010-04-02 12:49:54 -07002270 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2271 "timestamp %lld us (%.2f secs)",
2272 info->mBuffer, offset,
2273 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002274
Andreas Huber784202e2009-10-15 13:46:54 -07002275 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002276 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002277 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002278
2279 if (err != OK) {
2280 setState(ERROR);
2281 return;
2282 }
2283
2284 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002285
2286 // This component does not ever signal the EOS flag on output buffers,
2287 // Thanks for nothing.
2288 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2289 mNoMoreOutputData = true;
2290 mBufferFilled.signal();
2291 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002292}
2293
2294void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2295 CHECK_EQ(info->mOwnedByComponent, false);
2296
Andreas Huber404cc412009-08-25 14:26:05 -07002297 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002298 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002299 "calling fillOutputBuffer");
2300 return;
2301 }
2302
Andreas Huber4c483422009-09-02 16:05:36 -07002303 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002304 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002305
2306 if (err != OK) {
2307 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2308
2309 setState(ERROR);
2310 return;
2311 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002312
2313 info->mOwnedByComponent = true;
2314}
2315
2316void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2317 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2318 for (size_t i = 0; i < buffers->size(); ++i) {
2319 if ((*buffers)[i].mBuffer == buffer) {
2320 drainInputBuffer(&buffers->editItemAt(i));
2321 return;
2322 }
2323 }
2324
2325 CHECK(!"should not be here.");
2326}
2327
2328void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2329 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2330 for (size_t i = 0; i < buffers->size(); ++i) {
2331 if ((*buffers)[i].mBuffer == buffer) {
2332 fillOutputBuffer(&buffers->editItemAt(i));
2333 return;
2334 }
2335 }
2336
2337 CHECK(!"should not be here.");
2338}
2339
2340void OMXCodec::setState(State newState) {
2341 mState = newState;
2342 mAsyncCompletion.signal();
2343
2344 // This may cause some spurious wakeups but is necessary to
2345 // unblock the reader if we enter ERROR state.
2346 mBufferFilled.signal();
2347}
2348
Andreas Huberda050cf22009-09-02 14:01:43 -07002349void OMXCodec::setRawAudioFormat(
2350 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002351
2352 // port definition
2353 OMX_PARAM_PORTDEFINITIONTYPE def;
2354 InitOMXParams(&def);
2355 def.nPortIndex = portIndex;
2356 status_t err = mOMX->getParameter(
2357 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2358 CHECK_EQ(err, OK);
2359 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2360 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2361 &def, sizeof(def)), OK);
2362
2363 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002364 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002365 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002366 pcmParams.nPortIndex = portIndex;
2367
James Dongabed93a2010-04-22 17:27:04 -07002368 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002369 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2370
2371 CHECK_EQ(err, OK);
2372
2373 pcmParams.nChannels = numChannels;
2374 pcmParams.eNumData = OMX_NumericalDataSigned;
2375 pcmParams.bInterleaved = OMX_TRUE;
2376 pcmParams.nBitPerSample = 16;
2377 pcmParams.nSamplingRate = sampleRate;
2378 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2379
2380 if (numChannels == 1) {
2381 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2382 } else {
2383 CHECK_EQ(numChannels, 2);
2384
2385 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2386 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2387 }
2388
Andreas Huber784202e2009-10-15 13:46:54 -07002389 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002390 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2391
2392 CHECK_EQ(err, OK);
2393}
2394
James Dong17299ab2010-05-14 15:45:22 -07002395static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2396 if (isAMRWB) {
2397 if (bps <= 6600) {
2398 return OMX_AUDIO_AMRBandModeWB0;
2399 } else if (bps <= 8850) {
2400 return OMX_AUDIO_AMRBandModeWB1;
2401 } else if (bps <= 12650) {
2402 return OMX_AUDIO_AMRBandModeWB2;
2403 } else if (bps <= 14250) {
2404 return OMX_AUDIO_AMRBandModeWB3;
2405 } else if (bps <= 15850) {
2406 return OMX_AUDIO_AMRBandModeWB4;
2407 } else if (bps <= 18250) {
2408 return OMX_AUDIO_AMRBandModeWB5;
2409 } else if (bps <= 19850) {
2410 return OMX_AUDIO_AMRBandModeWB6;
2411 } else if (bps <= 23050) {
2412 return OMX_AUDIO_AMRBandModeWB7;
2413 }
2414
2415 // 23850 bps
2416 return OMX_AUDIO_AMRBandModeWB8;
2417 } else { // AMRNB
2418 if (bps <= 4750) {
2419 return OMX_AUDIO_AMRBandModeNB0;
2420 } else if (bps <= 5150) {
2421 return OMX_AUDIO_AMRBandModeNB1;
2422 } else if (bps <= 5900) {
2423 return OMX_AUDIO_AMRBandModeNB2;
2424 } else if (bps <= 6700) {
2425 return OMX_AUDIO_AMRBandModeNB3;
2426 } else if (bps <= 7400) {
2427 return OMX_AUDIO_AMRBandModeNB4;
2428 } else if (bps <= 7950) {
2429 return OMX_AUDIO_AMRBandModeNB5;
2430 } else if (bps <= 10200) {
2431 return OMX_AUDIO_AMRBandModeNB6;
2432 }
2433
2434 // 12200 bps
2435 return OMX_AUDIO_AMRBandModeNB7;
2436 }
2437}
2438
2439void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002440 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002441
Andreas Huber8768f2c2009-12-01 15:26:54 -08002442 OMX_AUDIO_PARAM_AMRTYPE def;
2443 InitOMXParams(&def);
2444 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002445
Andreas Huber8768f2c2009-12-01 15:26:54 -08002446 status_t err =
2447 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002448
Andreas Huber8768f2c2009-12-01 15:26:54 -08002449 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002450
Andreas Huber8768f2c2009-12-01 15:26:54 -08002451 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002452
James Dong17299ab2010-05-14 15:45:22 -07002453 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002454 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2455 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002456
2457 ////////////////////////
2458
2459 if (mIsEncoder) {
2460 sp<MetaData> format = mSource->getFormat();
2461 int32_t sampleRate;
2462 int32_t numChannels;
2463 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2464 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2465
2466 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2467 }
2468}
2469
James Dong17299ab2010-05-14 15:45:22 -07002470void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002471 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002472 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002473 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002474 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002475
2476 //////////////// output port ////////////////////
2477 // format
2478 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2479 format.nPortIndex = kPortIndexOutput;
2480 format.nIndex = 0;
2481 status_t err = OMX_ErrorNone;
2482 while (OMX_ErrorNone == err) {
2483 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2484 &format, sizeof(format)), OK);
2485 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2486 break;
2487 }
2488 format.nIndex++;
2489 }
2490 CHECK_EQ(OK, err);
2491 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2492 &format, sizeof(format)), OK);
2493
2494 // port definition
2495 OMX_PARAM_PORTDEFINITIONTYPE def;
2496 InitOMXParams(&def);
2497 def.nPortIndex = kPortIndexOutput;
2498 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2499 &def, sizeof(def)), OK);
2500 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2501 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2502 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2503 &def, sizeof(def)), OK);
2504
2505 // profile
2506 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2507 InitOMXParams(&profile);
2508 profile.nPortIndex = kPortIndexOutput;
2509 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2510 &profile, sizeof(profile)), OK);
2511 profile.nChannels = numChannels;
2512 profile.eChannelMode = (numChannels == 1?
2513 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2514 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002515 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002516 profile.nAudioBandWidth = 0;
2517 profile.nFrameLength = 0;
2518 profile.nAACtools = OMX_AUDIO_AACToolAll;
2519 profile.nAACERtools = OMX_AUDIO_AACERNone;
2520 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2521 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2522 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2523 &profile, sizeof(profile)), OK);
2524
Andreas Huberda050cf22009-09-02 14:01:43 -07002525 } else {
2526 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002527 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002528 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002529
Andreas Huber784202e2009-10-15 13:46:54 -07002530 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002531 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2532 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002533
Andreas Huberda050cf22009-09-02 14:01:43 -07002534 profile.nChannels = numChannels;
2535 profile.nSampleRate = sampleRate;
2536 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002537
Andreas Huber784202e2009-10-15 13:46:54 -07002538 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002539 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2540 CHECK_EQ(err, OK);
2541 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002542}
2543
2544void OMXCodec::setImageOutputFormat(
2545 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002546 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002547
2548#if 0
2549 OMX_INDEXTYPE index;
2550 status_t err = mOMX->get_extension_index(
2551 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2552 CHECK_EQ(err, OK);
2553
2554 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2555 CHECK_EQ(err, OK);
2556#endif
2557
2558 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002559 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002560 def.nPortIndex = kPortIndexOutput;
2561
Andreas Huber784202e2009-10-15 13:46:54 -07002562 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002563 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2564 CHECK_EQ(err, OK);
2565
2566 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2567
2568 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002569
Andreas Huberbe06d262009-08-14 14:37:10 -07002570 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2571 imageDef->eColorFormat = format;
2572 imageDef->nFrameWidth = width;
2573 imageDef->nFrameHeight = height;
2574
2575 switch (format) {
2576 case OMX_COLOR_FormatYUV420PackedPlanar:
2577 case OMX_COLOR_FormatYUV411Planar:
2578 {
2579 def.nBufferSize = (width * height * 3) / 2;
2580 break;
2581 }
2582
2583 case OMX_COLOR_FormatCbYCrY:
2584 {
2585 def.nBufferSize = width * height * 2;
2586 break;
2587 }
2588
2589 case OMX_COLOR_Format32bitARGB8888:
2590 {
2591 def.nBufferSize = width * height * 4;
2592 break;
2593 }
2594
Andreas Huber201511c2009-09-08 14:01:44 -07002595 case OMX_COLOR_Format16bitARGB4444:
2596 case OMX_COLOR_Format16bitARGB1555:
2597 case OMX_COLOR_Format16bitRGB565:
2598 case OMX_COLOR_Format16bitBGR565:
2599 {
2600 def.nBufferSize = width * height * 2;
2601 break;
2602 }
2603
Andreas Huberbe06d262009-08-14 14:37:10 -07002604 default:
2605 CHECK(!"Should not be here. Unknown color format.");
2606 break;
2607 }
2608
Andreas Huber5c0a9132009-08-20 11:16:40 -07002609 def.nBufferCountActual = def.nBufferCountMin;
2610
Andreas Huber784202e2009-10-15 13:46:54 -07002611 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002612 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2613 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002614}
Andreas Huberbe06d262009-08-14 14:37:10 -07002615
Andreas Huber5c0a9132009-08-20 11:16:40 -07002616void OMXCodec::setJPEGInputFormat(
2617 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2618 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002619 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002620 def.nPortIndex = kPortIndexInput;
2621
Andreas Huber784202e2009-10-15 13:46:54 -07002622 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002623 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2624 CHECK_EQ(err, OK);
2625
Andreas Huber5c0a9132009-08-20 11:16:40 -07002626 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2627 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2628
Andreas Huberbe06d262009-08-14 14:37:10 -07002629 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2630 imageDef->nFrameWidth = width;
2631 imageDef->nFrameHeight = height;
2632
Andreas Huber5c0a9132009-08-20 11:16:40 -07002633 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002634 def.nBufferCountActual = def.nBufferCountMin;
2635
Andreas Huber784202e2009-10-15 13:46:54 -07002636 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002637 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2638 CHECK_EQ(err, OK);
2639}
2640
2641void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2642 CodecSpecificData *specific =
2643 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2644
2645 specific->mSize = size;
2646 memcpy(specific->mData, data, size);
2647
2648 mCodecSpecificData.push(specific);
2649}
2650
2651void OMXCodec::clearCodecSpecificData() {
2652 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2653 free(mCodecSpecificData.editItemAt(i));
2654 }
2655 mCodecSpecificData.clear();
2656 mCodecSpecificDataIndex = 0;
2657}
2658
James Dong36e573b2010-06-19 09:04:18 -07002659status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07002660 Mutex::Autolock autoLock(mLock);
2661
Andreas Huberbe06d262009-08-14 14:37:10 -07002662 if (mState != LOADED) {
2663 return UNKNOWN_ERROR;
2664 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002665
Andreas Huberbe06d262009-08-14 14:37:10 -07002666 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002667 if (mQuirks & kWantsNALFragments) {
2668 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002669 }
James Dong36e573b2010-06-19 09:04:18 -07002670 if (meta) {
2671 int64_t startTimeUs = 0;
2672 int64_t timeUs;
2673 if (meta->findInt64(kKeyTime, &timeUs)) {
2674 startTimeUs = timeUs;
2675 }
2676 params->setInt64(kKeyTime, startTimeUs);
2677 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002678 status_t err = mSource->start(params.get());
2679
2680 if (err != OK) {
2681 return err;
2682 }
2683
2684 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002685 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002686 mSignalledEOS = false;
2687 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002688 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002689 mSeekTimeUs = -1;
2690 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07002691 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002692
2693 return init();
2694}
2695
2696status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002697 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002698
2699 Mutex::Autolock autoLock(mLock);
2700
2701 while (isIntermediateState(mState)) {
2702 mAsyncCompletion.wait(mLock);
2703 }
2704
2705 switch (mState) {
2706 case LOADED:
2707 case ERROR:
2708 break;
2709
2710 case EXECUTING:
2711 {
2712 setState(EXECUTING_TO_IDLE);
2713
Andreas Huber127fcdc2009-08-26 16:27:02 -07002714 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002715 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002716 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002717
Andreas Huber127fcdc2009-08-26 16:27:02 -07002718 bool emulateInputFlushCompletion =
2719 !flushPortAsync(kPortIndexInput);
2720
2721 bool emulateOutputFlushCompletion =
2722 !flushPortAsync(kPortIndexOutput);
2723
2724 if (emulateInputFlushCompletion) {
2725 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2726 }
2727
2728 if (emulateOutputFlushCompletion) {
2729 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2730 }
2731 } else {
2732 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2733 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2734
2735 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002736 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002737 CHECK_EQ(err, OK);
2738 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002739
2740 while (mState != LOADED && mState != ERROR) {
2741 mAsyncCompletion.wait(mLock);
2742 }
2743
2744 break;
2745 }
2746
2747 default:
2748 {
2749 CHECK(!"should not be here.");
2750 break;
2751 }
2752 }
2753
Andreas Hubera4357ad2010-04-02 12:49:54 -07002754 if (mLeftOverBuffer) {
2755 mLeftOverBuffer->release();
2756 mLeftOverBuffer = NULL;
2757 }
2758
Andreas Huberbe06d262009-08-14 14:37:10 -07002759 mSource->stop();
2760
Andreas Huber4a9375e2010-02-09 11:54:33 -08002761 CODEC_LOGV("stopped");
2762
Andreas Huberbe06d262009-08-14 14:37:10 -07002763 return OK;
2764}
2765
2766sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002767 Mutex::Autolock autoLock(mLock);
2768
Andreas Huberbe06d262009-08-14 14:37:10 -07002769 return mOutputFormat;
2770}
2771
2772status_t OMXCodec::read(
2773 MediaBuffer **buffer, const ReadOptions *options) {
2774 *buffer = NULL;
2775
2776 Mutex::Autolock autoLock(mLock);
2777
Andreas Huberd06e5b82009-08-28 13:18:14 -07002778 if (mState != EXECUTING && mState != RECONFIGURING) {
2779 return UNKNOWN_ERROR;
2780 }
2781
Andreas Hubere981c332009-10-22 13:49:30 -07002782 bool seeking = false;
2783 int64_t seekTimeUs;
2784 if (options && options->getSeekTo(&seekTimeUs)) {
2785 seeking = true;
2786 }
2787
Andreas Huber42978e52009-08-27 10:08:39 -07002788 if (mInitialBufferSubmit) {
2789 mInitialBufferSubmit = false;
2790
Andreas Hubere981c332009-10-22 13:49:30 -07002791 if (seeking) {
2792 CHECK(seekTimeUs >= 0);
2793 mSeekTimeUs = seekTimeUs;
2794
2795 // There's no reason to trigger the code below, there's
2796 // nothing to flush yet.
2797 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002798 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07002799 }
2800
Andreas Huber42978e52009-08-27 10:08:39 -07002801 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002802
Andreas Huberd06e5b82009-08-28 13:18:14 -07002803 if (mState == EXECUTING) {
2804 // Otherwise mState == RECONFIGURING and this code will trigger
2805 // after the output port is reenabled.
2806 fillOutputBuffers();
2807 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002808 }
2809
Andreas Hubere981c332009-10-22 13:49:30 -07002810 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002811 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002812
2813 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002814
2815 CHECK(seekTimeUs >= 0);
2816 mSeekTimeUs = seekTimeUs;
2817
2818 mFilledBuffers.clear();
2819
2820 CHECK_EQ(mState, EXECUTING);
2821
Andreas Huber404cc412009-08-25 14:26:05 -07002822 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2823 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2824
2825 if (emulateInputFlushCompletion) {
2826 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2827 }
2828
2829 if (emulateOutputFlushCompletion) {
2830 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2831 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002832
2833 while (mSeekTimeUs >= 0) {
2834 mBufferFilled.wait(mLock);
2835 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002836 }
2837
2838 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2839 mBufferFilled.wait(mLock);
2840 }
2841
2842 if (mState == ERROR) {
2843 return UNKNOWN_ERROR;
2844 }
2845
2846 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08002847 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07002848 }
2849
Andreas Hubercfd55572009-10-09 14:11:28 -07002850 if (mOutputPortSettingsHaveChanged) {
2851 mOutputPortSettingsHaveChanged = false;
2852
2853 return INFO_FORMAT_CHANGED;
2854 }
2855
Andreas Huberbe06d262009-08-14 14:37:10 -07002856 size_t index = *mFilledBuffers.begin();
2857 mFilledBuffers.erase(mFilledBuffers.begin());
2858
2859 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2860 info->mMediaBuffer->add_ref();
2861 *buffer = info->mMediaBuffer;
2862
2863 return OK;
2864}
2865
2866void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2867 Mutex::Autolock autoLock(mLock);
2868
2869 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2870 for (size_t i = 0; i < buffers->size(); ++i) {
2871 BufferInfo *info = &buffers->editItemAt(i);
2872
2873 if (info->mMediaBuffer == buffer) {
2874 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2875 fillOutputBuffer(info);
2876 return;
2877 }
2878 }
2879
2880 CHECK(!"should not be here.");
2881}
2882
2883static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2884 static const char *kNames[] = {
2885 "OMX_IMAGE_CodingUnused",
2886 "OMX_IMAGE_CodingAutoDetect",
2887 "OMX_IMAGE_CodingJPEG",
2888 "OMX_IMAGE_CodingJPEG2K",
2889 "OMX_IMAGE_CodingEXIF",
2890 "OMX_IMAGE_CodingTIFF",
2891 "OMX_IMAGE_CodingGIF",
2892 "OMX_IMAGE_CodingPNG",
2893 "OMX_IMAGE_CodingLZW",
2894 "OMX_IMAGE_CodingBMP",
2895 };
2896
2897 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2898
2899 if (type < 0 || (size_t)type >= numNames) {
2900 return "UNKNOWN";
2901 } else {
2902 return kNames[type];
2903 }
2904}
2905
2906static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2907 static const char *kNames[] = {
2908 "OMX_COLOR_FormatUnused",
2909 "OMX_COLOR_FormatMonochrome",
2910 "OMX_COLOR_Format8bitRGB332",
2911 "OMX_COLOR_Format12bitRGB444",
2912 "OMX_COLOR_Format16bitARGB4444",
2913 "OMX_COLOR_Format16bitARGB1555",
2914 "OMX_COLOR_Format16bitRGB565",
2915 "OMX_COLOR_Format16bitBGR565",
2916 "OMX_COLOR_Format18bitRGB666",
2917 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002918 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002919 "OMX_COLOR_Format24bitRGB888",
2920 "OMX_COLOR_Format24bitBGR888",
2921 "OMX_COLOR_Format24bitARGB1887",
2922 "OMX_COLOR_Format25bitARGB1888",
2923 "OMX_COLOR_Format32bitBGRA8888",
2924 "OMX_COLOR_Format32bitARGB8888",
2925 "OMX_COLOR_FormatYUV411Planar",
2926 "OMX_COLOR_FormatYUV411PackedPlanar",
2927 "OMX_COLOR_FormatYUV420Planar",
2928 "OMX_COLOR_FormatYUV420PackedPlanar",
2929 "OMX_COLOR_FormatYUV420SemiPlanar",
2930 "OMX_COLOR_FormatYUV422Planar",
2931 "OMX_COLOR_FormatYUV422PackedPlanar",
2932 "OMX_COLOR_FormatYUV422SemiPlanar",
2933 "OMX_COLOR_FormatYCbYCr",
2934 "OMX_COLOR_FormatYCrYCb",
2935 "OMX_COLOR_FormatCbYCrY",
2936 "OMX_COLOR_FormatCrYCbY",
2937 "OMX_COLOR_FormatYUV444Interleaved",
2938 "OMX_COLOR_FormatRawBayer8bit",
2939 "OMX_COLOR_FormatRawBayer10bit",
2940 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002941 "OMX_COLOR_FormatL2",
2942 "OMX_COLOR_FormatL4",
2943 "OMX_COLOR_FormatL8",
2944 "OMX_COLOR_FormatL16",
2945 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002946 "OMX_COLOR_FormatL32",
2947 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2948 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2949 "OMX_COLOR_Format18BitBGR666",
2950 "OMX_COLOR_Format24BitARGB6666",
2951 "OMX_COLOR_Format24BitABGR6666",
2952 };
2953
2954 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2955
Andreas Huberbe06d262009-08-14 14:37:10 -07002956 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2957 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2958 } else if (type < 0 || (size_t)type >= numNames) {
2959 return "UNKNOWN";
2960 } else {
2961 return kNames[type];
2962 }
2963}
2964
2965static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2966 static const char *kNames[] = {
2967 "OMX_VIDEO_CodingUnused",
2968 "OMX_VIDEO_CodingAutoDetect",
2969 "OMX_VIDEO_CodingMPEG2",
2970 "OMX_VIDEO_CodingH263",
2971 "OMX_VIDEO_CodingMPEG4",
2972 "OMX_VIDEO_CodingWMV",
2973 "OMX_VIDEO_CodingRV",
2974 "OMX_VIDEO_CodingAVC",
2975 "OMX_VIDEO_CodingMJPEG",
2976 };
2977
2978 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2979
2980 if (type < 0 || (size_t)type >= numNames) {
2981 return "UNKNOWN";
2982 } else {
2983 return kNames[type];
2984 }
2985}
2986
2987static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2988 static const char *kNames[] = {
2989 "OMX_AUDIO_CodingUnused",
2990 "OMX_AUDIO_CodingAutoDetect",
2991 "OMX_AUDIO_CodingPCM",
2992 "OMX_AUDIO_CodingADPCM",
2993 "OMX_AUDIO_CodingAMR",
2994 "OMX_AUDIO_CodingGSMFR",
2995 "OMX_AUDIO_CodingGSMEFR",
2996 "OMX_AUDIO_CodingGSMHR",
2997 "OMX_AUDIO_CodingPDCFR",
2998 "OMX_AUDIO_CodingPDCEFR",
2999 "OMX_AUDIO_CodingPDCHR",
3000 "OMX_AUDIO_CodingTDMAFR",
3001 "OMX_AUDIO_CodingTDMAEFR",
3002 "OMX_AUDIO_CodingQCELP8",
3003 "OMX_AUDIO_CodingQCELP13",
3004 "OMX_AUDIO_CodingEVRC",
3005 "OMX_AUDIO_CodingSMV",
3006 "OMX_AUDIO_CodingG711",
3007 "OMX_AUDIO_CodingG723",
3008 "OMX_AUDIO_CodingG726",
3009 "OMX_AUDIO_CodingG729",
3010 "OMX_AUDIO_CodingAAC",
3011 "OMX_AUDIO_CodingMP3",
3012 "OMX_AUDIO_CodingSBC",
3013 "OMX_AUDIO_CodingVORBIS",
3014 "OMX_AUDIO_CodingWMA",
3015 "OMX_AUDIO_CodingRA",
3016 "OMX_AUDIO_CodingMIDI",
3017 };
3018
3019 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3020
3021 if (type < 0 || (size_t)type >= numNames) {
3022 return "UNKNOWN";
3023 } else {
3024 return kNames[type];
3025 }
3026}
3027
3028static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3029 static const char *kNames[] = {
3030 "OMX_AUDIO_PCMModeLinear",
3031 "OMX_AUDIO_PCMModeALaw",
3032 "OMX_AUDIO_PCMModeMULaw",
3033 };
3034
3035 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3036
3037 if (type < 0 || (size_t)type >= numNames) {
3038 return "UNKNOWN";
3039 } else {
3040 return kNames[type];
3041 }
3042}
3043
Andreas Huber7ae02c82009-09-09 16:29:47 -07003044static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3045 static const char *kNames[] = {
3046 "OMX_AUDIO_AMRBandModeUnused",
3047 "OMX_AUDIO_AMRBandModeNB0",
3048 "OMX_AUDIO_AMRBandModeNB1",
3049 "OMX_AUDIO_AMRBandModeNB2",
3050 "OMX_AUDIO_AMRBandModeNB3",
3051 "OMX_AUDIO_AMRBandModeNB4",
3052 "OMX_AUDIO_AMRBandModeNB5",
3053 "OMX_AUDIO_AMRBandModeNB6",
3054 "OMX_AUDIO_AMRBandModeNB7",
3055 "OMX_AUDIO_AMRBandModeWB0",
3056 "OMX_AUDIO_AMRBandModeWB1",
3057 "OMX_AUDIO_AMRBandModeWB2",
3058 "OMX_AUDIO_AMRBandModeWB3",
3059 "OMX_AUDIO_AMRBandModeWB4",
3060 "OMX_AUDIO_AMRBandModeWB5",
3061 "OMX_AUDIO_AMRBandModeWB6",
3062 "OMX_AUDIO_AMRBandModeWB7",
3063 "OMX_AUDIO_AMRBandModeWB8",
3064 };
3065
3066 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3067
3068 if (type < 0 || (size_t)type >= numNames) {
3069 return "UNKNOWN";
3070 } else {
3071 return kNames[type];
3072 }
3073}
3074
3075static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3076 static const char *kNames[] = {
3077 "OMX_AUDIO_AMRFrameFormatConformance",
3078 "OMX_AUDIO_AMRFrameFormatIF1",
3079 "OMX_AUDIO_AMRFrameFormatIF2",
3080 "OMX_AUDIO_AMRFrameFormatFSF",
3081 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3082 "OMX_AUDIO_AMRFrameFormatITU",
3083 };
3084
3085 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3086
3087 if (type < 0 || (size_t)type >= numNames) {
3088 return "UNKNOWN";
3089 } else {
3090 return kNames[type];
3091 }
3092}
Andreas Huberbe06d262009-08-14 14:37:10 -07003093
3094void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3095 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003096 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003097 def.nPortIndex = portIndex;
3098
Andreas Huber784202e2009-10-15 13:46:54 -07003099 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003100 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3101 CHECK_EQ(err, OK);
3102
3103 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3104
3105 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3106 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3107
3108 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3109 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3110 printf(" nBufferSize = %ld\n", def.nBufferSize);
3111
3112 switch (def.eDomain) {
3113 case OMX_PortDomainImage:
3114 {
3115 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3116
3117 printf("\n");
3118 printf(" // Image\n");
3119 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3120 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3121 printf(" nStride = %ld\n", imageDef->nStride);
3122
3123 printf(" eCompressionFormat = %s\n",
3124 imageCompressionFormatString(imageDef->eCompressionFormat));
3125
3126 printf(" eColorFormat = %s\n",
3127 colorFormatString(imageDef->eColorFormat));
3128
3129 break;
3130 }
3131
3132 case OMX_PortDomainVideo:
3133 {
3134 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3135
3136 printf("\n");
3137 printf(" // Video\n");
3138 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3139 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3140 printf(" nStride = %ld\n", videoDef->nStride);
3141
3142 printf(" eCompressionFormat = %s\n",
3143 videoCompressionFormatString(videoDef->eCompressionFormat));
3144
3145 printf(" eColorFormat = %s\n",
3146 colorFormatString(videoDef->eColorFormat));
3147
3148 break;
3149 }
3150
3151 case OMX_PortDomainAudio:
3152 {
3153 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3154
3155 printf("\n");
3156 printf(" // Audio\n");
3157 printf(" eEncoding = %s\n",
3158 audioCodingTypeString(audioDef->eEncoding));
3159
3160 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3161 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003162 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003163 params.nPortIndex = portIndex;
3164
Andreas Huber784202e2009-10-15 13:46:54 -07003165 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003166 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3167 CHECK_EQ(err, OK);
3168
3169 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3170 printf(" nChannels = %ld\n", params.nChannels);
3171 printf(" bInterleaved = %d\n", params.bInterleaved);
3172 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3173
3174 printf(" eNumData = %s\n",
3175 params.eNumData == OMX_NumericalDataSigned
3176 ? "signed" : "unsigned");
3177
3178 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003179 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3180 OMX_AUDIO_PARAM_AMRTYPE amr;
3181 InitOMXParams(&amr);
3182 amr.nPortIndex = portIndex;
3183
Andreas Huber784202e2009-10-15 13:46:54 -07003184 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003185 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3186 CHECK_EQ(err, OK);
3187
3188 printf(" nChannels = %ld\n", amr.nChannels);
3189 printf(" eAMRBandMode = %s\n",
3190 amrBandModeString(amr.eAMRBandMode));
3191 printf(" eAMRFrameFormat = %s\n",
3192 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003193 }
3194
3195 break;
3196 }
3197
3198 default:
3199 {
3200 printf(" // Unknown\n");
3201 break;
3202 }
3203 }
3204
3205 printf("}\n");
3206}
3207
3208void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3209 mOutputFormat = new MetaData;
3210 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
3211
3212 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003213 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003214 def.nPortIndex = kPortIndexOutput;
3215
Andreas Huber784202e2009-10-15 13:46:54 -07003216 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003217 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3218 CHECK_EQ(err, OK);
3219
3220 switch (def.eDomain) {
3221 case OMX_PortDomainImage:
3222 {
3223 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3224 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3225
Andreas Hubere6c40962009-09-10 14:13:30 -07003226 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003227 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3228 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3229 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3230 break;
3231 }
3232
3233 case OMX_PortDomainAudio:
3234 {
3235 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3236
Andreas Huberda050cf22009-09-02 14:01:43 -07003237 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3238 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003239 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003240 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003241
Andreas Huber784202e2009-10-15 13:46:54 -07003242 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003243 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3244 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003245
Andreas Huberda050cf22009-09-02 14:01:43 -07003246 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3247 CHECK_EQ(params.nBitPerSample, 16);
3248 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003249
Andreas Huberda050cf22009-09-02 14:01:43 -07003250 int32_t numChannels, sampleRate;
3251 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3252 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003253
Andreas Huberda050cf22009-09-02 14:01:43 -07003254 if ((OMX_U32)numChannels != params.nChannels) {
3255 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003256 "the input stream contains (contains %d channels, "
3257 "codec outputs %ld channels).",
3258 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003259 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003260
Andreas Hubere6c40962009-09-10 14:13:30 -07003261 mOutputFormat->setCString(
3262 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003263
3264 // Use the codec-advertised number of channels, as some
3265 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003266 // mono. If we know the codec lies about this information,
3267 // use the actual number of channels instead.
3268 mOutputFormat->setInt32(
3269 kKeyChannelCount,
3270 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3271 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003272
3273 // The codec-reported sampleRate is not reliable...
3274 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3275 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003276 OMX_AUDIO_PARAM_AMRTYPE amr;
3277 InitOMXParams(&amr);
3278 amr.nPortIndex = kPortIndexOutput;
3279
Andreas Huber784202e2009-10-15 13:46:54 -07003280 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003281 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3282 CHECK_EQ(err, OK);
3283
3284 CHECK_EQ(amr.nChannels, 1);
3285 mOutputFormat->setInt32(kKeyChannelCount, 1);
3286
3287 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3288 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003289 mOutputFormat->setCString(
3290 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003291 mOutputFormat->setInt32(kKeySampleRate, 8000);
3292 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3293 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003294 mOutputFormat->setCString(
3295 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003296 mOutputFormat->setInt32(kKeySampleRate, 16000);
3297 } else {
3298 CHECK(!"Unknown AMR band mode.");
3299 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003300 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003301 mOutputFormat->setCString(
3302 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003303 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003304 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3305 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003306 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003307 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3308 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003309 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003310 } else {
3311 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003312 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003313 break;
3314 }
3315
3316 case OMX_PortDomainVideo:
3317 {
3318 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3319
3320 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003321 mOutputFormat->setCString(
3322 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003323 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003324 mOutputFormat->setCString(
3325 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003326 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003327 mOutputFormat->setCString(
3328 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003329 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003330 mOutputFormat->setCString(
3331 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003332 } else {
3333 CHECK(!"Unknown compression format.");
3334 }
3335
3336 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3337 // This component appears to be lying to me.
3338 mOutputFormat->setInt32(
3339 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3340 mOutputFormat->setInt32(
3341 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3342 } else {
3343 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3344 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3345 }
3346
3347 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3348 break;
3349 }
3350
3351 default:
3352 {
3353 CHECK(!"should not be here, neither audio nor video.");
3354 break;
3355 }
3356 }
3357}
3358
Andreas Huber1f24b302010-06-10 11:12:39 -07003359status_t OMXCodec::pause() {
3360 Mutex::Autolock autoLock(mLock);
3361
3362 mPaused = true;
3363
3364 return OK;
3365}
3366
Andreas Hubere6c40962009-09-10 14:13:30 -07003367////////////////////////////////////////////////////////////////////////////////
3368
3369status_t QueryCodecs(
3370 const sp<IOMX> &omx,
3371 const char *mime, bool queryDecoders,
3372 Vector<CodecCapabilities> *results) {
3373 results->clear();
3374
3375 for (int index = 0;; ++index) {
3376 const char *componentName;
3377
3378 if (!queryDecoders) {
3379 componentName = GetCodec(
3380 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3381 mime, index);
3382 } else {
3383 componentName = GetCodec(
3384 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3385 mime, index);
3386 }
3387
3388 if (!componentName) {
3389 return OK;
3390 }
3391
Andreas Huber1a189a82010-03-24 13:49:20 -07003392 if (strncmp(componentName, "OMX.", 4)) {
3393 // Not an OpenMax component but a software codec.
3394
3395 results->push();
3396 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3397 caps->mComponentName = componentName;
3398
3399 continue;
3400 }
3401
Andreas Huber784202e2009-10-15 13:46:54 -07003402 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003403 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003404 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003405
3406 if (err != OK) {
3407 continue;
3408 }
3409
James Dong722d5912010-04-13 10:56:59 -07003410 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003411
3412 results->push();
3413 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3414 caps->mComponentName = componentName;
3415
3416 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3417 InitOMXParams(&param);
3418
3419 param.nPortIndex = queryDecoders ? 0 : 1;
3420
3421 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003422 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003423 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3424 &param, sizeof(param));
3425
3426 if (err != OK) {
3427 break;
3428 }
3429
3430 CodecProfileLevel profileLevel;
3431 profileLevel.mProfile = param.eProfile;
3432 profileLevel.mLevel = param.eLevel;
3433
3434 caps->mProfileLevels.push(profileLevel);
3435 }
3436
Andreas Huber784202e2009-10-15 13:46:54 -07003437 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003438 }
3439}
3440
Andreas Huberbe06d262009-08-14 14:37:10 -07003441} // namespace android