blob: 3c3bd93f1186da977d94638624dd9093fe24f5bc [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>
Jamie Gennis58a36ad2010-10-07 14:08:38 -070042#include <media/stagefright/HardwareAPI.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070043#include <media/stagefright/MediaBuffer.h>
44#include <media/stagefright/MediaBufferGroup.h>
45#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070046#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070047#include <media/stagefright/MediaExtractor.h>
48#include <media/stagefright/MetaData.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070049#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070050#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070051#include <utils/Vector.h>
52
53#include <OMX_Audio.h>
54#include <OMX_Component.h>
55
Andreas Huber8946ab22010-09-15 16:20:42 -070056#include "include/ThreadedSource.h"
57
Andreas Huberbe06d262009-08-14 14:37:10 -070058namespace android {
59
Andreas Huber8b432b12009-10-07 13:36:52 -070060static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
61
Andreas Huberbe06d262009-08-14 14:37:10 -070062struct CodecInfo {
63 const char *mime;
64 const char *codec;
65};
66
Andreas Huberfb1c2f82009-12-15 13:25:11 -080067#define FACTORY_CREATE(name) \
68static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
69 return new name(source); \
70}
71
James Dong17299ab2010-05-14 15:45:22 -070072#define FACTORY_CREATE_ENCODER(name) \
73static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \
74 return new name(source, meta); \
75}
76
Andreas Huberfb1c2f82009-12-15 13:25:11 -080077#define FACTORY_REF(name) { #name, Make##name },
78
79FACTORY_CREATE(MP3Decoder)
80FACTORY_CREATE(AMRNBDecoder)
81FACTORY_CREATE(AMRWBDecoder)
82FACTORY_CREATE(AACDecoder)
83FACTORY_CREATE(AVCDecoder)
Andreas Huber520b2a72010-08-09 09:54:59 -070084FACTORY_CREATE(G711Decoder)
James Dong02f5b542009-12-15 16:26:55 -080085FACTORY_CREATE(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -070086FACTORY_CREATE(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -070087FACTORY_CREATE(VPXDecoder)
James Dong17299ab2010-05-14 15:45:22 -070088FACTORY_CREATE_ENCODER(AMRNBEncoder)
89FACTORY_CREATE_ENCODER(AMRWBEncoder)
90FACTORY_CREATE_ENCODER(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -070091FACTORY_CREATE_ENCODER(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -070092FACTORY_CREATE_ENCODER(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -070093
94static sp<MediaSource> InstantiateSoftwareEncoder(
95 const char *name, const sp<MediaSource> &source,
96 const sp<MetaData> &meta) {
97 struct FactoryInfo {
98 const char *name;
99 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &);
100 };
101
102 static const FactoryInfo kFactoryInfo[] = {
103 FACTORY_REF(AMRNBEncoder)
104 FACTORY_REF(AMRWBEncoder)
105 FACTORY_REF(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -0700106 FACTORY_REF(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -0700107 FACTORY_REF(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -0700108 };
109 for (size_t i = 0;
110 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
111 if (!strcmp(name, kFactoryInfo[i].name)) {
112 return (*kFactoryInfo[i].CreateFunc)(source, meta);
113 }
114 }
115
116 return NULL;
117}
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800118
119static sp<MediaSource> InstantiateSoftwareCodec(
120 const char *name, const sp<MediaSource> &source) {
121 struct FactoryInfo {
122 const char *name;
123 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
124 };
125
126 static const FactoryInfo kFactoryInfo[] = {
127 FACTORY_REF(MP3Decoder)
128 FACTORY_REF(AMRNBDecoder)
129 FACTORY_REF(AMRWBDecoder)
130 FACTORY_REF(AACDecoder)
131 FACTORY_REF(AVCDecoder)
Andreas Huber520b2a72010-08-09 09:54:59 -0700132 FACTORY_REF(G711Decoder)
James Dong02f5b542009-12-15 16:26:55 -0800133 FACTORY_REF(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -0700134 FACTORY_REF(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -0700135 FACTORY_REF(VPXDecoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800136 };
137 for (size_t i = 0;
138 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
139 if (!strcmp(name, kFactoryInfo[i].name)) {
Andreas Huber8946ab22010-09-15 16:20:42 -0700140 if (!strcmp(name, "VPXDecoder")) {
141 return new ThreadedSource(
142 (*kFactoryInfo[i].CreateFunc)(source));
143 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800144 return (*kFactoryInfo[i].CreateFunc)(source);
145 }
146 }
147
148 return NULL;
149}
150
151#undef FACTORY_REF
152#undef FACTORY_CREATE
153
Andreas Huberbe06d262009-08-14 14:37:10 -0700154static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700155 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
Andreas Huberd222c842010-08-26 14:29:34 -0700156// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.Nvidia.mp3.decoder" },
James Dong374aee62010-04-26 10:23:30 -0700157// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800158 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700159// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberd222c842010-08-26 14:29:34 -0700160// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.Nvidia.amr.decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800161 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huberd222c842010-08-26 14:29:34 -0700162// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.Nvidia.amrwb.decoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700163 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800164 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huberd222c842010-08-26 14:29:34 -0700165// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.Nvidia.aac.decoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700166 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800167 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber520b2a72010-08-09 09:54:59 -0700168 { MEDIA_MIMETYPE_AUDIO_G711_ALAW, "G711Decoder" },
169 { MEDIA_MIMETYPE_AUDIO_G711_MLAW, "G711Decoder" },
Andreas Huber9f9ae602010-10-27 14:53:55 -0700170 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.Nvidia.mp4.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700171 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.decoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700172 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
173 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700174 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800175 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber9f9ae602010-10-27 14:53:55 -0700176 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.Nvidia.h263.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700177 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.decoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700178 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700179 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800180 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
pgudadhe6ad2c352010-07-26 15:04:33 -0700181 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.Nvidia.h264.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700182 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.decoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700183 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
184 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700185 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800186 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber388379f2010-05-07 10:35:13 -0700187 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" },
Andreas Huber47ba30e2010-05-24 14:38:02 -0700188 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700189};
190
191static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700192 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800193 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700194 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700195 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700196 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700197 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700198 { 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" },
James Dong2e87f7b2010-09-23 17:46:34 -0700201 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.Nvidia.mp4.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700202 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700203 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700204 { 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" },
James Dong2e87f7b2010-09-23 17:46:34 -0700207 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.Nvidia.h263.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700208 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700209 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700210 { 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" },
pgudadhe9c305322010-07-26 13:59:29 -0700213 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.Nvidia.h264.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700214 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Encoder" },
James Dong1cc31e62010-07-02 17:44:44 -0700215 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCEncoder" },
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) {
James Dong5592bcc2010-10-22 17:10:43 -0700310 if (!strncmp("OMX.", componentName, 4)) {
311 return false;
Andreas Huberbe06d262009-08-14 14:37:10 -0700312 }
313
James Dong5592bcc2010-10-22 17:10:43 -0700314 return true;
Andreas Hubere13526a2009-10-22 10:43:34 -0700315}
316
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800317// A sort order in which non-OMX components are first,
James Dong5592bcc2010-10-22 17:10:43 -0700318// followed by software codecs, and followed by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700319static int CompareSoftwareCodecsFirst(
320 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800321 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
322 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
323
324 if (isNotOMX1) {
325 if (isNotOMX2) { return 0; }
326 return -1;
327 }
328 if (isNotOMX2) {
329 return 1;
330 }
331
Andreas Hubere13526a2009-10-22 10:43:34 -0700332 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
333 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
334
335 if (isSoftwareCodec1) {
336 if (isSoftwareCodec2) { return 0; }
337 return -1;
338 }
339
340 if (isSoftwareCodec2) {
341 return 1;
342 }
343
344 return 0;
345}
346
347// static
Andreas Huber1e194162010-10-06 16:43:57 -0700348uint32_t OMXCodec::getComponentQuirks(
349 const char *componentName, bool isEncoder) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700350 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700351
Dima Zavin30ba6cb2010-08-23 11:10:03 -0700352 if (!strcmp(componentName, "OMX.Nvidia.amr.decoder") ||
353 !strcmp(componentName, "OMX.Nvidia.amrwb.decoder") ||
354 !strcmp(componentName, "OMX.Nvidia.aac.decoder") ||
355 !strcmp(componentName, "OMX.Nvidia.mp3.decoder")) {
356 quirks |= kDecoderLiesAboutNumberOfChannels;
357 }
358
Andreas Huberbe06d262009-08-14 14:37:10 -0700359 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
360 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800361 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700362 }
363 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
364 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700365 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700366 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700367 }
368 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
369 quirks |= kRequiresLoadedToIdleAfterAllocation;
370 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700371 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dong90862e22010-08-26 19:12:59 -0700372 if (!strncmp(componentName, "OMX.qcom.video.encoder.avc", 26)) {
373
374 // The AVC encoder advertises the size of output buffers
375 // based on the input video resolution and assumes
376 // the worst/least compression ratio is 0.5. It is found that
377 // sometimes, the output buffer size is larger than
378 // size advertised by the encoder.
379 quirks |= kRequiresLargerEncoderOutputBuffer;
380 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700381 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700382 if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) {
383 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700384 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700385 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800386 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700387 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700388 if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) {
389 quirks |= kRequiresAllocateBufferOnInputPorts;
390 quirks |= kRequiresAllocateBufferOnOutputPorts;
391 quirks |= kDefersOutputBufferAllocation;
392 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700393
Andreas Huber2dc64d82009-09-11 12:58:53 -0700394 if (!strncmp(componentName, "OMX.TI.", 7)) {
395 // Apparently I must not use OMX_UseBuffer on either input or
396 // output ports on any of the TI components or quote:
397 // "(I) may have unexpected problem (sic) which can be timing related
398 // and hard to reproduce."
399
400 quirks |= kRequiresAllocateBufferOnInputPorts;
401 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dongdca66e12010-06-14 11:14:38 -0700402 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) {
James Dong4f501f02010-06-07 14:41:41 -0700403 quirks |= kAvoidMemcopyInputRecordingFrames;
404 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700405 }
406
Andreas Huberb8de9572010-02-22 14:58:45 -0800407 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
408 quirks |= kInputBufferSizesAreBogus;
409 }
410
Andreas Huber1e194162010-10-06 16:43:57 -0700411 if (!strncmp(componentName, "OMX.SEC.", 8) && !isEncoder) {
412 // These output buffers contain no video data, just some
413 // opaque information that allows the overlay to display their
414 // contents.
415 quirks |= kOutputBuffersAreUnreadable;
416 }
417
Andreas Hubere13526a2009-10-22 10:43:34 -0700418 return quirks;
419}
420
421// static
422void OMXCodec::findMatchingCodecs(
423 const char *mime,
424 bool createEncoder, const char *matchComponentName,
425 uint32_t flags,
426 Vector<String8> *matchingCodecs) {
427 matchingCodecs->clear();
428
429 for (int index = 0;; ++index) {
430 const char *componentName;
431
432 if (createEncoder) {
433 componentName = GetCodec(
434 kEncoderInfo,
435 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
436 mime, index);
437 } else {
438 componentName = GetCodec(
439 kDecoderInfo,
440 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
441 mime, index);
442 }
443
444 if (!componentName) {
445 break;
446 }
447
448 // If a specific codec is requested, skip the non-matching ones.
449 if (matchComponentName && strcmp(componentName, matchComponentName)) {
450 continue;
451 }
452
James Dong170a9292010-10-22 17:28:15 -0700453 // When requesting software-only codecs, only push software codecs
454 // When requesting hardware-only codecs, only push hardware codecs
455 // When there is request neither for software-only nor for
456 // hardware-only codecs, push all codecs
457 if (((flags & kSoftwareCodecsOnly) && IsSoftwareCodec(componentName)) ||
458 ((flags & kHardwareCodecsOnly) && !IsSoftwareCodec(componentName)) ||
459 (!(flags & (kSoftwareCodecsOnly | kHardwareCodecsOnly)))) {
460
461 matchingCodecs->push(String8(componentName));
462 }
Andreas Hubere13526a2009-10-22 10:43:34 -0700463 }
464
465 if (flags & kPreferSoftwareCodecs) {
466 matchingCodecs->sort(CompareSoftwareCodecsFirst);
467 }
468}
469
470// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800471sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700472 const sp<IOMX> &omx,
473 const sp<MetaData> &meta, bool createEncoder,
474 const sp<MediaSource> &source,
475 const char *matchComponentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700476 uint32_t flags,
477 const sp<ANativeWindow> &nativeWindow) {
Andreas Hubere13526a2009-10-22 10:43:34 -0700478 const char *mime;
479 bool success = meta->findCString(kKeyMIMEType, &mime);
480 CHECK(success);
481
482 Vector<String8> matchingCodecs;
483 findMatchingCodecs(
484 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
485
486 if (matchingCodecs.isEmpty()) {
487 return NULL;
488 }
489
490 sp<OMXCodecObserver> observer = new OMXCodecObserver;
491 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700492
493 const char *componentName;
494 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
495 componentName = matchingCodecs[i].string();
496
James Dong17299ab2010-05-14 15:45:22 -0700497 sp<MediaSource> softwareCodec = createEncoder?
498 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800499 InstantiateSoftwareCodec(componentName, source);
500
501 if (softwareCodec != NULL) {
502 LOGV("Successfully allocated software codec '%s'", componentName);
503
504 return softwareCodec;
505 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800506
Andreas Hubere13526a2009-10-22 10:43:34 -0700507 LOGV("Attempting to allocate OMX node '%s'", componentName);
508
Andreas Huber5a40e392010-10-18 09:57:42 -0700509 uint32_t quirks = getComponentQuirks(componentName, createEncoder);
510
511 if (!createEncoder
512 && (quirks & kOutputBuffersAreUnreadable)
513 && (flags & kClientNeedsFramebuffer)) {
514 if (strncmp(componentName, "OMX.SEC.", 8)) {
515 // For OMX.SEC.* decoders we can enable a special mode that
516 // gives the client access to the framebuffer contents.
517
518 LOGW("Component '%s' does not give the client access to "
519 "the framebuffer contents. Skipping.",
520 componentName);
521
522 continue;
523 }
524 }
525
Andreas Hubere13526a2009-10-22 10:43:34 -0700526 status_t err = omx->allocateNode(componentName, observer, &node);
527 if (err == OK) {
528 LOGV("Successfully allocated OMX node '%s'", componentName);
529
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700530 sp<OMXCodec> codec = new OMXCodec(
Andreas Huber5a40e392010-10-18 09:57:42 -0700531 omx, node, quirks,
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700532 createEncoder, mime, componentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700533 source, nativeWindow);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700534
535 observer->setCodec(codec);
536
Andreas Huber4c19bf92010-09-08 14:32:20 -0700537 err = codec->configureCodec(meta, flags);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700538
539 if (err == OK) {
540 return codec;
541 }
542
543 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700544 }
545 }
546
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700547 return NULL;
548}
Andreas Hubere13526a2009-10-22 10:43:34 -0700549
Andreas Huber4c19bf92010-09-08 14:32:20 -0700550status_t OMXCodec::configureCodec(const sp<MetaData> &meta, uint32_t flags) {
551 if (!(flags & kIgnoreCodecSpecificData)) {
552 uint32_t type;
553 const void *data;
554 size_t size;
555 if (meta->findData(kKeyESDS, &type, &data, &size)) {
556 ESDS esds((const char *)data, size);
557 CHECK_EQ(esds.InitCheck(), OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700558
Andreas Huber4c19bf92010-09-08 14:32:20 -0700559 const void *codec_specific_data;
560 size_t codec_specific_data_size;
561 esds.getCodecSpecificInfo(
562 &codec_specific_data, &codec_specific_data_size);
Andreas Huberbe06d262009-08-14 14:37:10 -0700563
Andreas Huber4c19bf92010-09-08 14:32:20 -0700564 addCodecSpecificData(
565 codec_specific_data, codec_specific_data_size);
566 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
567 // Parse the AVCDecoderConfigurationRecord
Andreas Huberebf66ea2009-08-19 13:32:58 -0700568
Andreas Huber4c19bf92010-09-08 14:32:20 -0700569 const uint8_t *ptr = (const uint8_t *)data;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700570
Andreas Huber4c19bf92010-09-08 14:32:20 -0700571 CHECK(size >= 7);
572 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
573 uint8_t profile = ptr[1];
574 uint8_t level = ptr[3];
Andreas Huberebf66ea2009-08-19 13:32:58 -0700575
Andreas Huber4c19bf92010-09-08 14:32:20 -0700576 // There is decodable content out there that fails the following
577 // assertion, let's be lenient for now...
578 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700579
Andreas Huber4c19bf92010-09-08 14:32:20 -0700580 size_t lengthSize = 1 + (ptr[4] & 3);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700581
Andreas Huber4c19bf92010-09-08 14:32:20 -0700582 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
583 // violates it...
584 // CHECK((ptr[5] >> 5) == 7); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700585
Andreas Huber4c19bf92010-09-08 14:32:20 -0700586 size_t numSeqParameterSets = ptr[5] & 31;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700587
Andreas Huber4c19bf92010-09-08 14:32:20 -0700588 ptr += 6;
589 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700590
Andreas Huber4c19bf92010-09-08 14:32:20 -0700591 for (size_t i = 0; i < numSeqParameterSets; ++i) {
592 CHECK(size >= 2);
593 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700594
Andreas Huber4c19bf92010-09-08 14:32:20 -0700595 ptr += 2;
596 size -= 2;
Andreas Huberbe06d262009-08-14 14:37:10 -0700597
Andreas Huber4c19bf92010-09-08 14:32:20 -0700598 CHECK(size >= length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700599
Andreas Huber4c19bf92010-09-08 14:32:20 -0700600 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700601
Andreas Huber4c19bf92010-09-08 14:32:20 -0700602 ptr += length;
603 size -= length;
604 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700605
Andreas Huber4c19bf92010-09-08 14:32:20 -0700606 CHECK(size >= 1);
607 size_t numPictureParameterSets = *ptr;
608 ++ptr;
609 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700610
Andreas Huber4c19bf92010-09-08 14:32:20 -0700611 for (size_t i = 0; i < numPictureParameterSets; ++i) {
612 CHECK(size >= 2);
613 size_t length = U16_AT(ptr);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700614
Andreas Huber4c19bf92010-09-08 14:32:20 -0700615 ptr += 2;
616 size -= 2;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700617
Andreas Huber4c19bf92010-09-08 14:32:20 -0700618 CHECK(size >= length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700619
Andreas Huber4c19bf92010-09-08 14:32:20 -0700620 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700621
Andreas Huber4c19bf92010-09-08 14:32:20 -0700622 ptr += length;
623 size -= length;
624 }
Andreas Huberebf66ea2009-08-19 13:32:58 -0700625
Andreas Huber4c19bf92010-09-08 14:32:20 -0700626 CODEC_LOGV(
627 "AVC profile = %d (%s), level = %d",
628 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700629
Andreas Huber4c19bf92010-09-08 14:32:20 -0700630 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
631 && (profile != kAVCProfileBaseline || level > 30)) {
632 // This stream exceeds the decoder's capabilities. The decoder
633 // does not handle this gracefully and would clobber the heap
634 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700635
Andreas Huber4c19bf92010-09-08 14:32:20 -0700636 LOGE("Profile and/or level exceed the decoder's capabilities.");
637 return ERROR_UNSUPPORTED;
638 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700639 }
640 }
641
James Dong17299ab2010-05-14 15:45:22 -0700642 int32_t bitRate = 0;
643 if (mIsEncoder) {
644 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
645 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700646 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700647 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700648 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700649 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700650 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700651 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700652 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700653 int32_t numChannels, sampleRate;
654 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
655 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
656
James Dong17299ab2010-05-14 15:45:22 -0700657 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700658 }
James Dongabed93a2010-04-22 17:27:04 -0700659
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700660 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700661
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700662 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700663 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700664 } else {
James Dong1244eab2010-06-08 11:58:53 -0700665 int32_t width, height;
666 bool success = meta->findInt32(kKeyWidth, &width);
667 success = success && meta->findInt32(kKeyHeight, &height);
668 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700669 status_t err = setVideoOutputFormat(
670 mMIME, width, height);
671
672 if (err != OK) {
673 return err;
674 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700675 }
676 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700677
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700678 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
679 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700680 OMX_COLOR_FORMATTYPE format =
681 OMX_COLOR_Format32bitARGB8888;
682 // OMX_COLOR_FormatYUV420PackedPlanar;
683 // OMX_COLOR_FormatCbYCrY;
684 // OMX_COLOR_FormatYUV411Planar;
685
686 int32_t width, height;
687 bool success = meta->findInt32(kKeyWidth, &width);
688 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700689
690 int32_t compressedSize;
691 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700692 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700693
694 CHECK(success);
695 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700696
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700697 setImageOutputFormat(format, width, height);
698 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700699 }
700
Andreas Huberda050cf22009-09-02 14:01:43 -0700701 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800702 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700703 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700704 }
705
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700706 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700707 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
708 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700709 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700710 }
711
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700712 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700713
Andreas Huber5a40e392010-10-18 09:57:42 -0700714 if ((flags & kClientNeedsFramebuffer)
715 && !strncmp(mComponentName, "OMX.SEC.", 8)) {
716 OMX_INDEXTYPE index;
717
718 status_t err =
719 mOMX->getExtensionIndex(
720 mNode,
721 "OMX.SEC.index.ThumbnailMode",
722 &index);
723
724 if (err != OK) {
725 return err;
726 }
727
728 OMX_BOOL enable = OMX_TRUE;
729 err = mOMX->setConfig(mNode, index, &enable, sizeof(enable));
730
731 if (err != OK) {
732 CODEC_LOGE("setConfig('OMX.SEC.index.ThumbnailMode') "
733 "returned error 0x%08x", err);
734
735 return err;
736 }
737
738 mQuirks &= ~kOutputBuffersAreUnreadable;
739 }
740
Jamie Gennisdbfb32e2010-10-20 15:53:59 -0700741 if (mNativeWindow != NULL
742 && !mIsEncoder
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700743 && !strncasecmp(mMIME, "video/", 6)
744 && !strncmp(mComponentName, "OMX.", 4)) {
745 status_t err = initNativeWindow();
746 if (err != OK) {
747 return err;
748 }
749 }
750
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700751 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700752}
753
Andreas Huberda050cf22009-09-02 14:01:43 -0700754void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
755 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700756 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700757 def.nPortIndex = portIndex;
758
Andreas Huber784202e2009-10-15 13:46:54 -0700759 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700760 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
761 CHECK_EQ(err, OK);
762
Andreas Huberb8de9572010-02-22 14:58:45 -0800763 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
764 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700765 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700766 }
767
Andreas Huber784202e2009-10-15 13:46:54 -0700768 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700769 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
770 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800771
772 err = mOMX->getParameter(
773 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
774 CHECK_EQ(err, OK);
775
776 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800777 if (portIndex == kPortIndexInput
778 && (mQuirks & kInputBufferSizesAreBogus)) {
779 CHECK_EQ(def.nBufferSize, size);
780 } else {
781 CHECK(def.nBufferSize >= size);
782 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700783}
784
Andreas Huberbe06d262009-08-14 14:37:10 -0700785status_t OMXCodec::setVideoPortFormatType(
786 OMX_U32 portIndex,
787 OMX_VIDEO_CODINGTYPE compressionFormat,
788 OMX_COLOR_FORMATTYPE colorFormat) {
789 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700790 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700791 format.nPortIndex = portIndex;
792 format.nIndex = 0;
793 bool found = false;
794
795 OMX_U32 index = 0;
796 for (;;) {
797 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700798 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700799 mNode, OMX_IndexParamVideoPortFormat,
800 &format, sizeof(format));
801
802 if (err != OK) {
803 return err;
804 }
805
806 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700807 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700808
809#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700810 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700811 portIndex,
812 index, format.eCompressionFormat, format.eColorFormat);
813#endif
814
815 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
816 if (portIndex == kPortIndexInput
817 && colorFormat == format.eColorFormat) {
818 // eCompressionFormat does not seem right.
819 found = true;
820 break;
821 }
822 if (portIndex == kPortIndexOutput
823 && compressionFormat == format.eCompressionFormat) {
824 // eColorFormat does not seem right.
825 found = true;
826 break;
827 }
828 }
829
830 if (format.eCompressionFormat == compressionFormat
831 && format.eColorFormat == colorFormat) {
832 found = true;
833 break;
834 }
835
836 ++index;
837 }
838
839 if (!found) {
840 return UNKNOWN_ERROR;
841 }
842
Andreas Huber53a76bd2009-10-06 16:20:44 -0700843 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700844 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700845 mNode, OMX_IndexParamVideoPortFormat,
846 &format, sizeof(format));
847
848 return err;
849}
850
Andreas Huberb482ce82009-10-29 12:02:48 -0700851static size_t getFrameSize(
852 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
853 switch (colorFormat) {
854 case OMX_COLOR_FormatYCbYCr:
855 case OMX_COLOR_FormatCbYCrY:
856 return width * height * 2;
857
Andreas Huber71c27d92010-03-19 11:43:15 -0700858 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700859 case OMX_COLOR_FormatYUV420SemiPlanar:
860 return (width * height * 3) / 2;
861
862 default:
863 CHECK(!"Should not be here. Unsupported color format.");
864 break;
865 }
866}
867
James Dongafd97e82010-08-03 17:19:23 -0700868status_t OMXCodec::findTargetColorFormat(
869 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) {
870 LOGV("findTargetColorFormat");
871 CHECK(mIsEncoder);
872
873 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
874 int32_t targetColorFormat;
875 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) {
876 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat;
877 } else {
878 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
879 *colorFormat = OMX_COLOR_FormatYCbYCr;
880 }
881 }
882
883 // Check whether the target color format is supported.
884 return isColorFormatSupported(*colorFormat, kPortIndexInput);
885}
886
887status_t OMXCodec::isColorFormatSupported(
888 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) {
889 LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat));
890
891 // Enumerate all the color formats supported by
892 // the omx component to see whether the given
893 // color format is supported.
894 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
895 InitOMXParams(&portFormat);
896 portFormat.nPortIndex = portIndex;
897 OMX_U32 index = 0;
898 portFormat.nIndex = index;
899 while (true) {
900 if (OMX_ErrorNone != mOMX->getParameter(
901 mNode, OMX_IndexParamVideoPortFormat,
902 &portFormat, sizeof(portFormat))) {
James Dongb5024da2010-09-13 16:30:51 -0700903 break;
James Dongafd97e82010-08-03 17:19:23 -0700904 }
905 // Make sure that omx component does not overwrite
906 // the incremented index (bug 2897413).
907 CHECK_EQ(index, portFormat.nIndex);
908 if ((portFormat.eColorFormat == colorFormat)) {
909 LOGV("Found supported color format: %d", portFormat.eColorFormat);
910 return OK; // colorFormat is supported!
911 }
912 ++index;
913 portFormat.nIndex = index;
914
915 // OMX Spec defines less than 50 color formats
916 // 1000 is more than enough for us to tell whether the omx
917 // component in question is buggy or not.
918 if (index >= 1000) {
919 LOGE("More than %ld color formats are supported???", index);
920 break;
921 }
922 }
James Dongb5024da2010-09-13 16:30:51 -0700923
924 LOGE("color format %d is not supported", colorFormat);
James Dongafd97e82010-08-03 17:19:23 -0700925 return UNKNOWN_ERROR;
926}
927
Andreas Huberbe06d262009-08-14 14:37:10 -0700928void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700929 const char *mime, const sp<MetaData>& meta) {
930
931 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
932 bool success = meta->findInt32(kKeyWidth, &width);
933 success = success && meta->findInt32(kKeyHeight, &height);
934 success = success && meta->findInt32(kKeySampleRate, &frameRate);
935 success = success && meta->findInt32(kKeyBitRate, &bitRate);
936 success = success && meta->findInt32(kKeyStride, &stride);
937 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
938 CHECK(success);
939 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700940
941 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700942 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700943 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700944 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700945 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700946 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700947 compressionFormat = OMX_VIDEO_CodingH263;
948 } else {
949 LOGE("Not a supported video mime type: %s", mime);
950 CHECK(!"Should not be here. Not a supported video mime type.");
951 }
952
James Dongafd97e82010-08-03 17:19:23 -0700953 OMX_COLOR_FORMATTYPE colorFormat;
954 CHECK_EQ(OK, findTargetColorFormat(meta, &colorFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -0700955
James Dongb00e2462010-04-26 17:48:26 -0700956 status_t err;
957 OMX_PARAM_PORTDEFINITIONTYPE def;
958 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
959
960 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700961 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700962 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700963 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700964
James Dongb00e2462010-04-26 17:48:26 -0700965 InitOMXParams(&def);
966 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700967
James Dongb00e2462010-04-26 17:48:26 -0700968 err = mOMX->getParameter(
969 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
970 CHECK_EQ(err, OK);
971
James Dong1244eab2010-06-08 11:58:53 -0700972 def.nBufferSize = getFrameSize(colorFormat,
973 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700974
975 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
976
977 video_def->nFrameWidth = width;
978 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700979 video_def->nStride = stride;
980 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700981 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700982 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
983 video_def->eColorFormat = colorFormat;
984
James Dongb00e2462010-04-26 17:48:26 -0700985 err = mOMX->setParameter(
986 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
987 CHECK_EQ(err, OK);
988
989 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700990 CHECK_EQ(setVideoPortFormatType(
991 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
992 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700993 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700994 def.nPortIndex = kPortIndexOutput;
995
James Dongb00e2462010-04-26 17:48:26 -0700996 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700997 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
998
999 CHECK_EQ(err, OK);
1000 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1001
1002 video_def->nFrameWidth = width;
1003 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -07001004 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -07001005 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -07001006 video_def->eCompressionFormat = compressionFormat;
1007 video_def->eColorFormat = OMX_COLOR_FormatUnused;
James Dong90862e22010-08-26 19:12:59 -07001008 if (mQuirks & kRequiresLargerEncoderOutputBuffer) {
1009 // Increases the output buffer size
1010 def.nBufferSize = ((def.nBufferSize * 3) >> 1);
1011 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001012
Andreas Huber784202e2009-10-15 13:46:54 -07001013 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001014 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1015 CHECK_EQ(err, OK);
1016
James Dongb00e2462010-04-26 17:48:26 -07001017 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -07001018 switch (compressionFormat) {
1019 case OMX_VIDEO_CodingMPEG4:
1020 {
James Dong1244eab2010-06-08 11:58:53 -07001021 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001022 break;
1023 }
1024
1025 case OMX_VIDEO_CodingH263:
James Dongc0ab2a62010-06-29 16:29:19 -07001026 CHECK_EQ(setupH263EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001027 break;
1028
Andreas Huberea6a38c2009-11-16 15:43:38 -08001029 case OMX_VIDEO_CodingAVC:
1030 {
James Dong1244eab2010-06-08 11:58:53 -07001031 CHECK_EQ(setupAVCEncoderParameters(meta), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001032 break;
1033 }
1034
Andreas Huberb482ce82009-10-29 12:02:48 -07001035 default:
1036 CHECK(!"Support for this compressionFormat to be implemented.");
1037 break;
1038 }
1039}
1040
James Dong1244eab2010-06-08 11:58:53 -07001041static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
1042 if (iFramesInterval < 0) {
1043 return 0xFFFFFFFF;
1044 } else if (iFramesInterval == 0) {
1045 return 0;
1046 }
1047 OMX_U32 ret = frameRate * iFramesInterval;
1048 CHECK(ret > 1);
1049 return ret;
1050}
1051
James Dongc0ab2a62010-06-29 16:29:19 -07001052status_t OMXCodec::setupErrorCorrectionParameters() {
1053 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
1054 InitOMXParams(&errorCorrectionType);
1055 errorCorrectionType.nPortIndex = kPortIndexOutput;
1056
1057 status_t err = mOMX->getParameter(
1058 mNode, OMX_IndexParamVideoErrorCorrection,
1059 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001060 if (err != OK) {
1061 LOGW("Error correction param query is not supported");
1062 return OK; // Optional feature. Ignore this failure
1063 }
James Dongc0ab2a62010-06-29 16:29:19 -07001064
1065 errorCorrectionType.bEnableHEC = OMX_FALSE;
1066 errorCorrectionType.bEnableResync = OMX_TRUE;
1067 errorCorrectionType.nResynchMarkerSpacing = 256;
1068 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
1069 errorCorrectionType.bEnableRVLC = OMX_FALSE;
1070
1071 err = mOMX->setParameter(
1072 mNode, OMX_IndexParamVideoErrorCorrection,
1073 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001074 if (err != OK) {
1075 LOGW("Error correction param configuration is not supported");
1076 }
1077
1078 // Optional feature. Ignore the failure.
James Dongc0ab2a62010-06-29 16:29:19 -07001079 return OK;
1080}
1081
1082status_t OMXCodec::setupBitRate(int32_t bitRate) {
1083 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
1084 InitOMXParams(&bitrateType);
1085 bitrateType.nPortIndex = kPortIndexOutput;
1086
1087 status_t err = mOMX->getParameter(
1088 mNode, OMX_IndexParamVideoBitrate,
1089 &bitrateType, sizeof(bitrateType));
1090 CHECK_EQ(err, OK);
1091
1092 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
1093 bitrateType.nTargetBitrate = bitRate;
1094
1095 err = mOMX->setParameter(
1096 mNode, OMX_IndexParamVideoBitrate,
1097 &bitrateType, sizeof(bitrateType));
1098 CHECK_EQ(err, OK);
1099 return OK;
1100}
1101
James Dong81c929a2010-07-01 15:02:14 -07001102status_t OMXCodec::getVideoProfileLevel(
1103 const sp<MetaData>& meta,
1104 const CodecProfileLevel& defaultProfileLevel,
1105 CodecProfileLevel &profileLevel) {
1106 CODEC_LOGV("Default profile: %ld, level %ld",
1107 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
1108
1109 // Are the default profile and level overwriten?
1110 int32_t profile, level;
1111 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
1112 profile = defaultProfileLevel.mProfile;
1113 }
1114 if (!meta->findInt32(kKeyVideoLevel, &level)) {
1115 level = defaultProfileLevel.mLevel;
1116 }
1117 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
1118
1119 // Are the target profile and level supported by the encoder?
1120 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
1121 InitOMXParams(&param);
1122 param.nPortIndex = kPortIndexOutput;
1123 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
1124 status_t err = mOMX->getParameter(
1125 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
1126 &param, sizeof(param));
1127
James Dongdfb89912010-09-15 21:07:52 -07001128 if (err != OK) break;
James Dong81c929a2010-07-01 15:02:14 -07001129
1130 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
1131 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -07001132 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -07001133 supportedProfile, supportedLevel);
1134
1135 if (profile == supportedProfile &&
James Dongdfb89912010-09-15 21:07:52 -07001136 level <= supportedLevel) {
1137 // We can further check whether the level is a valid
1138 // value; but we will leave that to the omx encoder component
1139 // via OMX_SetParameter call.
James Dong81c929a2010-07-01 15:02:14 -07001140 profileLevel.mProfile = profile;
1141 profileLevel.mLevel = level;
1142 return OK;
1143 }
1144 }
1145
1146 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
1147 profile, level);
1148 return BAD_VALUE;
1149}
1150
James Dongc0ab2a62010-06-29 16:29:19 -07001151status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
1152 int32_t iFramesInterval, frameRate, bitRate;
1153 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1154 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1155 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1156 CHECK(success);
1157 OMX_VIDEO_PARAM_H263TYPE h263type;
1158 InitOMXParams(&h263type);
1159 h263type.nPortIndex = kPortIndexOutput;
1160
1161 status_t err = mOMX->getParameter(
1162 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1163 CHECK_EQ(err, OK);
1164
1165 h263type.nAllowedPictureTypes =
1166 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1167
1168 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1169 if (h263type.nPFrames == 0) {
1170 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1171 }
1172 h263type.nBFrames = 0;
1173
James Dong81c929a2010-07-01 15:02:14 -07001174 // Check profile and level parameters
1175 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001176 defaultProfileLevel.mProfile = h263type.eProfile;
1177 defaultProfileLevel.mLevel = h263type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001178 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1179 if (err != OK) return err;
1180 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1181 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001182
1183 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1184 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1185 h263type.nPictureHeaderRepetition = 0;
1186 h263type.nGOBHeaderInterval = 0;
1187
1188 err = mOMX->setParameter(
1189 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1190 CHECK_EQ(err, OK);
1191
1192 CHECK_EQ(setupBitRate(bitRate), OK);
1193 CHECK_EQ(setupErrorCorrectionParameters(), OK);
1194
1195 return OK;
1196}
1197
James Dong1244eab2010-06-08 11:58:53 -07001198status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1199 int32_t iFramesInterval, frameRate, bitRate;
1200 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1201 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1202 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1203 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001204 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1205 InitOMXParams(&mpeg4type);
1206 mpeg4type.nPortIndex = kPortIndexOutput;
1207
1208 status_t err = mOMX->getParameter(
1209 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1210 CHECK_EQ(err, OK);
1211
1212 mpeg4type.nSliceHeaderSpacing = 0;
1213 mpeg4type.bSVH = OMX_FALSE;
1214 mpeg4type.bGov = OMX_FALSE;
1215
1216 mpeg4type.nAllowedPictureTypes =
1217 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1218
James Dong1244eab2010-06-08 11:58:53 -07001219 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1220 if (mpeg4type.nPFrames == 0) {
1221 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1222 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001223 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001224 mpeg4type.nIDCVLCThreshold = 0;
1225 mpeg4type.bACPred = OMX_TRUE;
1226 mpeg4type.nMaxPacketSize = 256;
1227 mpeg4type.nTimeIncRes = 1000;
1228 mpeg4type.nHeaderExtension = 0;
1229 mpeg4type.bReversibleVLC = OMX_FALSE;
1230
James Dong81c929a2010-07-01 15:02:14 -07001231 // Check profile and level parameters
1232 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001233 defaultProfileLevel.mProfile = mpeg4type.eProfile;
1234 defaultProfileLevel.mLevel = mpeg4type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001235 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1236 if (err != OK) return err;
1237 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1238 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001239
1240 err = mOMX->setParameter(
1241 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1242 CHECK_EQ(err, OK);
1243
James Dongc0ab2a62010-06-29 16:29:19 -07001244 CHECK_EQ(setupBitRate(bitRate), OK);
1245 CHECK_EQ(setupErrorCorrectionParameters(), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001246
1247 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001248}
1249
James Dong1244eab2010-06-08 11:58:53 -07001250status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1251 int32_t iFramesInterval, frameRate, bitRate;
1252 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1253 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1254 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1255 CHECK(success);
1256
Andreas Huberea6a38c2009-11-16 15:43:38 -08001257 OMX_VIDEO_PARAM_AVCTYPE h264type;
1258 InitOMXParams(&h264type);
1259 h264type.nPortIndex = kPortIndexOutput;
1260
1261 status_t err = mOMX->getParameter(
1262 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1263 CHECK_EQ(err, OK);
1264
1265 h264type.nAllowedPictureTypes =
1266 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1267
1268 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001269 h264type.nBFrames = 0; // No B frames support yet
1270 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1271 if (h264type.nPFrames == 0) {
1272 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1273 }
James Dong81c929a2010-07-01 15:02:14 -07001274
1275 // Check profile and level parameters
1276 CodecProfileLevel defaultProfileLevel, profileLevel;
1277 defaultProfileLevel.mProfile = h264type.eProfile;
1278 defaultProfileLevel.mLevel = h264type.eLevel;
1279 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1280 if (err != OK) return err;
1281 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1282 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1283
1284 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1285 h264type.bUseHadamard = OMX_TRUE;
1286 h264type.nRefFrames = 1;
1287 h264type.nRefIdx10ActiveMinus1 = 0;
1288 h264type.nRefIdx11ActiveMinus1 = 0;
1289 h264type.bEntropyCodingCABAC = OMX_FALSE;
1290 h264type.bWeightedPPrediction = OMX_FALSE;
1291 h264type.bconstIpred = OMX_FALSE;
1292 h264type.bDirect8x8Inference = OMX_FALSE;
1293 h264type.bDirectSpatialTemporal = OMX_FALSE;
1294 h264type.nCabacInitIdc = 0;
1295 }
1296
1297 if (h264type.nBFrames != 0) {
1298 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1299 }
1300
Andreas Huberea6a38c2009-11-16 15:43:38 -08001301 h264type.bEnableUEP = OMX_FALSE;
1302 h264type.bEnableFMO = OMX_FALSE;
1303 h264type.bEnableASO = OMX_FALSE;
1304 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001305 h264type.bFrameMBsOnly = OMX_TRUE;
1306 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001307 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1308
pgudadhe9c305322010-07-26 13:59:29 -07001309 if (!strcasecmp("OMX.Nvidia.h264.encoder", mComponentName)) {
1310 h264type.eLevel = OMX_VIDEO_AVCLevelMax;
1311 }
1312
Andreas Huberea6a38c2009-11-16 15:43:38 -08001313 err = mOMX->setParameter(
1314 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1315 CHECK_EQ(err, OK);
1316
James Dongc0ab2a62010-06-29 16:29:19 -07001317 CHECK_EQ(setupBitRate(bitRate), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001318
1319 return OK;
1320}
1321
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001322status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001323 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001324 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001325
Andreas Huberbe06d262009-08-14 14:37:10 -07001326 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001327 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001328 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001329 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001330 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001331 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001332 compressionFormat = OMX_VIDEO_CodingH263;
1333 } else {
1334 LOGE("Not a supported video mime type: %s", mime);
1335 CHECK(!"Should not be here. Not a supported video mime type.");
1336 }
1337
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001338 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001339 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1340
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001341 if (err != OK) {
1342 return err;
1343 }
1344
Andreas Huberbe06d262009-08-14 14:37:10 -07001345#if 1
1346 {
1347 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001348 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001349 format.nPortIndex = kPortIndexOutput;
1350 format.nIndex = 0;
1351
Andreas Huber784202e2009-10-15 13:46:54 -07001352 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001353 mNode, OMX_IndexParamVideoPortFormat,
1354 &format, sizeof(format));
1355 CHECK_EQ(err, OK);
1356 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1357
1358 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1359
1360 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1361 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1362 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1363 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1364
Andreas Huber784202e2009-10-15 13:46:54 -07001365 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001366 mNode, OMX_IndexParamVideoPortFormat,
1367 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001368
1369 if (err != OK) {
1370 return err;
1371 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001372 }
1373#endif
1374
1375 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001376 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001377 def.nPortIndex = kPortIndexInput;
1378
Andreas Huber4c483422009-09-02 16:05:36 -07001379 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1380
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001381 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001382 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1383
1384 CHECK_EQ(err, OK);
1385
1386#if 1
1387 // XXX Need a (much) better heuristic to compute input buffer sizes.
1388 const size_t X = 64 * 1024;
1389 if (def.nBufferSize < X) {
1390 def.nBufferSize = X;
1391 }
1392#endif
1393
1394 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1395
1396 video_def->nFrameWidth = width;
1397 video_def->nFrameHeight = height;
1398
Andreas Huberb482ce82009-10-29 12:02:48 -07001399 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001400 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1401
Andreas Huber784202e2009-10-15 13:46:54 -07001402 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001403 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001404
1405 if (err != OK) {
1406 return err;
1407 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001408
1409 ////////////////////////////////////////////////////////////////////////////
1410
Andreas Huber4c483422009-09-02 16:05:36 -07001411 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001412 def.nPortIndex = kPortIndexOutput;
1413
Andreas Huber784202e2009-10-15 13:46:54 -07001414 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001415 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1416 CHECK_EQ(err, OK);
1417 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1418
1419#if 0
1420 def.nBufferSize =
1421 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1422#endif
1423
1424 video_def->nFrameWidth = width;
1425 video_def->nFrameHeight = height;
1426
Andreas Huber784202e2009-10-15 13:46:54 -07001427 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001428 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001429
1430 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001431}
1432
Andreas Huberbe06d262009-08-14 14:37:10 -07001433OMXCodec::OMXCodec(
1434 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001435 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001436 const char *mime,
1437 const char *componentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001438 const sp<MediaSource> &source,
1439 const sp<ANativeWindow> &nativeWindow)
Andreas Huberbe06d262009-08-14 14:37:10 -07001440 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001441 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001442 mNode(node),
1443 mQuirks(quirks),
1444 mIsEncoder(isEncoder),
1445 mMIME(strdup(mime)),
1446 mComponentName(strdup(componentName)),
1447 mSource(source),
1448 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001449 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001450 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001451 mSignalledEOS(false),
1452 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001453 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001454 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001455 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1456 mTargetTimeUs(-1),
James Dong53d4e0d2010-07-21 14:51:35 -07001457 mSkipTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001458 mLeftOverBuffer(NULL),
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001459 mPaused(false),
1460 mNativeWindow(nativeWindow) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001461 mPortStatus[kPortIndexInput] = ENABLED;
1462 mPortStatus[kPortIndexOutput] = ENABLED;
1463
Andreas Huber4c483422009-09-02 16:05:36 -07001464 setComponentRole();
1465}
1466
Andreas Hubere6c40962009-09-10 14:13:30 -07001467// static
1468void OMXCodec::setComponentRole(
1469 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1470 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001471 struct MimeToRole {
1472 const char *mime;
1473 const char *decoderRole;
1474 const char *encoderRole;
1475 };
1476
1477 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001478 { MEDIA_MIMETYPE_AUDIO_MPEG,
1479 "audio_decoder.mp3", "audio_encoder.mp3" },
1480 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1481 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1482 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1483 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1484 { MEDIA_MIMETYPE_AUDIO_AAC,
1485 "audio_decoder.aac", "audio_encoder.aac" },
1486 { MEDIA_MIMETYPE_VIDEO_AVC,
1487 "video_decoder.avc", "video_encoder.avc" },
1488 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1489 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1490 { MEDIA_MIMETYPE_VIDEO_H263,
1491 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001492 };
1493
1494 static const size_t kNumMimeToRole =
1495 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1496
1497 size_t i;
1498 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001499 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001500 break;
1501 }
1502 }
1503
1504 if (i == kNumMimeToRole) {
1505 return;
1506 }
1507
1508 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001509 isEncoder ? kMimeToRole[i].encoderRole
1510 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001511
1512 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001513 OMX_PARAM_COMPONENTROLETYPE roleParams;
1514 InitOMXParams(&roleParams);
1515
1516 strncpy((char *)roleParams.cRole,
1517 role, OMX_MAX_STRINGNAME_SIZE - 1);
1518
1519 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1520
Andreas Huber784202e2009-10-15 13:46:54 -07001521 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001522 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001523 &roleParams, sizeof(roleParams));
1524
1525 if (err != OK) {
1526 LOGW("Failed to set standard component role '%s'.", role);
1527 }
1528 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001529}
1530
Andreas Hubere6c40962009-09-10 14:13:30 -07001531void OMXCodec::setComponentRole() {
1532 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1533}
1534
Andreas Huberbe06d262009-08-14 14:37:10 -07001535OMXCodec::~OMXCodec() {
Andreas Huberf98197a2010-09-17 11:49:39 -07001536 mSource.clear();
1537
Andreas Huber4f5e6022009-08-19 09:29:34 -07001538 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001539
Andreas Huber784202e2009-10-15 13:46:54 -07001540 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001541 CHECK_EQ(err, OK);
1542
1543 mNode = NULL;
1544 setState(DEAD);
1545
1546 clearCodecSpecificData();
1547
1548 free(mComponentName);
1549 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001550
Andreas Huberbe06d262009-08-14 14:37:10 -07001551 free(mMIME);
1552 mMIME = NULL;
1553}
1554
1555status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001556 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001557
1558 CHECK_EQ(mState, LOADED);
1559
1560 status_t err;
1561 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001562 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001563 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001564 setState(LOADED_TO_IDLE);
1565 }
1566
1567 err = allocateBuffers();
1568 CHECK_EQ(err, OK);
1569
1570 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001571 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001572 CHECK_EQ(err, OK);
1573
1574 setState(LOADED_TO_IDLE);
1575 }
1576
1577 while (mState != EXECUTING && mState != ERROR) {
1578 mAsyncCompletion.wait(mLock);
1579 }
1580
1581 return mState == ERROR ? UNKNOWN_ERROR : OK;
1582}
1583
1584// static
1585bool OMXCodec::isIntermediateState(State state) {
1586 return state == LOADED_TO_IDLE
1587 || state == IDLE_TO_EXECUTING
1588 || state == EXECUTING_TO_IDLE
1589 || state == IDLE_TO_LOADED
1590 || state == RECONFIGURING;
1591}
1592
1593status_t OMXCodec::allocateBuffers() {
1594 status_t err = allocateBuffersOnPort(kPortIndexInput);
1595
1596 if (err != OK) {
1597 return err;
1598 }
1599
1600 return allocateBuffersOnPort(kPortIndexOutput);
1601}
1602
1603status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
Jamie Gennisdbfb32e2010-10-20 15:53:59 -07001604 if (mNativeWindow != NULL && portIndex == kPortIndexOutput) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001605 return allocateOutputBuffersFromNativeWindow();
1606 }
1607
Andreas Huberbe06d262009-08-14 14:37:10 -07001608 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001609 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001610 def.nPortIndex = portIndex;
1611
Andreas Huber784202e2009-10-15 13:46:54 -07001612 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001613 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1614
1615 if (err != OK) {
1616 return err;
1617 }
1618
Andreas Huber57648e42010-08-04 10:14:30 -07001619 CODEC_LOGI("allocating %lu buffers of size %lu on %s port",
1620 def.nBufferCountActual, def.nBufferSize,
1621 portIndex == kPortIndexInput ? "input" : "output");
1622
Andreas Huber5c0a9132009-08-20 11:16:40 -07001623 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001624 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001625
Andreas Huberbe06d262009-08-14 14:37:10 -07001626 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001627 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001628 CHECK(mem.get() != NULL);
1629
Andreas Huberc712b9f2010-01-20 15:05:46 -08001630 BufferInfo info;
1631 info.mData = NULL;
1632 info.mSize = def.nBufferSize;
1633
Andreas Huberbe06d262009-08-14 14:37:10 -07001634 IOMX::buffer_id buffer;
1635 if (portIndex == kPortIndexInput
1636 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001637 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001638 mem.clear();
1639
Andreas Huberf1fe0642010-01-15 15:28:19 -08001640 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001641 mNode, portIndex, def.nBufferSize, &buffer,
1642 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001643 } else {
1644 err = mOMX->allocateBufferWithBackup(
1645 mNode, portIndex, mem, &buffer);
1646 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001647 } else if (portIndex == kPortIndexOutput
1648 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001649 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001650 mem.clear();
1651
Andreas Huberf1fe0642010-01-15 15:28:19 -08001652 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001653 mNode, portIndex, def.nBufferSize, &buffer,
1654 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001655 } else {
1656 err = mOMX->allocateBufferWithBackup(
1657 mNode, portIndex, mem, &buffer);
1658 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001659 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001660 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001661 }
1662
1663 if (err != OK) {
1664 LOGE("allocate_buffer_with_backup failed");
1665 return err;
1666 }
1667
Andreas Huberc712b9f2010-01-20 15:05:46 -08001668 if (mem != NULL) {
1669 info.mData = mem->pointer();
1670 }
1671
Andreas Huberbe06d262009-08-14 14:37:10 -07001672 info.mBuffer = buffer;
1673 info.mOwnedByComponent = false;
Jamie Gennisdbfb32e2010-10-20 15:53:59 -07001674 info.mOwnedByNativeWindow = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07001675 info.mMem = mem;
1676 info.mMediaBuffer = NULL;
1677
1678 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001679 if (!(mOMXLivesLocally
1680 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1681 && (mQuirks & kDefersOutputBufferAllocation))) {
1682 // If the node does not fill in the buffer ptr at this time,
1683 // we will defer creating the MediaBuffer until receiving
1684 // the first FILL_BUFFER_DONE notification instead.
1685 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1686 info.mMediaBuffer->setObserver(this);
1687 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001688 }
1689
1690 mPortBuffers[portIndex].push(info);
1691
Andreas Huber4c483422009-09-02 16:05:36 -07001692 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001693 portIndex == kPortIndexInput ? "input" : "output");
1694 }
1695
Andreas Huber2ea14e22009-12-16 09:30:55 -08001696 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001697
1698 return OK;
1699}
1700
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001701status_t OMXCodec::allocateOutputBuffersFromNativeWindow() {
1702 // Get the number of buffers needed.
1703 OMX_PARAM_PORTDEFINITIONTYPE def;
1704 InitOMXParams(&def);
1705 def.nPortIndex = kPortIndexOutput;
1706
1707 status_t err = mOMX->getParameter(
1708 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1709 if (err != OK) {
1710 return err;
1711 }
1712
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001713 err = native_window_set_buffers_geometry(
1714 mNativeWindow.get(),
1715 def.format.video.nFrameWidth,
1716 def.format.video.nFrameHeight,
Jamie Gennis044ace62010-10-29 15:19:29 -07001717 def.format.video.eColorFormat);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001718
1719 if (err != 0) {
1720 LOGE("native_window_set_buffers_geometry failed: %s (%d)",
1721 strerror(-err), -err);
1722 return err;
1723 }
1724
1725 // Increase the buffer count by one to allow for the ANativeWindow to hold
1726 // on to one of the buffers.
1727 def.nBufferCountActual++;
1728 err = mOMX->setParameter(
1729 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1730 if (err != OK) {
1731 return err;
1732 }
1733
1734 // Set up the native window.
1735 // XXX TODO: Get the gralloc usage flags from the OMX plugin!
1736 err = native_window_set_usage(
1737 mNativeWindow.get(), GRALLOC_USAGE_HW_TEXTURE);
1738 if (err != 0) {
1739 LOGE("native_window_set_usage failed: %s (%d)", strerror(-err), -err);
1740 return err;
1741 }
1742
1743 err = native_window_set_buffer_count(
1744 mNativeWindow.get(), def.nBufferCountActual);
1745 if (err != 0) {
1746 LOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
1747 -err);
1748 return err;
1749 }
1750
1751 // XXX TODO: Do something so the ANativeWindow knows that we'll need to get
1752 // the same set of buffers.
1753
1754 CODEC_LOGI("allocating %lu buffers from a native window of size %lu on "
1755 "output port", def.nBufferCountActual, def.nBufferSize);
1756
1757 // Dequeue buffers and send them to OMX
1758 OMX_U32 i;
1759 for (i = 0; i < def.nBufferCountActual; i++) {
1760 android_native_buffer_t* buf;
1761 err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf);
1762 if (err != 0) {
1763 LOGE("dequeueBuffer failed: %s (%d)", strerror(-err), -err);
1764 break;
1765 }
1766
1767 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false));
1768 IOMX::buffer_id bufferId;
1769 err = mOMX->useGraphicBuffer(mNode, kPortIndexOutput, graphicBuffer,
1770 &bufferId);
1771 if (err != 0) {
1772 break;
1773 }
1774
1775 CODEC_LOGV("registered graphic buffer with ID %p (pointer = %p)",
1776 bufferId, graphicBuffer.get());
1777
1778 BufferInfo info;
1779 info.mData = NULL;
1780 info.mSize = def.nBufferSize;
1781 info.mBuffer = bufferId;
1782 info.mOwnedByComponent = false;
1783 info.mOwnedByNativeWindow = false;
1784 info.mMem = NULL;
1785 info.mMediaBuffer = new MediaBuffer(graphicBuffer);
1786 info.mMediaBuffer->setObserver(this);
1787
1788 mPortBuffers[kPortIndexOutput].push(info);
1789 }
1790
1791 OMX_U32 cancelStart;
1792 OMX_U32 cancelEnd;
1793
1794 if (err != 0) {
1795 // If an error occurred while dequeuing we need to cancel any buffers
1796 // that were dequeued.
1797 cancelStart = 0;
1798 cancelEnd = i;
1799 } else {
1800 // Return the last two buffers to the native window.
1801 // XXX TODO: The number of buffers the native window owns should probably be
1802 // queried from it when we put the native window in fixed buffer pool mode
1803 // (which needs to be implemented). Currently it's hard-coded to 2.
1804 cancelStart = def.nBufferCountActual - 2;
1805 cancelEnd = def.nBufferCountActual;
1806 }
1807
1808 for (OMX_U32 i = cancelStart; i < cancelEnd; i++) {
1809 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(i);
1810 cancelBufferToNativeWindow(info);
1811 }
1812
1813 return err;
1814}
1815
1816status_t OMXCodec::cancelBufferToNativeWindow(BufferInfo *info) {
1817 CHECK(!info->mOwnedByNativeWindow);
1818 CODEC_LOGV("Calling cancelBuffer on buffer %p", info->mBuffer);
1819 int err = mNativeWindow->cancelBuffer(
1820 mNativeWindow.get(), info->mMediaBuffer->graphicBuffer().get());
1821 if (err != 0) {
1822 CODEC_LOGE("cancelBuffer failed w/ error 0x%08x", err);
1823
1824 setState(ERROR);
1825 return err;
1826 }
1827 info->mOwnedByNativeWindow = true;
1828 return OK;
1829}
1830
1831OMXCodec::BufferInfo* OMXCodec::dequeueBufferFromNativeWindow() {
1832 // Dequeue the next buffer from the native window.
1833 android_native_buffer_t* buf;
1834 int err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf);
1835 if (err != 0) {
1836 CODEC_LOGE("dequeueBuffer failed w/ error 0x%08x", err);
1837
1838 setState(ERROR);
1839 return 0;
1840 }
1841
1842 // Determine which buffer we just dequeued.
1843 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1844 BufferInfo *bufInfo = 0;
1845 for (size_t i = 0; i < buffers->size(); i++) {
1846 sp<GraphicBuffer> graphicBuffer = buffers->itemAt(i).
1847 mMediaBuffer->graphicBuffer();
1848 if (graphicBuffer->handle == buf->handle) {
1849 bufInfo = &buffers->editItemAt(i);
1850 break;
1851 }
1852 }
1853
1854 if (bufInfo == 0) {
1855 CODEC_LOGE("dequeued unrecognized buffer: %p", buf);
1856
1857 setState(ERROR);
1858 return 0;
1859 }
1860
1861 // The native window no longer owns the buffer.
1862 CHECK(bufInfo->mOwnedByNativeWindow);
1863 bufInfo->mOwnedByNativeWindow = false;
1864
1865 return bufInfo;
1866}
1867
Andreas Huberbe06d262009-08-14 14:37:10 -07001868void OMXCodec::on_message(const omx_message &msg) {
1869 Mutex::Autolock autoLock(mLock);
1870
1871 switch (msg.type) {
1872 case omx_message::EVENT:
1873 {
1874 onEvent(
1875 msg.u.event_data.event, msg.u.event_data.data1,
1876 msg.u.event_data.data2);
1877
1878 break;
1879 }
1880
1881 case omx_message::EMPTY_BUFFER_DONE:
1882 {
1883 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1884
Andreas Huber4c483422009-09-02 16:05:36 -07001885 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001886
1887 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1888 size_t i = 0;
1889 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1890 ++i;
1891 }
1892
1893 CHECK(i < buffers->size());
1894 if (!(*buffers)[i].mOwnedByComponent) {
1895 LOGW("We already own input buffer %p, yet received "
1896 "an EMPTY_BUFFER_DONE.", buffer);
1897 }
1898
1899 buffers->editItemAt(i).mOwnedByComponent = false;
1900
1901 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001902 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001903
1904 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001905 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001906 CHECK_EQ(err, OK);
1907
1908 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001909 } else if (mState != ERROR
1910 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001911 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1912 drainInputBuffer(&buffers->editItemAt(i));
1913 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001914 break;
1915 }
1916
1917 case omx_message::FILL_BUFFER_DONE:
1918 {
1919 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1920 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1921
Andreas Huber2ea14e22009-12-16 09:30:55 -08001922 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001923 buffer,
1924 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001925 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001926 msg.u.extended_buffer_data.timestamp,
1927 msg.u.extended_buffer_data.timestamp / 1E6);
1928
1929 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1930 size_t i = 0;
1931 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1932 ++i;
1933 }
1934
1935 CHECK(i < buffers->size());
1936 BufferInfo *info = &buffers->editItemAt(i);
1937
1938 if (!info->mOwnedByComponent) {
1939 LOGW("We already own output buffer %p, yet received "
1940 "a FILL_BUFFER_DONE.", buffer);
1941 }
1942
1943 info->mOwnedByComponent = false;
1944
1945 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001946 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001947
1948 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001949 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001950 CHECK_EQ(err, OK);
1951
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001952 // Cancel the buffer if it belongs to an ANativeWindow.
1953 if (info->mMediaBuffer != NULL) {
1954 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer();
1955 if (!info->mOwnedByNativeWindow && graphicBuffer != 0) {
1956 cancelBufferToNativeWindow(info);
1957 // Ignore any errors
1958 }
1959 }
1960
Andreas Huberbe06d262009-08-14 14:37:10 -07001961 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001962#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001963 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1964 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001965 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001966 mNoMoreOutputData = true;
1967 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001968#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001969 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1970 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001971
Andreas Huber52733b82010-01-25 10:41:35 -08001972 if (info->mMediaBuffer == NULL) {
1973 CHECK(mOMXLivesLocally);
1974 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1975 CHECK(mQuirks & kDefersOutputBufferAllocation);
1976
1977 // The qcom video decoders on Nexus don't actually allocate
1978 // output buffer memory on a call to OMX_AllocateBuffer
1979 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1980 // structure is only filled in later.
1981
1982 info->mMediaBuffer = new MediaBuffer(
1983 msg.u.extended_buffer_data.data_ptr,
1984 info->mSize);
1985 info->mMediaBuffer->setObserver(this);
1986 }
1987
Andreas Huberbe06d262009-08-14 14:37:10 -07001988 MediaBuffer *buffer = info->mMediaBuffer;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001989 bool isGraphicBuffer = buffer->graphicBuffer() != NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -07001990
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001991 if (!isGraphicBuffer
1992 && msg.u.extended_buffer_data.range_offset
Andreas Huberf88f8442010-08-10 11:18:36 -07001993 + msg.u.extended_buffer_data.range_length
1994 > buffer->size()) {
1995 CODEC_LOGE(
1996 "Codec lied about its buffer size requirements, "
1997 "sending a buffer larger than the originally "
1998 "advertised size in FILL_BUFFER_DONE!");
1999 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002000 buffer->set_range(
2001 msg.u.extended_buffer_data.range_offset,
2002 msg.u.extended_buffer_data.range_length);
2003
2004 buffer->meta_data()->clear();
2005
Andreas Huberfa8de752009-10-08 10:07:49 -07002006 buffer->meta_data()->setInt64(
2007 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07002008
2009 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
2010 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
2011 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08002012 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
2013 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
2014 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002015
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002016 if (isGraphicBuffer || mQuirks & kOutputBuffersAreUnreadable) {
Andreas Huber1e194162010-10-06 16:43:57 -07002017 buffer->meta_data()->setInt32(kKeyIsUnreadable, true);
2018 }
2019
Andreas Huberbe06d262009-08-14 14:37:10 -07002020 buffer->meta_data()->setPointer(
2021 kKeyPlatformPrivate,
2022 msg.u.extended_buffer_data.platform_private);
2023
2024 buffer->meta_data()->setPointer(
2025 kKeyBufferID,
2026 msg.u.extended_buffer_data.buffer);
2027
Andreas Huber2ea14e22009-12-16 09:30:55 -08002028 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
2029 CODEC_LOGV("No more output data.");
2030 mNoMoreOutputData = true;
2031 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002032
2033 if (mTargetTimeUs >= 0) {
2034 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
2035
2036 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
2037 CODEC_LOGV(
2038 "skipping output buffer at timestamp %lld us",
2039 msg.u.extended_buffer_data.timestamp);
2040
2041 fillOutputBuffer(info);
2042 break;
2043 }
2044
2045 CODEC_LOGV(
2046 "returning output buffer at target timestamp "
2047 "%lld us",
2048 msg.u.extended_buffer_data.timestamp);
2049
2050 mTargetTimeUs = -1;
2051 }
2052
2053 mFilledBuffers.push_back(i);
2054 mBufferFilled.signal();
Andreas Huberbe06d262009-08-14 14:37:10 -07002055 }
2056
2057 break;
2058 }
2059
2060 default:
2061 {
2062 CHECK(!"should not be here.");
2063 break;
2064 }
2065 }
2066}
2067
2068void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
2069 switch (event) {
2070 case OMX_EventCmdComplete:
2071 {
2072 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
2073 break;
2074 }
2075
2076 case OMX_EventError:
2077 {
Andreas Huberaf0a1882010-09-21 15:08:52 -07002078 CODEC_LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07002079
2080 setState(ERROR);
2081 break;
2082 }
2083
2084 case OMX_EventPortSettingsChanged:
2085 {
Andreas Huber08478d12010-10-07 13:48:34 -07002086 CODEC_LOGV("OMX_EventPortSettingsChanged(port=%ld, data2=0x%08lx)",
2087 data1, data2);
2088
Andreas Huber29c03c62010-08-30 16:23:15 -07002089 if (data2 == 0 || data2 == OMX_IndexParamPortDefinition) {
2090 onPortSettingsChanged(data1);
Andreas Huber08478d12010-10-07 13:48:34 -07002091 } else if (data1 == kPortIndexOutput
2092 && data2 == OMX_IndexConfigCommonOutputCrop) {
2093
2094 OMX_CONFIG_RECTTYPE rect;
2095 rect.nPortIndex = kPortIndexOutput;
2096 InitOMXParams(&rect);
2097
2098 status_t err =
2099 mOMX->getConfig(
2100 mNode, OMX_IndexConfigCommonOutputCrop,
2101 &rect, sizeof(rect));
2102
2103 if (err == OK) {
2104 CODEC_LOGV(
2105 "output crop (%ld, %ld, %ld, %ld)",
2106 rect.nLeft, rect.nTop, rect.nWidth, rect.nHeight);
Andreas Huber5145d672010-11-01 16:01:05 -07002107
2108 if (mNativeWindow != NULL) {
2109 android_native_rect_t crop;
2110 crop.left = rect.nLeft;
2111 crop.top = rect.nTop;
2112 crop.right = crop.left + rect.nWidth - 1;
2113 crop.bottom = crop.top + rect.nHeight - 1;
2114
2115 CHECK_EQ(0, native_window_set_crop(
2116 mNativeWindow.get(), &crop));
2117 }
Andreas Huber08478d12010-10-07 13:48:34 -07002118 } else {
2119 CODEC_LOGE("getConfig(OMX_IndexConfigCommonOutputCrop) "
2120 "returned error 0x%08x", err);
2121 }
Andreas Huber29c03c62010-08-30 16:23:15 -07002122 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002123 break;
2124 }
2125
Andreas Huber2ea14e22009-12-16 09:30:55 -08002126#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07002127 case OMX_EventBufferFlag:
2128 {
Andreas Huber4c483422009-09-02 16:05:36 -07002129 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07002130
2131 if (data1 == kPortIndexOutput) {
2132 mNoMoreOutputData = true;
2133 }
2134 break;
2135 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002136#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07002137
2138 default:
2139 {
Andreas Huber4c483422009-09-02 16:05:36 -07002140 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07002141 break;
2142 }
2143 }
2144}
2145
Andreas Huberb1678602009-10-19 13:06:40 -07002146// Has the format changed in any way that the client would have to be aware of?
2147static bool formatHasNotablyChanged(
2148 const sp<MetaData> &from, const sp<MetaData> &to) {
2149 if (from.get() == NULL && to.get() == NULL) {
2150 return false;
2151 }
2152
Andreas Huberf68c1682009-10-21 14:01:30 -07002153 if ((from.get() == NULL && to.get() != NULL)
2154 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07002155 return true;
2156 }
2157
2158 const char *mime_from, *mime_to;
2159 CHECK(from->findCString(kKeyMIMEType, &mime_from));
2160 CHECK(to->findCString(kKeyMIMEType, &mime_to));
2161
2162 if (strcasecmp(mime_from, mime_to)) {
2163 return true;
2164 }
2165
2166 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
2167 int32_t colorFormat_from, colorFormat_to;
2168 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
2169 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
2170
2171 if (colorFormat_from != colorFormat_to) {
2172 return true;
2173 }
2174
2175 int32_t width_from, width_to;
2176 CHECK(from->findInt32(kKeyWidth, &width_from));
2177 CHECK(to->findInt32(kKeyWidth, &width_to));
2178
2179 if (width_from != width_to) {
2180 return true;
2181 }
2182
2183 int32_t height_from, height_to;
2184 CHECK(from->findInt32(kKeyHeight, &height_from));
2185 CHECK(to->findInt32(kKeyHeight, &height_to));
2186
2187 if (height_from != height_to) {
2188 return true;
2189 }
2190 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
2191 int32_t numChannels_from, numChannels_to;
2192 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
2193 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
2194
2195 if (numChannels_from != numChannels_to) {
2196 return true;
2197 }
2198
2199 int32_t sampleRate_from, sampleRate_to;
2200 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
2201 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
2202
2203 if (sampleRate_from != sampleRate_to) {
2204 return true;
2205 }
2206 }
2207
2208 return false;
2209}
2210
Andreas Huberbe06d262009-08-14 14:37:10 -07002211void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
2212 switch (cmd) {
2213 case OMX_CommandStateSet:
2214 {
2215 onStateChange((OMX_STATETYPE)data);
2216 break;
2217 }
2218
2219 case OMX_CommandPortDisable:
2220 {
2221 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07002222 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002223
2224 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2225 CHECK_EQ(mPortStatus[portIndex], DISABLING);
2226 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
2227
2228 mPortStatus[portIndex] = DISABLED;
2229
2230 if (mState == RECONFIGURING) {
2231 CHECK_EQ(portIndex, kPortIndexOutput);
2232
Andreas Huberb1678602009-10-19 13:06:40 -07002233 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07002234 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07002235
2236 // Don't notify clients if the output port settings change
2237 // wasn't of importance to them, i.e. it may be that just the
2238 // number of buffers has changed and nothing else.
2239 mOutputPortSettingsHaveChanged =
2240 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07002241
Andreas Huberbe06d262009-08-14 14:37:10 -07002242 enablePortAsync(portIndex);
2243
2244 status_t err = allocateBuffersOnPort(portIndex);
2245 CHECK_EQ(err, OK);
2246 }
2247 break;
2248 }
2249
2250 case OMX_CommandPortEnable:
2251 {
2252 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07002253 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002254
2255 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2256 CHECK_EQ(mPortStatus[portIndex], ENABLING);
2257
2258 mPortStatus[portIndex] = ENABLED;
2259
2260 if (mState == RECONFIGURING) {
2261 CHECK_EQ(portIndex, kPortIndexOutput);
2262
2263 setState(EXECUTING);
2264
2265 fillOutputBuffers();
2266 }
2267 break;
2268 }
2269
2270 case OMX_CommandFlush:
2271 {
2272 OMX_U32 portIndex = data;
2273
Andreas Huber4c483422009-09-02 16:05:36 -07002274 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002275
2276 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
2277 mPortStatus[portIndex] = ENABLED;
2278
2279 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
2280 mPortBuffers[portIndex].size());
2281
2282 if (mState == RECONFIGURING) {
2283 CHECK_EQ(portIndex, kPortIndexOutput);
2284
2285 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002286 } else if (mState == EXECUTING_TO_IDLE) {
2287 if (mPortStatus[kPortIndexInput] == ENABLED
2288 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002289 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002290 "transition from EXECUTING to IDLE.");
2291
2292 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2293 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2294
2295 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002296 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002297 CHECK_EQ(err, OK);
2298 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002299 } else {
2300 // We're flushing both ports in preparation for seeking.
2301
2302 if (mPortStatus[kPortIndexInput] == ENABLED
2303 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002304 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07002305 " seek-time.");
2306
Andreas Huber1f24b302010-06-10 11:12:39 -07002307 // We implicitly resume pulling on our upstream source.
2308 mPaused = false;
2309
Andreas Huberbe06d262009-08-14 14:37:10 -07002310 drainInputBuffers();
2311 fillOutputBuffers();
2312 }
2313 }
2314
2315 break;
2316 }
2317
2318 default:
2319 {
Andreas Huber4c483422009-09-02 16:05:36 -07002320 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07002321 break;
2322 }
2323 }
2324}
2325
2326void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002327 CODEC_LOGV("onStateChange %d", newState);
2328
Andreas Huberbe06d262009-08-14 14:37:10 -07002329 switch (newState) {
2330 case OMX_StateIdle:
2331 {
Andreas Huber4c483422009-09-02 16:05:36 -07002332 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002333 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07002334 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002335 mNode, OMX_CommandStateSet, OMX_StateExecuting);
2336
2337 CHECK_EQ(err, OK);
2338
2339 setState(IDLE_TO_EXECUTING);
2340 } else {
2341 CHECK_EQ(mState, EXECUTING_TO_IDLE);
2342
2343 CHECK_EQ(
2344 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
2345 mPortBuffers[kPortIndexInput].size());
2346
2347 CHECK_EQ(
2348 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
2349 mPortBuffers[kPortIndexOutput].size());
2350
Andreas Huber784202e2009-10-15 13:46:54 -07002351 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002352 mNode, OMX_CommandStateSet, OMX_StateLoaded);
2353
2354 CHECK_EQ(err, OK);
2355
2356 err = freeBuffersOnPort(kPortIndexInput);
2357 CHECK_EQ(err, OK);
2358
2359 err = freeBuffersOnPort(kPortIndexOutput);
2360 CHECK_EQ(err, OK);
2361
2362 mPortStatus[kPortIndexInput] = ENABLED;
2363 mPortStatus[kPortIndexOutput] = ENABLED;
2364
2365 setState(IDLE_TO_LOADED);
2366 }
2367 break;
2368 }
2369
2370 case OMX_StateExecuting:
2371 {
2372 CHECK_EQ(mState, IDLE_TO_EXECUTING);
2373
Andreas Huber4c483422009-09-02 16:05:36 -07002374 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002375
2376 setState(EXECUTING);
2377
Andreas Huber42978e52009-08-27 10:08:39 -07002378 // Buffers will be submitted to the component in the first
2379 // call to OMXCodec::read as mInitialBufferSubmit is true at
2380 // this point. This ensures that this on_message call returns,
2381 // releases the lock and ::init can notice the state change and
2382 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07002383 break;
2384 }
2385
2386 case OMX_StateLoaded:
2387 {
2388 CHECK_EQ(mState, IDLE_TO_LOADED);
2389
Andreas Huber4c483422009-09-02 16:05:36 -07002390 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002391
2392 setState(LOADED);
2393 break;
2394 }
2395
Andreas Huberc712b9f2010-01-20 15:05:46 -08002396 case OMX_StateInvalid:
2397 {
2398 setState(ERROR);
2399 break;
2400 }
2401
Andreas Huberbe06d262009-08-14 14:37:10 -07002402 default:
2403 {
2404 CHECK(!"should not be here.");
2405 break;
2406 }
2407 }
2408}
2409
2410// static
2411size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
2412 size_t n = 0;
2413 for (size_t i = 0; i < buffers.size(); ++i) {
2414 if (!buffers[i].mOwnedByComponent) {
2415 ++n;
2416 }
2417 }
2418
2419 return n;
2420}
2421
2422status_t OMXCodec::freeBuffersOnPort(
2423 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2424 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2425
2426 status_t stickyErr = OK;
2427
2428 for (size_t i = buffers->size(); i-- > 0;) {
2429 BufferInfo *info = &buffers->editItemAt(i);
2430
2431 if (onlyThoseWeOwn && info->mOwnedByComponent) {
2432 continue;
2433 }
2434
2435 CHECK_EQ(info->mOwnedByComponent, false);
2436
Andreas Huber92022852009-09-14 15:24:14 -07002437 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2438
Andreas Huberbe06d262009-08-14 14:37:10 -07002439 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002440 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07002441
2442 if (err != OK) {
2443 stickyErr = err;
2444 }
2445
2446 if (info->mMediaBuffer != NULL) {
2447 info->mMediaBuffer->setObserver(NULL);
2448
2449 // Make sure nobody but us owns this buffer at this point.
2450 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2451
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002452 // Cancel the buffer if it belongs to an ANativeWindow.
2453 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer();
2454 if (!info->mOwnedByNativeWindow && graphicBuffer != 0) {
2455 status_t err = cancelBufferToNativeWindow(info);
2456 if (err != OK) {
2457 stickyErr = err;
2458 }
2459 }
2460
Andreas Huberbe06d262009-08-14 14:37:10 -07002461 info->mMediaBuffer->release();
2462 }
2463
2464 buffers->removeAt(i);
2465 }
2466
2467 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2468
2469 return stickyErr;
2470}
2471
2472void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002473 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002474
2475 CHECK_EQ(mState, EXECUTING);
2476 CHECK_EQ(portIndex, kPortIndexOutput);
2477 setState(RECONFIGURING);
2478
2479 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002480 if (!flushPortAsync(portIndex)) {
2481 onCmdComplete(OMX_CommandFlush, portIndex);
2482 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002483 } else {
2484 disablePortAsync(portIndex);
2485 }
2486}
2487
Andreas Huber404cc412009-08-25 14:26:05 -07002488bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002489 CHECK(mState == EXECUTING || mState == RECONFIGURING
2490 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002491
Andreas Huber4c483422009-09-02 16:05:36 -07002492 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002493 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2494 mPortBuffers[portIndex].size());
2495
Andreas Huberbe06d262009-08-14 14:37:10 -07002496 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2497 mPortStatus[portIndex] = SHUTTING_DOWN;
2498
Andreas Huber404cc412009-08-25 14:26:05 -07002499 if ((mQuirks & kRequiresFlushCompleteEmulation)
2500 && countBuffersWeOwn(mPortBuffers[portIndex])
2501 == mPortBuffers[portIndex].size()) {
2502 // No flush is necessary and this component fails to send a
2503 // flush-complete event in this case.
2504
2505 return false;
2506 }
2507
Andreas Huberbe06d262009-08-14 14:37:10 -07002508 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002509 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002510 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002511
2512 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002513}
2514
2515void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2516 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2517
2518 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2519 mPortStatus[portIndex] = DISABLING;
2520
Andreas Huberd222c842010-08-26 14:29:34 -07002521 CODEC_LOGV("sending OMX_CommandPortDisable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002522 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002523 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002524 CHECK_EQ(err, OK);
2525
2526 freeBuffersOnPort(portIndex, true);
2527}
2528
2529void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2530 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2531
2532 CHECK_EQ(mPortStatus[portIndex], DISABLED);
2533 mPortStatus[portIndex] = ENABLING;
2534
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002535 CODEC_LOGV("sending OMX_CommandPortEnable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002536 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002537 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002538 CHECK_EQ(err, OK);
2539}
2540
2541void OMXCodec::fillOutputBuffers() {
2542 CHECK_EQ(mState, EXECUTING);
2543
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002544 // This is a workaround for some decoders not properly reporting
2545 // end-of-output-stream. If we own all input buffers and also own
2546 // all output buffers and we already signalled end-of-input-stream,
2547 // the end-of-output-stream is implied.
2548 if (mSignalledEOS
2549 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2550 == mPortBuffers[kPortIndexInput].size()
2551 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2552 == mPortBuffers[kPortIndexOutput].size()) {
2553 mNoMoreOutputData = true;
2554 mBufferFilled.signal();
2555
2556 return;
2557 }
2558
Andreas Huberbe06d262009-08-14 14:37:10 -07002559 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2560 for (size_t i = 0; i < buffers->size(); ++i) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002561 BufferInfo *info = &buffers->editItemAt(i);
2562 if (!info->mOwnedByNativeWindow) {
2563 fillOutputBuffer(&buffers->editItemAt(i));
2564 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002565 }
2566}
2567
2568void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002569 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002570
2571 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2572 for (size_t i = 0; i < buffers->size(); ++i) {
2573 drainInputBuffer(&buffers->editItemAt(i));
2574 }
2575}
2576
2577void OMXCodec::drainInputBuffer(BufferInfo *info) {
2578 CHECK_EQ(info->mOwnedByComponent, false);
2579
2580 if (mSignalledEOS) {
2581 return;
2582 }
2583
2584 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2585 const CodecSpecificData *specific =
2586 mCodecSpecificData[mCodecSpecificDataIndex];
2587
2588 size_t size = specific->mSize;
2589
Andreas Hubere6c40962009-09-10 14:13:30 -07002590 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002591 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002592 static const uint8_t kNALStartCode[4] =
2593 { 0x00, 0x00, 0x00, 0x01 };
2594
Andreas Huberc712b9f2010-01-20 15:05:46 -08002595 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002596
2597 size += 4;
2598
Andreas Huberc712b9f2010-01-20 15:05:46 -08002599 memcpy(info->mData, kNALStartCode, 4);
2600 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002601 specific->mData, specific->mSize);
2602 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002603 CHECK(info->mSize >= specific->mSize);
2604 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002605 }
2606
Andreas Huber2ea14e22009-12-16 09:30:55 -08002607 mNoMoreOutputData = false;
2608
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002609 CODEC_LOGV("calling emptyBuffer with codec specific data");
2610
Andreas Huber784202e2009-10-15 13:46:54 -07002611 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002612 mNode, info->mBuffer, 0, size,
2613 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2614 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002615 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002616
2617 info->mOwnedByComponent = true;
2618
2619 ++mCodecSpecificDataIndex;
2620 return;
2621 }
2622
Andreas Huber1f24b302010-06-10 11:12:39 -07002623 if (mPaused) {
2624 return;
2625 }
2626
Andreas Huberbe06d262009-08-14 14:37:10 -07002627 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002628
Andreas Hubera4357ad2010-04-02 12:49:54 -07002629 bool signalEOS = false;
2630 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002631
Andreas Hubera4357ad2010-04-02 12:49:54 -07002632 size_t offset = 0;
2633 int32_t n = 0;
2634 for (;;) {
2635 MediaBuffer *srcBuffer;
James Dong53d4e0d2010-07-21 14:51:35 -07002636 MediaSource::ReadOptions options;
2637 if (mSkipTimeUs >= 0) {
2638 options.setSkipFrame(mSkipTimeUs);
2639 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002640 if (mSeekTimeUs >= 0) {
2641 if (mLeftOverBuffer) {
2642 mLeftOverBuffer->release();
2643 mLeftOverBuffer = NULL;
2644 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002645 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002646
2647 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002648 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002649 mBufferFilled.signal();
2650
2651 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002652
2653 if (err == OK) {
2654 int64_t targetTimeUs;
2655 if (srcBuffer->meta_data()->findInt64(
2656 kKeyTargetTime, &targetTimeUs)
2657 && targetTimeUs >= 0) {
2658 mTargetTimeUs = targetTimeUs;
2659 } else {
2660 mTargetTimeUs = -1;
2661 }
2662 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002663 } else if (mLeftOverBuffer) {
2664 srcBuffer = mLeftOverBuffer;
2665 mLeftOverBuffer = NULL;
2666
2667 err = OK;
2668 } else {
James Dong53d4e0d2010-07-21 14:51:35 -07002669 err = mSource->read(&srcBuffer, &options);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002670 }
2671
2672 if (err != OK) {
2673 signalEOS = true;
2674 mFinalStatus = err;
2675 mSignalledEOS = true;
2676 break;
2677 }
2678
2679 size_t remainingBytes = info->mSize - offset;
2680
2681 if (srcBuffer->range_length() > remainingBytes) {
2682 if (offset == 0) {
2683 CODEC_LOGE(
2684 "Codec's input buffers are too small to accomodate "
2685 "buffer read from source (info->mSize = %d, srcLength = %d)",
2686 info->mSize, srcBuffer->range_length());
2687
2688 srcBuffer->release();
2689 srcBuffer = NULL;
2690
2691 setState(ERROR);
2692 return;
2693 }
2694
2695 mLeftOverBuffer = srcBuffer;
2696 break;
2697 }
2698
James Dong4f501f02010-06-07 14:41:41 -07002699 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2700 CHECK(mOMXLivesLocally && offset == 0);
2701 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2702 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2703 } else {
2704 memcpy((uint8_t *)info->mData + offset,
2705 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2706 srcBuffer->range_length());
2707 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002708
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002709 int64_t lastBufferTimeUs;
2710 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002711 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002712
Andreas Hubera4357ad2010-04-02 12:49:54 -07002713 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002714 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002715 }
2716
2717 offset += srcBuffer->range_length();
2718
2719 srcBuffer->release();
2720 srcBuffer = NULL;
2721
2722 ++n;
2723
2724 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2725 break;
2726 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002727
2728 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2729
2730 if (coalescedDurationUs > 250000ll) {
2731 // Don't coalesce more than 250ms worth of encoded data at once.
2732 break;
2733 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002734 }
2735
2736 if (n > 1) {
2737 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002738 }
2739
2740 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002741
Andreas Hubera4357ad2010-04-02 12:49:54 -07002742 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002743 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002744 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002745 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002746 }
2747
Andreas Hubera4357ad2010-04-02 12:49:54 -07002748 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2749 "timestamp %lld us (%.2f secs)",
2750 info->mBuffer, offset,
2751 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002752
Andreas Huber784202e2009-10-15 13:46:54 -07002753 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002754 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002755 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002756
2757 if (err != OK) {
2758 setState(ERROR);
2759 return;
2760 }
2761
2762 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002763
2764 // This component does not ever signal the EOS flag on output buffers,
2765 // Thanks for nothing.
2766 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2767 mNoMoreOutputData = true;
2768 mBufferFilled.signal();
2769 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002770}
2771
2772void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2773 CHECK_EQ(info->mOwnedByComponent, false);
2774
Andreas Huber404cc412009-08-25 14:26:05 -07002775 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002776 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002777 "calling fillOutputBuffer");
2778 return;
2779 }
2780
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002781 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer();
2782 if (graphicBuffer != 0) {
2783 // When using a native buffer we need to lock the buffer before giving
2784 // it to OMX.
2785 CHECK(!info->mOwnedByNativeWindow);
2786 CODEC_LOGV("Calling lockBuffer on %p", info->mBuffer);
2787 int err = mNativeWindow->lockBuffer(mNativeWindow.get(),
2788 graphicBuffer.get());
2789 if (err != 0) {
2790 CODEC_LOGE("lockBuffer failed w/ error 0x%08x", err);
2791
2792 setState(ERROR);
2793 return;
2794 }
2795 }
2796
2797 CODEC_LOGV("Calling fillBuffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002798 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002799
2800 if (err != OK) {
2801 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2802
2803 setState(ERROR);
2804 return;
2805 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002806
2807 info->mOwnedByComponent = true;
2808}
2809
2810void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2811 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2812 for (size_t i = 0; i < buffers->size(); ++i) {
2813 if ((*buffers)[i].mBuffer == buffer) {
2814 drainInputBuffer(&buffers->editItemAt(i));
2815 return;
2816 }
2817 }
2818
2819 CHECK(!"should not be here.");
2820}
2821
2822void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2823 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2824 for (size_t i = 0; i < buffers->size(); ++i) {
2825 if ((*buffers)[i].mBuffer == buffer) {
2826 fillOutputBuffer(&buffers->editItemAt(i));
2827 return;
2828 }
2829 }
2830
2831 CHECK(!"should not be here.");
2832}
2833
2834void OMXCodec::setState(State newState) {
2835 mState = newState;
2836 mAsyncCompletion.signal();
2837
2838 // This may cause some spurious wakeups but is necessary to
2839 // unblock the reader if we enter ERROR state.
2840 mBufferFilled.signal();
2841}
2842
Andreas Huberda050cf22009-09-02 14:01:43 -07002843void OMXCodec::setRawAudioFormat(
2844 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002845
2846 // port definition
2847 OMX_PARAM_PORTDEFINITIONTYPE def;
2848 InitOMXParams(&def);
2849 def.nPortIndex = portIndex;
2850 status_t err = mOMX->getParameter(
2851 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2852 CHECK_EQ(err, OK);
2853 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2854 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2855 &def, sizeof(def)), OK);
2856
2857 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002858 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002859 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002860 pcmParams.nPortIndex = portIndex;
2861
James Dongabed93a2010-04-22 17:27:04 -07002862 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002863 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2864
2865 CHECK_EQ(err, OK);
2866
2867 pcmParams.nChannels = numChannels;
2868 pcmParams.eNumData = OMX_NumericalDataSigned;
2869 pcmParams.bInterleaved = OMX_TRUE;
2870 pcmParams.nBitPerSample = 16;
2871 pcmParams.nSamplingRate = sampleRate;
2872 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2873
2874 if (numChannels == 1) {
2875 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2876 } else {
2877 CHECK_EQ(numChannels, 2);
2878
2879 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2880 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2881 }
2882
Andreas Huber784202e2009-10-15 13:46:54 -07002883 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002884 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2885
2886 CHECK_EQ(err, OK);
2887}
2888
James Dong17299ab2010-05-14 15:45:22 -07002889static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2890 if (isAMRWB) {
2891 if (bps <= 6600) {
2892 return OMX_AUDIO_AMRBandModeWB0;
2893 } else if (bps <= 8850) {
2894 return OMX_AUDIO_AMRBandModeWB1;
2895 } else if (bps <= 12650) {
2896 return OMX_AUDIO_AMRBandModeWB2;
2897 } else if (bps <= 14250) {
2898 return OMX_AUDIO_AMRBandModeWB3;
2899 } else if (bps <= 15850) {
2900 return OMX_AUDIO_AMRBandModeWB4;
2901 } else if (bps <= 18250) {
2902 return OMX_AUDIO_AMRBandModeWB5;
2903 } else if (bps <= 19850) {
2904 return OMX_AUDIO_AMRBandModeWB6;
2905 } else if (bps <= 23050) {
2906 return OMX_AUDIO_AMRBandModeWB7;
2907 }
2908
2909 // 23850 bps
2910 return OMX_AUDIO_AMRBandModeWB8;
2911 } else { // AMRNB
2912 if (bps <= 4750) {
2913 return OMX_AUDIO_AMRBandModeNB0;
2914 } else if (bps <= 5150) {
2915 return OMX_AUDIO_AMRBandModeNB1;
2916 } else if (bps <= 5900) {
2917 return OMX_AUDIO_AMRBandModeNB2;
2918 } else if (bps <= 6700) {
2919 return OMX_AUDIO_AMRBandModeNB3;
2920 } else if (bps <= 7400) {
2921 return OMX_AUDIO_AMRBandModeNB4;
2922 } else if (bps <= 7950) {
2923 return OMX_AUDIO_AMRBandModeNB5;
2924 } else if (bps <= 10200) {
2925 return OMX_AUDIO_AMRBandModeNB6;
2926 }
2927
2928 // 12200 bps
2929 return OMX_AUDIO_AMRBandModeNB7;
2930 }
2931}
2932
2933void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002934 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002935
Andreas Huber8768f2c2009-12-01 15:26:54 -08002936 OMX_AUDIO_PARAM_AMRTYPE def;
2937 InitOMXParams(&def);
2938 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002939
Andreas Huber8768f2c2009-12-01 15:26:54 -08002940 status_t err =
2941 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002942
Andreas Huber8768f2c2009-12-01 15:26:54 -08002943 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002944
Andreas Huber8768f2c2009-12-01 15:26:54 -08002945 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002946
James Dong17299ab2010-05-14 15:45:22 -07002947 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002948 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2949 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002950
2951 ////////////////////////
2952
2953 if (mIsEncoder) {
2954 sp<MetaData> format = mSource->getFormat();
2955 int32_t sampleRate;
2956 int32_t numChannels;
2957 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2958 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2959
2960 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2961 }
2962}
2963
James Dong17299ab2010-05-14 15:45:22 -07002964void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002965 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002966 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002967 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002968 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002969
2970 //////////////// output port ////////////////////
2971 // format
2972 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2973 format.nPortIndex = kPortIndexOutput;
2974 format.nIndex = 0;
2975 status_t err = OMX_ErrorNone;
2976 while (OMX_ErrorNone == err) {
2977 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2978 &format, sizeof(format)), OK);
2979 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2980 break;
2981 }
2982 format.nIndex++;
2983 }
2984 CHECK_EQ(OK, err);
2985 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2986 &format, sizeof(format)), OK);
2987
2988 // port definition
2989 OMX_PARAM_PORTDEFINITIONTYPE def;
2990 InitOMXParams(&def);
2991 def.nPortIndex = kPortIndexOutput;
2992 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2993 &def, sizeof(def)), OK);
2994 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2995 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2996 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2997 &def, sizeof(def)), OK);
2998
2999 // profile
3000 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
3001 InitOMXParams(&profile);
3002 profile.nPortIndex = kPortIndexOutput;
3003 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
3004 &profile, sizeof(profile)), OK);
3005 profile.nChannels = numChannels;
3006 profile.eChannelMode = (numChannels == 1?
3007 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
3008 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07003009 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003010 profile.nAudioBandWidth = 0;
3011 profile.nFrameLength = 0;
3012 profile.nAACtools = OMX_AUDIO_AACToolAll;
3013 profile.nAACERtools = OMX_AUDIO_AACERNone;
3014 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
3015 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
3016 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
3017 &profile, sizeof(profile)), OK);
3018
Andreas Huberda050cf22009-09-02 14:01:43 -07003019 } else {
3020 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07003021 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07003022 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003023
Andreas Huber784202e2009-10-15 13:46:54 -07003024 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003025 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
3026 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003027
Andreas Huberda050cf22009-09-02 14:01:43 -07003028 profile.nChannels = numChannels;
3029 profile.nSampleRate = sampleRate;
3030 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07003031
Andreas Huber784202e2009-10-15 13:46:54 -07003032 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003033 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
3034 CHECK_EQ(err, OK);
3035 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003036}
3037
3038void OMXCodec::setImageOutputFormat(
3039 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07003040 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07003041
3042#if 0
3043 OMX_INDEXTYPE index;
3044 status_t err = mOMX->get_extension_index(
3045 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
3046 CHECK_EQ(err, OK);
3047
3048 err = mOMX->set_config(mNode, index, &format, sizeof(format));
3049 CHECK_EQ(err, OK);
3050#endif
3051
3052 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003053 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003054 def.nPortIndex = kPortIndexOutput;
3055
Andreas Huber784202e2009-10-15 13:46:54 -07003056 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003057 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3058 CHECK_EQ(err, OK);
3059
3060 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
3061
3062 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07003063
Andreas Huberbe06d262009-08-14 14:37:10 -07003064 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3065 imageDef->eColorFormat = format;
3066 imageDef->nFrameWidth = width;
3067 imageDef->nFrameHeight = height;
3068
3069 switch (format) {
3070 case OMX_COLOR_FormatYUV420PackedPlanar:
3071 case OMX_COLOR_FormatYUV411Planar:
3072 {
3073 def.nBufferSize = (width * height * 3) / 2;
3074 break;
3075 }
3076
3077 case OMX_COLOR_FormatCbYCrY:
3078 {
3079 def.nBufferSize = width * height * 2;
3080 break;
3081 }
3082
3083 case OMX_COLOR_Format32bitARGB8888:
3084 {
3085 def.nBufferSize = width * height * 4;
3086 break;
3087 }
3088
Andreas Huber201511c2009-09-08 14:01:44 -07003089 case OMX_COLOR_Format16bitARGB4444:
3090 case OMX_COLOR_Format16bitARGB1555:
3091 case OMX_COLOR_Format16bitRGB565:
3092 case OMX_COLOR_Format16bitBGR565:
3093 {
3094 def.nBufferSize = width * height * 2;
3095 break;
3096 }
3097
Andreas Huberbe06d262009-08-14 14:37:10 -07003098 default:
3099 CHECK(!"Should not be here. Unknown color format.");
3100 break;
3101 }
3102
Andreas Huber5c0a9132009-08-20 11:16:40 -07003103 def.nBufferCountActual = def.nBufferCountMin;
3104
Andreas Huber784202e2009-10-15 13:46:54 -07003105 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003106 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3107 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07003108}
Andreas Huberbe06d262009-08-14 14:37:10 -07003109
Andreas Huber5c0a9132009-08-20 11:16:40 -07003110void OMXCodec::setJPEGInputFormat(
3111 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
3112 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003113 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003114 def.nPortIndex = kPortIndexInput;
3115
Andreas Huber784202e2009-10-15 13:46:54 -07003116 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003117 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3118 CHECK_EQ(err, OK);
3119
Andreas Huber5c0a9132009-08-20 11:16:40 -07003120 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
3121 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3122
Andreas Huberbe06d262009-08-14 14:37:10 -07003123 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
3124 imageDef->nFrameWidth = width;
3125 imageDef->nFrameHeight = height;
3126
Andreas Huber5c0a9132009-08-20 11:16:40 -07003127 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07003128 def.nBufferCountActual = def.nBufferCountMin;
3129
Andreas Huber784202e2009-10-15 13:46:54 -07003130 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003131 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3132 CHECK_EQ(err, OK);
3133}
3134
3135void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
3136 CodecSpecificData *specific =
3137 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
3138
3139 specific->mSize = size;
3140 memcpy(specific->mData, data, size);
3141
3142 mCodecSpecificData.push(specific);
3143}
3144
3145void OMXCodec::clearCodecSpecificData() {
3146 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
3147 free(mCodecSpecificData.editItemAt(i));
3148 }
3149 mCodecSpecificData.clear();
3150 mCodecSpecificDataIndex = 0;
3151}
3152
James Dong36e573b2010-06-19 09:04:18 -07003153status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07003154 Mutex::Autolock autoLock(mLock);
3155
Andreas Huberbe06d262009-08-14 14:37:10 -07003156 if (mState != LOADED) {
3157 return UNKNOWN_ERROR;
3158 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07003159
Andreas Huberbe06d262009-08-14 14:37:10 -07003160 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07003161 if (mQuirks & kWantsNALFragments) {
3162 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07003163 }
James Dong36e573b2010-06-19 09:04:18 -07003164 if (meta) {
3165 int64_t startTimeUs = 0;
3166 int64_t timeUs;
3167 if (meta->findInt64(kKeyTime, &timeUs)) {
3168 startTimeUs = timeUs;
3169 }
3170 params->setInt64(kKeyTime, startTimeUs);
3171 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003172 status_t err = mSource->start(params.get());
3173
3174 if (err != OK) {
3175 return err;
3176 }
3177
3178 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07003179 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07003180 mSignalledEOS = false;
3181 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07003182 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003183 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003184 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
3185 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07003186 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07003187 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003188
3189 return init();
3190}
3191
3192status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08003193 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07003194
3195 Mutex::Autolock autoLock(mLock);
3196
3197 while (isIntermediateState(mState)) {
3198 mAsyncCompletion.wait(mLock);
3199 }
3200
3201 switch (mState) {
3202 case LOADED:
3203 case ERROR:
3204 break;
3205
3206 case EXECUTING:
3207 {
3208 setState(EXECUTING_TO_IDLE);
3209
Andreas Huber127fcdc2009-08-26 16:27:02 -07003210 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07003211 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07003212 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07003213
Andreas Huber127fcdc2009-08-26 16:27:02 -07003214 bool emulateInputFlushCompletion =
3215 !flushPortAsync(kPortIndexInput);
3216
3217 bool emulateOutputFlushCompletion =
3218 !flushPortAsync(kPortIndexOutput);
3219
3220 if (emulateInputFlushCompletion) {
3221 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3222 }
3223
3224 if (emulateOutputFlushCompletion) {
3225 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3226 }
3227 } else {
3228 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
3229 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
3230
3231 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07003232 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07003233 CHECK_EQ(err, OK);
3234 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003235
3236 while (mState != LOADED && mState != ERROR) {
3237 mAsyncCompletion.wait(mLock);
3238 }
3239
3240 break;
3241 }
3242
3243 default:
3244 {
3245 CHECK(!"should not be here.");
3246 break;
3247 }
3248 }
3249
Andreas Hubera4357ad2010-04-02 12:49:54 -07003250 if (mLeftOverBuffer) {
3251 mLeftOverBuffer->release();
3252 mLeftOverBuffer = NULL;
3253 }
3254
Andreas Huberbe06d262009-08-14 14:37:10 -07003255 mSource->stop();
3256
Andreas Huber4a9375e2010-02-09 11:54:33 -08003257 CODEC_LOGV("stopped");
3258
Andreas Huberbe06d262009-08-14 14:37:10 -07003259 return OK;
3260}
3261
3262sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07003263 Mutex::Autolock autoLock(mLock);
3264
Andreas Huberbe06d262009-08-14 14:37:10 -07003265 return mOutputFormat;
3266}
3267
3268status_t OMXCodec::read(
3269 MediaBuffer **buffer, const ReadOptions *options) {
3270 *buffer = NULL;
3271
3272 Mutex::Autolock autoLock(mLock);
3273
Andreas Huberd06e5b82009-08-28 13:18:14 -07003274 if (mState != EXECUTING && mState != RECONFIGURING) {
3275 return UNKNOWN_ERROR;
3276 }
3277
Andreas Hubere981c332009-10-22 13:49:30 -07003278 bool seeking = false;
3279 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003280 ReadOptions::SeekMode seekMode;
3281 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07003282 seeking = true;
3283 }
James Dong53d4e0d2010-07-21 14:51:35 -07003284 int64_t skipTimeUs;
3285 if (options && options->getSkipFrame(&skipTimeUs)) {
3286 mSkipTimeUs = skipTimeUs;
3287 } else {
3288 mSkipTimeUs = -1;
3289 }
Andreas Hubere981c332009-10-22 13:49:30 -07003290
Andreas Huber42978e52009-08-27 10:08:39 -07003291 if (mInitialBufferSubmit) {
3292 mInitialBufferSubmit = false;
3293
Andreas Hubere981c332009-10-22 13:49:30 -07003294 if (seeking) {
3295 CHECK(seekTimeUs >= 0);
3296 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003297 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07003298
3299 // There's no reason to trigger the code below, there's
3300 // nothing to flush yet.
3301 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07003302 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07003303 }
3304
Andreas Huber42978e52009-08-27 10:08:39 -07003305 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07003306
Andreas Huberd06e5b82009-08-28 13:18:14 -07003307 if (mState == EXECUTING) {
3308 // Otherwise mState == RECONFIGURING and this code will trigger
3309 // after the output port is reenabled.
3310 fillOutputBuffers();
3311 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003312 }
3313
Andreas Hubere981c332009-10-22 13:49:30 -07003314 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07003315 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07003316
3317 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003318
3319 CHECK(seekTimeUs >= 0);
3320 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003321 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07003322
3323 mFilledBuffers.clear();
3324
3325 CHECK_EQ(mState, EXECUTING);
3326
Andreas Huber404cc412009-08-25 14:26:05 -07003327 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
3328 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
3329
3330 if (emulateInputFlushCompletion) {
3331 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3332 }
3333
3334 if (emulateOutputFlushCompletion) {
3335 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3336 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08003337
3338 while (mSeekTimeUs >= 0) {
3339 mBufferFilled.wait(mLock);
3340 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003341 }
3342
3343 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
3344 mBufferFilled.wait(mLock);
3345 }
3346
3347 if (mState == ERROR) {
3348 return UNKNOWN_ERROR;
3349 }
3350
3351 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08003352 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07003353 }
3354
Andreas Hubercfd55572009-10-09 14:11:28 -07003355 if (mOutputPortSettingsHaveChanged) {
3356 mOutputPortSettingsHaveChanged = false;
3357
3358 return INFO_FORMAT_CHANGED;
3359 }
3360
Andreas Huberbe06d262009-08-14 14:37:10 -07003361 size_t index = *mFilledBuffers.begin();
3362 mFilledBuffers.erase(mFilledBuffers.begin());
3363
3364 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
3365 info->mMediaBuffer->add_ref();
3366 *buffer = info->mMediaBuffer;
3367
3368 return OK;
3369}
3370
3371void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
3372 Mutex::Autolock autoLock(mLock);
3373
3374 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
3375 for (size_t i = 0; i < buffers->size(); ++i) {
3376 BufferInfo *info = &buffers->editItemAt(i);
3377
3378 if (info->mMediaBuffer == buffer) {
3379 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003380 if (buffer->graphicBuffer() == 0) {
3381 fillOutputBuffer(info);
3382 } else {
3383 sp<MetaData> metaData = info->mMediaBuffer->meta_data();
3384 int32_t rendered = 0;
3385 if (!metaData->findInt32(kKeyRendered, &rendered)) {
3386 rendered = 0;
3387 }
3388 if (!rendered) {
3389 status_t err = cancelBufferToNativeWindow(info);
3390 if (err < 0) {
3391 return;
3392 }
3393 } else {
3394 info->mOwnedByNativeWindow = true;
3395 }
3396
3397 // Dequeue the next buffer from the native window.
3398 BufferInfo *nextBufInfo = dequeueBufferFromNativeWindow();
3399 if (nextBufInfo == 0) {
3400 return;
3401 }
3402
3403 // Give the buffer to the OMX node to fill.
3404 fillOutputBuffer(nextBufInfo);
3405 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003406 return;
3407 }
3408 }
3409
3410 CHECK(!"should not be here.");
3411}
3412
3413static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
3414 static const char *kNames[] = {
3415 "OMX_IMAGE_CodingUnused",
3416 "OMX_IMAGE_CodingAutoDetect",
3417 "OMX_IMAGE_CodingJPEG",
3418 "OMX_IMAGE_CodingJPEG2K",
3419 "OMX_IMAGE_CodingEXIF",
3420 "OMX_IMAGE_CodingTIFF",
3421 "OMX_IMAGE_CodingGIF",
3422 "OMX_IMAGE_CodingPNG",
3423 "OMX_IMAGE_CodingLZW",
3424 "OMX_IMAGE_CodingBMP",
3425 };
3426
3427 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3428
3429 if (type < 0 || (size_t)type >= numNames) {
3430 return "UNKNOWN";
3431 } else {
3432 return kNames[type];
3433 }
3434}
3435
3436static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
3437 static const char *kNames[] = {
3438 "OMX_COLOR_FormatUnused",
3439 "OMX_COLOR_FormatMonochrome",
3440 "OMX_COLOR_Format8bitRGB332",
3441 "OMX_COLOR_Format12bitRGB444",
3442 "OMX_COLOR_Format16bitARGB4444",
3443 "OMX_COLOR_Format16bitARGB1555",
3444 "OMX_COLOR_Format16bitRGB565",
3445 "OMX_COLOR_Format16bitBGR565",
3446 "OMX_COLOR_Format18bitRGB666",
3447 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003448 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07003449 "OMX_COLOR_Format24bitRGB888",
3450 "OMX_COLOR_Format24bitBGR888",
3451 "OMX_COLOR_Format24bitARGB1887",
3452 "OMX_COLOR_Format25bitARGB1888",
3453 "OMX_COLOR_Format32bitBGRA8888",
3454 "OMX_COLOR_Format32bitARGB8888",
3455 "OMX_COLOR_FormatYUV411Planar",
3456 "OMX_COLOR_FormatYUV411PackedPlanar",
3457 "OMX_COLOR_FormatYUV420Planar",
3458 "OMX_COLOR_FormatYUV420PackedPlanar",
3459 "OMX_COLOR_FormatYUV420SemiPlanar",
3460 "OMX_COLOR_FormatYUV422Planar",
3461 "OMX_COLOR_FormatYUV422PackedPlanar",
3462 "OMX_COLOR_FormatYUV422SemiPlanar",
3463 "OMX_COLOR_FormatYCbYCr",
3464 "OMX_COLOR_FormatYCrYCb",
3465 "OMX_COLOR_FormatCbYCrY",
3466 "OMX_COLOR_FormatCrYCbY",
3467 "OMX_COLOR_FormatYUV444Interleaved",
3468 "OMX_COLOR_FormatRawBayer8bit",
3469 "OMX_COLOR_FormatRawBayer10bit",
3470 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003471 "OMX_COLOR_FormatL2",
3472 "OMX_COLOR_FormatL4",
3473 "OMX_COLOR_FormatL8",
3474 "OMX_COLOR_FormatL16",
3475 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07003476 "OMX_COLOR_FormatL32",
3477 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
3478 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
3479 "OMX_COLOR_Format18BitBGR666",
3480 "OMX_COLOR_Format24BitARGB6666",
3481 "OMX_COLOR_Format24BitABGR6666",
3482 };
3483
3484 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3485
Andreas Huberbe06d262009-08-14 14:37:10 -07003486 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3487 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3488 } else if (type < 0 || (size_t)type >= numNames) {
3489 return "UNKNOWN";
3490 } else {
3491 return kNames[type];
3492 }
3493}
3494
3495static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3496 static const char *kNames[] = {
3497 "OMX_VIDEO_CodingUnused",
3498 "OMX_VIDEO_CodingAutoDetect",
3499 "OMX_VIDEO_CodingMPEG2",
3500 "OMX_VIDEO_CodingH263",
3501 "OMX_VIDEO_CodingMPEG4",
3502 "OMX_VIDEO_CodingWMV",
3503 "OMX_VIDEO_CodingRV",
3504 "OMX_VIDEO_CodingAVC",
3505 "OMX_VIDEO_CodingMJPEG",
3506 };
3507
3508 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3509
3510 if (type < 0 || (size_t)type >= numNames) {
3511 return "UNKNOWN";
3512 } else {
3513 return kNames[type];
3514 }
3515}
3516
3517static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3518 static const char *kNames[] = {
3519 "OMX_AUDIO_CodingUnused",
3520 "OMX_AUDIO_CodingAutoDetect",
3521 "OMX_AUDIO_CodingPCM",
3522 "OMX_AUDIO_CodingADPCM",
3523 "OMX_AUDIO_CodingAMR",
3524 "OMX_AUDIO_CodingGSMFR",
3525 "OMX_AUDIO_CodingGSMEFR",
3526 "OMX_AUDIO_CodingGSMHR",
3527 "OMX_AUDIO_CodingPDCFR",
3528 "OMX_AUDIO_CodingPDCEFR",
3529 "OMX_AUDIO_CodingPDCHR",
3530 "OMX_AUDIO_CodingTDMAFR",
3531 "OMX_AUDIO_CodingTDMAEFR",
3532 "OMX_AUDIO_CodingQCELP8",
3533 "OMX_AUDIO_CodingQCELP13",
3534 "OMX_AUDIO_CodingEVRC",
3535 "OMX_AUDIO_CodingSMV",
3536 "OMX_AUDIO_CodingG711",
3537 "OMX_AUDIO_CodingG723",
3538 "OMX_AUDIO_CodingG726",
3539 "OMX_AUDIO_CodingG729",
3540 "OMX_AUDIO_CodingAAC",
3541 "OMX_AUDIO_CodingMP3",
3542 "OMX_AUDIO_CodingSBC",
3543 "OMX_AUDIO_CodingVORBIS",
3544 "OMX_AUDIO_CodingWMA",
3545 "OMX_AUDIO_CodingRA",
3546 "OMX_AUDIO_CodingMIDI",
3547 };
3548
3549 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3550
3551 if (type < 0 || (size_t)type >= numNames) {
3552 return "UNKNOWN";
3553 } else {
3554 return kNames[type];
3555 }
3556}
3557
3558static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3559 static const char *kNames[] = {
3560 "OMX_AUDIO_PCMModeLinear",
3561 "OMX_AUDIO_PCMModeALaw",
3562 "OMX_AUDIO_PCMModeMULaw",
3563 };
3564
3565 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3566
3567 if (type < 0 || (size_t)type >= numNames) {
3568 return "UNKNOWN";
3569 } else {
3570 return kNames[type];
3571 }
3572}
3573
Andreas Huber7ae02c82009-09-09 16:29:47 -07003574static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3575 static const char *kNames[] = {
3576 "OMX_AUDIO_AMRBandModeUnused",
3577 "OMX_AUDIO_AMRBandModeNB0",
3578 "OMX_AUDIO_AMRBandModeNB1",
3579 "OMX_AUDIO_AMRBandModeNB2",
3580 "OMX_AUDIO_AMRBandModeNB3",
3581 "OMX_AUDIO_AMRBandModeNB4",
3582 "OMX_AUDIO_AMRBandModeNB5",
3583 "OMX_AUDIO_AMRBandModeNB6",
3584 "OMX_AUDIO_AMRBandModeNB7",
3585 "OMX_AUDIO_AMRBandModeWB0",
3586 "OMX_AUDIO_AMRBandModeWB1",
3587 "OMX_AUDIO_AMRBandModeWB2",
3588 "OMX_AUDIO_AMRBandModeWB3",
3589 "OMX_AUDIO_AMRBandModeWB4",
3590 "OMX_AUDIO_AMRBandModeWB5",
3591 "OMX_AUDIO_AMRBandModeWB6",
3592 "OMX_AUDIO_AMRBandModeWB7",
3593 "OMX_AUDIO_AMRBandModeWB8",
3594 };
3595
3596 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3597
3598 if (type < 0 || (size_t)type >= numNames) {
3599 return "UNKNOWN";
3600 } else {
3601 return kNames[type];
3602 }
3603}
3604
3605static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3606 static const char *kNames[] = {
3607 "OMX_AUDIO_AMRFrameFormatConformance",
3608 "OMX_AUDIO_AMRFrameFormatIF1",
3609 "OMX_AUDIO_AMRFrameFormatIF2",
3610 "OMX_AUDIO_AMRFrameFormatFSF",
3611 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3612 "OMX_AUDIO_AMRFrameFormatITU",
3613 };
3614
3615 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3616
3617 if (type < 0 || (size_t)type >= numNames) {
3618 return "UNKNOWN";
3619 } else {
3620 return kNames[type];
3621 }
3622}
Andreas Huberbe06d262009-08-14 14:37:10 -07003623
3624void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3625 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003626 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003627 def.nPortIndex = portIndex;
3628
Andreas Huber784202e2009-10-15 13:46:54 -07003629 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003630 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3631 CHECK_EQ(err, OK);
3632
3633 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3634
3635 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3636 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3637
3638 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3639 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3640 printf(" nBufferSize = %ld\n", def.nBufferSize);
3641
3642 switch (def.eDomain) {
3643 case OMX_PortDomainImage:
3644 {
3645 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3646
3647 printf("\n");
3648 printf(" // Image\n");
3649 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3650 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3651 printf(" nStride = %ld\n", imageDef->nStride);
3652
3653 printf(" eCompressionFormat = %s\n",
3654 imageCompressionFormatString(imageDef->eCompressionFormat));
3655
3656 printf(" eColorFormat = %s\n",
3657 colorFormatString(imageDef->eColorFormat));
3658
3659 break;
3660 }
3661
3662 case OMX_PortDomainVideo:
3663 {
3664 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3665
3666 printf("\n");
3667 printf(" // Video\n");
3668 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3669 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3670 printf(" nStride = %ld\n", videoDef->nStride);
3671
3672 printf(" eCompressionFormat = %s\n",
3673 videoCompressionFormatString(videoDef->eCompressionFormat));
3674
3675 printf(" eColorFormat = %s\n",
3676 colorFormatString(videoDef->eColorFormat));
3677
3678 break;
3679 }
3680
3681 case OMX_PortDomainAudio:
3682 {
3683 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3684
3685 printf("\n");
3686 printf(" // Audio\n");
3687 printf(" eEncoding = %s\n",
3688 audioCodingTypeString(audioDef->eEncoding));
3689
3690 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3691 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003692 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003693 params.nPortIndex = portIndex;
3694
Andreas Huber784202e2009-10-15 13:46:54 -07003695 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003696 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3697 CHECK_EQ(err, OK);
3698
3699 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3700 printf(" nChannels = %ld\n", params.nChannels);
3701 printf(" bInterleaved = %d\n", params.bInterleaved);
3702 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3703
3704 printf(" eNumData = %s\n",
3705 params.eNumData == OMX_NumericalDataSigned
3706 ? "signed" : "unsigned");
3707
3708 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003709 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3710 OMX_AUDIO_PARAM_AMRTYPE amr;
3711 InitOMXParams(&amr);
3712 amr.nPortIndex = portIndex;
3713
Andreas Huber784202e2009-10-15 13:46:54 -07003714 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003715 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3716 CHECK_EQ(err, OK);
3717
3718 printf(" nChannels = %ld\n", amr.nChannels);
3719 printf(" eAMRBandMode = %s\n",
3720 amrBandModeString(amr.eAMRBandMode));
3721 printf(" eAMRFrameFormat = %s\n",
3722 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003723 }
3724
3725 break;
3726 }
3727
3728 default:
3729 {
3730 printf(" // Unknown\n");
3731 break;
3732 }
3733 }
3734
3735 printf("}\n");
3736}
3737
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003738status_t OMXCodec::initNativeWindow() {
3739 // Enable use of a GraphicBuffer as the output for this node. This must
3740 // happen before getting the IndexParamPortDefinition parameter because it
3741 // will affect the pixel format that the node reports.
3742 status_t err = mOMX->enableGraphicBuffers(mNode, kPortIndexOutput, OMX_TRUE);
3743 if (err != 0) {
3744 return err;
3745 }
3746
3747 return OK;
3748}
3749
Andreas Huberbe06d262009-08-14 14:37:10 -07003750void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3751 mOutputFormat = new MetaData;
3752 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003753 if (mIsEncoder) {
3754 int32_t timeScale;
3755 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3756 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3757 }
3758 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003759
3760 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003761 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003762 def.nPortIndex = kPortIndexOutput;
3763
Andreas Huber784202e2009-10-15 13:46:54 -07003764 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003765 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3766 CHECK_EQ(err, OK);
3767
3768 switch (def.eDomain) {
3769 case OMX_PortDomainImage:
3770 {
3771 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3772 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3773
Andreas Hubere6c40962009-09-10 14:13:30 -07003774 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003775 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3776 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3777 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3778 break;
3779 }
3780
3781 case OMX_PortDomainAudio:
3782 {
3783 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3784
Andreas Huberda050cf22009-09-02 14:01:43 -07003785 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3786 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003787 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003788 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003789
Andreas Huber784202e2009-10-15 13:46:54 -07003790 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003791 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3792 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003793
Andreas Huberda050cf22009-09-02 14:01:43 -07003794 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3795 CHECK_EQ(params.nBitPerSample, 16);
3796 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003797
Andreas Huberda050cf22009-09-02 14:01:43 -07003798 int32_t numChannels, sampleRate;
3799 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3800 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003801
Andreas Huberda050cf22009-09-02 14:01:43 -07003802 if ((OMX_U32)numChannels != params.nChannels) {
3803 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003804 "the input stream contains (contains %d channels, "
3805 "codec outputs %ld channels).",
3806 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003807 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003808
Andreas Hubere6c40962009-09-10 14:13:30 -07003809 mOutputFormat->setCString(
3810 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003811
3812 // Use the codec-advertised number of channels, as some
3813 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003814 // mono. If we know the codec lies about this information,
3815 // use the actual number of channels instead.
3816 mOutputFormat->setInt32(
3817 kKeyChannelCount,
3818 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3819 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003820
3821 // The codec-reported sampleRate is not reliable...
3822 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3823 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003824 OMX_AUDIO_PARAM_AMRTYPE amr;
3825 InitOMXParams(&amr);
3826 amr.nPortIndex = kPortIndexOutput;
3827
Andreas Huber784202e2009-10-15 13:46:54 -07003828 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003829 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3830 CHECK_EQ(err, OK);
3831
3832 CHECK_EQ(amr.nChannels, 1);
3833 mOutputFormat->setInt32(kKeyChannelCount, 1);
3834
3835 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3836 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003837 mOutputFormat->setCString(
3838 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003839 mOutputFormat->setInt32(kKeySampleRate, 8000);
3840 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3841 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003842 mOutputFormat->setCString(
3843 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003844 mOutputFormat->setInt32(kKeySampleRate, 16000);
3845 } else {
3846 CHECK(!"Unknown AMR band mode.");
3847 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003848 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003849 mOutputFormat->setCString(
3850 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003851 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003852 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3853 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003854 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003855 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3856 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003857 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003858 } else {
3859 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003860 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003861 break;
3862 }
3863
3864 case OMX_PortDomainVideo:
3865 {
3866 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3867
3868 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003869 mOutputFormat->setCString(
3870 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003871 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003872 mOutputFormat->setCString(
3873 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003874 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003875 mOutputFormat->setCString(
3876 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003877 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003878 mOutputFormat->setCString(
3879 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003880 } else {
3881 CHECK(!"Unknown compression format.");
3882 }
3883
James Dong5592bcc2010-10-22 17:10:43 -07003884 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3885 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
Andreas Huberbe06d262009-08-14 14:37:10 -07003886 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3887 break;
3888 }
3889
3890 default:
3891 {
3892 CHECK(!"should not be here, neither audio nor video.");
3893 break;
3894 }
3895 }
3896}
3897
Andreas Huber1f24b302010-06-10 11:12:39 -07003898status_t OMXCodec::pause() {
3899 Mutex::Autolock autoLock(mLock);
3900
3901 mPaused = true;
3902
3903 return OK;
3904}
3905
Andreas Hubere6c40962009-09-10 14:13:30 -07003906////////////////////////////////////////////////////////////////////////////////
3907
3908status_t QueryCodecs(
3909 const sp<IOMX> &omx,
3910 const char *mime, bool queryDecoders,
3911 Vector<CodecCapabilities> *results) {
3912 results->clear();
3913
3914 for (int index = 0;; ++index) {
3915 const char *componentName;
3916
3917 if (!queryDecoders) {
3918 componentName = GetCodec(
3919 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3920 mime, index);
3921 } else {
3922 componentName = GetCodec(
3923 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3924 mime, index);
3925 }
3926
3927 if (!componentName) {
3928 return OK;
3929 }
3930
Andreas Huber1a189a82010-03-24 13:49:20 -07003931 if (strncmp(componentName, "OMX.", 4)) {
3932 // Not an OpenMax component but a software codec.
3933
3934 results->push();
3935 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3936 caps->mComponentName = componentName;
3937
3938 continue;
3939 }
3940
Andreas Huber784202e2009-10-15 13:46:54 -07003941 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003942 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003943 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003944
3945 if (err != OK) {
3946 continue;
3947 }
3948
James Dong722d5912010-04-13 10:56:59 -07003949 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003950
3951 results->push();
3952 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3953 caps->mComponentName = componentName;
3954
3955 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3956 InitOMXParams(&param);
3957
3958 param.nPortIndex = queryDecoders ? 0 : 1;
3959
3960 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003961 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003962 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3963 &param, sizeof(param));
3964
3965 if (err != OK) {
3966 break;
3967 }
3968
3969 CodecProfileLevel profileLevel;
3970 profileLevel.mProfile = param.eProfile;
3971 profileLevel.mLevel = param.eLevel;
3972
3973 caps->mProfileLevels.push(profileLevel);
3974 }
3975
Andreas Huber784202e2009-10-15 13:46:54 -07003976 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003977 }
3978}
3979
Andreas Huberbe06d262009-08-14 14:37:10 -07003980} // namespace android