blob: 9a49a9b7ba585e7aede58c1e433fa18c3881ee9c [file] [log] [blame]
Andreas Huberbe06d262009-08-14 14:37:10 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "OMXCodec"
19#include <utils/Log.h>
20
Andreas Huberdacaa732009-12-07 09:56:32 -080021#include "include/AACDecoder.h"
James Dong17299ab2010-05-14 15:45:22 -070022#include "include/AACEncoder.h"
Andreas Hubera30d4002009-12-08 15:40:06 -080023#include "include/AMRNBDecoder.h"
Andreas Huberd49b526dd2009-12-11 15:07:25 -080024#include "include/AMRNBEncoder.h"
Andreas Hubera30d4002009-12-08 15:40:06 -080025#include "include/AMRWBDecoder.h"
James Dong17299ab2010-05-14 15:45:22 -070026#include "include/AMRWBEncoder.h"
Andreas Huber4a0ec3f2009-12-10 09:44:29 -080027#include "include/AVCDecoder.h"
James Dong1cc31e62010-07-02 17:44:44 -070028#include "include/AVCEncoder.h"
Andreas Huber520b2a72010-08-09 09:54:59 -070029#include "include/G711Decoder.h"
James Dong02f5b542009-12-15 16:26:55 -080030#include "include/M4vH263Decoder.h"
James Dong42ef0c72010-07-12 21:46:25 -070031#include "include/M4vH263Encoder.h"
Andreas Huber250f2432009-12-07 14:22:35 -080032#include "include/MP3Decoder.h"
Andreas Huber388379f2010-05-07 10:35:13 -070033#include "include/VorbisDecoder.h"
Andreas Huber47ba30e2010-05-24 14:38:02 -070034#include "include/VPXDecoder.h"
Andreas Huber8c7ab032009-12-07 11:23:44 -080035
Andreas Huberbd7b43b2009-10-13 10:22:55 -070036#include "include/ESDS.h"
37
Andreas Huberbe06d262009-08-14 14:37:10 -070038#include <binder/IServiceManager.h>
39#include <binder/MemoryDealer.h>
40#include <binder/ProcessState.h>
41#include <media/IMediaPlayerService.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070042#include <media/stagefright/MediaBuffer.h>
43#include <media/stagefright/MediaBufferGroup.h>
44#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070045#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070046#include <media/stagefright/MediaExtractor.h>
47#include <media/stagefright/MetaData.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070048#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070049#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070050#include <utils/Vector.h>
51
52#include <OMX_Audio.h>
53#include <OMX_Component.h>
54
Andreas Huber8946ab22010-09-15 16:20:42 -070055#include "include/ThreadedSource.h"
56
Andreas Huberbe06d262009-08-14 14:37:10 -070057namespace android {
58
Andreas Huber8b432b12009-10-07 13:36:52 -070059static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
60
Andreas Huberbe06d262009-08-14 14:37:10 -070061struct CodecInfo {
62 const char *mime;
63 const char *codec;
64};
65
Andreas Huberfb1c2f82009-12-15 13:25:11 -080066#define FACTORY_CREATE(name) \
67static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
68 return new name(source); \
69}
70
James Dong17299ab2010-05-14 15:45:22 -070071#define FACTORY_CREATE_ENCODER(name) \
72static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \
73 return new name(source, meta); \
74}
75
Andreas Huberfb1c2f82009-12-15 13:25:11 -080076#define FACTORY_REF(name) { #name, Make##name },
77
78FACTORY_CREATE(MP3Decoder)
79FACTORY_CREATE(AMRNBDecoder)
80FACTORY_CREATE(AMRWBDecoder)
81FACTORY_CREATE(AACDecoder)
82FACTORY_CREATE(AVCDecoder)
Andreas Huber520b2a72010-08-09 09:54:59 -070083FACTORY_CREATE(G711Decoder)
James Dong02f5b542009-12-15 16:26:55 -080084FACTORY_CREATE(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -070085FACTORY_CREATE(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -070086FACTORY_CREATE(VPXDecoder)
James Dong17299ab2010-05-14 15:45:22 -070087FACTORY_CREATE_ENCODER(AMRNBEncoder)
88FACTORY_CREATE_ENCODER(AMRWBEncoder)
89FACTORY_CREATE_ENCODER(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -070090FACTORY_CREATE_ENCODER(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -070091FACTORY_CREATE_ENCODER(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -070092
93static sp<MediaSource> InstantiateSoftwareEncoder(
94 const char *name, const sp<MediaSource> &source,
95 const sp<MetaData> &meta) {
96 struct FactoryInfo {
97 const char *name;
98 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &);
99 };
100
101 static const FactoryInfo kFactoryInfo[] = {
102 FACTORY_REF(AMRNBEncoder)
103 FACTORY_REF(AMRWBEncoder)
104 FACTORY_REF(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -0700105 FACTORY_REF(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -0700106 FACTORY_REF(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -0700107 };
108 for (size_t i = 0;
109 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
110 if (!strcmp(name, kFactoryInfo[i].name)) {
111 return (*kFactoryInfo[i].CreateFunc)(source, meta);
112 }
113 }
114
115 return NULL;
116}
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800117
118static sp<MediaSource> InstantiateSoftwareCodec(
119 const char *name, const sp<MediaSource> &source) {
120 struct FactoryInfo {
121 const char *name;
122 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
123 };
124
125 static const FactoryInfo kFactoryInfo[] = {
126 FACTORY_REF(MP3Decoder)
127 FACTORY_REF(AMRNBDecoder)
128 FACTORY_REF(AMRWBDecoder)
129 FACTORY_REF(AACDecoder)
130 FACTORY_REF(AVCDecoder)
Andreas Huber520b2a72010-08-09 09:54:59 -0700131 FACTORY_REF(G711Decoder)
James Dong02f5b542009-12-15 16:26:55 -0800132 FACTORY_REF(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -0700133 FACTORY_REF(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -0700134 FACTORY_REF(VPXDecoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800135 };
136 for (size_t i = 0;
137 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
138 if (!strcmp(name, kFactoryInfo[i].name)) {
Andreas Huber8946ab22010-09-15 16:20:42 -0700139 if (!strcmp(name, "VPXDecoder")) {
140 return new ThreadedSource(
141 (*kFactoryInfo[i].CreateFunc)(source));
142 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800143 return (*kFactoryInfo[i].CreateFunc)(source);
144 }
145 }
146
147 return NULL;
148}
149
150#undef FACTORY_REF
151#undef FACTORY_CREATE
152
Andreas Huberbe06d262009-08-14 14:37:10 -0700153static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700154 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
James Dong374aee62010-04-26 10:23:30 -0700155// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800156 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700157// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700158// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800159 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700160// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700161 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800162 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700163// { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700164 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800165 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700166// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
Andreas Huber520b2a72010-08-09 09:54:59 -0700167 { MEDIA_MIMETYPE_AUDIO_G711_ALAW, "G711Decoder" },
168 { MEDIA_MIMETYPE_AUDIO_G711_MLAW, "G711Decoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700169 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.decoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700170 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
171 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700172 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800173 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700174// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700175 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.decoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700176 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700177 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800178 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700179// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700180 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.decoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700181 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
182 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700183 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800184 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700185// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huber388379f2010-05-07 10:35:13 -0700186 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" },
Andreas Huber47ba30e2010-05-24 14:38:02 -0700187 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700188};
189
190static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700191 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800192 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700193 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700194 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700195 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700196 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700197// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
198 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.encoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700199 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
200 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700201 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700202 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700203// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
204 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.encoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700205 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
206 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700207 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700208 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700209// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
210 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.encoder.avc" },
Andreas Huber71c27d92010-03-19 11:43:15 -0700211 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700212 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700213 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Encoder" },
James Dong1cc31e62010-07-02 17:44:44 -0700214 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700215// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700216};
217
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800218#undef OPTIONAL
219
Andreas Hubere0873732009-09-10 09:57:53 -0700220#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700221#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber42c444a2010-02-09 10:20:00 -0800222#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700223
Andreas Huberbe06d262009-08-14 14:37:10 -0700224struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700225 OMXCodecObserver() {
226 }
227
228 void setCodec(const sp<OMXCodec> &target) {
229 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700230 }
231
232 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700233 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700234 sp<OMXCodec> codec = mTarget.promote();
235
236 if (codec.get() != NULL) {
237 codec->on_message(msg);
238 }
239 }
240
241protected:
242 virtual ~OMXCodecObserver() {}
243
244private:
245 wp<OMXCodec> mTarget;
246
247 OMXCodecObserver(const OMXCodecObserver &);
248 OMXCodecObserver &operator=(const OMXCodecObserver &);
249};
250
251static const char *GetCodec(const CodecInfo *info, size_t numInfos,
252 const char *mime, int index) {
253 CHECK(index >= 0);
254 for(size_t i = 0; i < numInfos; ++i) {
255 if (!strcasecmp(mime, info[i].mime)) {
256 if (index == 0) {
257 return info[i].codec;
258 }
259
260 --index;
261 }
262 }
263
264 return NULL;
265}
266
Andreas Huberebf66ea2009-08-19 13:32:58 -0700267enum {
268 kAVCProfileBaseline = 0x42,
269 kAVCProfileMain = 0x4d,
270 kAVCProfileExtended = 0x58,
271 kAVCProfileHigh = 0x64,
272 kAVCProfileHigh10 = 0x6e,
273 kAVCProfileHigh422 = 0x7a,
274 kAVCProfileHigh444 = 0xf4,
275 kAVCProfileCAVLC444Intra = 0x2c
276};
277
278static const char *AVCProfileToString(uint8_t profile) {
279 switch (profile) {
280 case kAVCProfileBaseline:
281 return "Baseline";
282 case kAVCProfileMain:
283 return "Main";
284 case kAVCProfileExtended:
285 return "Extended";
286 case kAVCProfileHigh:
287 return "High";
288 case kAVCProfileHigh10:
289 return "High 10";
290 case kAVCProfileHigh422:
291 return "High 422";
292 case kAVCProfileHigh444:
293 return "High 444";
294 case kAVCProfileCAVLC444Intra:
295 return "CAVLC 444 Intra";
296 default: return "Unknown";
297 }
298}
299
Andreas Huber4c483422009-09-02 16:05:36 -0700300template<class T>
301static void InitOMXParams(T *params) {
302 params->nSize = sizeof(T);
303 params->nVersion.s.nVersionMajor = 1;
304 params->nVersion.s.nVersionMinor = 0;
305 params->nVersion.s.nRevision = 0;
306 params->nVersion.s.nStep = 0;
307}
308
Andreas Hubere13526a2009-10-22 10:43:34 -0700309static bool IsSoftwareCodec(const char *componentName) {
310 if (!strncmp("OMX.PV.", componentName, 7)) {
311 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700312 }
313
Andreas Hubere13526a2009-10-22 10:43:34 -0700314 return false;
315}
316
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800317// A sort order in which non-OMX components are first,
318// followed by software codecs, i.e. OMX.PV.*, followed
319// by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700320static int CompareSoftwareCodecsFirst(
321 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800322 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
323 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
324
325 if (isNotOMX1) {
326 if (isNotOMX2) { return 0; }
327 return -1;
328 }
329 if (isNotOMX2) {
330 return 1;
331 }
332
Andreas Hubere13526a2009-10-22 10:43:34 -0700333 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
334 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
335
336 if (isSoftwareCodec1) {
337 if (isSoftwareCodec2) { return 0; }
338 return -1;
339 }
340
341 if (isSoftwareCodec2) {
342 return 1;
343 }
344
345 return 0;
346}
347
348// static
Andreas Huber1e194162010-10-06 16:43:57 -0700349uint32_t OMXCodec::getComponentQuirks(
350 const char *componentName, bool isEncoder) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700351 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700352
Andreas Huberbe06d262009-08-14 14:37:10 -0700353 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700354 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700355 }
356 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
357 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800358 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700359 }
360 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
361 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700362 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700363 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700364 }
365 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
366 quirks |= kRequiresLoadedToIdleAfterAllocation;
367 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700368 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dong90862e22010-08-26 19:12:59 -0700369 if (!strncmp(componentName, "OMX.qcom.video.encoder.avc", 26)) {
370
371 // The AVC encoder advertises the size of output buffers
372 // based on the input video resolution and assumes
373 // the worst/least compression ratio is 0.5. It is found that
374 // sometimes, the output buffer size is larger than
375 // size advertised by the encoder.
376 quirks |= kRequiresLargerEncoderOutputBuffer;
377 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700378 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700379 if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) {
380 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700381 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700382 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800383 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700384 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700385 if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) {
386 quirks |= kRequiresAllocateBufferOnInputPorts;
387 quirks |= kRequiresAllocateBufferOnOutputPorts;
388 quirks |= kDefersOutputBufferAllocation;
389 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700390
Andreas Huber2dc64d82009-09-11 12:58:53 -0700391 if (!strncmp(componentName, "OMX.TI.", 7)) {
392 // Apparently I must not use OMX_UseBuffer on either input or
393 // output ports on any of the TI components or quote:
394 // "(I) may have unexpected problem (sic) which can be timing related
395 // and hard to reproduce."
396
397 quirks |= kRequiresAllocateBufferOnInputPorts;
398 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dongdca66e12010-06-14 11:14:38 -0700399 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) {
James Dong4f501f02010-06-07 14:41:41 -0700400 quirks |= kAvoidMemcopyInputRecordingFrames;
401 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700402 }
403
Andreas Huberb8de9572010-02-22 14:58:45 -0800404 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
405 quirks |= kInputBufferSizesAreBogus;
406 }
407
Andreas Huber1e194162010-10-06 16:43:57 -0700408 if (!strncmp(componentName, "OMX.SEC.", 8) && !isEncoder) {
409 // These output buffers contain no video data, just some
410 // opaque information that allows the overlay to display their
411 // contents.
412 quirks |= kOutputBuffersAreUnreadable;
413 }
414
Andreas Hubere13526a2009-10-22 10:43:34 -0700415 return quirks;
416}
417
418// static
419void OMXCodec::findMatchingCodecs(
420 const char *mime,
421 bool createEncoder, const char *matchComponentName,
422 uint32_t flags,
423 Vector<String8> *matchingCodecs) {
424 matchingCodecs->clear();
425
426 for (int index = 0;; ++index) {
427 const char *componentName;
428
429 if (createEncoder) {
430 componentName = GetCodec(
431 kEncoderInfo,
432 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
433 mime, index);
434 } else {
435 componentName = GetCodec(
436 kDecoderInfo,
437 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
438 mime, index);
439 }
440
441 if (!componentName) {
442 break;
443 }
444
445 // If a specific codec is requested, skip the non-matching ones.
446 if (matchComponentName && strcmp(componentName, matchComponentName)) {
447 continue;
448 }
449
450 matchingCodecs->push(String8(componentName));
451 }
452
453 if (flags & kPreferSoftwareCodecs) {
454 matchingCodecs->sort(CompareSoftwareCodecsFirst);
455 }
456}
457
458// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800459sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700460 const sp<IOMX> &omx,
461 const sp<MetaData> &meta, bool createEncoder,
462 const sp<MediaSource> &source,
463 const char *matchComponentName,
464 uint32_t flags) {
465 const char *mime;
466 bool success = meta->findCString(kKeyMIMEType, &mime);
467 CHECK(success);
468
469 Vector<String8> matchingCodecs;
470 findMatchingCodecs(
471 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
472
473 if (matchingCodecs.isEmpty()) {
474 return NULL;
475 }
476
477 sp<OMXCodecObserver> observer = new OMXCodecObserver;
478 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700479
480 const char *componentName;
481 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
482 componentName = matchingCodecs[i].string();
483
James Dong17299ab2010-05-14 15:45:22 -0700484 sp<MediaSource> softwareCodec = createEncoder?
485 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800486 InstantiateSoftwareCodec(componentName, source);
487
488 if (softwareCodec != NULL) {
489 LOGV("Successfully allocated software codec '%s'", componentName);
490
491 return softwareCodec;
492 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800493
Andreas Hubere13526a2009-10-22 10:43:34 -0700494 LOGV("Attempting to allocate OMX node '%s'", componentName);
495
Andreas Huber5a40e392010-10-18 09:57:42 -0700496 uint32_t quirks = getComponentQuirks(componentName, createEncoder);
497
498 if (!createEncoder
499 && (quirks & kOutputBuffersAreUnreadable)
500 && (flags & kClientNeedsFramebuffer)) {
501 if (strncmp(componentName, "OMX.SEC.", 8)) {
502 // For OMX.SEC.* decoders we can enable a special mode that
503 // gives the client access to the framebuffer contents.
504
505 LOGW("Component '%s' does not give the client access to "
506 "the framebuffer contents. Skipping.",
507 componentName);
508
509 continue;
510 }
511 }
512
Andreas Hubere13526a2009-10-22 10:43:34 -0700513 status_t err = omx->allocateNode(componentName, observer, &node);
514 if (err == OK) {
515 LOGV("Successfully allocated OMX node '%s'", componentName);
516
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700517 sp<OMXCodec> codec = new OMXCodec(
Andreas Huber5a40e392010-10-18 09:57:42 -0700518 omx, node, quirks,
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700519 createEncoder, mime, componentName,
520 source);
521
522 observer->setCodec(codec);
523
Andreas Huber4c19bf92010-09-08 14:32:20 -0700524 err = codec->configureCodec(meta, flags);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700525
526 if (err == OK) {
527 return codec;
528 }
529
530 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700531 }
532 }
533
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700534 return NULL;
535}
Andreas Hubere13526a2009-10-22 10:43:34 -0700536
Andreas Huber4c19bf92010-09-08 14:32:20 -0700537status_t OMXCodec::configureCodec(const sp<MetaData> &meta, uint32_t flags) {
538 if (!(flags & kIgnoreCodecSpecificData)) {
539 uint32_t type;
540 const void *data;
541 size_t size;
542 if (meta->findData(kKeyESDS, &type, &data, &size)) {
543 ESDS esds((const char *)data, size);
544 CHECK_EQ(esds.InitCheck(), OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700545
Andreas Huber4c19bf92010-09-08 14:32:20 -0700546 const void *codec_specific_data;
547 size_t codec_specific_data_size;
548 esds.getCodecSpecificInfo(
549 &codec_specific_data, &codec_specific_data_size);
Andreas Huberbe06d262009-08-14 14:37:10 -0700550
Andreas Huber4c19bf92010-09-08 14:32:20 -0700551 addCodecSpecificData(
552 codec_specific_data, codec_specific_data_size);
553 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
554 // Parse the AVCDecoderConfigurationRecord
Andreas Huberebf66ea2009-08-19 13:32:58 -0700555
Andreas Huber4c19bf92010-09-08 14:32:20 -0700556 const uint8_t *ptr = (const uint8_t *)data;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700557
Andreas Huber4c19bf92010-09-08 14:32:20 -0700558 CHECK(size >= 7);
559 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
560 uint8_t profile = ptr[1];
561 uint8_t level = ptr[3];
Andreas Huberebf66ea2009-08-19 13:32:58 -0700562
Andreas Huber4c19bf92010-09-08 14:32:20 -0700563 // There is decodable content out there that fails the following
564 // assertion, let's be lenient for now...
565 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700566
Andreas Huber4c19bf92010-09-08 14:32:20 -0700567 size_t lengthSize = 1 + (ptr[4] & 3);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700568
Andreas Huber4c19bf92010-09-08 14:32:20 -0700569 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
570 // violates it...
571 // CHECK((ptr[5] >> 5) == 7); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700572
Andreas Huber4c19bf92010-09-08 14:32:20 -0700573 size_t numSeqParameterSets = ptr[5] & 31;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700574
Andreas Huber4c19bf92010-09-08 14:32:20 -0700575 ptr += 6;
576 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700577
Andreas Huber4c19bf92010-09-08 14:32:20 -0700578 for (size_t i = 0; i < numSeqParameterSets; ++i) {
579 CHECK(size >= 2);
580 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700581
Andreas Huber4c19bf92010-09-08 14:32:20 -0700582 ptr += 2;
583 size -= 2;
Andreas Huberbe06d262009-08-14 14:37:10 -0700584
Andreas Huber4c19bf92010-09-08 14:32:20 -0700585 CHECK(size >= length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700586
Andreas Huber4c19bf92010-09-08 14:32:20 -0700587 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700588
Andreas Huber4c19bf92010-09-08 14:32:20 -0700589 ptr += length;
590 size -= length;
591 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700592
Andreas Huber4c19bf92010-09-08 14:32:20 -0700593 CHECK(size >= 1);
594 size_t numPictureParameterSets = *ptr;
595 ++ptr;
596 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700597
Andreas Huber4c19bf92010-09-08 14:32:20 -0700598 for (size_t i = 0; i < numPictureParameterSets; ++i) {
599 CHECK(size >= 2);
600 size_t length = U16_AT(ptr);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700601
Andreas Huber4c19bf92010-09-08 14:32:20 -0700602 ptr += 2;
603 size -= 2;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700604
Andreas Huber4c19bf92010-09-08 14:32:20 -0700605 CHECK(size >= length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700606
Andreas Huber4c19bf92010-09-08 14:32:20 -0700607 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700608
Andreas Huber4c19bf92010-09-08 14:32:20 -0700609 ptr += length;
610 size -= length;
611 }
Andreas Huberebf66ea2009-08-19 13:32:58 -0700612
Andreas Huber4c19bf92010-09-08 14:32:20 -0700613 CODEC_LOGV(
614 "AVC profile = %d (%s), level = %d",
615 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700616
Andreas Huber4c19bf92010-09-08 14:32:20 -0700617 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
618 && (profile != kAVCProfileBaseline || level > 30)) {
619 // This stream exceeds the decoder's capabilities. The decoder
620 // does not handle this gracefully and would clobber the heap
621 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700622
Andreas Huber4c19bf92010-09-08 14:32:20 -0700623 LOGE("Profile and/or level exceed the decoder's capabilities.");
624 return ERROR_UNSUPPORTED;
625 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700626 }
627 }
628
James Dong17299ab2010-05-14 15:45:22 -0700629 int32_t bitRate = 0;
630 if (mIsEncoder) {
631 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
632 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700633 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700634 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700635 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700636 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700637 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700638 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700639 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700640 int32_t numChannels, sampleRate;
641 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
642 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
643
James Dong17299ab2010-05-14 15:45:22 -0700644 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700645 }
James Dongabed93a2010-04-22 17:27:04 -0700646
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700647 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700648
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700649 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700650 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700651 } else {
James Dong1244eab2010-06-08 11:58:53 -0700652 int32_t width, height;
653 bool success = meta->findInt32(kKeyWidth, &width);
654 success = success && meta->findInt32(kKeyHeight, &height);
655 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700656 status_t err = setVideoOutputFormat(
657 mMIME, width, height);
658
659 if (err != OK) {
660 return err;
661 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700662 }
663 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700664
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700665 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
666 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700667 OMX_COLOR_FORMATTYPE format =
668 OMX_COLOR_Format32bitARGB8888;
669 // OMX_COLOR_FormatYUV420PackedPlanar;
670 // OMX_COLOR_FormatCbYCrY;
671 // OMX_COLOR_FormatYUV411Planar;
672
673 int32_t width, height;
674 bool success = meta->findInt32(kKeyWidth, &width);
675 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700676
677 int32_t compressedSize;
678 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700679 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700680
681 CHECK(success);
682 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700683
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700684 setImageOutputFormat(format, width, height);
685 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700686 }
687
Andreas Huberda050cf22009-09-02 14:01:43 -0700688 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800689 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700690 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700691 }
692
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700693 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700694 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
695 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700696 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700697 }
698
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700699 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700700
Andreas Huber5a40e392010-10-18 09:57:42 -0700701 if ((flags & kClientNeedsFramebuffer)
702 && !strncmp(mComponentName, "OMX.SEC.", 8)) {
703 OMX_INDEXTYPE index;
704
705 status_t err =
706 mOMX->getExtensionIndex(
707 mNode,
708 "OMX.SEC.index.ThumbnailMode",
709 &index);
710
711 if (err != OK) {
712 return err;
713 }
714
715 OMX_BOOL enable = OMX_TRUE;
716 err = mOMX->setConfig(mNode, index, &enable, sizeof(enable));
717
718 if (err != OK) {
719 CODEC_LOGE("setConfig('OMX.SEC.index.ThumbnailMode') "
720 "returned error 0x%08x", err);
721
722 return err;
723 }
724
725 mQuirks &= ~kOutputBuffersAreUnreadable;
726 }
727
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700728 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700729}
730
Andreas Huberda050cf22009-09-02 14:01:43 -0700731void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
732 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700733 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700734 def.nPortIndex = portIndex;
735
Andreas Huber784202e2009-10-15 13:46:54 -0700736 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700737 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
738 CHECK_EQ(err, OK);
739
Andreas Huberb8de9572010-02-22 14:58:45 -0800740 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
741 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700742 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700743 }
744
Andreas Huber784202e2009-10-15 13:46:54 -0700745 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700746 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
747 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800748
749 err = mOMX->getParameter(
750 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
751 CHECK_EQ(err, OK);
752
753 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800754 if (portIndex == kPortIndexInput
755 && (mQuirks & kInputBufferSizesAreBogus)) {
756 CHECK_EQ(def.nBufferSize, size);
757 } else {
758 CHECK(def.nBufferSize >= size);
759 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700760}
761
Andreas Huberbe06d262009-08-14 14:37:10 -0700762status_t OMXCodec::setVideoPortFormatType(
763 OMX_U32 portIndex,
764 OMX_VIDEO_CODINGTYPE compressionFormat,
765 OMX_COLOR_FORMATTYPE colorFormat) {
766 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700767 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700768 format.nPortIndex = portIndex;
769 format.nIndex = 0;
770 bool found = false;
771
772 OMX_U32 index = 0;
773 for (;;) {
774 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700775 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700776 mNode, OMX_IndexParamVideoPortFormat,
777 &format, sizeof(format));
778
779 if (err != OK) {
780 return err;
781 }
782
783 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700784 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700785
786#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700787 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700788 portIndex,
789 index, format.eCompressionFormat, format.eColorFormat);
790#endif
791
792 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
793 if (portIndex == kPortIndexInput
794 && colorFormat == format.eColorFormat) {
795 // eCompressionFormat does not seem right.
796 found = true;
797 break;
798 }
799 if (portIndex == kPortIndexOutput
800 && compressionFormat == format.eCompressionFormat) {
801 // eColorFormat does not seem right.
802 found = true;
803 break;
804 }
805 }
806
807 if (format.eCompressionFormat == compressionFormat
808 && format.eColorFormat == colorFormat) {
809 found = true;
810 break;
811 }
812
813 ++index;
814 }
815
816 if (!found) {
817 return UNKNOWN_ERROR;
818 }
819
Andreas Huber53a76bd2009-10-06 16:20:44 -0700820 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700821 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700822 mNode, OMX_IndexParamVideoPortFormat,
823 &format, sizeof(format));
824
825 return err;
826}
827
Andreas Huberb482ce82009-10-29 12:02:48 -0700828static size_t getFrameSize(
829 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
830 switch (colorFormat) {
831 case OMX_COLOR_FormatYCbYCr:
832 case OMX_COLOR_FormatCbYCrY:
833 return width * height * 2;
834
Andreas Huber71c27d92010-03-19 11:43:15 -0700835 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700836 case OMX_COLOR_FormatYUV420SemiPlanar:
837 return (width * height * 3) / 2;
838
839 default:
840 CHECK(!"Should not be here. Unsupported color format.");
841 break;
842 }
843}
844
James Dongafd97e82010-08-03 17:19:23 -0700845status_t OMXCodec::findTargetColorFormat(
846 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) {
847 LOGV("findTargetColorFormat");
848 CHECK(mIsEncoder);
849
850 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
851 int32_t targetColorFormat;
852 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) {
853 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat;
854 } else {
855 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
856 *colorFormat = OMX_COLOR_FormatYCbYCr;
857 }
858 }
859
860 // Check whether the target color format is supported.
861 return isColorFormatSupported(*colorFormat, kPortIndexInput);
862}
863
864status_t OMXCodec::isColorFormatSupported(
865 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) {
866 LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat));
867
868 // Enumerate all the color formats supported by
869 // the omx component to see whether the given
870 // color format is supported.
871 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
872 InitOMXParams(&portFormat);
873 portFormat.nPortIndex = portIndex;
874 OMX_U32 index = 0;
875 portFormat.nIndex = index;
876 while (true) {
877 if (OMX_ErrorNone != mOMX->getParameter(
878 mNode, OMX_IndexParamVideoPortFormat,
879 &portFormat, sizeof(portFormat))) {
James Dongb5024da2010-09-13 16:30:51 -0700880 break;
James Dongafd97e82010-08-03 17:19:23 -0700881 }
882 // Make sure that omx component does not overwrite
883 // the incremented index (bug 2897413).
884 CHECK_EQ(index, portFormat.nIndex);
885 if ((portFormat.eColorFormat == colorFormat)) {
886 LOGV("Found supported color format: %d", portFormat.eColorFormat);
887 return OK; // colorFormat is supported!
888 }
889 ++index;
890 portFormat.nIndex = index;
891
892 // OMX Spec defines less than 50 color formats
893 // 1000 is more than enough for us to tell whether the omx
894 // component in question is buggy or not.
895 if (index >= 1000) {
896 LOGE("More than %ld color formats are supported???", index);
897 break;
898 }
899 }
James Dongb5024da2010-09-13 16:30:51 -0700900
901 LOGE("color format %d is not supported", colorFormat);
James Dongafd97e82010-08-03 17:19:23 -0700902 return UNKNOWN_ERROR;
903}
904
Andreas Huberbe06d262009-08-14 14:37:10 -0700905void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700906 const char *mime, const sp<MetaData>& meta) {
907
908 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
909 bool success = meta->findInt32(kKeyWidth, &width);
910 success = success && meta->findInt32(kKeyHeight, &height);
911 success = success && meta->findInt32(kKeySampleRate, &frameRate);
912 success = success && meta->findInt32(kKeyBitRate, &bitRate);
913 success = success && meta->findInt32(kKeyStride, &stride);
914 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
915 CHECK(success);
916 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700917
918 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700919 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700920 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700921 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700922 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700923 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700924 compressionFormat = OMX_VIDEO_CodingH263;
925 } else {
926 LOGE("Not a supported video mime type: %s", mime);
927 CHECK(!"Should not be here. Not a supported video mime type.");
928 }
929
James Dongafd97e82010-08-03 17:19:23 -0700930 OMX_COLOR_FORMATTYPE colorFormat;
931 CHECK_EQ(OK, findTargetColorFormat(meta, &colorFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -0700932
James Dongb00e2462010-04-26 17:48:26 -0700933 status_t err;
934 OMX_PARAM_PORTDEFINITIONTYPE def;
935 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
936
937 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700938 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700939 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700940 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700941
James Dongb00e2462010-04-26 17:48:26 -0700942 InitOMXParams(&def);
943 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700944
James Dongb00e2462010-04-26 17:48:26 -0700945 err = mOMX->getParameter(
946 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
947 CHECK_EQ(err, OK);
948
James Dong1244eab2010-06-08 11:58:53 -0700949 def.nBufferSize = getFrameSize(colorFormat,
950 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700951
952 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
953
954 video_def->nFrameWidth = width;
955 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700956 video_def->nStride = stride;
957 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700958 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700959 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
960 video_def->eColorFormat = colorFormat;
961
James Dongb00e2462010-04-26 17:48:26 -0700962 err = mOMX->setParameter(
963 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
964 CHECK_EQ(err, OK);
965
966 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700967 CHECK_EQ(setVideoPortFormatType(
968 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
969 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700970 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700971 def.nPortIndex = kPortIndexOutput;
972
James Dongb00e2462010-04-26 17:48:26 -0700973 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700974 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
975
976 CHECK_EQ(err, OK);
977 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
978
979 video_def->nFrameWidth = width;
980 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700981 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700982 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700983 video_def->eCompressionFormat = compressionFormat;
984 video_def->eColorFormat = OMX_COLOR_FormatUnused;
James Dong90862e22010-08-26 19:12:59 -0700985 if (mQuirks & kRequiresLargerEncoderOutputBuffer) {
986 // Increases the output buffer size
987 def.nBufferSize = ((def.nBufferSize * 3) >> 1);
988 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700989
Andreas Huber784202e2009-10-15 13:46:54 -0700990 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700991 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
992 CHECK_EQ(err, OK);
993
James Dongb00e2462010-04-26 17:48:26 -0700994 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700995 switch (compressionFormat) {
996 case OMX_VIDEO_CodingMPEG4:
997 {
James Dong1244eab2010-06-08 11:58:53 -0700998 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700999 break;
1000 }
1001
1002 case OMX_VIDEO_CodingH263:
James Dongc0ab2a62010-06-29 16:29:19 -07001003 CHECK_EQ(setupH263EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001004 break;
1005
Andreas Huberea6a38c2009-11-16 15:43:38 -08001006 case OMX_VIDEO_CodingAVC:
1007 {
James Dong1244eab2010-06-08 11:58:53 -07001008 CHECK_EQ(setupAVCEncoderParameters(meta), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001009 break;
1010 }
1011
Andreas Huberb482ce82009-10-29 12:02:48 -07001012 default:
1013 CHECK(!"Support for this compressionFormat to be implemented.");
1014 break;
1015 }
1016}
1017
James Dong1244eab2010-06-08 11:58:53 -07001018static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
1019 if (iFramesInterval < 0) {
1020 return 0xFFFFFFFF;
1021 } else if (iFramesInterval == 0) {
1022 return 0;
1023 }
1024 OMX_U32 ret = frameRate * iFramesInterval;
1025 CHECK(ret > 1);
1026 return ret;
1027}
1028
James Dongc0ab2a62010-06-29 16:29:19 -07001029status_t OMXCodec::setupErrorCorrectionParameters() {
1030 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
1031 InitOMXParams(&errorCorrectionType);
1032 errorCorrectionType.nPortIndex = kPortIndexOutput;
1033
1034 status_t err = mOMX->getParameter(
1035 mNode, OMX_IndexParamVideoErrorCorrection,
1036 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001037 if (err != OK) {
1038 LOGW("Error correction param query is not supported");
1039 return OK; // Optional feature. Ignore this failure
1040 }
James Dongc0ab2a62010-06-29 16:29:19 -07001041
1042 errorCorrectionType.bEnableHEC = OMX_FALSE;
1043 errorCorrectionType.bEnableResync = OMX_TRUE;
1044 errorCorrectionType.nResynchMarkerSpacing = 256;
1045 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
1046 errorCorrectionType.bEnableRVLC = OMX_FALSE;
1047
1048 err = mOMX->setParameter(
1049 mNode, OMX_IndexParamVideoErrorCorrection,
1050 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001051 if (err != OK) {
1052 LOGW("Error correction param configuration is not supported");
1053 }
1054
1055 // Optional feature. Ignore the failure.
James Dongc0ab2a62010-06-29 16:29:19 -07001056 return OK;
1057}
1058
1059status_t OMXCodec::setupBitRate(int32_t bitRate) {
1060 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
1061 InitOMXParams(&bitrateType);
1062 bitrateType.nPortIndex = kPortIndexOutput;
1063
1064 status_t err = mOMX->getParameter(
1065 mNode, OMX_IndexParamVideoBitrate,
1066 &bitrateType, sizeof(bitrateType));
1067 CHECK_EQ(err, OK);
1068
1069 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
1070 bitrateType.nTargetBitrate = bitRate;
1071
1072 err = mOMX->setParameter(
1073 mNode, OMX_IndexParamVideoBitrate,
1074 &bitrateType, sizeof(bitrateType));
1075 CHECK_EQ(err, OK);
1076 return OK;
1077}
1078
James Dong81c929a2010-07-01 15:02:14 -07001079status_t OMXCodec::getVideoProfileLevel(
1080 const sp<MetaData>& meta,
1081 const CodecProfileLevel& defaultProfileLevel,
1082 CodecProfileLevel &profileLevel) {
1083 CODEC_LOGV("Default profile: %ld, level %ld",
1084 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
1085
1086 // Are the default profile and level overwriten?
1087 int32_t profile, level;
1088 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
1089 profile = defaultProfileLevel.mProfile;
1090 }
1091 if (!meta->findInt32(kKeyVideoLevel, &level)) {
1092 level = defaultProfileLevel.mLevel;
1093 }
1094 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
1095
1096 // Are the target profile and level supported by the encoder?
1097 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
1098 InitOMXParams(&param);
1099 param.nPortIndex = kPortIndexOutput;
1100 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
1101 status_t err = mOMX->getParameter(
1102 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
1103 &param, sizeof(param));
1104
James Dongdfb89912010-09-15 21:07:52 -07001105 if (err != OK) break;
James Dong81c929a2010-07-01 15:02:14 -07001106
1107 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
1108 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -07001109 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -07001110 supportedProfile, supportedLevel);
1111
1112 if (profile == supportedProfile &&
James Dongdfb89912010-09-15 21:07:52 -07001113 level <= supportedLevel) {
1114 // We can further check whether the level is a valid
1115 // value; but we will leave that to the omx encoder component
1116 // via OMX_SetParameter call.
James Dong81c929a2010-07-01 15:02:14 -07001117 profileLevel.mProfile = profile;
1118 profileLevel.mLevel = level;
1119 return OK;
1120 }
1121 }
1122
1123 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
1124 profile, level);
1125 return BAD_VALUE;
1126}
1127
James Dongc0ab2a62010-06-29 16:29:19 -07001128status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
1129 int32_t iFramesInterval, frameRate, bitRate;
1130 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1131 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1132 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1133 CHECK(success);
1134 OMX_VIDEO_PARAM_H263TYPE h263type;
1135 InitOMXParams(&h263type);
1136 h263type.nPortIndex = kPortIndexOutput;
1137
1138 status_t err = mOMX->getParameter(
1139 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1140 CHECK_EQ(err, OK);
1141
1142 h263type.nAllowedPictureTypes =
1143 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1144
1145 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1146 if (h263type.nPFrames == 0) {
1147 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1148 }
1149 h263type.nBFrames = 0;
1150
James Dong81c929a2010-07-01 15:02:14 -07001151 // Check profile and level parameters
1152 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001153 defaultProfileLevel.mProfile = h263type.eProfile;
1154 defaultProfileLevel.mLevel = h263type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001155 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1156 if (err != OK) return err;
1157 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1158 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001159
1160 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1161 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1162 h263type.nPictureHeaderRepetition = 0;
1163 h263type.nGOBHeaderInterval = 0;
1164
1165 err = mOMX->setParameter(
1166 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1167 CHECK_EQ(err, OK);
1168
1169 CHECK_EQ(setupBitRate(bitRate), OK);
1170 CHECK_EQ(setupErrorCorrectionParameters(), OK);
1171
1172 return OK;
1173}
1174
James Dong1244eab2010-06-08 11:58:53 -07001175status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1176 int32_t iFramesInterval, frameRate, bitRate;
1177 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1178 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1179 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1180 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001181 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1182 InitOMXParams(&mpeg4type);
1183 mpeg4type.nPortIndex = kPortIndexOutput;
1184
1185 status_t err = mOMX->getParameter(
1186 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1187 CHECK_EQ(err, OK);
1188
1189 mpeg4type.nSliceHeaderSpacing = 0;
1190 mpeg4type.bSVH = OMX_FALSE;
1191 mpeg4type.bGov = OMX_FALSE;
1192
1193 mpeg4type.nAllowedPictureTypes =
1194 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1195
James Dong1244eab2010-06-08 11:58:53 -07001196 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1197 if (mpeg4type.nPFrames == 0) {
1198 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1199 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001200 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001201 mpeg4type.nIDCVLCThreshold = 0;
1202 mpeg4type.bACPred = OMX_TRUE;
1203 mpeg4type.nMaxPacketSize = 256;
1204 mpeg4type.nTimeIncRes = 1000;
1205 mpeg4type.nHeaderExtension = 0;
1206 mpeg4type.bReversibleVLC = OMX_FALSE;
1207
James Dong81c929a2010-07-01 15:02:14 -07001208 // Check profile and level parameters
1209 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001210 defaultProfileLevel.mProfile = mpeg4type.eProfile;
1211 defaultProfileLevel.mLevel = mpeg4type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001212 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1213 if (err != OK) return err;
1214 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1215 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001216
1217 err = mOMX->setParameter(
1218 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1219 CHECK_EQ(err, OK);
1220
James Dongc0ab2a62010-06-29 16:29:19 -07001221 CHECK_EQ(setupBitRate(bitRate), OK);
1222 CHECK_EQ(setupErrorCorrectionParameters(), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001223
1224 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001225}
1226
James Dong1244eab2010-06-08 11:58:53 -07001227status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1228 int32_t iFramesInterval, frameRate, bitRate;
1229 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1230 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1231 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1232 CHECK(success);
1233
Andreas Huberea6a38c2009-11-16 15:43:38 -08001234 OMX_VIDEO_PARAM_AVCTYPE h264type;
1235 InitOMXParams(&h264type);
1236 h264type.nPortIndex = kPortIndexOutput;
1237
1238 status_t err = mOMX->getParameter(
1239 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1240 CHECK_EQ(err, OK);
1241
1242 h264type.nAllowedPictureTypes =
1243 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1244
1245 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001246 h264type.nBFrames = 0; // No B frames support yet
1247 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1248 if (h264type.nPFrames == 0) {
1249 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1250 }
James Dong81c929a2010-07-01 15:02:14 -07001251
1252 // Check profile and level parameters
1253 CodecProfileLevel defaultProfileLevel, profileLevel;
1254 defaultProfileLevel.mProfile = h264type.eProfile;
1255 defaultProfileLevel.mLevel = h264type.eLevel;
1256 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1257 if (err != OK) return err;
1258 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1259 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1260
1261 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1262 h264type.bUseHadamard = OMX_TRUE;
1263 h264type.nRefFrames = 1;
1264 h264type.nRefIdx10ActiveMinus1 = 0;
1265 h264type.nRefIdx11ActiveMinus1 = 0;
1266 h264type.bEntropyCodingCABAC = OMX_FALSE;
1267 h264type.bWeightedPPrediction = OMX_FALSE;
1268 h264type.bconstIpred = OMX_FALSE;
1269 h264type.bDirect8x8Inference = OMX_FALSE;
1270 h264type.bDirectSpatialTemporal = OMX_FALSE;
1271 h264type.nCabacInitIdc = 0;
1272 }
1273
1274 if (h264type.nBFrames != 0) {
1275 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1276 }
1277
Andreas Huberea6a38c2009-11-16 15:43:38 -08001278 h264type.bEnableUEP = OMX_FALSE;
1279 h264type.bEnableFMO = OMX_FALSE;
1280 h264type.bEnableASO = OMX_FALSE;
1281 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001282 h264type.bFrameMBsOnly = OMX_TRUE;
1283 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001284 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1285
1286 err = mOMX->setParameter(
1287 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1288 CHECK_EQ(err, OK);
1289
James Dongc0ab2a62010-06-29 16:29:19 -07001290 CHECK_EQ(setupBitRate(bitRate), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001291
1292 return OK;
1293}
1294
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001295status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001296 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001297 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001298
Andreas Huberbe06d262009-08-14 14:37:10 -07001299 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001300 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001301 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001302 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001303 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001304 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001305 compressionFormat = OMX_VIDEO_CodingH263;
1306 } else {
1307 LOGE("Not a supported video mime type: %s", mime);
1308 CHECK(!"Should not be here. Not a supported video mime type.");
1309 }
1310
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001311 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001312 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1313
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001314 if (err != OK) {
1315 return err;
1316 }
1317
Andreas Huberbe06d262009-08-14 14:37:10 -07001318#if 1
1319 {
1320 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001321 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001322 format.nPortIndex = kPortIndexOutput;
1323 format.nIndex = 0;
1324
Andreas Huber784202e2009-10-15 13:46:54 -07001325 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001326 mNode, OMX_IndexParamVideoPortFormat,
1327 &format, sizeof(format));
1328 CHECK_EQ(err, OK);
1329 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1330
1331 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1332
1333 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1334 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1335 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1336 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1337
Andreas Huber784202e2009-10-15 13:46:54 -07001338 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001339 mNode, OMX_IndexParamVideoPortFormat,
1340 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001341
1342 if (err != OK) {
1343 return err;
1344 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001345 }
1346#endif
1347
1348 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001349 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001350 def.nPortIndex = kPortIndexInput;
1351
Andreas Huber4c483422009-09-02 16:05:36 -07001352 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1353
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001354 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001355 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1356
1357 CHECK_EQ(err, OK);
1358
1359#if 1
1360 // XXX Need a (much) better heuristic to compute input buffer sizes.
1361 const size_t X = 64 * 1024;
1362 if (def.nBufferSize < X) {
1363 def.nBufferSize = X;
1364 }
1365#endif
1366
1367 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1368
1369 video_def->nFrameWidth = width;
1370 video_def->nFrameHeight = height;
1371
Andreas Huberb482ce82009-10-29 12:02:48 -07001372 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001373 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1374
Andreas Huber784202e2009-10-15 13:46:54 -07001375 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001376 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001377
1378 if (err != OK) {
1379 return err;
1380 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001381
1382 ////////////////////////////////////////////////////////////////////////////
1383
Andreas Huber4c483422009-09-02 16:05:36 -07001384 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001385 def.nPortIndex = kPortIndexOutput;
1386
Andreas Huber784202e2009-10-15 13:46:54 -07001387 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001388 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1389 CHECK_EQ(err, OK);
1390 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1391
1392#if 0
1393 def.nBufferSize =
1394 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1395#endif
1396
1397 video_def->nFrameWidth = width;
1398 video_def->nFrameHeight = height;
1399
Andreas Huber784202e2009-10-15 13:46:54 -07001400 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001401 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001402
1403 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001404}
1405
Andreas Huberbe06d262009-08-14 14:37:10 -07001406OMXCodec::OMXCodec(
1407 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001408 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001409 const char *mime,
1410 const char *componentName,
1411 const sp<MediaSource> &source)
1412 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001413 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001414 mNode(node),
1415 mQuirks(quirks),
1416 mIsEncoder(isEncoder),
1417 mMIME(strdup(mime)),
1418 mComponentName(strdup(componentName)),
1419 mSource(source),
1420 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001421 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001422 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001423 mSignalledEOS(false),
1424 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001425 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001426 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001427 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1428 mTargetTimeUs(-1),
James Dong53d4e0d2010-07-21 14:51:35 -07001429 mSkipTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001430 mLeftOverBuffer(NULL),
1431 mPaused(false) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001432 mPortStatus[kPortIndexInput] = ENABLED;
1433 mPortStatus[kPortIndexOutput] = ENABLED;
1434
Andreas Huber4c483422009-09-02 16:05:36 -07001435 setComponentRole();
1436}
1437
Andreas Hubere6c40962009-09-10 14:13:30 -07001438// static
1439void OMXCodec::setComponentRole(
1440 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1441 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001442 struct MimeToRole {
1443 const char *mime;
1444 const char *decoderRole;
1445 const char *encoderRole;
1446 };
1447
1448 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001449 { MEDIA_MIMETYPE_AUDIO_MPEG,
1450 "audio_decoder.mp3", "audio_encoder.mp3" },
1451 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1452 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1453 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1454 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1455 { MEDIA_MIMETYPE_AUDIO_AAC,
1456 "audio_decoder.aac", "audio_encoder.aac" },
1457 { MEDIA_MIMETYPE_VIDEO_AVC,
1458 "video_decoder.avc", "video_encoder.avc" },
1459 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1460 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1461 { MEDIA_MIMETYPE_VIDEO_H263,
1462 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001463 };
1464
1465 static const size_t kNumMimeToRole =
1466 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1467
1468 size_t i;
1469 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001470 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001471 break;
1472 }
1473 }
1474
1475 if (i == kNumMimeToRole) {
1476 return;
1477 }
1478
1479 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001480 isEncoder ? kMimeToRole[i].encoderRole
1481 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001482
1483 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001484 OMX_PARAM_COMPONENTROLETYPE roleParams;
1485 InitOMXParams(&roleParams);
1486
1487 strncpy((char *)roleParams.cRole,
1488 role, OMX_MAX_STRINGNAME_SIZE - 1);
1489
1490 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1491
Andreas Huber784202e2009-10-15 13:46:54 -07001492 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001493 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001494 &roleParams, sizeof(roleParams));
1495
1496 if (err != OK) {
1497 LOGW("Failed to set standard component role '%s'.", role);
1498 }
1499 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001500}
1501
Andreas Hubere6c40962009-09-10 14:13:30 -07001502void OMXCodec::setComponentRole() {
1503 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1504}
1505
Andreas Huberbe06d262009-08-14 14:37:10 -07001506OMXCodec::~OMXCodec() {
Andreas Huberf98197a2010-09-17 11:49:39 -07001507 mSource.clear();
1508
Andreas Huber4f5e6022009-08-19 09:29:34 -07001509 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001510
Andreas Huber784202e2009-10-15 13:46:54 -07001511 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001512 CHECK_EQ(err, OK);
1513
1514 mNode = NULL;
1515 setState(DEAD);
1516
1517 clearCodecSpecificData();
1518
1519 free(mComponentName);
1520 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001521
Andreas Huberbe06d262009-08-14 14:37:10 -07001522 free(mMIME);
1523 mMIME = NULL;
1524}
1525
1526status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001527 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001528
1529 CHECK_EQ(mState, LOADED);
1530
1531 status_t err;
1532 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001533 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001534 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001535 setState(LOADED_TO_IDLE);
1536 }
1537
1538 err = allocateBuffers();
1539 CHECK_EQ(err, OK);
1540
1541 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001542 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001543 CHECK_EQ(err, OK);
1544
1545 setState(LOADED_TO_IDLE);
1546 }
1547
1548 while (mState != EXECUTING && mState != ERROR) {
1549 mAsyncCompletion.wait(mLock);
1550 }
1551
1552 return mState == ERROR ? UNKNOWN_ERROR : OK;
1553}
1554
1555// static
1556bool OMXCodec::isIntermediateState(State state) {
1557 return state == LOADED_TO_IDLE
1558 || state == IDLE_TO_EXECUTING
1559 || state == EXECUTING_TO_IDLE
1560 || state == IDLE_TO_LOADED
1561 || state == RECONFIGURING;
1562}
1563
1564status_t OMXCodec::allocateBuffers() {
1565 status_t err = allocateBuffersOnPort(kPortIndexInput);
1566
1567 if (err != OK) {
1568 return err;
1569 }
1570
1571 return allocateBuffersOnPort(kPortIndexOutput);
1572}
1573
1574status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1575 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001576 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001577 def.nPortIndex = portIndex;
1578
Andreas Huber784202e2009-10-15 13:46:54 -07001579 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001580 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1581
1582 if (err != OK) {
1583 return err;
1584 }
1585
Andreas Huber57648e42010-08-04 10:14:30 -07001586 CODEC_LOGI("allocating %lu buffers of size %lu on %s port",
1587 def.nBufferCountActual, def.nBufferSize,
1588 portIndex == kPortIndexInput ? "input" : "output");
1589
Andreas Huber5c0a9132009-08-20 11:16:40 -07001590 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001591 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001592
Andreas Huberbe06d262009-08-14 14:37:10 -07001593 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001594 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001595 CHECK(mem.get() != NULL);
1596
Andreas Huberc712b9f2010-01-20 15:05:46 -08001597 BufferInfo info;
1598 info.mData = NULL;
1599 info.mSize = def.nBufferSize;
1600
Andreas Huberbe06d262009-08-14 14:37:10 -07001601 IOMX::buffer_id buffer;
1602 if (portIndex == kPortIndexInput
1603 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001604 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001605 mem.clear();
1606
Andreas Huberf1fe0642010-01-15 15:28:19 -08001607 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001608 mNode, portIndex, def.nBufferSize, &buffer,
1609 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001610 } else {
1611 err = mOMX->allocateBufferWithBackup(
1612 mNode, portIndex, mem, &buffer);
1613 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001614 } else if (portIndex == kPortIndexOutput
1615 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001616 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001617 mem.clear();
1618
Andreas Huberf1fe0642010-01-15 15:28:19 -08001619 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001620 mNode, portIndex, def.nBufferSize, &buffer,
1621 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001622 } else {
1623 err = mOMX->allocateBufferWithBackup(
1624 mNode, portIndex, mem, &buffer);
1625 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001626 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001627 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001628 }
1629
1630 if (err != OK) {
1631 LOGE("allocate_buffer_with_backup failed");
1632 return err;
1633 }
1634
Andreas Huberc712b9f2010-01-20 15:05:46 -08001635 if (mem != NULL) {
1636 info.mData = mem->pointer();
1637 }
1638
Andreas Huberbe06d262009-08-14 14:37:10 -07001639 info.mBuffer = buffer;
1640 info.mOwnedByComponent = false;
1641 info.mMem = mem;
1642 info.mMediaBuffer = NULL;
1643
1644 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001645 if (!(mOMXLivesLocally
1646 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1647 && (mQuirks & kDefersOutputBufferAllocation))) {
1648 // If the node does not fill in the buffer ptr at this time,
1649 // we will defer creating the MediaBuffer until receiving
1650 // the first FILL_BUFFER_DONE notification instead.
1651 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1652 info.mMediaBuffer->setObserver(this);
1653 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001654 }
1655
1656 mPortBuffers[portIndex].push(info);
1657
Andreas Huber4c483422009-09-02 16:05:36 -07001658 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001659 portIndex == kPortIndexInput ? "input" : "output");
1660 }
1661
Andreas Huber2ea14e22009-12-16 09:30:55 -08001662 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001663
1664 return OK;
1665}
1666
1667void OMXCodec::on_message(const omx_message &msg) {
1668 Mutex::Autolock autoLock(mLock);
1669
1670 switch (msg.type) {
1671 case omx_message::EVENT:
1672 {
1673 onEvent(
1674 msg.u.event_data.event, msg.u.event_data.data1,
1675 msg.u.event_data.data2);
1676
1677 break;
1678 }
1679
1680 case omx_message::EMPTY_BUFFER_DONE:
1681 {
1682 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1683
Andreas Huber4c483422009-09-02 16:05:36 -07001684 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001685
1686 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1687 size_t i = 0;
1688 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1689 ++i;
1690 }
1691
1692 CHECK(i < buffers->size());
1693 if (!(*buffers)[i].mOwnedByComponent) {
1694 LOGW("We already own input buffer %p, yet received "
1695 "an EMPTY_BUFFER_DONE.", buffer);
1696 }
1697
1698 buffers->editItemAt(i).mOwnedByComponent = false;
1699
1700 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001701 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001702
1703 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001704 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001705 CHECK_EQ(err, OK);
1706
1707 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001708 } else if (mState != ERROR
1709 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001710 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1711 drainInputBuffer(&buffers->editItemAt(i));
1712 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001713 break;
1714 }
1715
1716 case omx_message::FILL_BUFFER_DONE:
1717 {
1718 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1719 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1720
Andreas Huber2ea14e22009-12-16 09:30:55 -08001721 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001722 buffer,
1723 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001724 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001725 msg.u.extended_buffer_data.timestamp,
1726 msg.u.extended_buffer_data.timestamp / 1E6);
1727
1728 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1729 size_t i = 0;
1730 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1731 ++i;
1732 }
1733
1734 CHECK(i < buffers->size());
1735 BufferInfo *info = &buffers->editItemAt(i);
1736
1737 if (!info->mOwnedByComponent) {
1738 LOGW("We already own output buffer %p, yet received "
1739 "a FILL_BUFFER_DONE.", buffer);
1740 }
1741
1742 info->mOwnedByComponent = false;
1743
1744 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001745 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001746
1747 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001748 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001749 CHECK_EQ(err, OK);
1750
1751 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001752#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001753 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1754 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001755 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001756 mNoMoreOutputData = true;
1757 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001758#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001759 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1760 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001761
Andreas Huber52733b82010-01-25 10:41:35 -08001762 if (info->mMediaBuffer == NULL) {
1763 CHECK(mOMXLivesLocally);
1764 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1765 CHECK(mQuirks & kDefersOutputBufferAllocation);
1766
1767 // The qcom video decoders on Nexus don't actually allocate
1768 // output buffer memory on a call to OMX_AllocateBuffer
1769 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1770 // structure is only filled in later.
1771
1772 info->mMediaBuffer = new MediaBuffer(
1773 msg.u.extended_buffer_data.data_ptr,
1774 info->mSize);
1775 info->mMediaBuffer->setObserver(this);
1776 }
1777
Andreas Huberbe06d262009-08-14 14:37:10 -07001778 MediaBuffer *buffer = info->mMediaBuffer;
1779
Andreas Huberf88f8442010-08-10 11:18:36 -07001780 if (msg.u.extended_buffer_data.range_offset
1781 + msg.u.extended_buffer_data.range_length
1782 > buffer->size()) {
1783 CODEC_LOGE(
1784 "Codec lied about its buffer size requirements, "
1785 "sending a buffer larger than the originally "
1786 "advertised size in FILL_BUFFER_DONE!");
1787 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001788 buffer->set_range(
1789 msg.u.extended_buffer_data.range_offset,
1790 msg.u.extended_buffer_data.range_length);
1791
1792 buffer->meta_data()->clear();
1793
Andreas Huberfa8de752009-10-08 10:07:49 -07001794 buffer->meta_data()->setInt64(
1795 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001796
1797 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1798 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1799 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001800 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1801 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1802 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001803
Andreas Huber1e194162010-10-06 16:43:57 -07001804 if (mQuirks & kOutputBuffersAreUnreadable) {
1805 buffer->meta_data()->setInt32(kKeyIsUnreadable, true);
1806 }
1807
Andreas Huberbe06d262009-08-14 14:37:10 -07001808 buffer->meta_data()->setPointer(
1809 kKeyPlatformPrivate,
1810 msg.u.extended_buffer_data.platform_private);
1811
1812 buffer->meta_data()->setPointer(
1813 kKeyBufferID,
1814 msg.u.extended_buffer_data.buffer);
1815
Andreas Huber2ea14e22009-12-16 09:30:55 -08001816 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1817 CODEC_LOGV("No more output data.");
1818 mNoMoreOutputData = true;
1819 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07001820
1821 if (mTargetTimeUs >= 0) {
1822 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
1823
1824 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
1825 CODEC_LOGV(
1826 "skipping output buffer at timestamp %lld us",
1827 msg.u.extended_buffer_data.timestamp);
1828
1829 fillOutputBuffer(info);
1830 break;
1831 }
1832
1833 CODEC_LOGV(
1834 "returning output buffer at target timestamp "
1835 "%lld us",
1836 msg.u.extended_buffer_data.timestamp);
1837
1838 mTargetTimeUs = -1;
1839 }
1840
1841 mFilledBuffers.push_back(i);
1842 mBufferFilled.signal();
Andreas Huberbe06d262009-08-14 14:37:10 -07001843 }
1844
1845 break;
1846 }
1847
1848 default:
1849 {
1850 CHECK(!"should not be here.");
1851 break;
1852 }
1853 }
1854}
1855
1856void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1857 switch (event) {
1858 case OMX_EventCmdComplete:
1859 {
1860 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1861 break;
1862 }
1863
1864 case OMX_EventError:
1865 {
Andreas Huberaf0a1882010-09-21 15:08:52 -07001866 CODEC_LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001867
1868 setState(ERROR);
1869 break;
1870 }
1871
1872 case OMX_EventPortSettingsChanged:
1873 {
1874 onPortSettingsChanged(data1);
1875 break;
1876 }
1877
Andreas Huber2ea14e22009-12-16 09:30:55 -08001878#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001879 case OMX_EventBufferFlag:
1880 {
Andreas Huber4c483422009-09-02 16:05:36 -07001881 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001882
1883 if (data1 == kPortIndexOutput) {
1884 mNoMoreOutputData = true;
1885 }
1886 break;
1887 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001888#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001889
1890 default:
1891 {
Andreas Huber4c483422009-09-02 16:05:36 -07001892 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001893 break;
1894 }
1895 }
1896}
1897
Andreas Huberb1678602009-10-19 13:06:40 -07001898// Has the format changed in any way that the client would have to be aware of?
1899static bool formatHasNotablyChanged(
1900 const sp<MetaData> &from, const sp<MetaData> &to) {
1901 if (from.get() == NULL && to.get() == NULL) {
1902 return false;
1903 }
1904
Andreas Huberf68c1682009-10-21 14:01:30 -07001905 if ((from.get() == NULL && to.get() != NULL)
1906 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001907 return true;
1908 }
1909
1910 const char *mime_from, *mime_to;
1911 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1912 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1913
1914 if (strcasecmp(mime_from, mime_to)) {
1915 return true;
1916 }
1917
1918 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1919 int32_t colorFormat_from, colorFormat_to;
1920 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1921 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1922
1923 if (colorFormat_from != colorFormat_to) {
1924 return true;
1925 }
1926
1927 int32_t width_from, width_to;
1928 CHECK(from->findInt32(kKeyWidth, &width_from));
1929 CHECK(to->findInt32(kKeyWidth, &width_to));
1930
1931 if (width_from != width_to) {
1932 return true;
1933 }
1934
1935 int32_t height_from, height_to;
1936 CHECK(from->findInt32(kKeyHeight, &height_from));
1937 CHECK(to->findInt32(kKeyHeight, &height_to));
1938
1939 if (height_from != height_to) {
1940 return true;
1941 }
1942 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1943 int32_t numChannels_from, numChannels_to;
1944 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1945 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1946
1947 if (numChannels_from != numChannels_to) {
1948 return true;
1949 }
1950
1951 int32_t sampleRate_from, sampleRate_to;
1952 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1953 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1954
1955 if (sampleRate_from != sampleRate_to) {
1956 return true;
1957 }
1958 }
1959
1960 return false;
1961}
1962
Andreas Huberbe06d262009-08-14 14:37:10 -07001963void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1964 switch (cmd) {
1965 case OMX_CommandStateSet:
1966 {
1967 onStateChange((OMX_STATETYPE)data);
1968 break;
1969 }
1970
1971 case OMX_CommandPortDisable:
1972 {
1973 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001974 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001975
1976 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1977 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1978 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1979
1980 mPortStatus[portIndex] = DISABLED;
1981
1982 if (mState == RECONFIGURING) {
1983 CHECK_EQ(portIndex, kPortIndexOutput);
1984
Andreas Huberb1678602009-10-19 13:06:40 -07001985 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001986 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001987
1988 // Don't notify clients if the output port settings change
1989 // wasn't of importance to them, i.e. it may be that just the
1990 // number of buffers has changed and nothing else.
1991 mOutputPortSettingsHaveChanged =
1992 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001993
Andreas Huberbe06d262009-08-14 14:37:10 -07001994 enablePortAsync(portIndex);
1995
1996 status_t err = allocateBuffersOnPort(portIndex);
1997 CHECK_EQ(err, OK);
1998 }
1999 break;
2000 }
2001
2002 case OMX_CommandPortEnable:
2003 {
2004 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07002005 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002006
2007 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2008 CHECK_EQ(mPortStatus[portIndex], ENABLING);
2009
2010 mPortStatus[portIndex] = ENABLED;
2011
2012 if (mState == RECONFIGURING) {
2013 CHECK_EQ(portIndex, kPortIndexOutput);
2014
2015 setState(EXECUTING);
2016
2017 fillOutputBuffers();
2018 }
2019 break;
2020 }
2021
2022 case OMX_CommandFlush:
2023 {
2024 OMX_U32 portIndex = data;
2025
Andreas Huber4c483422009-09-02 16:05:36 -07002026 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002027
2028 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
2029 mPortStatus[portIndex] = ENABLED;
2030
2031 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
2032 mPortBuffers[portIndex].size());
2033
2034 if (mState == RECONFIGURING) {
2035 CHECK_EQ(portIndex, kPortIndexOutput);
2036
2037 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002038 } else if (mState == EXECUTING_TO_IDLE) {
2039 if (mPortStatus[kPortIndexInput] == ENABLED
2040 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002041 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002042 "transition from EXECUTING to IDLE.");
2043
2044 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2045 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2046
2047 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002048 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002049 CHECK_EQ(err, OK);
2050 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002051 } else {
2052 // We're flushing both ports in preparation for seeking.
2053
2054 if (mPortStatus[kPortIndexInput] == ENABLED
2055 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002056 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07002057 " seek-time.");
2058
Andreas Huber1f24b302010-06-10 11:12:39 -07002059 // We implicitly resume pulling on our upstream source.
2060 mPaused = false;
2061
Andreas Huberbe06d262009-08-14 14:37:10 -07002062 drainInputBuffers();
2063 fillOutputBuffers();
2064 }
2065 }
2066
2067 break;
2068 }
2069
2070 default:
2071 {
Andreas Huber4c483422009-09-02 16:05:36 -07002072 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07002073 break;
2074 }
2075 }
2076}
2077
2078void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002079 CODEC_LOGV("onStateChange %d", newState);
2080
Andreas Huberbe06d262009-08-14 14:37:10 -07002081 switch (newState) {
2082 case OMX_StateIdle:
2083 {
Andreas Huber4c483422009-09-02 16:05:36 -07002084 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002085 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07002086 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002087 mNode, OMX_CommandStateSet, OMX_StateExecuting);
2088
2089 CHECK_EQ(err, OK);
2090
2091 setState(IDLE_TO_EXECUTING);
2092 } else {
2093 CHECK_EQ(mState, EXECUTING_TO_IDLE);
2094
2095 CHECK_EQ(
2096 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
2097 mPortBuffers[kPortIndexInput].size());
2098
2099 CHECK_EQ(
2100 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
2101 mPortBuffers[kPortIndexOutput].size());
2102
Andreas Huber784202e2009-10-15 13:46:54 -07002103 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002104 mNode, OMX_CommandStateSet, OMX_StateLoaded);
2105
2106 CHECK_EQ(err, OK);
2107
2108 err = freeBuffersOnPort(kPortIndexInput);
2109 CHECK_EQ(err, OK);
2110
2111 err = freeBuffersOnPort(kPortIndexOutput);
2112 CHECK_EQ(err, OK);
2113
2114 mPortStatus[kPortIndexInput] = ENABLED;
2115 mPortStatus[kPortIndexOutput] = ENABLED;
2116
2117 setState(IDLE_TO_LOADED);
2118 }
2119 break;
2120 }
2121
2122 case OMX_StateExecuting:
2123 {
2124 CHECK_EQ(mState, IDLE_TO_EXECUTING);
2125
Andreas Huber4c483422009-09-02 16:05:36 -07002126 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002127
2128 setState(EXECUTING);
2129
Andreas Huber42978e52009-08-27 10:08:39 -07002130 // Buffers will be submitted to the component in the first
2131 // call to OMXCodec::read as mInitialBufferSubmit is true at
2132 // this point. This ensures that this on_message call returns,
2133 // releases the lock and ::init can notice the state change and
2134 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07002135 break;
2136 }
2137
2138 case OMX_StateLoaded:
2139 {
2140 CHECK_EQ(mState, IDLE_TO_LOADED);
2141
Andreas Huber4c483422009-09-02 16:05:36 -07002142 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002143
2144 setState(LOADED);
2145 break;
2146 }
2147
Andreas Huberc712b9f2010-01-20 15:05:46 -08002148 case OMX_StateInvalid:
2149 {
2150 setState(ERROR);
2151 break;
2152 }
2153
Andreas Huberbe06d262009-08-14 14:37:10 -07002154 default:
2155 {
2156 CHECK(!"should not be here.");
2157 break;
2158 }
2159 }
2160}
2161
2162// static
2163size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
2164 size_t n = 0;
2165 for (size_t i = 0; i < buffers.size(); ++i) {
2166 if (!buffers[i].mOwnedByComponent) {
2167 ++n;
2168 }
2169 }
2170
2171 return n;
2172}
2173
2174status_t OMXCodec::freeBuffersOnPort(
2175 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2176 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2177
2178 status_t stickyErr = OK;
2179
2180 for (size_t i = buffers->size(); i-- > 0;) {
2181 BufferInfo *info = &buffers->editItemAt(i);
2182
2183 if (onlyThoseWeOwn && info->mOwnedByComponent) {
2184 continue;
2185 }
2186
2187 CHECK_EQ(info->mOwnedByComponent, false);
2188
Andreas Huber92022852009-09-14 15:24:14 -07002189 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2190
Andreas Huberbe06d262009-08-14 14:37:10 -07002191 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002192 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07002193
2194 if (err != OK) {
2195 stickyErr = err;
2196 }
2197
2198 if (info->mMediaBuffer != NULL) {
2199 info->mMediaBuffer->setObserver(NULL);
2200
2201 // Make sure nobody but us owns this buffer at this point.
2202 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2203
2204 info->mMediaBuffer->release();
2205 }
2206
2207 buffers->removeAt(i);
2208 }
2209
2210 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2211
2212 return stickyErr;
2213}
2214
2215void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002216 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002217
2218 CHECK_EQ(mState, EXECUTING);
2219 CHECK_EQ(portIndex, kPortIndexOutput);
2220 setState(RECONFIGURING);
2221
2222 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002223 if (!flushPortAsync(portIndex)) {
2224 onCmdComplete(OMX_CommandFlush, portIndex);
2225 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002226 } else {
2227 disablePortAsync(portIndex);
2228 }
2229}
2230
Andreas Huber404cc412009-08-25 14:26:05 -07002231bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002232 CHECK(mState == EXECUTING || mState == RECONFIGURING
2233 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002234
Andreas Huber4c483422009-09-02 16:05:36 -07002235 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002236 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2237 mPortBuffers[portIndex].size());
2238
Andreas Huberbe06d262009-08-14 14:37:10 -07002239 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2240 mPortStatus[portIndex] = SHUTTING_DOWN;
2241
Andreas Huber404cc412009-08-25 14:26:05 -07002242 if ((mQuirks & kRequiresFlushCompleteEmulation)
2243 && countBuffersWeOwn(mPortBuffers[portIndex])
2244 == mPortBuffers[portIndex].size()) {
2245 // No flush is necessary and this component fails to send a
2246 // flush-complete event in this case.
2247
2248 return false;
2249 }
2250
Andreas Huberbe06d262009-08-14 14:37:10 -07002251 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002252 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002253 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002254
2255 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002256}
2257
2258void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2259 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2260
2261 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2262 mPortStatus[portIndex] = DISABLING;
2263
2264 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002265 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002266 CHECK_EQ(err, OK);
2267
2268 freeBuffersOnPort(portIndex, true);
2269}
2270
2271void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2272 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2273
2274 CHECK_EQ(mPortStatus[portIndex], DISABLED);
2275 mPortStatus[portIndex] = ENABLING;
2276
2277 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002278 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002279 CHECK_EQ(err, OK);
2280}
2281
2282void OMXCodec::fillOutputBuffers() {
2283 CHECK_EQ(mState, EXECUTING);
2284
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002285 // This is a workaround for some decoders not properly reporting
2286 // end-of-output-stream. If we own all input buffers and also own
2287 // all output buffers and we already signalled end-of-input-stream,
2288 // the end-of-output-stream is implied.
2289 if (mSignalledEOS
2290 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2291 == mPortBuffers[kPortIndexInput].size()
2292 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2293 == mPortBuffers[kPortIndexOutput].size()) {
2294 mNoMoreOutputData = true;
2295 mBufferFilled.signal();
2296
2297 return;
2298 }
2299
Andreas Huberbe06d262009-08-14 14:37:10 -07002300 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2301 for (size_t i = 0; i < buffers->size(); ++i) {
2302 fillOutputBuffer(&buffers->editItemAt(i));
2303 }
2304}
2305
2306void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002307 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002308
2309 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2310 for (size_t i = 0; i < buffers->size(); ++i) {
2311 drainInputBuffer(&buffers->editItemAt(i));
2312 }
2313}
2314
2315void OMXCodec::drainInputBuffer(BufferInfo *info) {
2316 CHECK_EQ(info->mOwnedByComponent, false);
2317
2318 if (mSignalledEOS) {
2319 return;
2320 }
2321
2322 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2323 const CodecSpecificData *specific =
2324 mCodecSpecificData[mCodecSpecificDataIndex];
2325
2326 size_t size = specific->mSize;
2327
Andreas Hubere6c40962009-09-10 14:13:30 -07002328 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002329 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002330 static const uint8_t kNALStartCode[4] =
2331 { 0x00, 0x00, 0x00, 0x01 };
2332
Andreas Huberc712b9f2010-01-20 15:05:46 -08002333 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002334
2335 size += 4;
2336
Andreas Huberc712b9f2010-01-20 15:05:46 -08002337 memcpy(info->mData, kNALStartCode, 4);
2338 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002339 specific->mData, specific->mSize);
2340 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002341 CHECK(info->mSize >= specific->mSize);
2342 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002343 }
2344
Andreas Huber2ea14e22009-12-16 09:30:55 -08002345 mNoMoreOutputData = false;
2346
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002347 CODEC_LOGV("calling emptyBuffer with codec specific data");
2348
Andreas Huber784202e2009-10-15 13:46:54 -07002349 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002350 mNode, info->mBuffer, 0, size,
2351 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2352 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002353 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002354
2355 info->mOwnedByComponent = true;
2356
2357 ++mCodecSpecificDataIndex;
2358 return;
2359 }
2360
Andreas Huber1f24b302010-06-10 11:12:39 -07002361 if (mPaused) {
2362 return;
2363 }
2364
Andreas Huberbe06d262009-08-14 14:37:10 -07002365 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002366
Andreas Hubera4357ad2010-04-02 12:49:54 -07002367 bool signalEOS = false;
2368 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002369
Andreas Hubera4357ad2010-04-02 12:49:54 -07002370 size_t offset = 0;
2371 int32_t n = 0;
2372 for (;;) {
2373 MediaBuffer *srcBuffer;
James Dong53d4e0d2010-07-21 14:51:35 -07002374 MediaSource::ReadOptions options;
2375 if (mSkipTimeUs >= 0) {
2376 options.setSkipFrame(mSkipTimeUs);
2377 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002378 if (mSeekTimeUs >= 0) {
2379 if (mLeftOverBuffer) {
2380 mLeftOverBuffer->release();
2381 mLeftOverBuffer = NULL;
2382 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002383 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002384
2385 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002386 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002387 mBufferFilled.signal();
2388
2389 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002390
2391 if (err == OK) {
2392 int64_t targetTimeUs;
2393 if (srcBuffer->meta_data()->findInt64(
2394 kKeyTargetTime, &targetTimeUs)
2395 && targetTimeUs >= 0) {
2396 mTargetTimeUs = targetTimeUs;
2397 } else {
2398 mTargetTimeUs = -1;
2399 }
2400 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002401 } else if (mLeftOverBuffer) {
2402 srcBuffer = mLeftOverBuffer;
2403 mLeftOverBuffer = NULL;
2404
2405 err = OK;
2406 } else {
James Dong53d4e0d2010-07-21 14:51:35 -07002407 err = mSource->read(&srcBuffer, &options);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002408 }
2409
2410 if (err != OK) {
2411 signalEOS = true;
2412 mFinalStatus = err;
2413 mSignalledEOS = true;
2414 break;
2415 }
2416
2417 size_t remainingBytes = info->mSize - offset;
2418
2419 if (srcBuffer->range_length() > remainingBytes) {
2420 if (offset == 0) {
2421 CODEC_LOGE(
2422 "Codec's input buffers are too small to accomodate "
2423 "buffer read from source (info->mSize = %d, srcLength = %d)",
2424 info->mSize, srcBuffer->range_length());
2425
2426 srcBuffer->release();
2427 srcBuffer = NULL;
2428
2429 setState(ERROR);
2430 return;
2431 }
2432
2433 mLeftOverBuffer = srcBuffer;
2434 break;
2435 }
2436
James Dong4f501f02010-06-07 14:41:41 -07002437 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2438 CHECK(mOMXLivesLocally && offset == 0);
2439 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2440 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2441 } else {
2442 memcpy((uint8_t *)info->mData + offset,
2443 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2444 srcBuffer->range_length());
2445 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002446
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002447 int64_t lastBufferTimeUs;
2448 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002449 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002450
Andreas Hubera4357ad2010-04-02 12:49:54 -07002451 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002452 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002453 }
2454
2455 offset += srcBuffer->range_length();
2456
2457 srcBuffer->release();
2458 srcBuffer = NULL;
2459
2460 ++n;
2461
2462 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2463 break;
2464 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002465
2466 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2467
2468 if (coalescedDurationUs > 250000ll) {
2469 // Don't coalesce more than 250ms worth of encoded data at once.
2470 break;
2471 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002472 }
2473
2474 if (n > 1) {
2475 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002476 }
2477
2478 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002479
Andreas Hubera4357ad2010-04-02 12:49:54 -07002480 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002481 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002482 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002483 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002484 }
2485
Andreas Hubera4357ad2010-04-02 12:49:54 -07002486 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2487 "timestamp %lld us (%.2f secs)",
2488 info->mBuffer, offset,
2489 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002490
Andreas Huber784202e2009-10-15 13:46:54 -07002491 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002492 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002493 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002494
2495 if (err != OK) {
2496 setState(ERROR);
2497 return;
2498 }
2499
2500 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002501
2502 // This component does not ever signal the EOS flag on output buffers,
2503 // Thanks for nothing.
2504 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2505 mNoMoreOutputData = true;
2506 mBufferFilled.signal();
2507 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002508}
2509
2510void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2511 CHECK_EQ(info->mOwnedByComponent, false);
2512
Andreas Huber404cc412009-08-25 14:26:05 -07002513 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002514 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002515 "calling fillOutputBuffer");
2516 return;
2517 }
2518
Andreas Huber4c483422009-09-02 16:05:36 -07002519 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002520 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002521
2522 if (err != OK) {
2523 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2524
2525 setState(ERROR);
2526 return;
2527 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002528
2529 info->mOwnedByComponent = true;
2530}
2531
2532void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2533 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2534 for (size_t i = 0; i < buffers->size(); ++i) {
2535 if ((*buffers)[i].mBuffer == buffer) {
2536 drainInputBuffer(&buffers->editItemAt(i));
2537 return;
2538 }
2539 }
2540
2541 CHECK(!"should not be here.");
2542}
2543
2544void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2545 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2546 for (size_t i = 0; i < buffers->size(); ++i) {
2547 if ((*buffers)[i].mBuffer == buffer) {
2548 fillOutputBuffer(&buffers->editItemAt(i));
2549 return;
2550 }
2551 }
2552
2553 CHECK(!"should not be here.");
2554}
2555
2556void OMXCodec::setState(State newState) {
2557 mState = newState;
2558 mAsyncCompletion.signal();
2559
2560 // This may cause some spurious wakeups but is necessary to
2561 // unblock the reader if we enter ERROR state.
2562 mBufferFilled.signal();
2563}
2564
Andreas Huberda050cf22009-09-02 14:01:43 -07002565void OMXCodec::setRawAudioFormat(
2566 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002567
2568 // port definition
2569 OMX_PARAM_PORTDEFINITIONTYPE def;
2570 InitOMXParams(&def);
2571 def.nPortIndex = portIndex;
2572 status_t err = mOMX->getParameter(
2573 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2574 CHECK_EQ(err, OK);
2575 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2576 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2577 &def, sizeof(def)), OK);
2578
2579 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002580 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002581 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002582 pcmParams.nPortIndex = portIndex;
2583
James Dongabed93a2010-04-22 17:27:04 -07002584 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002585 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2586
2587 CHECK_EQ(err, OK);
2588
2589 pcmParams.nChannels = numChannels;
2590 pcmParams.eNumData = OMX_NumericalDataSigned;
2591 pcmParams.bInterleaved = OMX_TRUE;
2592 pcmParams.nBitPerSample = 16;
2593 pcmParams.nSamplingRate = sampleRate;
2594 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2595
2596 if (numChannels == 1) {
2597 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2598 } else {
2599 CHECK_EQ(numChannels, 2);
2600
2601 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2602 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2603 }
2604
Andreas Huber784202e2009-10-15 13:46:54 -07002605 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002606 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2607
2608 CHECK_EQ(err, OK);
2609}
2610
James Dong17299ab2010-05-14 15:45:22 -07002611static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2612 if (isAMRWB) {
2613 if (bps <= 6600) {
2614 return OMX_AUDIO_AMRBandModeWB0;
2615 } else if (bps <= 8850) {
2616 return OMX_AUDIO_AMRBandModeWB1;
2617 } else if (bps <= 12650) {
2618 return OMX_AUDIO_AMRBandModeWB2;
2619 } else if (bps <= 14250) {
2620 return OMX_AUDIO_AMRBandModeWB3;
2621 } else if (bps <= 15850) {
2622 return OMX_AUDIO_AMRBandModeWB4;
2623 } else if (bps <= 18250) {
2624 return OMX_AUDIO_AMRBandModeWB5;
2625 } else if (bps <= 19850) {
2626 return OMX_AUDIO_AMRBandModeWB6;
2627 } else if (bps <= 23050) {
2628 return OMX_AUDIO_AMRBandModeWB7;
2629 }
2630
2631 // 23850 bps
2632 return OMX_AUDIO_AMRBandModeWB8;
2633 } else { // AMRNB
2634 if (bps <= 4750) {
2635 return OMX_AUDIO_AMRBandModeNB0;
2636 } else if (bps <= 5150) {
2637 return OMX_AUDIO_AMRBandModeNB1;
2638 } else if (bps <= 5900) {
2639 return OMX_AUDIO_AMRBandModeNB2;
2640 } else if (bps <= 6700) {
2641 return OMX_AUDIO_AMRBandModeNB3;
2642 } else if (bps <= 7400) {
2643 return OMX_AUDIO_AMRBandModeNB4;
2644 } else if (bps <= 7950) {
2645 return OMX_AUDIO_AMRBandModeNB5;
2646 } else if (bps <= 10200) {
2647 return OMX_AUDIO_AMRBandModeNB6;
2648 }
2649
2650 // 12200 bps
2651 return OMX_AUDIO_AMRBandModeNB7;
2652 }
2653}
2654
2655void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002656 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002657
Andreas Huber8768f2c2009-12-01 15:26:54 -08002658 OMX_AUDIO_PARAM_AMRTYPE def;
2659 InitOMXParams(&def);
2660 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002661
Andreas Huber8768f2c2009-12-01 15:26:54 -08002662 status_t err =
2663 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002664
Andreas Huber8768f2c2009-12-01 15:26:54 -08002665 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002666
Andreas Huber8768f2c2009-12-01 15:26:54 -08002667 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002668
James Dong17299ab2010-05-14 15:45:22 -07002669 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002670 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2671 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002672
2673 ////////////////////////
2674
2675 if (mIsEncoder) {
2676 sp<MetaData> format = mSource->getFormat();
2677 int32_t sampleRate;
2678 int32_t numChannels;
2679 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2680 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2681
2682 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2683 }
2684}
2685
James Dong17299ab2010-05-14 15:45:22 -07002686void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002687 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002688 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002689 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002690 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002691
2692 //////////////// output port ////////////////////
2693 // format
2694 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2695 format.nPortIndex = kPortIndexOutput;
2696 format.nIndex = 0;
2697 status_t err = OMX_ErrorNone;
2698 while (OMX_ErrorNone == err) {
2699 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2700 &format, sizeof(format)), OK);
2701 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2702 break;
2703 }
2704 format.nIndex++;
2705 }
2706 CHECK_EQ(OK, err);
2707 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2708 &format, sizeof(format)), OK);
2709
2710 // port definition
2711 OMX_PARAM_PORTDEFINITIONTYPE def;
2712 InitOMXParams(&def);
2713 def.nPortIndex = kPortIndexOutput;
2714 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2715 &def, sizeof(def)), OK);
2716 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2717 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2718 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2719 &def, sizeof(def)), OK);
2720
2721 // profile
2722 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2723 InitOMXParams(&profile);
2724 profile.nPortIndex = kPortIndexOutput;
2725 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2726 &profile, sizeof(profile)), OK);
2727 profile.nChannels = numChannels;
2728 profile.eChannelMode = (numChannels == 1?
2729 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2730 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002731 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002732 profile.nAudioBandWidth = 0;
2733 profile.nFrameLength = 0;
2734 profile.nAACtools = OMX_AUDIO_AACToolAll;
2735 profile.nAACERtools = OMX_AUDIO_AACERNone;
2736 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2737 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2738 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2739 &profile, sizeof(profile)), OK);
2740
Andreas Huberda050cf22009-09-02 14:01:43 -07002741 } else {
2742 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002743 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002744 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002745
Andreas Huber784202e2009-10-15 13:46:54 -07002746 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002747 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2748 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002749
Andreas Huberda050cf22009-09-02 14:01:43 -07002750 profile.nChannels = numChannels;
2751 profile.nSampleRate = sampleRate;
2752 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002753
Andreas Huber784202e2009-10-15 13:46:54 -07002754 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002755 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2756 CHECK_EQ(err, OK);
2757 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002758}
2759
2760void OMXCodec::setImageOutputFormat(
2761 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002762 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002763
2764#if 0
2765 OMX_INDEXTYPE index;
2766 status_t err = mOMX->get_extension_index(
2767 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2768 CHECK_EQ(err, OK);
2769
2770 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2771 CHECK_EQ(err, OK);
2772#endif
2773
2774 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002775 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002776 def.nPortIndex = kPortIndexOutput;
2777
Andreas Huber784202e2009-10-15 13:46:54 -07002778 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002779 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2780 CHECK_EQ(err, OK);
2781
2782 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2783
2784 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002785
Andreas Huberbe06d262009-08-14 14:37:10 -07002786 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2787 imageDef->eColorFormat = format;
2788 imageDef->nFrameWidth = width;
2789 imageDef->nFrameHeight = height;
2790
2791 switch (format) {
2792 case OMX_COLOR_FormatYUV420PackedPlanar:
2793 case OMX_COLOR_FormatYUV411Planar:
2794 {
2795 def.nBufferSize = (width * height * 3) / 2;
2796 break;
2797 }
2798
2799 case OMX_COLOR_FormatCbYCrY:
2800 {
2801 def.nBufferSize = width * height * 2;
2802 break;
2803 }
2804
2805 case OMX_COLOR_Format32bitARGB8888:
2806 {
2807 def.nBufferSize = width * height * 4;
2808 break;
2809 }
2810
Andreas Huber201511c2009-09-08 14:01:44 -07002811 case OMX_COLOR_Format16bitARGB4444:
2812 case OMX_COLOR_Format16bitARGB1555:
2813 case OMX_COLOR_Format16bitRGB565:
2814 case OMX_COLOR_Format16bitBGR565:
2815 {
2816 def.nBufferSize = width * height * 2;
2817 break;
2818 }
2819
Andreas Huberbe06d262009-08-14 14:37:10 -07002820 default:
2821 CHECK(!"Should not be here. Unknown color format.");
2822 break;
2823 }
2824
Andreas Huber5c0a9132009-08-20 11:16:40 -07002825 def.nBufferCountActual = def.nBufferCountMin;
2826
Andreas Huber784202e2009-10-15 13:46:54 -07002827 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002828 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2829 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002830}
Andreas Huberbe06d262009-08-14 14:37:10 -07002831
Andreas Huber5c0a9132009-08-20 11:16:40 -07002832void OMXCodec::setJPEGInputFormat(
2833 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2834 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002835 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002836 def.nPortIndex = kPortIndexInput;
2837
Andreas Huber784202e2009-10-15 13:46:54 -07002838 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002839 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2840 CHECK_EQ(err, OK);
2841
Andreas Huber5c0a9132009-08-20 11:16:40 -07002842 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2843 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2844
Andreas Huberbe06d262009-08-14 14:37:10 -07002845 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2846 imageDef->nFrameWidth = width;
2847 imageDef->nFrameHeight = height;
2848
Andreas Huber5c0a9132009-08-20 11:16:40 -07002849 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002850 def.nBufferCountActual = def.nBufferCountMin;
2851
Andreas Huber784202e2009-10-15 13:46:54 -07002852 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002853 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2854 CHECK_EQ(err, OK);
2855}
2856
2857void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2858 CodecSpecificData *specific =
2859 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2860
2861 specific->mSize = size;
2862 memcpy(specific->mData, data, size);
2863
2864 mCodecSpecificData.push(specific);
2865}
2866
2867void OMXCodec::clearCodecSpecificData() {
2868 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2869 free(mCodecSpecificData.editItemAt(i));
2870 }
2871 mCodecSpecificData.clear();
2872 mCodecSpecificDataIndex = 0;
2873}
2874
James Dong36e573b2010-06-19 09:04:18 -07002875status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07002876 Mutex::Autolock autoLock(mLock);
2877
Andreas Huberbe06d262009-08-14 14:37:10 -07002878 if (mState != LOADED) {
2879 return UNKNOWN_ERROR;
2880 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002881
Andreas Huberbe06d262009-08-14 14:37:10 -07002882 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002883 if (mQuirks & kWantsNALFragments) {
2884 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002885 }
James Dong36e573b2010-06-19 09:04:18 -07002886 if (meta) {
2887 int64_t startTimeUs = 0;
2888 int64_t timeUs;
2889 if (meta->findInt64(kKeyTime, &timeUs)) {
2890 startTimeUs = timeUs;
2891 }
2892 params->setInt64(kKeyTime, startTimeUs);
2893 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002894 status_t err = mSource->start(params.get());
2895
2896 if (err != OK) {
2897 return err;
2898 }
2899
2900 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002901 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002902 mSignalledEOS = false;
2903 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002904 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002905 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002906 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
2907 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07002908 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07002909 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002910
2911 return init();
2912}
2913
2914status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002915 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002916
2917 Mutex::Autolock autoLock(mLock);
2918
2919 while (isIntermediateState(mState)) {
2920 mAsyncCompletion.wait(mLock);
2921 }
2922
2923 switch (mState) {
2924 case LOADED:
2925 case ERROR:
2926 break;
2927
2928 case EXECUTING:
2929 {
2930 setState(EXECUTING_TO_IDLE);
2931
Andreas Huber127fcdc2009-08-26 16:27:02 -07002932 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002933 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002934 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002935
Andreas Huber127fcdc2009-08-26 16:27:02 -07002936 bool emulateInputFlushCompletion =
2937 !flushPortAsync(kPortIndexInput);
2938
2939 bool emulateOutputFlushCompletion =
2940 !flushPortAsync(kPortIndexOutput);
2941
2942 if (emulateInputFlushCompletion) {
2943 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2944 }
2945
2946 if (emulateOutputFlushCompletion) {
2947 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2948 }
2949 } else {
2950 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2951 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2952
2953 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002954 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002955 CHECK_EQ(err, OK);
2956 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002957
2958 while (mState != LOADED && mState != ERROR) {
2959 mAsyncCompletion.wait(mLock);
2960 }
2961
2962 break;
2963 }
2964
2965 default:
2966 {
2967 CHECK(!"should not be here.");
2968 break;
2969 }
2970 }
2971
Andreas Hubera4357ad2010-04-02 12:49:54 -07002972 if (mLeftOverBuffer) {
2973 mLeftOverBuffer->release();
2974 mLeftOverBuffer = NULL;
2975 }
2976
Andreas Huberbe06d262009-08-14 14:37:10 -07002977 mSource->stop();
2978
Andreas Huber4a9375e2010-02-09 11:54:33 -08002979 CODEC_LOGV("stopped");
2980
Andreas Huberbe06d262009-08-14 14:37:10 -07002981 return OK;
2982}
2983
2984sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002985 Mutex::Autolock autoLock(mLock);
2986
Andreas Huberbe06d262009-08-14 14:37:10 -07002987 return mOutputFormat;
2988}
2989
2990status_t OMXCodec::read(
2991 MediaBuffer **buffer, const ReadOptions *options) {
2992 *buffer = NULL;
2993
2994 Mutex::Autolock autoLock(mLock);
2995
Andreas Huberd06e5b82009-08-28 13:18:14 -07002996 if (mState != EXECUTING && mState != RECONFIGURING) {
2997 return UNKNOWN_ERROR;
2998 }
2999
Andreas Hubere981c332009-10-22 13:49:30 -07003000 bool seeking = false;
3001 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003002 ReadOptions::SeekMode seekMode;
3003 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07003004 seeking = true;
3005 }
James Dong53d4e0d2010-07-21 14:51:35 -07003006 int64_t skipTimeUs;
3007 if (options && options->getSkipFrame(&skipTimeUs)) {
3008 mSkipTimeUs = skipTimeUs;
3009 } else {
3010 mSkipTimeUs = -1;
3011 }
Andreas Hubere981c332009-10-22 13:49:30 -07003012
Andreas Huber42978e52009-08-27 10:08:39 -07003013 if (mInitialBufferSubmit) {
3014 mInitialBufferSubmit = false;
3015
Andreas Hubere981c332009-10-22 13:49:30 -07003016 if (seeking) {
3017 CHECK(seekTimeUs >= 0);
3018 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003019 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07003020
3021 // There's no reason to trigger the code below, there's
3022 // nothing to flush yet.
3023 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07003024 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07003025 }
3026
Andreas Huber42978e52009-08-27 10:08:39 -07003027 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07003028
Andreas Huberd06e5b82009-08-28 13:18:14 -07003029 if (mState == EXECUTING) {
3030 // Otherwise mState == RECONFIGURING and this code will trigger
3031 // after the output port is reenabled.
3032 fillOutputBuffers();
3033 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003034 }
3035
Andreas Hubere981c332009-10-22 13:49:30 -07003036 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07003037 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07003038
3039 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003040
3041 CHECK(seekTimeUs >= 0);
3042 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003043 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07003044
3045 mFilledBuffers.clear();
3046
3047 CHECK_EQ(mState, EXECUTING);
3048
Andreas Huber404cc412009-08-25 14:26:05 -07003049 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
3050 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
3051
3052 if (emulateInputFlushCompletion) {
3053 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3054 }
3055
3056 if (emulateOutputFlushCompletion) {
3057 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3058 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08003059
3060 while (mSeekTimeUs >= 0) {
3061 mBufferFilled.wait(mLock);
3062 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003063 }
3064
3065 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
3066 mBufferFilled.wait(mLock);
3067 }
3068
3069 if (mState == ERROR) {
3070 return UNKNOWN_ERROR;
3071 }
3072
3073 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08003074 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07003075 }
3076
Andreas Hubercfd55572009-10-09 14:11:28 -07003077 if (mOutputPortSettingsHaveChanged) {
3078 mOutputPortSettingsHaveChanged = false;
3079
3080 return INFO_FORMAT_CHANGED;
3081 }
3082
Andreas Huberbe06d262009-08-14 14:37:10 -07003083 size_t index = *mFilledBuffers.begin();
3084 mFilledBuffers.erase(mFilledBuffers.begin());
3085
3086 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
3087 info->mMediaBuffer->add_ref();
3088 *buffer = info->mMediaBuffer;
3089
3090 return OK;
3091}
3092
3093void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
3094 Mutex::Autolock autoLock(mLock);
3095
3096 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
3097 for (size_t i = 0; i < buffers->size(); ++i) {
3098 BufferInfo *info = &buffers->editItemAt(i);
3099
3100 if (info->mMediaBuffer == buffer) {
3101 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
3102 fillOutputBuffer(info);
3103 return;
3104 }
3105 }
3106
3107 CHECK(!"should not be here.");
3108}
3109
3110static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
3111 static const char *kNames[] = {
3112 "OMX_IMAGE_CodingUnused",
3113 "OMX_IMAGE_CodingAutoDetect",
3114 "OMX_IMAGE_CodingJPEG",
3115 "OMX_IMAGE_CodingJPEG2K",
3116 "OMX_IMAGE_CodingEXIF",
3117 "OMX_IMAGE_CodingTIFF",
3118 "OMX_IMAGE_CodingGIF",
3119 "OMX_IMAGE_CodingPNG",
3120 "OMX_IMAGE_CodingLZW",
3121 "OMX_IMAGE_CodingBMP",
3122 };
3123
3124 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3125
3126 if (type < 0 || (size_t)type >= numNames) {
3127 return "UNKNOWN";
3128 } else {
3129 return kNames[type];
3130 }
3131}
3132
3133static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
3134 static const char *kNames[] = {
3135 "OMX_COLOR_FormatUnused",
3136 "OMX_COLOR_FormatMonochrome",
3137 "OMX_COLOR_Format8bitRGB332",
3138 "OMX_COLOR_Format12bitRGB444",
3139 "OMX_COLOR_Format16bitARGB4444",
3140 "OMX_COLOR_Format16bitARGB1555",
3141 "OMX_COLOR_Format16bitRGB565",
3142 "OMX_COLOR_Format16bitBGR565",
3143 "OMX_COLOR_Format18bitRGB666",
3144 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003145 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07003146 "OMX_COLOR_Format24bitRGB888",
3147 "OMX_COLOR_Format24bitBGR888",
3148 "OMX_COLOR_Format24bitARGB1887",
3149 "OMX_COLOR_Format25bitARGB1888",
3150 "OMX_COLOR_Format32bitBGRA8888",
3151 "OMX_COLOR_Format32bitARGB8888",
3152 "OMX_COLOR_FormatYUV411Planar",
3153 "OMX_COLOR_FormatYUV411PackedPlanar",
3154 "OMX_COLOR_FormatYUV420Planar",
3155 "OMX_COLOR_FormatYUV420PackedPlanar",
3156 "OMX_COLOR_FormatYUV420SemiPlanar",
3157 "OMX_COLOR_FormatYUV422Planar",
3158 "OMX_COLOR_FormatYUV422PackedPlanar",
3159 "OMX_COLOR_FormatYUV422SemiPlanar",
3160 "OMX_COLOR_FormatYCbYCr",
3161 "OMX_COLOR_FormatYCrYCb",
3162 "OMX_COLOR_FormatCbYCrY",
3163 "OMX_COLOR_FormatCrYCbY",
3164 "OMX_COLOR_FormatYUV444Interleaved",
3165 "OMX_COLOR_FormatRawBayer8bit",
3166 "OMX_COLOR_FormatRawBayer10bit",
3167 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003168 "OMX_COLOR_FormatL2",
3169 "OMX_COLOR_FormatL4",
3170 "OMX_COLOR_FormatL8",
3171 "OMX_COLOR_FormatL16",
3172 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07003173 "OMX_COLOR_FormatL32",
3174 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
3175 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
3176 "OMX_COLOR_Format18BitBGR666",
3177 "OMX_COLOR_Format24BitARGB6666",
3178 "OMX_COLOR_Format24BitABGR6666",
3179 };
3180
3181 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3182
Andreas Huberbe06d262009-08-14 14:37:10 -07003183 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3184 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3185 } else if (type < 0 || (size_t)type >= numNames) {
3186 return "UNKNOWN";
3187 } else {
3188 return kNames[type];
3189 }
3190}
3191
3192static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3193 static const char *kNames[] = {
3194 "OMX_VIDEO_CodingUnused",
3195 "OMX_VIDEO_CodingAutoDetect",
3196 "OMX_VIDEO_CodingMPEG2",
3197 "OMX_VIDEO_CodingH263",
3198 "OMX_VIDEO_CodingMPEG4",
3199 "OMX_VIDEO_CodingWMV",
3200 "OMX_VIDEO_CodingRV",
3201 "OMX_VIDEO_CodingAVC",
3202 "OMX_VIDEO_CodingMJPEG",
3203 };
3204
3205 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3206
3207 if (type < 0 || (size_t)type >= numNames) {
3208 return "UNKNOWN";
3209 } else {
3210 return kNames[type];
3211 }
3212}
3213
3214static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3215 static const char *kNames[] = {
3216 "OMX_AUDIO_CodingUnused",
3217 "OMX_AUDIO_CodingAutoDetect",
3218 "OMX_AUDIO_CodingPCM",
3219 "OMX_AUDIO_CodingADPCM",
3220 "OMX_AUDIO_CodingAMR",
3221 "OMX_AUDIO_CodingGSMFR",
3222 "OMX_AUDIO_CodingGSMEFR",
3223 "OMX_AUDIO_CodingGSMHR",
3224 "OMX_AUDIO_CodingPDCFR",
3225 "OMX_AUDIO_CodingPDCEFR",
3226 "OMX_AUDIO_CodingPDCHR",
3227 "OMX_AUDIO_CodingTDMAFR",
3228 "OMX_AUDIO_CodingTDMAEFR",
3229 "OMX_AUDIO_CodingQCELP8",
3230 "OMX_AUDIO_CodingQCELP13",
3231 "OMX_AUDIO_CodingEVRC",
3232 "OMX_AUDIO_CodingSMV",
3233 "OMX_AUDIO_CodingG711",
3234 "OMX_AUDIO_CodingG723",
3235 "OMX_AUDIO_CodingG726",
3236 "OMX_AUDIO_CodingG729",
3237 "OMX_AUDIO_CodingAAC",
3238 "OMX_AUDIO_CodingMP3",
3239 "OMX_AUDIO_CodingSBC",
3240 "OMX_AUDIO_CodingVORBIS",
3241 "OMX_AUDIO_CodingWMA",
3242 "OMX_AUDIO_CodingRA",
3243 "OMX_AUDIO_CodingMIDI",
3244 };
3245
3246 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3247
3248 if (type < 0 || (size_t)type >= numNames) {
3249 return "UNKNOWN";
3250 } else {
3251 return kNames[type];
3252 }
3253}
3254
3255static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3256 static const char *kNames[] = {
3257 "OMX_AUDIO_PCMModeLinear",
3258 "OMX_AUDIO_PCMModeALaw",
3259 "OMX_AUDIO_PCMModeMULaw",
3260 };
3261
3262 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3263
3264 if (type < 0 || (size_t)type >= numNames) {
3265 return "UNKNOWN";
3266 } else {
3267 return kNames[type];
3268 }
3269}
3270
Andreas Huber7ae02c82009-09-09 16:29:47 -07003271static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3272 static const char *kNames[] = {
3273 "OMX_AUDIO_AMRBandModeUnused",
3274 "OMX_AUDIO_AMRBandModeNB0",
3275 "OMX_AUDIO_AMRBandModeNB1",
3276 "OMX_AUDIO_AMRBandModeNB2",
3277 "OMX_AUDIO_AMRBandModeNB3",
3278 "OMX_AUDIO_AMRBandModeNB4",
3279 "OMX_AUDIO_AMRBandModeNB5",
3280 "OMX_AUDIO_AMRBandModeNB6",
3281 "OMX_AUDIO_AMRBandModeNB7",
3282 "OMX_AUDIO_AMRBandModeWB0",
3283 "OMX_AUDIO_AMRBandModeWB1",
3284 "OMX_AUDIO_AMRBandModeWB2",
3285 "OMX_AUDIO_AMRBandModeWB3",
3286 "OMX_AUDIO_AMRBandModeWB4",
3287 "OMX_AUDIO_AMRBandModeWB5",
3288 "OMX_AUDIO_AMRBandModeWB6",
3289 "OMX_AUDIO_AMRBandModeWB7",
3290 "OMX_AUDIO_AMRBandModeWB8",
3291 };
3292
3293 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3294
3295 if (type < 0 || (size_t)type >= numNames) {
3296 return "UNKNOWN";
3297 } else {
3298 return kNames[type];
3299 }
3300}
3301
3302static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3303 static const char *kNames[] = {
3304 "OMX_AUDIO_AMRFrameFormatConformance",
3305 "OMX_AUDIO_AMRFrameFormatIF1",
3306 "OMX_AUDIO_AMRFrameFormatIF2",
3307 "OMX_AUDIO_AMRFrameFormatFSF",
3308 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3309 "OMX_AUDIO_AMRFrameFormatITU",
3310 };
3311
3312 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3313
3314 if (type < 0 || (size_t)type >= numNames) {
3315 return "UNKNOWN";
3316 } else {
3317 return kNames[type];
3318 }
3319}
Andreas Huberbe06d262009-08-14 14:37:10 -07003320
3321void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3322 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003323 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003324 def.nPortIndex = portIndex;
3325
Andreas Huber784202e2009-10-15 13:46:54 -07003326 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003327 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3328 CHECK_EQ(err, OK);
3329
3330 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3331
3332 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3333 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3334
3335 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3336 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3337 printf(" nBufferSize = %ld\n", def.nBufferSize);
3338
3339 switch (def.eDomain) {
3340 case OMX_PortDomainImage:
3341 {
3342 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3343
3344 printf("\n");
3345 printf(" // Image\n");
3346 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3347 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3348 printf(" nStride = %ld\n", imageDef->nStride);
3349
3350 printf(" eCompressionFormat = %s\n",
3351 imageCompressionFormatString(imageDef->eCompressionFormat));
3352
3353 printf(" eColorFormat = %s\n",
3354 colorFormatString(imageDef->eColorFormat));
3355
3356 break;
3357 }
3358
3359 case OMX_PortDomainVideo:
3360 {
3361 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3362
3363 printf("\n");
3364 printf(" // Video\n");
3365 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3366 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3367 printf(" nStride = %ld\n", videoDef->nStride);
3368
3369 printf(" eCompressionFormat = %s\n",
3370 videoCompressionFormatString(videoDef->eCompressionFormat));
3371
3372 printf(" eColorFormat = %s\n",
3373 colorFormatString(videoDef->eColorFormat));
3374
3375 break;
3376 }
3377
3378 case OMX_PortDomainAudio:
3379 {
3380 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3381
3382 printf("\n");
3383 printf(" // Audio\n");
3384 printf(" eEncoding = %s\n",
3385 audioCodingTypeString(audioDef->eEncoding));
3386
3387 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3388 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003389 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003390 params.nPortIndex = portIndex;
3391
Andreas Huber784202e2009-10-15 13:46:54 -07003392 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003393 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3394 CHECK_EQ(err, OK);
3395
3396 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3397 printf(" nChannels = %ld\n", params.nChannels);
3398 printf(" bInterleaved = %d\n", params.bInterleaved);
3399 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3400
3401 printf(" eNumData = %s\n",
3402 params.eNumData == OMX_NumericalDataSigned
3403 ? "signed" : "unsigned");
3404
3405 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003406 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3407 OMX_AUDIO_PARAM_AMRTYPE amr;
3408 InitOMXParams(&amr);
3409 amr.nPortIndex = portIndex;
3410
Andreas Huber784202e2009-10-15 13:46:54 -07003411 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003412 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3413 CHECK_EQ(err, OK);
3414
3415 printf(" nChannels = %ld\n", amr.nChannels);
3416 printf(" eAMRBandMode = %s\n",
3417 amrBandModeString(amr.eAMRBandMode));
3418 printf(" eAMRFrameFormat = %s\n",
3419 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003420 }
3421
3422 break;
3423 }
3424
3425 default:
3426 {
3427 printf(" // Unknown\n");
3428 break;
3429 }
3430 }
3431
3432 printf("}\n");
3433}
3434
3435void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3436 mOutputFormat = new MetaData;
3437 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003438 if (mIsEncoder) {
3439 int32_t timeScale;
3440 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3441 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3442 }
3443 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003444
3445 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003446 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003447 def.nPortIndex = kPortIndexOutput;
3448
Andreas Huber784202e2009-10-15 13:46:54 -07003449 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003450 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3451 CHECK_EQ(err, OK);
3452
3453 switch (def.eDomain) {
3454 case OMX_PortDomainImage:
3455 {
3456 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3457 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3458
Andreas Hubere6c40962009-09-10 14:13:30 -07003459 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003460 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3461 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3462 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3463 break;
3464 }
3465
3466 case OMX_PortDomainAudio:
3467 {
3468 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3469
Andreas Huberda050cf22009-09-02 14:01:43 -07003470 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3471 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003472 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003473 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003474
Andreas Huber784202e2009-10-15 13:46:54 -07003475 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003476 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3477 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003478
Andreas Huberda050cf22009-09-02 14:01:43 -07003479 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3480 CHECK_EQ(params.nBitPerSample, 16);
3481 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003482
Andreas Huberda050cf22009-09-02 14:01:43 -07003483 int32_t numChannels, sampleRate;
3484 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3485 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003486
Andreas Huberda050cf22009-09-02 14:01:43 -07003487 if ((OMX_U32)numChannels != params.nChannels) {
3488 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003489 "the input stream contains (contains %d channels, "
3490 "codec outputs %ld channels).",
3491 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003492 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003493
Andreas Hubere6c40962009-09-10 14:13:30 -07003494 mOutputFormat->setCString(
3495 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003496
3497 // Use the codec-advertised number of channels, as some
3498 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003499 // mono. If we know the codec lies about this information,
3500 // use the actual number of channels instead.
3501 mOutputFormat->setInt32(
3502 kKeyChannelCount,
3503 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3504 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003505
3506 // The codec-reported sampleRate is not reliable...
3507 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3508 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003509 OMX_AUDIO_PARAM_AMRTYPE amr;
3510 InitOMXParams(&amr);
3511 amr.nPortIndex = kPortIndexOutput;
3512
Andreas Huber784202e2009-10-15 13:46:54 -07003513 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003514 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3515 CHECK_EQ(err, OK);
3516
3517 CHECK_EQ(amr.nChannels, 1);
3518 mOutputFormat->setInt32(kKeyChannelCount, 1);
3519
3520 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3521 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003522 mOutputFormat->setCString(
3523 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003524 mOutputFormat->setInt32(kKeySampleRate, 8000);
3525 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3526 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003527 mOutputFormat->setCString(
3528 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003529 mOutputFormat->setInt32(kKeySampleRate, 16000);
3530 } else {
3531 CHECK(!"Unknown AMR band mode.");
3532 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003533 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003534 mOutputFormat->setCString(
3535 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003536 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003537 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3538 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003539 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003540 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3541 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003542 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003543 } else {
3544 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003545 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003546 break;
3547 }
3548
3549 case OMX_PortDomainVideo:
3550 {
3551 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3552
3553 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003554 mOutputFormat->setCString(
3555 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003556 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003557 mOutputFormat->setCString(
3558 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003559 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003560 mOutputFormat->setCString(
3561 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003562 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003563 mOutputFormat->setCString(
3564 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003565 } else {
3566 CHECK(!"Unknown compression format.");
3567 }
3568
3569 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3570 // This component appears to be lying to me.
3571 mOutputFormat->setInt32(
3572 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3573 mOutputFormat->setInt32(
3574 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3575 } else {
3576 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3577 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3578 }
3579
3580 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3581 break;
3582 }
3583
3584 default:
3585 {
3586 CHECK(!"should not be here, neither audio nor video.");
3587 break;
3588 }
3589 }
3590}
3591
Andreas Huber1f24b302010-06-10 11:12:39 -07003592status_t OMXCodec::pause() {
3593 Mutex::Autolock autoLock(mLock);
3594
3595 mPaused = true;
3596
3597 return OK;
3598}
3599
Andreas Hubere6c40962009-09-10 14:13:30 -07003600////////////////////////////////////////////////////////////////////////////////
3601
3602status_t QueryCodecs(
3603 const sp<IOMX> &omx,
3604 const char *mime, bool queryDecoders,
3605 Vector<CodecCapabilities> *results) {
3606 results->clear();
3607
3608 for (int index = 0;; ++index) {
3609 const char *componentName;
3610
3611 if (!queryDecoders) {
3612 componentName = GetCodec(
3613 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3614 mime, index);
3615 } else {
3616 componentName = GetCodec(
3617 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3618 mime, index);
3619 }
3620
3621 if (!componentName) {
3622 return OK;
3623 }
3624
Andreas Huber1a189a82010-03-24 13:49:20 -07003625 if (strncmp(componentName, "OMX.", 4)) {
3626 // Not an OpenMax component but a software codec.
3627
3628 results->push();
3629 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3630 caps->mComponentName = componentName;
3631
3632 continue;
3633 }
3634
Andreas Huber784202e2009-10-15 13:46:54 -07003635 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003636 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003637 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003638
3639 if (err != OK) {
3640 continue;
3641 }
3642
James Dong722d5912010-04-13 10:56:59 -07003643 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003644
3645 results->push();
3646 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3647 caps->mComponentName = componentName;
3648
3649 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3650 InitOMXParams(&param);
3651
3652 param.nPortIndex = queryDecoders ? 0 : 1;
3653
3654 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003655 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003656 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3657 &param, sizeof(param));
3658
3659 if (err != OK) {
3660 break;
3661 }
3662
3663 CodecProfileLevel profileLevel;
3664 profileLevel.mProfile = param.eProfile;
3665 profileLevel.mLevel = param.eLevel;
3666
3667 caps->mProfileLevels.push(profileLevel);
3668 }
3669
Andreas Huber784202e2009-10-15 13:46:54 -07003670 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003671 }
3672}
3673
Andreas Huberbe06d262009-08-14 14:37:10 -07003674} // namespace android