blob: dacb8d374849b78b8cc65700ba50490c35cbd6fd [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 Dong4f501f02010-06-07 14:41:41 -0700834 video_def->xFramerate = (frameRate << 16); // Q16 format
835 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
921status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
922 int32_t iFramesInterval, frameRate, bitRate;
923 bool success = meta->findInt32(kKeyBitRate, &bitRate);
924 success = success && meta->findInt32(kKeySampleRate, &frameRate);
925 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
926 CHECK(success);
927 OMX_VIDEO_PARAM_H263TYPE h263type;
928 InitOMXParams(&h263type);
929 h263type.nPortIndex = kPortIndexOutput;
930
931 status_t err = mOMX->getParameter(
932 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
933 CHECK_EQ(err, OK);
934
935 h263type.nAllowedPictureTypes =
936 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
937
938 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
939 if (h263type.nPFrames == 0) {
940 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
941 }
942 h263type.nBFrames = 0;
943
944 h263type.eProfile = OMX_VIDEO_H263ProfileBaseline;
945 h263type.eLevel = OMX_VIDEO_H263Level45;
946
947 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
948 h263type.bForceRoundingTypeToZero = OMX_FALSE;
949 h263type.nPictureHeaderRepetition = 0;
950 h263type.nGOBHeaderInterval = 0;
951
952 err = mOMX->setParameter(
953 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
954 CHECK_EQ(err, OK);
955
956 CHECK_EQ(setupBitRate(bitRate), OK);
957 CHECK_EQ(setupErrorCorrectionParameters(), OK);
958
959 return OK;
960}
961
James Dong1244eab2010-06-08 11:58:53 -0700962status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
963 int32_t iFramesInterval, frameRate, bitRate;
964 bool success = meta->findInt32(kKeyBitRate, &bitRate);
965 success = success && meta->findInt32(kKeySampleRate, &frameRate);
966 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
967 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -0700968 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
969 InitOMXParams(&mpeg4type);
970 mpeg4type.nPortIndex = kPortIndexOutput;
971
972 status_t err = mOMX->getParameter(
973 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
974 CHECK_EQ(err, OK);
975
976 mpeg4type.nSliceHeaderSpacing = 0;
977 mpeg4type.bSVH = OMX_FALSE;
978 mpeg4type.bGov = OMX_FALSE;
979
980 mpeg4type.nAllowedPictureTypes =
981 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
982
James Dong1244eab2010-06-08 11:58:53 -0700983 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
984 if (mpeg4type.nPFrames == 0) {
985 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
986 }
Andreas Huberb482ce82009-10-29 12:02:48 -0700987 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -0700988 mpeg4type.nIDCVLCThreshold = 0;
989 mpeg4type.bACPred = OMX_TRUE;
990 mpeg4type.nMaxPacketSize = 256;
991 mpeg4type.nTimeIncRes = 1000;
992 mpeg4type.nHeaderExtension = 0;
993 mpeg4type.bReversibleVLC = OMX_FALSE;
994
James Dongc0ab2a62010-06-29 16:29:19 -0700995 mpeg4type.eProfile = OMX_VIDEO_MPEG4ProfileSimple;
Andreas Huberb482ce82009-10-29 12:02:48 -0700996 mpeg4type.eLevel = OMX_VIDEO_MPEG4Level2;
997
998 err = mOMX->setParameter(
999 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1000 CHECK_EQ(err, OK);
1001
James Dongc0ab2a62010-06-29 16:29:19 -07001002 CHECK_EQ(setupBitRate(bitRate), OK);
1003 CHECK_EQ(setupErrorCorrectionParameters(), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001004
1005 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001006}
1007
James Dong1244eab2010-06-08 11:58:53 -07001008status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1009 int32_t iFramesInterval, frameRate, bitRate;
1010 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1011 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1012 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1013 CHECK(success);
1014
Andreas Huberea6a38c2009-11-16 15:43:38 -08001015 OMX_VIDEO_PARAM_AVCTYPE h264type;
1016 InitOMXParams(&h264type);
1017 h264type.nPortIndex = kPortIndexOutput;
1018
1019 status_t err = mOMX->getParameter(
1020 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1021 CHECK_EQ(err, OK);
1022
1023 h264type.nAllowedPictureTypes =
1024 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1025
1026 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001027 h264type.nBFrames = 0; // No B frames support yet
1028 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1029 if (h264type.nPFrames == 0) {
1030 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1031 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001032 h264type.bUseHadamard = OMX_TRUE;
1033 h264type.nRefFrames = 1;
1034 h264type.nRefIdx10ActiveMinus1 = 0;
1035 h264type.nRefIdx11ActiveMinus1 = 0;
1036 h264type.bEnableUEP = OMX_FALSE;
1037 h264type.bEnableFMO = OMX_FALSE;
1038 h264type.bEnableASO = OMX_FALSE;
1039 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001040 h264type.bFrameMBsOnly = OMX_TRUE;
1041 h264type.bMBAFF = OMX_FALSE;
1042 h264type.bEntropyCodingCABAC = OMX_FALSE;
1043 h264type.bWeightedPPrediction = OMX_FALSE;
1044 h264type.bconstIpred = OMX_FALSE;
1045 h264type.bDirect8x8Inference = OMX_FALSE;
1046 h264type.bDirectSpatialTemporal = OMX_FALSE;
1047 h264type.nCabacInitIdc = 0;
1048 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1049
1050 err = mOMX->setParameter(
1051 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1052 CHECK_EQ(err, OK);
1053
James Dongc0ab2a62010-06-29 16:29:19 -07001054 CHECK_EQ(setupBitRate(bitRate), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001055
1056 return OK;
1057}
1058
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001059status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001060 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001061 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001062
Andreas Huberbe06d262009-08-14 14:37:10 -07001063 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001064 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001065 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001066 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001067 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001068 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001069 compressionFormat = OMX_VIDEO_CodingH263;
1070 } else {
1071 LOGE("Not a supported video mime type: %s", mime);
1072 CHECK(!"Should not be here. Not a supported video mime type.");
1073 }
1074
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001075 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001076 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1077
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001078 if (err != OK) {
1079 return err;
1080 }
1081
Andreas Huberbe06d262009-08-14 14:37:10 -07001082#if 1
1083 {
1084 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001085 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001086 format.nPortIndex = kPortIndexOutput;
1087 format.nIndex = 0;
1088
Andreas Huber784202e2009-10-15 13:46:54 -07001089 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001090 mNode, OMX_IndexParamVideoPortFormat,
1091 &format, sizeof(format));
1092 CHECK_EQ(err, OK);
1093 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1094
1095 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1096
1097 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1098 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1099 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1100 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1101
Andreas Huber784202e2009-10-15 13:46:54 -07001102 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001103 mNode, OMX_IndexParamVideoPortFormat,
1104 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001105
1106 if (err != OK) {
1107 return err;
1108 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001109 }
1110#endif
1111
1112 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001113 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001114 def.nPortIndex = kPortIndexInput;
1115
Andreas Huber4c483422009-09-02 16:05:36 -07001116 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1117
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001118 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001119 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1120
1121 CHECK_EQ(err, OK);
1122
1123#if 1
1124 // XXX Need a (much) better heuristic to compute input buffer sizes.
1125 const size_t X = 64 * 1024;
1126 if (def.nBufferSize < X) {
1127 def.nBufferSize = X;
1128 }
1129#endif
1130
1131 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1132
1133 video_def->nFrameWidth = width;
1134 video_def->nFrameHeight = height;
1135
Andreas Huberb482ce82009-10-29 12:02:48 -07001136 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001137 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1138
Andreas Huber784202e2009-10-15 13:46:54 -07001139 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001140 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001141
1142 if (err != OK) {
1143 return err;
1144 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001145
1146 ////////////////////////////////////////////////////////////////////////////
1147
Andreas Huber4c483422009-09-02 16:05:36 -07001148 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001149 def.nPortIndex = kPortIndexOutput;
1150
Andreas Huber784202e2009-10-15 13:46:54 -07001151 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001152 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1153 CHECK_EQ(err, OK);
1154 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1155
1156#if 0
1157 def.nBufferSize =
1158 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1159#endif
1160
1161 video_def->nFrameWidth = width;
1162 video_def->nFrameHeight = height;
1163
Andreas Huber784202e2009-10-15 13:46:54 -07001164 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001165 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001166
1167 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001168}
1169
Andreas Huberbe06d262009-08-14 14:37:10 -07001170OMXCodec::OMXCodec(
1171 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001172 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001173 const char *mime,
1174 const char *componentName,
1175 const sp<MediaSource> &source)
1176 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001177 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001178 mNode(node),
1179 mQuirks(quirks),
1180 mIsEncoder(isEncoder),
1181 mMIME(strdup(mime)),
1182 mComponentName(strdup(componentName)),
1183 mSource(source),
1184 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001185 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001186 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001187 mSignalledEOS(false),
1188 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001189 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001190 mSeekTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001191 mLeftOverBuffer(NULL),
1192 mPaused(false) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001193 mPortStatus[kPortIndexInput] = ENABLED;
1194 mPortStatus[kPortIndexOutput] = ENABLED;
1195
Andreas Huber4c483422009-09-02 16:05:36 -07001196 setComponentRole();
1197}
1198
Andreas Hubere6c40962009-09-10 14:13:30 -07001199// static
1200void OMXCodec::setComponentRole(
1201 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1202 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001203 struct MimeToRole {
1204 const char *mime;
1205 const char *decoderRole;
1206 const char *encoderRole;
1207 };
1208
1209 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001210 { MEDIA_MIMETYPE_AUDIO_MPEG,
1211 "audio_decoder.mp3", "audio_encoder.mp3" },
1212 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1213 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1214 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1215 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1216 { MEDIA_MIMETYPE_AUDIO_AAC,
1217 "audio_decoder.aac", "audio_encoder.aac" },
1218 { MEDIA_MIMETYPE_VIDEO_AVC,
1219 "video_decoder.avc", "video_encoder.avc" },
1220 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1221 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1222 { MEDIA_MIMETYPE_VIDEO_H263,
1223 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001224 };
1225
1226 static const size_t kNumMimeToRole =
1227 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1228
1229 size_t i;
1230 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001231 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001232 break;
1233 }
1234 }
1235
1236 if (i == kNumMimeToRole) {
1237 return;
1238 }
1239
1240 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001241 isEncoder ? kMimeToRole[i].encoderRole
1242 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001243
1244 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001245 OMX_PARAM_COMPONENTROLETYPE roleParams;
1246 InitOMXParams(&roleParams);
1247
1248 strncpy((char *)roleParams.cRole,
1249 role, OMX_MAX_STRINGNAME_SIZE - 1);
1250
1251 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1252
Andreas Huber784202e2009-10-15 13:46:54 -07001253 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001254 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001255 &roleParams, sizeof(roleParams));
1256
1257 if (err != OK) {
1258 LOGW("Failed to set standard component role '%s'.", role);
1259 }
1260 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001261}
1262
Andreas Hubere6c40962009-09-10 14:13:30 -07001263void OMXCodec::setComponentRole() {
1264 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1265}
1266
Andreas Huberbe06d262009-08-14 14:37:10 -07001267OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001268 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001269
Andreas Huber784202e2009-10-15 13:46:54 -07001270 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001271 CHECK_EQ(err, OK);
1272
1273 mNode = NULL;
1274 setState(DEAD);
1275
1276 clearCodecSpecificData();
1277
1278 free(mComponentName);
1279 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001280
Andreas Huberbe06d262009-08-14 14:37:10 -07001281 free(mMIME);
1282 mMIME = NULL;
1283}
1284
1285status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001286 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001287
1288 CHECK_EQ(mState, LOADED);
1289
1290 status_t err;
1291 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001292 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001293 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001294 setState(LOADED_TO_IDLE);
1295 }
1296
1297 err = allocateBuffers();
1298 CHECK_EQ(err, OK);
1299
1300 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001301 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001302 CHECK_EQ(err, OK);
1303
1304 setState(LOADED_TO_IDLE);
1305 }
1306
1307 while (mState != EXECUTING && mState != ERROR) {
1308 mAsyncCompletion.wait(mLock);
1309 }
1310
1311 return mState == ERROR ? UNKNOWN_ERROR : OK;
1312}
1313
1314// static
1315bool OMXCodec::isIntermediateState(State state) {
1316 return state == LOADED_TO_IDLE
1317 || state == IDLE_TO_EXECUTING
1318 || state == EXECUTING_TO_IDLE
1319 || state == IDLE_TO_LOADED
1320 || state == RECONFIGURING;
1321}
1322
1323status_t OMXCodec::allocateBuffers() {
1324 status_t err = allocateBuffersOnPort(kPortIndexInput);
1325
1326 if (err != OK) {
1327 return err;
1328 }
1329
1330 return allocateBuffersOnPort(kPortIndexOutput);
1331}
1332
1333status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1334 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001335 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001336 def.nPortIndex = portIndex;
1337
Andreas Huber784202e2009-10-15 13:46:54 -07001338 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001339 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1340
1341 if (err != OK) {
1342 return err;
1343 }
1344
Andreas Huber5c0a9132009-08-20 11:16:40 -07001345 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001346 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001347
Andreas Huberbe06d262009-08-14 14:37:10 -07001348 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001349 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001350 CHECK(mem.get() != NULL);
1351
Andreas Huberc712b9f2010-01-20 15:05:46 -08001352 BufferInfo info;
1353 info.mData = NULL;
1354 info.mSize = def.nBufferSize;
1355
Andreas Huberbe06d262009-08-14 14:37:10 -07001356 IOMX::buffer_id buffer;
1357 if (portIndex == kPortIndexInput
1358 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001359 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001360 mem.clear();
1361
Andreas Huberf1fe0642010-01-15 15:28:19 -08001362 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001363 mNode, portIndex, def.nBufferSize, &buffer,
1364 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001365 } else {
1366 err = mOMX->allocateBufferWithBackup(
1367 mNode, portIndex, mem, &buffer);
1368 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001369 } else if (portIndex == kPortIndexOutput
1370 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001371 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001372 mem.clear();
1373
Andreas Huberf1fe0642010-01-15 15:28:19 -08001374 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001375 mNode, portIndex, def.nBufferSize, &buffer,
1376 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001377 } else {
1378 err = mOMX->allocateBufferWithBackup(
1379 mNode, portIndex, mem, &buffer);
1380 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001381 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001382 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001383 }
1384
1385 if (err != OK) {
1386 LOGE("allocate_buffer_with_backup failed");
1387 return err;
1388 }
1389
Andreas Huberc712b9f2010-01-20 15:05:46 -08001390 if (mem != NULL) {
1391 info.mData = mem->pointer();
1392 }
1393
Andreas Huberbe06d262009-08-14 14:37:10 -07001394 info.mBuffer = buffer;
1395 info.mOwnedByComponent = false;
1396 info.mMem = mem;
1397 info.mMediaBuffer = NULL;
1398
1399 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001400 if (!(mOMXLivesLocally
1401 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1402 && (mQuirks & kDefersOutputBufferAllocation))) {
1403 // If the node does not fill in the buffer ptr at this time,
1404 // we will defer creating the MediaBuffer until receiving
1405 // the first FILL_BUFFER_DONE notification instead.
1406 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1407 info.mMediaBuffer->setObserver(this);
1408 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001409 }
1410
1411 mPortBuffers[portIndex].push(info);
1412
Andreas Huber4c483422009-09-02 16:05:36 -07001413 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001414 portIndex == kPortIndexInput ? "input" : "output");
1415 }
1416
Andreas Huber2ea14e22009-12-16 09:30:55 -08001417 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001418
1419 return OK;
1420}
1421
1422void OMXCodec::on_message(const omx_message &msg) {
1423 Mutex::Autolock autoLock(mLock);
1424
1425 switch (msg.type) {
1426 case omx_message::EVENT:
1427 {
1428 onEvent(
1429 msg.u.event_data.event, msg.u.event_data.data1,
1430 msg.u.event_data.data2);
1431
1432 break;
1433 }
1434
1435 case omx_message::EMPTY_BUFFER_DONE:
1436 {
1437 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1438
Andreas Huber4c483422009-09-02 16:05:36 -07001439 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001440
1441 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1442 size_t i = 0;
1443 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1444 ++i;
1445 }
1446
1447 CHECK(i < buffers->size());
1448 if (!(*buffers)[i].mOwnedByComponent) {
1449 LOGW("We already own input buffer %p, yet received "
1450 "an EMPTY_BUFFER_DONE.", buffer);
1451 }
1452
1453 buffers->editItemAt(i).mOwnedByComponent = false;
1454
1455 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001456 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001457
1458 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001459 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001460 CHECK_EQ(err, OK);
1461
1462 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001463 } else if (mState != ERROR
1464 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001465 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1466 drainInputBuffer(&buffers->editItemAt(i));
1467 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001468 break;
1469 }
1470
1471 case omx_message::FILL_BUFFER_DONE:
1472 {
1473 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1474 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1475
Andreas Huber2ea14e22009-12-16 09:30:55 -08001476 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001477 buffer,
1478 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001479 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001480 msg.u.extended_buffer_data.timestamp,
1481 msg.u.extended_buffer_data.timestamp / 1E6);
1482
1483 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1484 size_t i = 0;
1485 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1486 ++i;
1487 }
1488
1489 CHECK(i < buffers->size());
1490 BufferInfo *info = &buffers->editItemAt(i);
1491
1492 if (!info->mOwnedByComponent) {
1493 LOGW("We already own output buffer %p, yet received "
1494 "a FILL_BUFFER_DONE.", buffer);
1495 }
1496
1497 info->mOwnedByComponent = false;
1498
1499 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001500 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001501
1502 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001503 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001504 CHECK_EQ(err, OK);
1505
1506 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001507#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001508 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1509 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001510 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001511 mNoMoreOutputData = true;
1512 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001513#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001514 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1515 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001516
Andreas Huber52733b82010-01-25 10:41:35 -08001517 if (info->mMediaBuffer == NULL) {
1518 CHECK(mOMXLivesLocally);
1519 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1520 CHECK(mQuirks & kDefersOutputBufferAllocation);
1521
1522 // The qcom video decoders on Nexus don't actually allocate
1523 // output buffer memory on a call to OMX_AllocateBuffer
1524 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1525 // structure is only filled in later.
1526
1527 info->mMediaBuffer = new MediaBuffer(
1528 msg.u.extended_buffer_data.data_ptr,
1529 info->mSize);
1530 info->mMediaBuffer->setObserver(this);
1531 }
1532
Andreas Huberbe06d262009-08-14 14:37:10 -07001533 MediaBuffer *buffer = info->mMediaBuffer;
1534
1535 buffer->set_range(
1536 msg.u.extended_buffer_data.range_offset,
1537 msg.u.extended_buffer_data.range_length);
1538
1539 buffer->meta_data()->clear();
1540
Andreas Huberfa8de752009-10-08 10:07:49 -07001541 buffer->meta_data()->setInt64(
1542 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001543
1544 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1545 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1546 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001547 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1548 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1549 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001550
1551 buffer->meta_data()->setPointer(
1552 kKeyPlatformPrivate,
1553 msg.u.extended_buffer_data.platform_private);
1554
1555 buffer->meta_data()->setPointer(
1556 kKeyBufferID,
1557 msg.u.extended_buffer_data.buffer);
1558
1559 mFilledBuffers.push_back(i);
1560 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001561
1562 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1563 CODEC_LOGV("No more output data.");
1564 mNoMoreOutputData = true;
1565 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001566 }
1567
1568 break;
1569 }
1570
1571 default:
1572 {
1573 CHECK(!"should not be here.");
1574 break;
1575 }
1576 }
1577}
1578
1579void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1580 switch (event) {
1581 case OMX_EventCmdComplete:
1582 {
1583 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1584 break;
1585 }
1586
1587 case OMX_EventError:
1588 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001589 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001590
1591 setState(ERROR);
1592 break;
1593 }
1594
1595 case OMX_EventPortSettingsChanged:
1596 {
1597 onPortSettingsChanged(data1);
1598 break;
1599 }
1600
Andreas Huber2ea14e22009-12-16 09:30:55 -08001601#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001602 case OMX_EventBufferFlag:
1603 {
Andreas Huber4c483422009-09-02 16:05:36 -07001604 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001605
1606 if (data1 == kPortIndexOutput) {
1607 mNoMoreOutputData = true;
1608 }
1609 break;
1610 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001611#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001612
1613 default:
1614 {
Andreas Huber4c483422009-09-02 16:05:36 -07001615 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001616 break;
1617 }
1618 }
1619}
1620
Andreas Huberb1678602009-10-19 13:06:40 -07001621// Has the format changed in any way that the client would have to be aware of?
1622static bool formatHasNotablyChanged(
1623 const sp<MetaData> &from, const sp<MetaData> &to) {
1624 if (from.get() == NULL && to.get() == NULL) {
1625 return false;
1626 }
1627
Andreas Huberf68c1682009-10-21 14:01:30 -07001628 if ((from.get() == NULL && to.get() != NULL)
1629 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001630 return true;
1631 }
1632
1633 const char *mime_from, *mime_to;
1634 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1635 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1636
1637 if (strcasecmp(mime_from, mime_to)) {
1638 return true;
1639 }
1640
1641 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1642 int32_t colorFormat_from, colorFormat_to;
1643 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1644 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1645
1646 if (colorFormat_from != colorFormat_to) {
1647 return true;
1648 }
1649
1650 int32_t width_from, width_to;
1651 CHECK(from->findInt32(kKeyWidth, &width_from));
1652 CHECK(to->findInt32(kKeyWidth, &width_to));
1653
1654 if (width_from != width_to) {
1655 return true;
1656 }
1657
1658 int32_t height_from, height_to;
1659 CHECK(from->findInt32(kKeyHeight, &height_from));
1660 CHECK(to->findInt32(kKeyHeight, &height_to));
1661
1662 if (height_from != height_to) {
1663 return true;
1664 }
1665 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1666 int32_t numChannels_from, numChannels_to;
1667 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1668 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1669
1670 if (numChannels_from != numChannels_to) {
1671 return true;
1672 }
1673
1674 int32_t sampleRate_from, sampleRate_to;
1675 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1676 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1677
1678 if (sampleRate_from != sampleRate_to) {
1679 return true;
1680 }
1681 }
1682
1683 return false;
1684}
1685
Andreas Huberbe06d262009-08-14 14:37:10 -07001686void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1687 switch (cmd) {
1688 case OMX_CommandStateSet:
1689 {
1690 onStateChange((OMX_STATETYPE)data);
1691 break;
1692 }
1693
1694 case OMX_CommandPortDisable:
1695 {
1696 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001697 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001698
1699 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1700 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1701 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1702
1703 mPortStatus[portIndex] = DISABLED;
1704
1705 if (mState == RECONFIGURING) {
1706 CHECK_EQ(portIndex, kPortIndexOutput);
1707
Andreas Huberb1678602009-10-19 13:06:40 -07001708 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001709 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001710
1711 // Don't notify clients if the output port settings change
1712 // wasn't of importance to them, i.e. it may be that just the
1713 // number of buffers has changed and nothing else.
1714 mOutputPortSettingsHaveChanged =
1715 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001716
Andreas Huberbe06d262009-08-14 14:37:10 -07001717 enablePortAsync(portIndex);
1718
1719 status_t err = allocateBuffersOnPort(portIndex);
1720 CHECK_EQ(err, OK);
1721 }
1722 break;
1723 }
1724
1725 case OMX_CommandPortEnable:
1726 {
1727 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001728 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001729
1730 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1731 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1732
1733 mPortStatus[portIndex] = ENABLED;
1734
1735 if (mState == RECONFIGURING) {
1736 CHECK_EQ(portIndex, kPortIndexOutput);
1737
1738 setState(EXECUTING);
1739
1740 fillOutputBuffers();
1741 }
1742 break;
1743 }
1744
1745 case OMX_CommandFlush:
1746 {
1747 OMX_U32 portIndex = data;
1748
Andreas Huber4c483422009-09-02 16:05:36 -07001749 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001750
1751 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1752 mPortStatus[portIndex] = ENABLED;
1753
1754 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1755 mPortBuffers[portIndex].size());
1756
1757 if (mState == RECONFIGURING) {
1758 CHECK_EQ(portIndex, kPortIndexOutput);
1759
1760 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001761 } else if (mState == EXECUTING_TO_IDLE) {
1762 if (mPortStatus[kPortIndexInput] == ENABLED
1763 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001764 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001765 "transition from EXECUTING to IDLE.");
1766
1767 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1768 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1769
1770 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001771 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001772 CHECK_EQ(err, OK);
1773 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001774 } else {
1775 // We're flushing both ports in preparation for seeking.
1776
1777 if (mPortStatus[kPortIndexInput] == ENABLED
1778 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001779 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001780 " seek-time.");
1781
Andreas Huber1f24b302010-06-10 11:12:39 -07001782 // We implicitly resume pulling on our upstream source.
1783 mPaused = false;
1784
Andreas Huberbe06d262009-08-14 14:37:10 -07001785 drainInputBuffers();
1786 fillOutputBuffers();
1787 }
1788 }
1789
1790 break;
1791 }
1792
1793 default:
1794 {
Andreas Huber4c483422009-09-02 16:05:36 -07001795 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001796 break;
1797 }
1798 }
1799}
1800
1801void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001802 CODEC_LOGV("onStateChange %d", newState);
1803
Andreas Huberbe06d262009-08-14 14:37:10 -07001804 switch (newState) {
1805 case OMX_StateIdle:
1806 {
Andreas Huber4c483422009-09-02 16:05:36 -07001807 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001808 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001809 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001810 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1811
1812 CHECK_EQ(err, OK);
1813
1814 setState(IDLE_TO_EXECUTING);
1815 } else {
1816 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1817
1818 CHECK_EQ(
1819 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1820 mPortBuffers[kPortIndexInput].size());
1821
1822 CHECK_EQ(
1823 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1824 mPortBuffers[kPortIndexOutput].size());
1825
Andreas Huber784202e2009-10-15 13:46:54 -07001826 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001827 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1828
1829 CHECK_EQ(err, OK);
1830
1831 err = freeBuffersOnPort(kPortIndexInput);
1832 CHECK_EQ(err, OK);
1833
1834 err = freeBuffersOnPort(kPortIndexOutput);
1835 CHECK_EQ(err, OK);
1836
1837 mPortStatus[kPortIndexInput] = ENABLED;
1838 mPortStatus[kPortIndexOutput] = ENABLED;
1839
1840 setState(IDLE_TO_LOADED);
1841 }
1842 break;
1843 }
1844
1845 case OMX_StateExecuting:
1846 {
1847 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1848
Andreas Huber4c483422009-09-02 16:05:36 -07001849 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001850
1851 setState(EXECUTING);
1852
Andreas Huber42978e52009-08-27 10:08:39 -07001853 // Buffers will be submitted to the component in the first
1854 // call to OMXCodec::read as mInitialBufferSubmit is true at
1855 // this point. This ensures that this on_message call returns,
1856 // releases the lock and ::init can notice the state change and
1857 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001858 break;
1859 }
1860
1861 case OMX_StateLoaded:
1862 {
1863 CHECK_EQ(mState, IDLE_TO_LOADED);
1864
Andreas Huber4c483422009-09-02 16:05:36 -07001865 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001866
1867 setState(LOADED);
1868 break;
1869 }
1870
Andreas Huberc712b9f2010-01-20 15:05:46 -08001871 case OMX_StateInvalid:
1872 {
1873 setState(ERROR);
1874 break;
1875 }
1876
Andreas Huberbe06d262009-08-14 14:37:10 -07001877 default:
1878 {
1879 CHECK(!"should not be here.");
1880 break;
1881 }
1882 }
1883}
1884
1885// static
1886size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1887 size_t n = 0;
1888 for (size_t i = 0; i < buffers.size(); ++i) {
1889 if (!buffers[i].mOwnedByComponent) {
1890 ++n;
1891 }
1892 }
1893
1894 return n;
1895}
1896
1897status_t OMXCodec::freeBuffersOnPort(
1898 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1899 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1900
1901 status_t stickyErr = OK;
1902
1903 for (size_t i = buffers->size(); i-- > 0;) {
1904 BufferInfo *info = &buffers->editItemAt(i);
1905
1906 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1907 continue;
1908 }
1909
1910 CHECK_EQ(info->mOwnedByComponent, false);
1911
Andreas Huber92022852009-09-14 15:24:14 -07001912 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1913
Andreas Huberbe06d262009-08-14 14:37:10 -07001914 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001915 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001916
1917 if (err != OK) {
1918 stickyErr = err;
1919 }
1920
1921 if (info->mMediaBuffer != NULL) {
1922 info->mMediaBuffer->setObserver(NULL);
1923
1924 // Make sure nobody but us owns this buffer at this point.
1925 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1926
1927 info->mMediaBuffer->release();
1928 }
1929
1930 buffers->removeAt(i);
1931 }
1932
1933 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1934
1935 return stickyErr;
1936}
1937
1938void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001939 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001940
1941 CHECK_EQ(mState, EXECUTING);
1942 CHECK_EQ(portIndex, kPortIndexOutput);
1943 setState(RECONFIGURING);
1944
1945 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001946 if (!flushPortAsync(portIndex)) {
1947 onCmdComplete(OMX_CommandFlush, portIndex);
1948 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001949 } else {
1950 disablePortAsync(portIndex);
1951 }
1952}
1953
Andreas Huber404cc412009-08-25 14:26:05 -07001954bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001955 CHECK(mState == EXECUTING || mState == RECONFIGURING
1956 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001957
Andreas Huber4c483422009-09-02 16:05:36 -07001958 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001959 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1960 mPortBuffers[portIndex].size());
1961
Andreas Huberbe06d262009-08-14 14:37:10 -07001962 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1963 mPortStatus[portIndex] = SHUTTING_DOWN;
1964
Andreas Huber404cc412009-08-25 14:26:05 -07001965 if ((mQuirks & kRequiresFlushCompleteEmulation)
1966 && countBuffersWeOwn(mPortBuffers[portIndex])
1967 == mPortBuffers[portIndex].size()) {
1968 // No flush is necessary and this component fails to send a
1969 // flush-complete event in this case.
1970
1971 return false;
1972 }
1973
Andreas Huberbe06d262009-08-14 14:37:10 -07001974 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001975 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001976 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001977
1978 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001979}
1980
1981void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1982 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1983
1984 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1985 mPortStatus[portIndex] = DISABLING;
1986
1987 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001988 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001989 CHECK_EQ(err, OK);
1990
1991 freeBuffersOnPort(portIndex, true);
1992}
1993
1994void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1995 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1996
1997 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1998 mPortStatus[portIndex] = ENABLING;
1999
2000 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002001 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002002 CHECK_EQ(err, OK);
2003}
2004
2005void OMXCodec::fillOutputBuffers() {
2006 CHECK_EQ(mState, EXECUTING);
2007
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002008 // This is a workaround for some decoders not properly reporting
2009 // end-of-output-stream. If we own all input buffers and also own
2010 // all output buffers and we already signalled end-of-input-stream,
2011 // the end-of-output-stream is implied.
2012 if (mSignalledEOS
2013 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2014 == mPortBuffers[kPortIndexInput].size()
2015 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2016 == mPortBuffers[kPortIndexOutput].size()) {
2017 mNoMoreOutputData = true;
2018 mBufferFilled.signal();
2019
2020 return;
2021 }
2022
Andreas Huberbe06d262009-08-14 14:37:10 -07002023 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2024 for (size_t i = 0; i < buffers->size(); ++i) {
2025 fillOutputBuffer(&buffers->editItemAt(i));
2026 }
2027}
2028
2029void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002030 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002031
2032 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2033 for (size_t i = 0; i < buffers->size(); ++i) {
2034 drainInputBuffer(&buffers->editItemAt(i));
2035 }
2036}
2037
2038void OMXCodec::drainInputBuffer(BufferInfo *info) {
2039 CHECK_EQ(info->mOwnedByComponent, false);
2040
2041 if (mSignalledEOS) {
2042 return;
2043 }
2044
2045 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2046 const CodecSpecificData *specific =
2047 mCodecSpecificData[mCodecSpecificDataIndex];
2048
2049 size_t size = specific->mSize;
2050
Andreas Hubere6c40962009-09-10 14:13:30 -07002051 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002052 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002053 static const uint8_t kNALStartCode[4] =
2054 { 0x00, 0x00, 0x00, 0x01 };
2055
Andreas Huberc712b9f2010-01-20 15:05:46 -08002056 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002057
2058 size += 4;
2059
Andreas Huberc712b9f2010-01-20 15:05:46 -08002060 memcpy(info->mData, kNALStartCode, 4);
2061 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002062 specific->mData, specific->mSize);
2063 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002064 CHECK(info->mSize >= specific->mSize);
2065 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002066 }
2067
Andreas Huber2ea14e22009-12-16 09:30:55 -08002068 mNoMoreOutputData = false;
2069
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002070 CODEC_LOGV("calling emptyBuffer with codec specific data");
2071
Andreas Huber784202e2009-10-15 13:46:54 -07002072 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002073 mNode, info->mBuffer, 0, size,
2074 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2075 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002076 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002077
2078 info->mOwnedByComponent = true;
2079
2080 ++mCodecSpecificDataIndex;
2081 return;
2082 }
2083
Andreas Huber1f24b302010-06-10 11:12:39 -07002084 if (mPaused) {
2085 return;
2086 }
2087
Andreas Huberbe06d262009-08-14 14:37:10 -07002088 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002089
Andreas Hubera4357ad2010-04-02 12:49:54 -07002090 bool signalEOS = false;
2091 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002092
Andreas Hubera4357ad2010-04-02 12:49:54 -07002093 size_t offset = 0;
2094 int32_t n = 0;
2095 for (;;) {
2096 MediaBuffer *srcBuffer;
2097 if (mSeekTimeUs >= 0) {
2098 if (mLeftOverBuffer) {
2099 mLeftOverBuffer->release();
2100 mLeftOverBuffer = NULL;
2101 }
2102
2103 MediaSource::ReadOptions options;
2104 options.setSeekTo(mSeekTimeUs);
2105
2106 mSeekTimeUs = -1;
2107 mBufferFilled.signal();
2108
2109 err = mSource->read(&srcBuffer, &options);
2110 } else if (mLeftOverBuffer) {
2111 srcBuffer = mLeftOverBuffer;
2112 mLeftOverBuffer = NULL;
2113
2114 err = OK;
2115 } else {
2116 err = mSource->read(&srcBuffer);
2117 }
2118
2119 if (err != OK) {
2120 signalEOS = true;
2121 mFinalStatus = err;
2122 mSignalledEOS = true;
2123 break;
2124 }
2125
2126 size_t remainingBytes = info->mSize - offset;
2127
2128 if (srcBuffer->range_length() > remainingBytes) {
2129 if (offset == 0) {
2130 CODEC_LOGE(
2131 "Codec's input buffers are too small to accomodate "
2132 "buffer read from source (info->mSize = %d, srcLength = %d)",
2133 info->mSize, srcBuffer->range_length());
2134
2135 srcBuffer->release();
2136 srcBuffer = NULL;
2137
2138 setState(ERROR);
2139 return;
2140 }
2141
2142 mLeftOverBuffer = srcBuffer;
2143 break;
2144 }
2145
James Dong4f501f02010-06-07 14:41:41 -07002146 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2147 CHECK(mOMXLivesLocally && offset == 0);
2148 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2149 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2150 } else {
2151 memcpy((uint8_t *)info->mData + offset,
2152 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2153 srcBuffer->range_length());
2154 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002155
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002156 int64_t lastBufferTimeUs;
2157 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
2158 CHECK(timestampUs >= 0);
2159
Andreas Hubera4357ad2010-04-02 12:49:54 -07002160 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002161 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002162 }
2163
2164 offset += srcBuffer->range_length();
2165
2166 srcBuffer->release();
2167 srcBuffer = NULL;
2168
2169 ++n;
2170
2171 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2172 break;
2173 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002174
2175 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2176
2177 if (coalescedDurationUs > 250000ll) {
2178 // Don't coalesce more than 250ms worth of encoded data at once.
2179 break;
2180 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002181 }
2182
2183 if (n > 1) {
2184 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002185 }
2186
2187 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002188
Andreas Hubera4357ad2010-04-02 12:49:54 -07002189 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002190 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002191 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002192 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002193 }
2194
Andreas Hubera4357ad2010-04-02 12:49:54 -07002195 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2196 "timestamp %lld us (%.2f secs)",
2197 info->mBuffer, offset,
2198 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002199
Andreas Huber784202e2009-10-15 13:46:54 -07002200 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002201 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002202 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002203
2204 if (err != OK) {
2205 setState(ERROR);
2206 return;
2207 }
2208
2209 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002210
2211 // This component does not ever signal the EOS flag on output buffers,
2212 // Thanks for nothing.
2213 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2214 mNoMoreOutputData = true;
2215 mBufferFilled.signal();
2216 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002217}
2218
2219void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2220 CHECK_EQ(info->mOwnedByComponent, false);
2221
Andreas Huber404cc412009-08-25 14:26:05 -07002222 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002223 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002224 "calling fillOutputBuffer");
2225 return;
2226 }
2227
Andreas Huber4c483422009-09-02 16:05:36 -07002228 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002229 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002230
2231 if (err != OK) {
2232 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2233
2234 setState(ERROR);
2235 return;
2236 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002237
2238 info->mOwnedByComponent = true;
2239}
2240
2241void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2242 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2243 for (size_t i = 0; i < buffers->size(); ++i) {
2244 if ((*buffers)[i].mBuffer == buffer) {
2245 drainInputBuffer(&buffers->editItemAt(i));
2246 return;
2247 }
2248 }
2249
2250 CHECK(!"should not be here.");
2251}
2252
2253void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2254 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2255 for (size_t i = 0; i < buffers->size(); ++i) {
2256 if ((*buffers)[i].mBuffer == buffer) {
2257 fillOutputBuffer(&buffers->editItemAt(i));
2258 return;
2259 }
2260 }
2261
2262 CHECK(!"should not be here.");
2263}
2264
2265void OMXCodec::setState(State newState) {
2266 mState = newState;
2267 mAsyncCompletion.signal();
2268
2269 // This may cause some spurious wakeups but is necessary to
2270 // unblock the reader if we enter ERROR state.
2271 mBufferFilled.signal();
2272}
2273
Andreas Huberda050cf22009-09-02 14:01:43 -07002274void OMXCodec::setRawAudioFormat(
2275 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002276
2277 // port definition
2278 OMX_PARAM_PORTDEFINITIONTYPE def;
2279 InitOMXParams(&def);
2280 def.nPortIndex = portIndex;
2281 status_t err = mOMX->getParameter(
2282 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2283 CHECK_EQ(err, OK);
2284 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2285 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2286 &def, sizeof(def)), OK);
2287
2288 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002289 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002290 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002291 pcmParams.nPortIndex = portIndex;
2292
James Dongabed93a2010-04-22 17:27:04 -07002293 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002294 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2295
2296 CHECK_EQ(err, OK);
2297
2298 pcmParams.nChannels = numChannels;
2299 pcmParams.eNumData = OMX_NumericalDataSigned;
2300 pcmParams.bInterleaved = OMX_TRUE;
2301 pcmParams.nBitPerSample = 16;
2302 pcmParams.nSamplingRate = sampleRate;
2303 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2304
2305 if (numChannels == 1) {
2306 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2307 } else {
2308 CHECK_EQ(numChannels, 2);
2309
2310 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2311 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2312 }
2313
Andreas Huber784202e2009-10-15 13:46:54 -07002314 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002315 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2316
2317 CHECK_EQ(err, OK);
2318}
2319
James Dong17299ab2010-05-14 15:45:22 -07002320static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2321 if (isAMRWB) {
2322 if (bps <= 6600) {
2323 return OMX_AUDIO_AMRBandModeWB0;
2324 } else if (bps <= 8850) {
2325 return OMX_AUDIO_AMRBandModeWB1;
2326 } else if (bps <= 12650) {
2327 return OMX_AUDIO_AMRBandModeWB2;
2328 } else if (bps <= 14250) {
2329 return OMX_AUDIO_AMRBandModeWB3;
2330 } else if (bps <= 15850) {
2331 return OMX_AUDIO_AMRBandModeWB4;
2332 } else if (bps <= 18250) {
2333 return OMX_AUDIO_AMRBandModeWB5;
2334 } else if (bps <= 19850) {
2335 return OMX_AUDIO_AMRBandModeWB6;
2336 } else if (bps <= 23050) {
2337 return OMX_AUDIO_AMRBandModeWB7;
2338 }
2339
2340 // 23850 bps
2341 return OMX_AUDIO_AMRBandModeWB8;
2342 } else { // AMRNB
2343 if (bps <= 4750) {
2344 return OMX_AUDIO_AMRBandModeNB0;
2345 } else if (bps <= 5150) {
2346 return OMX_AUDIO_AMRBandModeNB1;
2347 } else if (bps <= 5900) {
2348 return OMX_AUDIO_AMRBandModeNB2;
2349 } else if (bps <= 6700) {
2350 return OMX_AUDIO_AMRBandModeNB3;
2351 } else if (bps <= 7400) {
2352 return OMX_AUDIO_AMRBandModeNB4;
2353 } else if (bps <= 7950) {
2354 return OMX_AUDIO_AMRBandModeNB5;
2355 } else if (bps <= 10200) {
2356 return OMX_AUDIO_AMRBandModeNB6;
2357 }
2358
2359 // 12200 bps
2360 return OMX_AUDIO_AMRBandModeNB7;
2361 }
2362}
2363
2364void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002365 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002366
Andreas Huber8768f2c2009-12-01 15:26:54 -08002367 OMX_AUDIO_PARAM_AMRTYPE def;
2368 InitOMXParams(&def);
2369 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002370
Andreas Huber8768f2c2009-12-01 15:26:54 -08002371 status_t err =
2372 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002373
Andreas Huber8768f2c2009-12-01 15:26:54 -08002374 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002375
Andreas Huber8768f2c2009-12-01 15:26:54 -08002376 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002377
James Dong17299ab2010-05-14 15:45:22 -07002378 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002379 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2380 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002381
2382 ////////////////////////
2383
2384 if (mIsEncoder) {
2385 sp<MetaData> format = mSource->getFormat();
2386 int32_t sampleRate;
2387 int32_t numChannels;
2388 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2389 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2390
2391 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2392 }
2393}
2394
James Dong17299ab2010-05-14 15:45:22 -07002395void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002396 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002397 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002398 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002399 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002400
2401 //////////////// output port ////////////////////
2402 // format
2403 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2404 format.nPortIndex = kPortIndexOutput;
2405 format.nIndex = 0;
2406 status_t err = OMX_ErrorNone;
2407 while (OMX_ErrorNone == err) {
2408 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2409 &format, sizeof(format)), OK);
2410 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2411 break;
2412 }
2413 format.nIndex++;
2414 }
2415 CHECK_EQ(OK, err);
2416 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2417 &format, sizeof(format)), OK);
2418
2419 // port definition
2420 OMX_PARAM_PORTDEFINITIONTYPE def;
2421 InitOMXParams(&def);
2422 def.nPortIndex = kPortIndexOutput;
2423 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2424 &def, sizeof(def)), OK);
2425 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2426 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2427 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2428 &def, sizeof(def)), OK);
2429
2430 // profile
2431 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2432 InitOMXParams(&profile);
2433 profile.nPortIndex = kPortIndexOutput;
2434 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2435 &profile, sizeof(profile)), OK);
2436 profile.nChannels = numChannels;
2437 profile.eChannelMode = (numChannels == 1?
2438 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2439 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002440 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002441 profile.nAudioBandWidth = 0;
2442 profile.nFrameLength = 0;
2443 profile.nAACtools = OMX_AUDIO_AACToolAll;
2444 profile.nAACERtools = OMX_AUDIO_AACERNone;
2445 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2446 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2447 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2448 &profile, sizeof(profile)), OK);
2449
Andreas Huberda050cf22009-09-02 14:01:43 -07002450 } else {
2451 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002452 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002453 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002454
Andreas Huber784202e2009-10-15 13:46:54 -07002455 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002456 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2457 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002458
Andreas Huberda050cf22009-09-02 14:01:43 -07002459 profile.nChannels = numChannels;
2460 profile.nSampleRate = sampleRate;
2461 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002462
Andreas Huber784202e2009-10-15 13:46:54 -07002463 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002464 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2465 CHECK_EQ(err, OK);
2466 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002467}
2468
2469void OMXCodec::setImageOutputFormat(
2470 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002471 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002472
2473#if 0
2474 OMX_INDEXTYPE index;
2475 status_t err = mOMX->get_extension_index(
2476 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2477 CHECK_EQ(err, OK);
2478
2479 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2480 CHECK_EQ(err, OK);
2481#endif
2482
2483 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002484 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002485 def.nPortIndex = kPortIndexOutput;
2486
Andreas Huber784202e2009-10-15 13:46:54 -07002487 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002488 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2489 CHECK_EQ(err, OK);
2490
2491 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2492
2493 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002494
Andreas Huberbe06d262009-08-14 14:37:10 -07002495 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2496 imageDef->eColorFormat = format;
2497 imageDef->nFrameWidth = width;
2498 imageDef->nFrameHeight = height;
2499
2500 switch (format) {
2501 case OMX_COLOR_FormatYUV420PackedPlanar:
2502 case OMX_COLOR_FormatYUV411Planar:
2503 {
2504 def.nBufferSize = (width * height * 3) / 2;
2505 break;
2506 }
2507
2508 case OMX_COLOR_FormatCbYCrY:
2509 {
2510 def.nBufferSize = width * height * 2;
2511 break;
2512 }
2513
2514 case OMX_COLOR_Format32bitARGB8888:
2515 {
2516 def.nBufferSize = width * height * 4;
2517 break;
2518 }
2519
Andreas Huber201511c2009-09-08 14:01:44 -07002520 case OMX_COLOR_Format16bitARGB4444:
2521 case OMX_COLOR_Format16bitARGB1555:
2522 case OMX_COLOR_Format16bitRGB565:
2523 case OMX_COLOR_Format16bitBGR565:
2524 {
2525 def.nBufferSize = width * height * 2;
2526 break;
2527 }
2528
Andreas Huberbe06d262009-08-14 14:37:10 -07002529 default:
2530 CHECK(!"Should not be here. Unknown color format.");
2531 break;
2532 }
2533
Andreas Huber5c0a9132009-08-20 11:16:40 -07002534 def.nBufferCountActual = def.nBufferCountMin;
2535
Andreas Huber784202e2009-10-15 13:46:54 -07002536 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002537 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2538 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002539}
Andreas Huberbe06d262009-08-14 14:37:10 -07002540
Andreas Huber5c0a9132009-08-20 11:16:40 -07002541void OMXCodec::setJPEGInputFormat(
2542 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2543 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002544 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002545 def.nPortIndex = kPortIndexInput;
2546
Andreas Huber784202e2009-10-15 13:46:54 -07002547 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002548 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2549 CHECK_EQ(err, OK);
2550
Andreas Huber5c0a9132009-08-20 11:16:40 -07002551 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2552 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2553
Andreas Huberbe06d262009-08-14 14:37:10 -07002554 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2555 imageDef->nFrameWidth = width;
2556 imageDef->nFrameHeight = height;
2557
Andreas Huber5c0a9132009-08-20 11:16:40 -07002558 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002559 def.nBufferCountActual = def.nBufferCountMin;
2560
Andreas Huber784202e2009-10-15 13:46:54 -07002561 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002562 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2563 CHECK_EQ(err, OK);
2564}
2565
2566void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2567 CodecSpecificData *specific =
2568 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2569
2570 specific->mSize = size;
2571 memcpy(specific->mData, data, size);
2572
2573 mCodecSpecificData.push(specific);
2574}
2575
2576void OMXCodec::clearCodecSpecificData() {
2577 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2578 free(mCodecSpecificData.editItemAt(i));
2579 }
2580 mCodecSpecificData.clear();
2581 mCodecSpecificDataIndex = 0;
2582}
2583
James Dong36e573b2010-06-19 09:04:18 -07002584status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07002585 Mutex::Autolock autoLock(mLock);
2586
Andreas Huberbe06d262009-08-14 14:37:10 -07002587 if (mState != LOADED) {
2588 return UNKNOWN_ERROR;
2589 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002590
Andreas Huberbe06d262009-08-14 14:37:10 -07002591 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002592 if (mQuirks & kWantsNALFragments) {
2593 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002594 }
James Dong36e573b2010-06-19 09:04:18 -07002595 if (meta) {
2596 int64_t startTimeUs = 0;
2597 int64_t timeUs;
2598 if (meta->findInt64(kKeyTime, &timeUs)) {
2599 startTimeUs = timeUs;
2600 }
2601 params->setInt64(kKeyTime, startTimeUs);
2602 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002603 status_t err = mSource->start(params.get());
2604
2605 if (err != OK) {
2606 return err;
2607 }
2608
2609 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002610 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002611 mSignalledEOS = false;
2612 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002613 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002614 mSeekTimeUs = -1;
2615 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07002616 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002617
2618 return init();
2619}
2620
2621status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002622 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002623
2624 Mutex::Autolock autoLock(mLock);
2625
2626 while (isIntermediateState(mState)) {
2627 mAsyncCompletion.wait(mLock);
2628 }
2629
2630 switch (mState) {
2631 case LOADED:
2632 case ERROR:
2633 break;
2634
2635 case EXECUTING:
2636 {
2637 setState(EXECUTING_TO_IDLE);
2638
Andreas Huber127fcdc2009-08-26 16:27:02 -07002639 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002640 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002641 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002642
Andreas Huber127fcdc2009-08-26 16:27:02 -07002643 bool emulateInputFlushCompletion =
2644 !flushPortAsync(kPortIndexInput);
2645
2646 bool emulateOutputFlushCompletion =
2647 !flushPortAsync(kPortIndexOutput);
2648
2649 if (emulateInputFlushCompletion) {
2650 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2651 }
2652
2653 if (emulateOutputFlushCompletion) {
2654 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2655 }
2656 } else {
2657 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2658 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2659
2660 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002661 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002662 CHECK_EQ(err, OK);
2663 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002664
2665 while (mState != LOADED && mState != ERROR) {
2666 mAsyncCompletion.wait(mLock);
2667 }
2668
2669 break;
2670 }
2671
2672 default:
2673 {
2674 CHECK(!"should not be here.");
2675 break;
2676 }
2677 }
2678
Andreas Hubera4357ad2010-04-02 12:49:54 -07002679 if (mLeftOverBuffer) {
2680 mLeftOverBuffer->release();
2681 mLeftOverBuffer = NULL;
2682 }
2683
Andreas Huberbe06d262009-08-14 14:37:10 -07002684 mSource->stop();
2685
Andreas Huber4a9375e2010-02-09 11:54:33 -08002686 CODEC_LOGV("stopped");
2687
Andreas Huberbe06d262009-08-14 14:37:10 -07002688 return OK;
2689}
2690
2691sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002692 Mutex::Autolock autoLock(mLock);
2693
Andreas Huberbe06d262009-08-14 14:37:10 -07002694 return mOutputFormat;
2695}
2696
2697status_t OMXCodec::read(
2698 MediaBuffer **buffer, const ReadOptions *options) {
2699 *buffer = NULL;
2700
2701 Mutex::Autolock autoLock(mLock);
2702
Andreas Huberd06e5b82009-08-28 13:18:14 -07002703 if (mState != EXECUTING && mState != RECONFIGURING) {
2704 return UNKNOWN_ERROR;
2705 }
2706
Andreas Hubere981c332009-10-22 13:49:30 -07002707 bool seeking = false;
2708 int64_t seekTimeUs;
2709 if (options && options->getSeekTo(&seekTimeUs)) {
2710 seeking = true;
2711 }
2712
Andreas Huber42978e52009-08-27 10:08:39 -07002713 if (mInitialBufferSubmit) {
2714 mInitialBufferSubmit = false;
2715
Andreas Hubere981c332009-10-22 13:49:30 -07002716 if (seeking) {
2717 CHECK(seekTimeUs >= 0);
2718 mSeekTimeUs = seekTimeUs;
2719
2720 // There's no reason to trigger the code below, there's
2721 // nothing to flush yet.
2722 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002723 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07002724 }
2725
Andreas Huber42978e52009-08-27 10:08:39 -07002726 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002727
Andreas Huberd06e5b82009-08-28 13:18:14 -07002728 if (mState == EXECUTING) {
2729 // Otherwise mState == RECONFIGURING and this code will trigger
2730 // after the output port is reenabled.
2731 fillOutputBuffers();
2732 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002733 }
2734
Andreas Hubere981c332009-10-22 13:49:30 -07002735 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002736 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002737
2738 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002739
2740 CHECK(seekTimeUs >= 0);
2741 mSeekTimeUs = seekTimeUs;
2742
2743 mFilledBuffers.clear();
2744
2745 CHECK_EQ(mState, EXECUTING);
2746
Andreas Huber404cc412009-08-25 14:26:05 -07002747 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2748 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2749
2750 if (emulateInputFlushCompletion) {
2751 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2752 }
2753
2754 if (emulateOutputFlushCompletion) {
2755 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2756 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002757
2758 while (mSeekTimeUs >= 0) {
2759 mBufferFilled.wait(mLock);
2760 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002761 }
2762
2763 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2764 mBufferFilled.wait(mLock);
2765 }
2766
2767 if (mState == ERROR) {
2768 return UNKNOWN_ERROR;
2769 }
2770
2771 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08002772 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07002773 }
2774
Andreas Hubercfd55572009-10-09 14:11:28 -07002775 if (mOutputPortSettingsHaveChanged) {
2776 mOutputPortSettingsHaveChanged = false;
2777
2778 return INFO_FORMAT_CHANGED;
2779 }
2780
Andreas Huberbe06d262009-08-14 14:37:10 -07002781 size_t index = *mFilledBuffers.begin();
2782 mFilledBuffers.erase(mFilledBuffers.begin());
2783
2784 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2785 info->mMediaBuffer->add_ref();
2786 *buffer = info->mMediaBuffer;
2787
2788 return OK;
2789}
2790
2791void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2792 Mutex::Autolock autoLock(mLock);
2793
2794 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2795 for (size_t i = 0; i < buffers->size(); ++i) {
2796 BufferInfo *info = &buffers->editItemAt(i);
2797
2798 if (info->mMediaBuffer == buffer) {
2799 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2800 fillOutputBuffer(info);
2801 return;
2802 }
2803 }
2804
2805 CHECK(!"should not be here.");
2806}
2807
2808static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2809 static const char *kNames[] = {
2810 "OMX_IMAGE_CodingUnused",
2811 "OMX_IMAGE_CodingAutoDetect",
2812 "OMX_IMAGE_CodingJPEG",
2813 "OMX_IMAGE_CodingJPEG2K",
2814 "OMX_IMAGE_CodingEXIF",
2815 "OMX_IMAGE_CodingTIFF",
2816 "OMX_IMAGE_CodingGIF",
2817 "OMX_IMAGE_CodingPNG",
2818 "OMX_IMAGE_CodingLZW",
2819 "OMX_IMAGE_CodingBMP",
2820 };
2821
2822 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2823
2824 if (type < 0 || (size_t)type >= numNames) {
2825 return "UNKNOWN";
2826 } else {
2827 return kNames[type];
2828 }
2829}
2830
2831static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2832 static const char *kNames[] = {
2833 "OMX_COLOR_FormatUnused",
2834 "OMX_COLOR_FormatMonochrome",
2835 "OMX_COLOR_Format8bitRGB332",
2836 "OMX_COLOR_Format12bitRGB444",
2837 "OMX_COLOR_Format16bitARGB4444",
2838 "OMX_COLOR_Format16bitARGB1555",
2839 "OMX_COLOR_Format16bitRGB565",
2840 "OMX_COLOR_Format16bitBGR565",
2841 "OMX_COLOR_Format18bitRGB666",
2842 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002843 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002844 "OMX_COLOR_Format24bitRGB888",
2845 "OMX_COLOR_Format24bitBGR888",
2846 "OMX_COLOR_Format24bitARGB1887",
2847 "OMX_COLOR_Format25bitARGB1888",
2848 "OMX_COLOR_Format32bitBGRA8888",
2849 "OMX_COLOR_Format32bitARGB8888",
2850 "OMX_COLOR_FormatYUV411Planar",
2851 "OMX_COLOR_FormatYUV411PackedPlanar",
2852 "OMX_COLOR_FormatYUV420Planar",
2853 "OMX_COLOR_FormatYUV420PackedPlanar",
2854 "OMX_COLOR_FormatYUV420SemiPlanar",
2855 "OMX_COLOR_FormatYUV422Planar",
2856 "OMX_COLOR_FormatYUV422PackedPlanar",
2857 "OMX_COLOR_FormatYUV422SemiPlanar",
2858 "OMX_COLOR_FormatYCbYCr",
2859 "OMX_COLOR_FormatYCrYCb",
2860 "OMX_COLOR_FormatCbYCrY",
2861 "OMX_COLOR_FormatCrYCbY",
2862 "OMX_COLOR_FormatYUV444Interleaved",
2863 "OMX_COLOR_FormatRawBayer8bit",
2864 "OMX_COLOR_FormatRawBayer10bit",
2865 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002866 "OMX_COLOR_FormatL2",
2867 "OMX_COLOR_FormatL4",
2868 "OMX_COLOR_FormatL8",
2869 "OMX_COLOR_FormatL16",
2870 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002871 "OMX_COLOR_FormatL32",
2872 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2873 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2874 "OMX_COLOR_Format18BitBGR666",
2875 "OMX_COLOR_Format24BitARGB6666",
2876 "OMX_COLOR_Format24BitABGR6666",
2877 };
2878
2879 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2880
Andreas Huberbe06d262009-08-14 14:37:10 -07002881 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2882 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2883 } else if (type < 0 || (size_t)type >= numNames) {
2884 return "UNKNOWN";
2885 } else {
2886 return kNames[type];
2887 }
2888}
2889
2890static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2891 static const char *kNames[] = {
2892 "OMX_VIDEO_CodingUnused",
2893 "OMX_VIDEO_CodingAutoDetect",
2894 "OMX_VIDEO_CodingMPEG2",
2895 "OMX_VIDEO_CodingH263",
2896 "OMX_VIDEO_CodingMPEG4",
2897 "OMX_VIDEO_CodingWMV",
2898 "OMX_VIDEO_CodingRV",
2899 "OMX_VIDEO_CodingAVC",
2900 "OMX_VIDEO_CodingMJPEG",
2901 };
2902
2903 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2904
2905 if (type < 0 || (size_t)type >= numNames) {
2906 return "UNKNOWN";
2907 } else {
2908 return kNames[type];
2909 }
2910}
2911
2912static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2913 static const char *kNames[] = {
2914 "OMX_AUDIO_CodingUnused",
2915 "OMX_AUDIO_CodingAutoDetect",
2916 "OMX_AUDIO_CodingPCM",
2917 "OMX_AUDIO_CodingADPCM",
2918 "OMX_AUDIO_CodingAMR",
2919 "OMX_AUDIO_CodingGSMFR",
2920 "OMX_AUDIO_CodingGSMEFR",
2921 "OMX_AUDIO_CodingGSMHR",
2922 "OMX_AUDIO_CodingPDCFR",
2923 "OMX_AUDIO_CodingPDCEFR",
2924 "OMX_AUDIO_CodingPDCHR",
2925 "OMX_AUDIO_CodingTDMAFR",
2926 "OMX_AUDIO_CodingTDMAEFR",
2927 "OMX_AUDIO_CodingQCELP8",
2928 "OMX_AUDIO_CodingQCELP13",
2929 "OMX_AUDIO_CodingEVRC",
2930 "OMX_AUDIO_CodingSMV",
2931 "OMX_AUDIO_CodingG711",
2932 "OMX_AUDIO_CodingG723",
2933 "OMX_AUDIO_CodingG726",
2934 "OMX_AUDIO_CodingG729",
2935 "OMX_AUDIO_CodingAAC",
2936 "OMX_AUDIO_CodingMP3",
2937 "OMX_AUDIO_CodingSBC",
2938 "OMX_AUDIO_CodingVORBIS",
2939 "OMX_AUDIO_CodingWMA",
2940 "OMX_AUDIO_CodingRA",
2941 "OMX_AUDIO_CodingMIDI",
2942 };
2943
2944 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2945
2946 if (type < 0 || (size_t)type >= numNames) {
2947 return "UNKNOWN";
2948 } else {
2949 return kNames[type];
2950 }
2951}
2952
2953static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2954 static const char *kNames[] = {
2955 "OMX_AUDIO_PCMModeLinear",
2956 "OMX_AUDIO_PCMModeALaw",
2957 "OMX_AUDIO_PCMModeMULaw",
2958 };
2959
2960 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2961
2962 if (type < 0 || (size_t)type >= numNames) {
2963 return "UNKNOWN";
2964 } else {
2965 return kNames[type];
2966 }
2967}
2968
Andreas Huber7ae02c82009-09-09 16:29:47 -07002969static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2970 static const char *kNames[] = {
2971 "OMX_AUDIO_AMRBandModeUnused",
2972 "OMX_AUDIO_AMRBandModeNB0",
2973 "OMX_AUDIO_AMRBandModeNB1",
2974 "OMX_AUDIO_AMRBandModeNB2",
2975 "OMX_AUDIO_AMRBandModeNB3",
2976 "OMX_AUDIO_AMRBandModeNB4",
2977 "OMX_AUDIO_AMRBandModeNB5",
2978 "OMX_AUDIO_AMRBandModeNB6",
2979 "OMX_AUDIO_AMRBandModeNB7",
2980 "OMX_AUDIO_AMRBandModeWB0",
2981 "OMX_AUDIO_AMRBandModeWB1",
2982 "OMX_AUDIO_AMRBandModeWB2",
2983 "OMX_AUDIO_AMRBandModeWB3",
2984 "OMX_AUDIO_AMRBandModeWB4",
2985 "OMX_AUDIO_AMRBandModeWB5",
2986 "OMX_AUDIO_AMRBandModeWB6",
2987 "OMX_AUDIO_AMRBandModeWB7",
2988 "OMX_AUDIO_AMRBandModeWB8",
2989 };
2990
2991 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2992
2993 if (type < 0 || (size_t)type >= numNames) {
2994 return "UNKNOWN";
2995 } else {
2996 return kNames[type];
2997 }
2998}
2999
3000static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3001 static const char *kNames[] = {
3002 "OMX_AUDIO_AMRFrameFormatConformance",
3003 "OMX_AUDIO_AMRFrameFormatIF1",
3004 "OMX_AUDIO_AMRFrameFormatIF2",
3005 "OMX_AUDIO_AMRFrameFormatFSF",
3006 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3007 "OMX_AUDIO_AMRFrameFormatITU",
3008 };
3009
3010 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3011
3012 if (type < 0 || (size_t)type >= numNames) {
3013 return "UNKNOWN";
3014 } else {
3015 return kNames[type];
3016 }
3017}
Andreas Huberbe06d262009-08-14 14:37:10 -07003018
3019void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3020 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003021 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003022 def.nPortIndex = portIndex;
3023
Andreas Huber784202e2009-10-15 13:46:54 -07003024 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003025 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3026 CHECK_EQ(err, OK);
3027
3028 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3029
3030 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3031 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3032
3033 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3034 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3035 printf(" nBufferSize = %ld\n", def.nBufferSize);
3036
3037 switch (def.eDomain) {
3038 case OMX_PortDomainImage:
3039 {
3040 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3041
3042 printf("\n");
3043 printf(" // Image\n");
3044 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3045 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3046 printf(" nStride = %ld\n", imageDef->nStride);
3047
3048 printf(" eCompressionFormat = %s\n",
3049 imageCompressionFormatString(imageDef->eCompressionFormat));
3050
3051 printf(" eColorFormat = %s\n",
3052 colorFormatString(imageDef->eColorFormat));
3053
3054 break;
3055 }
3056
3057 case OMX_PortDomainVideo:
3058 {
3059 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3060
3061 printf("\n");
3062 printf(" // Video\n");
3063 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3064 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3065 printf(" nStride = %ld\n", videoDef->nStride);
3066
3067 printf(" eCompressionFormat = %s\n",
3068 videoCompressionFormatString(videoDef->eCompressionFormat));
3069
3070 printf(" eColorFormat = %s\n",
3071 colorFormatString(videoDef->eColorFormat));
3072
3073 break;
3074 }
3075
3076 case OMX_PortDomainAudio:
3077 {
3078 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3079
3080 printf("\n");
3081 printf(" // Audio\n");
3082 printf(" eEncoding = %s\n",
3083 audioCodingTypeString(audioDef->eEncoding));
3084
3085 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3086 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003087 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003088 params.nPortIndex = portIndex;
3089
Andreas Huber784202e2009-10-15 13:46:54 -07003090 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003091 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3092 CHECK_EQ(err, OK);
3093
3094 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3095 printf(" nChannels = %ld\n", params.nChannels);
3096 printf(" bInterleaved = %d\n", params.bInterleaved);
3097 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3098
3099 printf(" eNumData = %s\n",
3100 params.eNumData == OMX_NumericalDataSigned
3101 ? "signed" : "unsigned");
3102
3103 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003104 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3105 OMX_AUDIO_PARAM_AMRTYPE amr;
3106 InitOMXParams(&amr);
3107 amr.nPortIndex = portIndex;
3108
Andreas Huber784202e2009-10-15 13:46:54 -07003109 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003110 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3111 CHECK_EQ(err, OK);
3112
3113 printf(" nChannels = %ld\n", amr.nChannels);
3114 printf(" eAMRBandMode = %s\n",
3115 amrBandModeString(amr.eAMRBandMode));
3116 printf(" eAMRFrameFormat = %s\n",
3117 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003118 }
3119
3120 break;
3121 }
3122
3123 default:
3124 {
3125 printf(" // Unknown\n");
3126 break;
3127 }
3128 }
3129
3130 printf("}\n");
3131}
3132
3133void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3134 mOutputFormat = new MetaData;
3135 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
3136
3137 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003138 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003139 def.nPortIndex = kPortIndexOutput;
3140
Andreas Huber784202e2009-10-15 13:46:54 -07003141 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003142 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3143 CHECK_EQ(err, OK);
3144
3145 switch (def.eDomain) {
3146 case OMX_PortDomainImage:
3147 {
3148 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3149 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3150
Andreas Hubere6c40962009-09-10 14:13:30 -07003151 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003152 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3153 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3154 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3155 break;
3156 }
3157
3158 case OMX_PortDomainAudio:
3159 {
3160 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3161
Andreas Huberda050cf22009-09-02 14:01:43 -07003162 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3163 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003164 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003165 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003166
Andreas Huber784202e2009-10-15 13:46:54 -07003167 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003168 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3169 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003170
Andreas Huberda050cf22009-09-02 14:01:43 -07003171 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3172 CHECK_EQ(params.nBitPerSample, 16);
3173 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003174
Andreas Huberda050cf22009-09-02 14:01:43 -07003175 int32_t numChannels, sampleRate;
3176 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3177 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003178
Andreas Huberda050cf22009-09-02 14:01:43 -07003179 if ((OMX_U32)numChannels != params.nChannels) {
3180 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003181 "the input stream contains (contains %d channels, "
3182 "codec outputs %ld channels).",
3183 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003184 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003185
Andreas Hubere6c40962009-09-10 14:13:30 -07003186 mOutputFormat->setCString(
3187 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003188
3189 // Use the codec-advertised number of channels, as some
3190 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003191 // mono. If we know the codec lies about this information,
3192 // use the actual number of channels instead.
3193 mOutputFormat->setInt32(
3194 kKeyChannelCount,
3195 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3196 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003197
3198 // The codec-reported sampleRate is not reliable...
3199 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3200 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003201 OMX_AUDIO_PARAM_AMRTYPE amr;
3202 InitOMXParams(&amr);
3203 amr.nPortIndex = kPortIndexOutput;
3204
Andreas Huber784202e2009-10-15 13:46:54 -07003205 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003206 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3207 CHECK_EQ(err, OK);
3208
3209 CHECK_EQ(amr.nChannels, 1);
3210 mOutputFormat->setInt32(kKeyChannelCount, 1);
3211
3212 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3213 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003214 mOutputFormat->setCString(
3215 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003216 mOutputFormat->setInt32(kKeySampleRate, 8000);
3217 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3218 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003219 mOutputFormat->setCString(
3220 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003221 mOutputFormat->setInt32(kKeySampleRate, 16000);
3222 } else {
3223 CHECK(!"Unknown AMR band mode.");
3224 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003225 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003226 mOutputFormat->setCString(
3227 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003228 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003229 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3230 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003231 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003232 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3233 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003234 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003235 } else {
3236 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003237 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003238 break;
3239 }
3240
3241 case OMX_PortDomainVideo:
3242 {
3243 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3244
3245 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003246 mOutputFormat->setCString(
3247 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003248 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003249 mOutputFormat->setCString(
3250 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003251 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003252 mOutputFormat->setCString(
3253 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003254 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003255 mOutputFormat->setCString(
3256 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003257 } else {
3258 CHECK(!"Unknown compression format.");
3259 }
3260
3261 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3262 // This component appears to be lying to me.
3263 mOutputFormat->setInt32(
3264 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3265 mOutputFormat->setInt32(
3266 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3267 } else {
3268 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3269 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3270 }
3271
3272 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3273 break;
3274 }
3275
3276 default:
3277 {
3278 CHECK(!"should not be here, neither audio nor video.");
3279 break;
3280 }
3281 }
3282}
3283
Andreas Huber1f24b302010-06-10 11:12:39 -07003284status_t OMXCodec::pause() {
3285 Mutex::Autolock autoLock(mLock);
3286
3287 mPaused = true;
3288
3289 return OK;
3290}
3291
Andreas Hubere6c40962009-09-10 14:13:30 -07003292////////////////////////////////////////////////////////////////////////////////
3293
3294status_t QueryCodecs(
3295 const sp<IOMX> &omx,
3296 const char *mime, bool queryDecoders,
3297 Vector<CodecCapabilities> *results) {
3298 results->clear();
3299
3300 for (int index = 0;; ++index) {
3301 const char *componentName;
3302
3303 if (!queryDecoders) {
3304 componentName = GetCodec(
3305 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3306 mime, index);
3307 } else {
3308 componentName = GetCodec(
3309 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3310 mime, index);
3311 }
3312
3313 if (!componentName) {
3314 return OK;
3315 }
3316
Andreas Huber1a189a82010-03-24 13:49:20 -07003317 if (strncmp(componentName, "OMX.", 4)) {
3318 // Not an OpenMax component but a software codec.
3319
3320 results->push();
3321 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3322 caps->mComponentName = componentName;
3323
3324 continue;
3325 }
3326
Andreas Huber784202e2009-10-15 13:46:54 -07003327 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003328 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003329 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003330
3331 if (err != OK) {
3332 continue;
3333 }
3334
James Dong722d5912010-04-13 10:56:59 -07003335 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003336
3337 results->push();
3338 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3339 caps->mComponentName = componentName;
3340
3341 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3342 InitOMXParams(&param);
3343
3344 param.nPortIndex = queryDecoders ? 0 : 1;
3345
3346 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003347 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003348 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3349 &param, sizeof(param));
3350
3351 if (err != OK) {
3352 break;
3353 }
3354
3355 CodecProfileLevel profileLevel;
3356 profileLevel.mProfile = param.eProfile;
3357 profileLevel.mLevel = param.eLevel;
3358
3359 caps->mProfileLevels.push(profileLevel);
3360 }
3361
Andreas Huber784202e2009-10-15 13:46:54 -07003362 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003363 }
3364}
3365
Andreas Huberbe06d262009-08-14 14:37:10 -07003366} // namespace android