blob: 5d26fd5f72060b86486cae218b7d588a9c86f3cb [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>
Andreas Huber1bb0ffd2010-11-22 13:06:35 -080041#include <media/stagefright/foundation/ADebug.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070042#include <media/IMediaPlayerService.h>
Jamie Gennis58a36ad2010-10-07 14:08:38 -070043#include <media/stagefright/HardwareAPI.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070044#include <media/stagefright/MediaBuffer.h>
45#include <media/stagefright/MediaBufferGroup.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"
Andreas Huberf7e2e312010-11-15 09:01:13 -080057#include "include/avc_utils.h"
Andreas Huber8946ab22010-09-15 16:20:42 -070058
Andreas Huberbe06d262009-08-14 14:37:10 -070059namespace android {
60
Andreas Huber8b432b12009-10-07 13:36:52 -070061static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
62
Andreas Huberbe06d262009-08-14 14:37:10 -070063struct CodecInfo {
64 const char *mime;
65 const char *codec;
66};
67
Andreas Huberfb1c2f82009-12-15 13:25:11 -080068#define FACTORY_CREATE(name) \
69static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
70 return new name(source); \
71}
72
James Dong17299ab2010-05-14 15:45:22 -070073#define FACTORY_CREATE_ENCODER(name) \
74static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \
75 return new name(source, meta); \
76}
77
Andreas Huberfb1c2f82009-12-15 13:25:11 -080078#define FACTORY_REF(name) { #name, Make##name },
79
80FACTORY_CREATE(MP3Decoder)
81FACTORY_CREATE(AMRNBDecoder)
82FACTORY_CREATE(AMRWBDecoder)
83FACTORY_CREATE(AACDecoder)
84FACTORY_CREATE(AVCDecoder)
Andreas Huber520b2a72010-08-09 09:54:59 -070085FACTORY_CREATE(G711Decoder)
James Dong02f5b542009-12-15 16:26:55 -080086FACTORY_CREATE(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -070087FACTORY_CREATE(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -070088FACTORY_CREATE(VPXDecoder)
James Dong17299ab2010-05-14 15:45:22 -070089FACTORY_CREATE_ENCODER(AMRNBEncoder)
90FACTORY_CREATE_ENCODER(AMRWBEncoder)
91FACTORY_CREATE_ENCODER(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -070092FACTORY_CREATE_ENCODER(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -070093FACTORY_CREATE_ENCODER(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -070094
95static sp<MediaSource> InstantiateSoftwareEncoder(
96 const char *name, const sp<MediaSource> &source,
97 const sp<MetaData> &meta) {
98 struct FactoryInfo {
99 const char *name;
100 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &);
101 };
102
103 static const FactoryInfo kFactoryInfo[] = {
104 FACTORY_REF(AMRNBEncoder)
105 FACTORY_REF(AMRWBEncoder)
106 FACTORY_REF(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -0700107 FACTORY_REF(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -0700108 FACTORY_REF(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -0700109 };
110 for (size_t i = 0;
111 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
112 if (!strcmp(name, kFactoryInfo[i].name)) {
113 return (*kFactoryInfo[i].CreateFunc)(source, meta);
114 }
115 }
116
117 return NULL;
118}
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800119
120static sp<MediaSource> InstantiateSoftwareCodec(
121 const char *name, const sp<MediaSource> &source) {
122 struct FactoryInfo {
123 const char *name;
124 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
125 };
126
127 static const FactoryInfo kFactoryInfo[] = {
128 FACTORY_REF(MP3Decoder)
129 FACTORY_REF(AMRNBDecoder)
130 FACTORY_REF(AMRWBDecoder)
131 FACTORY_REF(AACDecoder)
132 FACTORY_REF(AVCDecoder)
Andreas Huber520b2a72010-08-09 09:54:59 -0700133 FACTORY_REF(G711Decoder)
James Dong02f5b542009-12-15 16:26:55 -0800134 FACTORY_REF(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -0700135 FACTORY_REF(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -0700136 FACTORY_REF(VPXDecoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800137 };
138 for (size_t i = 0;
139 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
140 if (!strcmp(name, kFactoryInfo[i].name)) {
Andreas Huber8946ab22010-09-15 16:20:42 -0700141 if (!strcmp(name, "VPXDecoder")) {
142 return new ThreadedSource(
143 (*kFactoryInfo[i].CreateFunc)(source));
144 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800145 return (*kFactoryInfo[i].CreateFunc)(source);
146 }
147 }
148
149 return NULL;
150}
151
152#undef FACTORY_REF
153#undef FACTORY_CREATE
154
Andreas Huberbe06d262009-08-14 14:37:10 -0700155static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700156 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
Andreas Huberd222c842010-08-26 14:29:34 -0700157// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.Nvidia.mp3.decoder" },
James Dong374aee62010-04-26 10:23:30 -0700158// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800159 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700160// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberd222c842010-08-26 14:29:34 -0700161// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.Nvidia.amr.decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800162 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huberd222c842010-08-26 14:29:34 -0700163// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.Nvidia.amrwb.decoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700164 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800165 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huberd222c842010-08-26 14:29:34 -0700166// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.Nvidia.aac.decoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700167 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800168 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber520b2a72010-08-09 09:54:59 -0700169 { MEDIA_MIMETYPE_AUDIO_G711_ALAW, "G711Decoder" },
170 { MEDIA_MIMETYPE_AUDIO_G711_MLAW, "G711Decoder" },
Andreas Huber9f9ae602010-10-27 14:53:55 -0700171 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.Nvidia.mp4.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700172 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.decoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700173 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
174 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700175 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800176 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber9f9ae602010-10-27 14:53:55 -0700177 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.Nvidia.h263.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700178 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.decoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700179 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700180 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800181 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
pgudadhe6ad2c352010-07-26 15:04:33 -0700182 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.Nvidia.h264.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700183 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.decoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700184 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
185 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700186 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800187 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber388379f2010-05-07 10:35:13 -0700188 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" },
Andreas Huber47ba30e2010-05-24 14:38:02 -0700189 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700190};
191
192static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700193 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800194 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700195 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700196 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700197 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700198 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700199 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.encoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700200 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
201 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
James Dong2e87f7b2010-09-23 17:46:34 -0700202 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.Nvidia.mp4.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700203 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700204 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700205 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.encoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700206 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
207 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
James Dong2e87f7b2010-09-23 17:46:34 -0700208 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.Nvidia.h263.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700209 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700210 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700211 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.encoder.avc" },
Andreas Huber71c27d92010-03-19 11:43:15 -0700212 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700213 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
pgudadhe9c305322010-07-26 13:59:29 -0700214 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.Nvidia.h264.encoder" },
Andreas Huber524e6f62010-09-16 11:23:09 -0700215 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Encoder" },
James Dong1cc31e62010-07-02 17:44:44 -0700216 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCEncoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700217};
218
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800219#undef OPTIONAL
220
Andreas Hubere0873732009-09-10 09:57:53 -0700221#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700222#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber42c444a2010-02-09 10:20:00 -0800223#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700224
Andreas Huberbe06d262009-08-14 14:37:10 -0700225struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700226 OMXCodecObserver() {
227 }
228
229 void setCodec(const sp<OMXCodec> &target) {
230 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700231 }
232
233 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700234 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700235 sp<OMXCodec> codec = mTarget.promote();
236
237 if (codec.get() != NULL) {
James Dong681e89c2011-01-10 08:55:02 -0800238 Mutex::Autolock autoLock(codec->mLock);
Andreas Huberbe06d262009-08-14 14:37:10 -0700239 codec->on_message(msg);
James Dong681e89c2011-01-10 08:55:02 -0800240 codec.clear();
Andreas Huberbe06d262009-08-14 14:37:10 -0700241 }
242 }
243
244protected:
245 virtual ~OMXCodecObserver() {}
246
247private:
248 wp<OMXCodec> mTarget;
249
250 OMXCodecObserver(const OMXCodecObserver &);
251 OMXCodecObserver &operator=(const OMXCodecObserver &);
252};
253
254static const char *GetCodec(const CodecInfo *info, size_t numInfos,
255 const char *mime, int index) {
256 CHECK(index >= 0);
257 for(size_t i = 0; i < numInfos; ++i) {
258 if (!strcasecmp(mime, info[i].mime)) {
259 if (index == 0) {
260 return info[i].codec;
261 }
262
263 --index;
264 }
265 }
266
267 return NULL;
268}
269
Andreas Huber4c483422009-09-02 16:05:36 -0700270template<class T>
271static void InitOMXParams(T *params) {
272 params->nSize = sizeof(T);
273 params->nVersion.s.nVersionMajor = 1;
274 params->nVersion.s.nVersionMinor = 0;
275 params->nVersion.s.nRevision = 0;
276 params->nVersion.s.nStep = 0;
277}
278
Andreas Hubere13526a2009-10-22 10:43:34 -0700279static bool IsSoftwareCodec(const char *componentName) {
James Dong5592bcc2010-10-22 17:10:43 -0700280 if (!strncmp("OMX.", componentName, 4)) {
281 return false;
Andreas Huberbe06d262009-08-14 14:37:10 -0700282 }
283
James Dong5592bcc2010-10-22 17:10:43 -0700284 return true;
Andreas Hubere13526a2009-10-22 10:43:34 -0700285}
286
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800287// A sort order in which non-OMX components are first,
James Dong5592bcc2010-10-22 17:10:43 -0700288// followed by software codecs, and followed by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700289static int CompareSoftwareCodecsFirst(
290 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800291 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
292 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
293
294 if (isNotOMX1) {
295 if (isNotOMX2) { return 0; }
296 return -1;
297 }
298 if (isNotOMX2) {
299 return 1;
300 }
301
Andreas Hubere13526a2009-10-22 10:43:34 -0700302 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
303 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
304
305 if (isSoftwareCodec1) {
306 if (isSoftwareCodec2) { return 0; }
307 return -1;
308 }
309
310 if (isSoftwareCodec2) {
311 return 1;
312 }
313
314 return 0;
315}
316
317// static
Andreas Huber1e194162010-10-06 16:43:57 -0700318uint32_t OMXCodec::getComponentQuirks(
319 const char *componentName, bool isEncoder) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700320 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700321
Dima Zavin30ba6cb2010-08-23 11:10:03 -0700322 if (!strcmp(componentName, "OMX.Nvidia.amr.decoder") ||
323 !strcmp(componentName, "OMX.Nvidia.amrwb.decoder") ||
324 !strcmp(componentName, "OMX.Nvidia.aac.decoder") ||
325 !strcmp(componentName, "OMX.Nvidia.mp3.decoder")) {
326 quirks |= kDecoderLiesAboutNumberOfChannels;
327 }
328
Andreas Huberbe06d262009-08-14 14:37:10 -0700329 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
330 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800331 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700332 }
333 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
334 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700335 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700336 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700337 }
338 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
339 quirks |= kRequiresLoadedToIdleAfterAllocation;
340 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700341 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dong90862e22010-08-26 19:12:59 -0700342 if (!strncmp(componentName, "OMX.qcom.video.encoder.avc", 26)) {
343
344 // The AVC encoder advertises the size of output buffers
345 // based on the input video resolution and assumes
346 // the worst/least compression ratio is 0.5. It is found that
347 // sometimes, the output buffer size is larger than
348 // size advertised by the encoder.
349 quirks |= kRequiresLargerEncoderOutputBuffer;
350 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700351 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700352 if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) {
353 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700354 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700355 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800356 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700357 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700358 if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) {
359 quirks |= kRequiresAllocateBufferOnInputPorts;
360 quirks |= kRequiresAllocateBufferOnOutputPorts;
361 quirks |= kDefersOutputBufferAllocation;
362 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700363
Andreas Huber2dc64d82009-09-11 12:58:53 -0700364 if (!strncmp(componentName, "OMX.TI.", 7)) {
365 // Apparently I must not use OMX_UseBuffer on either input or
366 // output ports on any of the TI components or quote:
367 // "(I) may have unexpected problem (sic) which can be timing related
368 // and hard to reproduce."
369
370 quirks |= kRequiresAllocateBufferOnInputPorts;
371 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dongdca66e12010-06-14 11:14:38 -0700372 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) {
James Dong4f501f02010-06-07 14:41:41 -0700373 quirks |= kAvoidMemcopyInputRecordingFrames;
374 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700375 }
376
Andreas Huberb8de9572010-02-22 14:58:45 -0800377 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
378 quirks |= kInputBufferSizesAreBogus;
379 }
380
Andreas Huber1e194162010-10-06 16:43:57 -0700381 if (!strncmp(componentName, "OMX.SEC.", 8) && !isEncoder) {
382 // These output buffers contain no video data, just some
383 // opaque information that allows the overlay to display their
384 // contents.
385 quirks |= kOutputBuffersAreUnreadable;
386 }
387
Andreas Hubere13526a2009-10-22 10:43:34 -0700388 return quirks;
389}
390
391// static
392void OMXCodec::findMatchingCodecs(
393 const char *mime,
394 bool createEncoder, const char *matchComponentName,
395 uint32_t flags,
396 Vector<String8> *matchingCodecs) {
397 matchingCodecs->clear();
398
399 for (int index = 0;; ++index) {
400 const char *componentName;
401
402 if (createEncoder) {
403 componentName = GetCodec(
404 kEncoderInfo,
405 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
406 mime, index);
407 } else {
408 componentName = GetCodec(
409 kDecoderInfo,
410 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
411 mime, index);
412 }
413
414 if (!componentName) {
415 break;
416 }
417
418 // If a specific codec is requested, skip the non-matching ones.
419 if (matchComponentName && strcmp(componentName, matchComponentName)) {
420 continue;
421 }
422
James Dong170a9292010-10-22 17:28:15 -0700423 // When requesting software-only codecs, only push software codecs
424 // When requesting hardware-only codecs, only push hardware codecs
425 // When there is request neither for software-only nor for
426 // hardware-only codecs, push all codecs
427 if (((flags & kSoftwareCodecsOnly) && IsSoftwareCodec(componentName)) ||
428 ((flags & kHardwareCodecsOnly) && !IsSoftwareCodec(componentName)) ||
429 (!(flags & (kSoftwareCodecsOnly | kHardwareCodecsOnly)))) {
430
431 matchingCodecs->push(String8(componentName));
432 }
Andreas Hubere13526a2009-10-22 10:43:34 -0700433 }
434
435 if (flags & kPreferSoftwareCodecs) {
436 matchingCodecs->sort(CompareSoftwareCodecsFirst);
437 }
438}
439
440// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800441sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700442 const sp<IOMX> &omx,
443 const sp<MetaData> &meta, bool createEncoder,
444 const sp<MediaSource> &source,
445 const char *matchComponentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700446 uint32_t flags,
447 const sp<ANativeWindow> &nativeWindow) {
Andreas Hubere13526a2009-10-22 10:43:34 -0700448 const char *mime;
449 bool success = meta->findCString(kKeyMIMEType, &mime);
450 CHECK(success);
451
452 Vector<String8> matchingCodecs;
453 findMatchingCodecs(
454 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
455
456 if (matchingCodecs.isEmpty()) {
457 return NULL;
458 }
459
460 sp<OMXCodecObserver> observer = new OMXCodecObserver;
461 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700462
463 const char *componentName;
464 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
465 componentName = matchingCodecs[i].string();
466
James Dong17299ab2010-05-14 15:45:22 -0700467 sp<MediaSource> softwareCodec = createEncoder?
468 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800469 InstantiateSoftwareCodec(componentName, source);
470
471 if (softwareCodec != NULL) {
472 LOGV("Successfully allocated software codec '%s'", componentName);
473
474 return softwareCodec;
475 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800476
Andreas Hubere13526a2009-10-22 10:43:34 -0700477 LOGV("Attempting to allocate OMX node '%s'", componentName);
478
Andreas Huber5a40e392010-10-18 09:57:42 -0700479 uint32_t quirks = getComponentQuirks(componentName, createEncoder);
480
481 if (!createEncoder
482 && (quirks & kOutputBuffersAreUnreadable)
483 && (flags & kClientNeedsFramebuffer)) {
484 if (strncmp(componentName, "OMX.SEC.", 8)) {
485 // For OMX.SEC.* decoders we can enable a special mode that
486 // gives the client access to the framebuffer contents.
487
488 LOGW("Component '%s' does not give the client access to "
489 "the framebuffer contents. Skipping.",
490 componentName);
491
492 continue;
493 }
494 }
495
Andreas Hubere13526a2009-10-22 10:43:34 -0700496 status_t err = omx->allocateNode(componentName, observer, &node);
497 if (err == OK) {
498 LOGV("Successfully allocated OMX node '%s'", componentName);
499
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700500 sp<OMXCodec> codec = new OMXCodec(
Andreas Huber5a40e392010-10-18 09:57:42 -0700501 omx, node, quirks,
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700502 createEncoder, mime, componentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700503 source, nativeWindow);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700504
505 observer->setCodec(codec);
506
Andreas Huber4c19bf92010-09-08 14:32:20 -0700507 err = codec->configureCodec(meta, flags);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700508
509 if (err == OK) {
510 return codec;
511 }
512
513 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700514 }
515 }
516
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700517 return NULL;
518}
Andreas Hubere13526a2009-10-22 10:43:34 -0700519
Andreas Huber4c19bf92010-09-08 14:32:20 -0700520status_t OMXCodec::configureCodec(const sp<MetaData> &meta, uint32_t flags) {
James Dong05c2fd52010-11-02 13:20:11 -0700521 mIsMetaDataStoredInVideoBuffers = false;
522 if (flags & kStoreMetaDataInVideoBuffers) {
523 mIsMetaDataStoredInVideoBuffers = true;
524 }
James Dong5f3ab062011-01-25 16:31:28 -0800525
526 mOnlySubmitOneBufferAtOneTime = false;
527 if (flags & kOnlySubmitOneInputBufferAtOneTime) {
528 mOnlySubmitOneBufferAtOneTime = true;
529 }
530
Glenn Kastenb8763f62011-01-28 12:37:51 -0800531 mEnableGrallocUsageProtected = false;
532 if (flags & kEnableGrallocUsageProtected) {
533 mEnableGrallocUsageProtected = true;
534 }
535 LOGV("configureCodec protected=%d", mEnableGrallocUsageProtected);
536
Andreas Huber4c19bf92010-09-08 14:32:20 -0700537 if (!(flags & kIgnoreCodecSpecificData)) {
538 uint32_t type;
539 const void *data;
540 size_t size;
541 if (meta->findData(kKeyESDS, &type, &data, &size)) {
542 ESDS esds((const char *)data, size);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800543 CHECK_EQ(esds.InitCheck(), (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700544
Andreas Huber4c19bf92010-09-08 14:32:20 -0700545 const void *codec_specific_data;
546 size_t codec_specific_data_size;
547 esds.getCodecSpecificInfo(
548 &codec_specific_data, &codec_specific_data_size);
Andreas Huberbe06d262009-08-14 14:37:10 -0700549
Andreas Huber4c19bf92010-09-08 14:32:20 -0700550 addCodecSpecificData(
551 codec_specific_data, codec_specific_data_size);
552 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
553 // Parse the AVCDecoderConfigurationRecord
Andreas Huberebf66ea2009-08-19 13:32:58 -0700554
Andreas Huber4c19bf92010-09-08 14:32:20 -0700555 const uint8_t *ptr = (const uint8_t *)data;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700556
Andreas Huber4c19bf92010-09-08 14:32:20 -0700557 CHECK(size >= 7);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800558 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1
Andreas Huber4c19bf92010-09-08 14:32:20 -0700559 uint8_t profile = ptr[1];
560 uint8_t level = ptr[3];
Andreas Huberebf66ea2009-08-19 13:32:58 -0700561
Andreas Huber4c19bf92010-09-08 14:32:20 -0700562 // There is decodable content out there that fails the following
563 // assertion, let's be lenient for now...
564 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700565
Andreas Huber4c19bf92010-09-08 14:32:20 -0700566 size_t lengthSize = 1 + (ptr[4] & 3);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700567
Andreas Huber4c19bf92010-09-08 14:32:20 -0700568 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
569 // violates it...
570 // CHECK((ptr[5] >> 5) == 7); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700571
Andreas Huber4c19bf92010-09-08 14:32:20 -0700572 size_t numSeqParameterSets = ptr[5] & 31;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700573
Andreas Huber4c19bf92010-09-08 14:32:20 -0700574 ptr += 6;
575 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700576
Andreas Huber4c19bf92010-09-08 14:32:20 -0700577 for (size_t i = 0; i < numSeqParameterSets; ++i) {
578 CHECK(size >= 2);
579 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700580
Andreas Huber4c19bf92010-09-08 14:32:20 -0700581 ptr += 2;
582 size -= 2;
Andreas Huberbe06d262009-08-14 14:37:10 -0700583
Andreas Huber4c19bf92010-09-08 14:32:20 -0700584 CHECK(size >= length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700585
Andreas Huber4c19bf92010-09-08 14:32:20 -0700586 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700587
Andreas Huber4c19bf92010-09-08 14:32:20 -0700588 ptr += length;
589 size -= length;
590 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700591
Andreas Huber4c19bf92010-09-08 14:32:20 -0700592 CHECK(size >= 1);
593 size_t numPictureParameterSets = *ptr;
594 ++ptr;
595 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700596
Andreas Huber4c19bf92010-09-08 14:32:20 -0700597 for (size_t i = 0; i < numPictureParameterSets; ++i) {
598 CHECK(size >= 2);
599 size_t length = U16_AT(ptr);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700600
Andreas Huber4c19bf92010-09-08 14:32:20 -0700601 ptr += 2;
602 size -= 2;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700603
Andreas Huber4c19bf92010-09-08 14:32:20 -0700604 CHECK(size >= length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700605
Andreas Huber4c19bf92010-09-08 14:32:20 -0700606 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700607
Andreas Huber4c19bf92010-09-08 14:32:20 -0700608 ptr += length;
609 size -= length;
610 }
Andreas Huberebf66ea2009-08-19 13:32:58 -0700611
Andreas Huber6ac2cb22010-11-18 14:06:07 -0800612 CODEC_LOGI(
Andreas Huber4c19bf92010-09-08 14:32:20 -0700613 "AVC profile = %d (%s), level = %d",
614 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700615
Andreas Huber4c19bf92010-09-08 14:32:20 -0700616 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
617 && (profile != kAVCProfileBaseline || level > 30)) {
618 // This stream exceeds the decoder's capabilities. The decoder
619 // does not handle this gracefully and would clobber the heap
620 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700621
Andreas Huber4c19bf92010-09-08 14:32:20 -0700622 LOGE("Profile and/or level exceed the decoder's capabilities.");
623 return ERROR_UNSUPPORTED;
624 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700625 }
626 }
627
James Dong17299ab2010-05-14 15:45:22 -0700628 int32_t bitRate = 0;
629 if (mIsEncoder) {
630 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
631 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700632 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700633 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700634 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700635 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700636 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700637 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700638 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700639 int32_t numChannels, sampleRate;
640 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
641 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
642
James Dong17299ab2010-05-14 15:45:22 -0700643 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700644 }
James Dongabed93a2010-04-22 17:27:04 -0700645
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700646 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700647
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700648 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700649 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700650 } else {
James Dong1244eab2010-06-08 11:58:53 -0700651 int32_t width, height;
652 bool success = meta->findInt32(kKeyWidth, &width);
653 success = success && meta->findInt32(kKeyHeight, &height);
654 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700655 status_t err = setVideoOutputFormat(
656 mMIME, width, height);
657
658 if (err != OK) {
659 return err;
660 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700661 }
662 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700663
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700664 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
665 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700666 OMX_COLOR_FORMATTYPE format =
667 OMX_COLOR_Format32bitARGB8888;
668 // OMX_COLOR_FormatYUV420PackedPlanar;
669 // OMX_COLOR_FormatCbYCrY;
670 // OMX_COLOR_FormatYUV411Planar;
671
672 int32_t width, height;
673 bool success = meta->findInt32(kKeyWidth, &width);
674 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700675
676 int32_t compressedSize;
677 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700678 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700679
680 CHECK(success);
681 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700682
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700683 setImageOutputFormat(format, width, height);
684 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700685 }
686
Andreas Huberda050cf22009-09-02 14:01:43 -0700687 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800688 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700689 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700690 }
691
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700692 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700693 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
694 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700695 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700696 }
697
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700698 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700699
Andreas Huber5a40e392010-10-18 09:57:42 -0700700 if ((flags & kClientNeedsFramebuffer)
701 && !strncmp(mComponentName, "OMX.SEC.", 8)) {
702 OMX_INDEXTYPE index;
703
704 status_t err =
705 mOMX->getExtensionIndex(
706 mNode,
707 "OMX.SEC.index.ThumbnailMode",
708 &index);
709
710 if (err != OK) {
711 return err;
712 }
713
714 OMX_BOOL enable = OMX_TRUE;
715 err = mOMX->setConfig(mNode, index, &enable, sizeof(enable));
716
717 if (err != OK) {
718 CODEC_LOGE("setConfig('OMX.SEC.index.ThumbnailMode') "
719 "returned error 0x%08x", err);
720
721 return err;
722 }
723
724 mQuirks &= ~kOutputBuffersAreUnreadable;
725 }
726
Jamie Gennisdbfb32e2010-10-20 15:53:59 -0700727 if (mNativeWindow != NULL
728 && !mIsEncoder
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700729 && !strncasecmp(mMIME, "video/", 6)
730 && !strncmp(mComponentName, "OMX.", 4)) {
731 status_t err = initNativeWindow();
732 if (err != OK) {
733 return err;
734 }
735 }
736
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700737 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700738}
739
Andreas Huberda050cf22009-09-02 14:01:43 -0700740void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
741 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700742 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700743 def.nPortIndex = portIndex;
744
Andreas Huber784202e2009-10-15 13:46:54 -0700745 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700746 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800747 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -0700748
Andreas Huberb8de9572010-02-22 14:58:45 -0800749 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
750 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700751 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700752 }
753
Andreas Huber784202e2009-10-15 13:46:54 -0700754 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700755 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800756 CHECK_EQ(err, (status_t)OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800757
758 err = mOMX->getParameter(
759 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800760 CHECK_EQ(err, (status_t)OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800761
762 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800763 if (portIndex == kPortIndexInput
764 && (mQuirks & kInputBufferSizesAreBogus)) {
765 CHECK_EQ(def.nBufferSize, size);
766 } else {
767 CHECK(def.nBufferSize >= size);
768 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700769}
770
Andreas Huberbe06d262009-08-14 14:37:10 -0700771status_t OMXCodec::setVideoPortFormatType(
772 OMX_U32 portIndex,
773 OMX_VIDEO_CODINGTYPE compressionFormat,
774 OMX_COLOR_FORMATTYPE colorFormat) {
775 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700776 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700777 format.nPortIndex = portIndex;
778 format.nIndex = 0;
779 bool found = false;
780
781 OMX_U32 index = 0;
782 for (;;) {
783 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700784 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700785 mNode, OMX_IndexParamVideoPortFormat,
786 &format, sizeof(format));
787
788 if (err != OK) {
789 return err;
790 }
791
792 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700793 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700794
795#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700796 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700797 portIndex,
798 index, format.eCompressionFormat, format.eColorFormat);
799#endif
800
801 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
802 if (portIndex == kPortIndexInput
803 && colorFormat == format.eColorFormat) {
804 // eCompressionFormat does not seem right.
805 found = true;
806 break;
807 }
808 if (portIndex == kPortIndexOutput
809 && compressionFormat == format.eCompressionFormat) {
810 // eColorFormat does not seem right.
811 found = true;
812 break;
813 }
814 }
815
816 if (format.eCompressionFormat == compressionFormat
817 && format.eColorFormat == colorFormat) {
818 found = true;
819 break;
820 }
821
822 ++index;
823 }
824
825 if (!found) {
826 return UNKNOWN_ERROR;
827 }
828
Andreas Huber53a76bd2009-10-06 16:20:44 -0700829 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700830 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700831 mNode, OMX_IndexParamVideoPortFormat,
832 &format, sizeof(format));
833
834 return err;
835}
836
Andreas Huberb482ce82009-10-29 12:02:48 -0700837static size_t getFrameSize(
838 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
839 switch (colorFormat) {
840 case OMX_COLOR_FormatYCbYCr:
841 case OMX_COLOR_FormatCbYCrY:
842 return width * height * 2;
843
Andreas Huber71c27d92010-03-19 11:43:15 -0700844 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700845 case OMX_COLOR_FormatYUV420SemiPlanar:
846 return (width * height * 3) / 2;
847
848 default:
849 CHECK(!"Should not be here. Unsupported color format.");
850 break;
851 }
852}
853
James Dongafd97e82010-08-03 17:19:23 -0700854status_t OMXCodec::findTargetColorFormat(
855 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) {
856 LOGV("findTargetColorFormat");
857 CHECK(mIsEncoder);
858
859 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
860 int32_t targetColorFormat;
861 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) {
862 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat;
863 } else {
864 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
865 *colorFormat = OMX_COLOR_FormatYCbYCr;
866 }
867 }
868
869 // Check whether the target color format is supported.
870 return isColorFormatSupported(*colorFormat, kPortIndexInput);
871}
872
873status_t OMXCodec::isColorFormatSupported(
874 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) {
875 LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat));
876
877 // Enumerate all the color formats supported by
878 // the omx component to see whether the given
879 // color format is supported.
880 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
881 InitOMXParams(&portFormat);
882 portFormat.nPortIndex = portIndex;
883 OMX_U32 index = 0;
884 portFormat.nIndex = index;
885 while (true) {
886 if (OMX_ErrorNone != mOMX->getParameter(
887 mNode, OMX_IndexParamVideoPortFormat,
888 &portFormat, sizeof(portFormat))) {
James Dongb5024da2010-09-13 16:30:51 -0700889 break;
James Dongafd97e82010-08-03 17:19:23 -0700890 }
891 // Make sure that omx component does not overwrite
892 // the incremented index (bug 2897413).
893 CHECK_EQ(index, portFormat.nIndex);
894 if ((portFormat.eColorFormat == colorFormat)) {
895 LOGV("Found supported color format: %d", portFormat.eColorFormat);
896 return OK; // colorFormat is supported!
897 }
898 ++index;
899 portFormat.nIndex = index;
900
901 // OMX Spec defines less than 50 color formats
902 // 1000 is more than enough for us to tell whether the omx
903 // component in question is buggy or not.
904 if (index >= 1000) {
905 LOGE("More than %ld color formats are supported???", index);
906 break;
907 }
908 }
James Dongb5024da2010-09-13 16:30:51 -0700909
910 LOGE("color format %d is not supported", colorFormat);
James Dongafd97e82010-08-03 17:19:23 -0700911 return UNKNOWN_ERROR;
912}
913
Andreas Huberbe06d262009-08-14 14:37:10 -0700914void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700915 const char *mime, const sp<MetaData>& meta) {
916
917 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
918 bool success = meta->findInt32(kKeyWidth, &width);
919 success = success && meta->findInt32(kKeyHeight, &height);
James Dongaac193c2010-11-10 20:43:53 -0800920 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -0700921 success = success && meta->findInt32(kKeyBitRate, &bitRate);
922 success = success && meta->findInt32(kKeyStride, &stride);
923 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
924 CHECK(success);
925 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700926
927 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700928 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700929 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700930 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700931 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700932 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700933 compressionFormat = OMX_VIDEO_CodingH263;
934 } else {
935 LOGE("Not a supported video mime type: %s", mime);
936 CHECK(!"Should not be here. Not a supported video mime type.");
937 }
938
James Dongafd97e82010-08-03 17:19:23 -0700939 OMX_COLOR_FORMATTYPE colorFormat;
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800940 CHECK_EQ((status_t)OK, findTargetColorFormat(meta, &colorFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -0700941
James Dongb00e2462010-04-26 17:48:26 -0700942 status_t err;
943 OMX_PARAM_PORTDEFINITIONTYPE def;
944 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
945
946 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700947 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700948 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800949 colorFormat), (status_t)OK);
James Dong4f501f02010-06-07 14:41:41 -0700950
James Dongb00e2462010-04-26 17:48:26 -0700951 InitOMXParams(&def);
952 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700953
James Dongb00e2462010-04-26 17:48:26 -0700954 err = mOMX->getParameter(
955 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800956 CHECK_EQ(err, (status_t)OK);
James Dongb00e2462010-04-26 17:48:26 -0700957
James Dong1244eab2010-06-08 11:58:53 -0700958 def.nBufferSize = getFrameSize(colorFormat,
959 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700960
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800961 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
James Dongb00e2462010-04-26 17:48:26 -0700962
963 video_def->nFrameWidth = width;
964 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700965 video_def->nStride = stride;
966 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700967 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700968 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
969 video_def->eColorFormat = colorFormat;
970
James Dongb00e2462010-04-26 17:48:26 -0700971 err = mOMX->setParameter(
972 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800973 CHECK_EQ(err, (status_t)OK);
James Dongb00e2462010-04-26 17:48:26 -0700974
975 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700976 CHECK_EQ(setVideoPortFormatType(
977 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800978 (status_t)OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700979 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700980 def.nPortIndex = kPortIndexOutput;
981
James Dongb00e2462010-04-26 17:48:26 -0700982 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700983 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
984
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800985 CHECK_EQ(err, (status_t)OK);
986 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -0700987
988 video_def->nFrameWidth = width;
989 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700990 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700991 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700992 video_def->eCompressionFormat = compressionFormat;
993 video_def->eColorFormat = OMX_COLOR_FormatUnused;
James Dong90862e22010-08-26 19:12:59 -0700994 if (mQuirks & kRequiresLargerEncoderOutputBuffer) {
995 // Increases the output buffer size
996 def.nBufferSize = ((def.nBufferSize * 3) >> 1);
997 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700998
Andreas Huber784202e2009-10-15 13:46:54 -0700999 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001000 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001001 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001002
James Dongb00e2462010-04-26 17:48:26 -07001003 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -07001004 switch (compressionFormat) {
1005 case OMX_VIDEO_CodingMPEG4:
1006 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001007 CHECK_EQ(setupMPEG4EncoderParameters(meta), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001008 break;
1009 }
1010
1011 case OMX_VIDEO_CodingH263:
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001012 CHECK_EQ(setupH263EncoderParameters(meta), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001013 break;
1014
Andreas Huberea6a38c2009-11-16 15:43:38 -08001015 case OMX_VIDEO_CodingAVC:
1016 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001017 CHECK_EQ(setupAVCEncoderParameters(meta), (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001018 break;
1019 }
1020
Andreas Huberb482ce82009-10-29 12:02:48 -07001021 default:
1022 CHECK(!"Support for this compressionFormat to be implemented.");
1023 break;
1024 }
1025}
1026
James Dong1244eab2010-06-08 11:58:53 -07001027static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
1028 if (iFramesInterval < 0) {
1029 return 0xFFFFFFFF;
1030 } else if (iFramesInterval == 0) {
1031 return 0;
1032 }
1033 OMX_U32 ret = frameRate * iFramesInterval;
1034 CHECK(ret > 1);
1035 return ret;
1036}
1037
James Dongc0ab2a62010-06-29 16:29:19 -07001038status_t OMXCodec::setupErrorCorrectionParameters() {
1039 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
1040 InitOMXParams(&errorCorrectionType);
1041 errorCorrectionType.nPortIndex = kPortIndexOutput;
1042
1043 status_t err = mOMX->getParameter(
1044 mNode, OMX_IndexParamVideoErrorCorrection,
1045 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001046 if (err != OK) {
1047 LOGW("Error correction param query is not supported");
1048 return OK; // Optional feature. Ignore this failure
1049 }
James Dongc0ab2a62010-06-29 16:29:19 -07001050
1051 errorCorrectionType.bEnableHEC = OMX_FALSE;
1052 errorCorrectionType.bEnableResync = OMX_TRUE;
1053 errorCorrectionType.nResynchMarkerSpacing = 256;
1054 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
1055 errorCorrectionType.bEnableRVLC = OMX_FALSE;
1056
1057 err = mOMX->setParameter(
1058 mNode, OMX_IndexParamVideoErrorCorrection,
1059 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001060 if (err != OK) {
1061 LOGW("Error correction param configuration is not supported");
1062 }
1063
1064 // Optional feature. Ignore the failure.
James Dongc0ab2a62010-06-29 16:29:19 -07001065 return OK;
1066}
1067
1068status_t OMXCodec::setupBitRate(int32_t bitRate) {
1069 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
1070 InitOMXParams(&bitrateType);
1071 bitrateType.nPortIndex = kPortIndexOutput;
1072
1073 status_t err = mOMX->getParameter(
1074 mNode, OMX_IndexParamVideoBitrate,
1075 &bitrateType, sizeof(bitrateType));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001076 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001077
1078 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
1079 bitrateType.nTargetBitrate = bitRate;
1080
1081 err = mOMX->setParameter(
1082 mNode, OMX_IndexParamVideoBitrate,
1083 &bitrateType, sizeof(bitrateType));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001084 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001085 return OK;
1086}
1087
James Dong81c929a2010-07-01 15:02:14 -07001088status_t OMXCodec::getVideoProfileLevel(
1089 const sp<MetaData>& meta,
1090 const CodecProfileLevel& defaultProfileLevel,
1091 CodecProfileLevel &profileLevel) {
1092 CODEC_LOGV("Default profile: %ld, level %ld",
1093 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
1094
1095 // Are the default profile and level overwriten?
1096 int32_t profile, level;
1097 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
1098 profile = defaultProfileLevel.mProfile;
1099 }
1100 if (!meta->findInt32(kKeyVideoLevel, &level)) {
1101 level = defaultProfileLevel.mLevel;
1102 }
1103 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
1104
1105 // Are the target profile and level supported by the encoder?
1106 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
1107 InitOMXParams(&param);
1108 param.nPortIndex = kPortIndexOutput;
1109 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
1110 status_t err = mOMX->getParameter(
1111 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
1112 &param, sizeof(param));
1113
James Dongdfb89912010-09-15 21:07:52 -07001114 if (err != OK) break;
James Dong81c929a2010-07-01 15:02:14 -07001115
1116 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
1117 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -07001118 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -07001119 supportedProfile, supportedLevel);
1120
1121 if (profile == supportedProfile &&
James Dongdfb89912010-09-15 21:07:52 -07001122 level <= supportedLevel) {
1123 // We can further check whether the level is a valid
1124 // value; but we will leave that to the omx encoder component
1125 // via OMX_SetParameter call.
James Dong81c929a2010-07-01 15:02:14 -07001126 profileLevel.mProfile = profile;
1127 profileLevel.mLevel = level;
1128 return OK;
1129 }
1130 }
1131
1132 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
1133 profile, level);
1134 return BAD_VALUE;
1135}
1136
James Dongc0ab2a62010-06-29 16:29:19 -07001137status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
1138 int32_t iFramesInterval, frameRate, bitRate;
1139 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001140 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dongc0ab2a62010-06-29 16:29:19 -07001141 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1142 CHECK(success);
1143 OMX_VIDEO_PARAM_H263TYPE h263type;
1144 InitOMXParams(&h263type);
1145 h263type.nPortIndex = kPortIndexOutput;
1146
1147 status_t err = mOMX->getParameter(
1148 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001149 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001150
1151 h263type.nAllowedPictureTypes =
1152 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1153
1154 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1155 if (h263type.nPFrames == 0) {
1156 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1157 }
1158 h263type.nBFrames = 0;
1159
James Dong81c929a2010-07-01 15:02:14 -07001160 // Check profile and level parameters
1161 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001162 defaultProfileLevel.mProfile = h263type.eProfile;
1163 defaultProfileLevel.mLevel = h263type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001164 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1165 if (err != OK) return err;
1166 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1167 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001168
1169 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1170 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1171 h263type.nPictureHeaderRepetition = 0;
1172 h263type.nGOBHeaderInterval = 0;
1173
1174 err = mOMX->setParameter(
1175 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001176 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001177
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001178 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
1179 CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001180
1181 return OK;
1182}
1183
James Dong1244eab2010-06-08 11:58:53 -07001184status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1185 int32_t iFramesInterval, frameRate, bitRate;
1186 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001187 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -07001188 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1189 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001190 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1191 InitOMXParams(&mpeg4type);
1192 mpeg4type.nPortIndex = kPortIndexOutput;
1193
1194 status_t err = mOMX->getParameter(
1195 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001196 CHECK_EQ(err, (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001197
1198 mpeg4type.nSliceHeaderSpacing = 0;
1199 mpeg4type.bSVH = OMX_FALSE;
1200 mpeg4type.bGov = OMX_FALSE;
1201
1202 mpeg4type.nAllowedPictureTypes =
1203 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1204
James Dong1244eab2010-06-08 11:58:53 -07001205 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1206 if (mpeg4type.nPFrames == 0) {
1207 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1208 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001209 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001210 mpeg4type.nIDCVLCThreshold = 0;
1211 mpeg4type.bACPred = OMX_TRUE;
1212 mpeg4type.nMaxPacketSize = 256;
1213 mpeg4type.nTimeIncRes = 1000;
1214 mpeg4type.nHeaderExtension = 0;
1215 mpeg4type.bReversibleVLC = OMX_FALSE;
1216
James Dong81c929a2010-07-01 15:02:14 -07001217 // Check profile and level parameters
1218 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001219 defaultProfileLevel.mProfile = mpeg4type.eProfile;
1220 defaultProfileLevel.mLevel = mpeg4type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001221 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1222 if (err != OK) return err;
1223 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1224 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001225
1226 err = mOMX->setParameter(
1227 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001228 CHECK_EQ(err, (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001229
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001230 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
1231 CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001232
1233 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001234}
1235
James Dong1244eab2010-06-08 11:58:53 -07001236status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1237 int32_t iFramesInterval, frameRate, bitRate;
1238 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001239 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -07001240 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1241 CHECK(success);
1242
Andreas Huberea6a38c2009-11-16 15:43:38 -08001243 OMX_VIDEO_PARAM_AVCTYPE h264type;
1244 InitOMXParams(&h264type);
1245 h264type.nPortIndex = kPortIndexOutput;
1246
1247 status_t err = mOMX->getParameter(
1248 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001249 CHECK_EQ(err, (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001250
1251 h264type.nAllowedPictureTypes =
1252 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1253
1254 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001255 h264type.nBFrames = 0; // No B frames support yet
1256 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1257 if (h264type.nPFrames == 0) {
1258 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1259 }
James Dong81c929a2010-07-01 15:02:14 -07001260
1261 // Check profile and level parameters
1262 CodecProfileLevel defaultProfileLevel, profileLevel;
1263 defaultProfileLevel.mProfile = h264type.eProfile;
1264 defaultProfileLevel.mLevel = h264type.eLevel;
1265 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1266 if (err != OK) return err;
1267 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1268 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1269
1270 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1271 h264type.bUseHadamard = OMX_TRUE;
1272 h264type.nRefFrames = 1;
1273 h264type.nRefIdx10ActiveMinus1 = 0;
1274 h264type.nRefIdx11ActiveMinus1 = 0;
1275 h264type.bEntropyCodingCABAC = OMX_FALSE;
1276 h264type.bWeightedPPrediction = OMX_FALSE;
1277 h264type.bconstIpred = OMX_FALSE;
1278 h264type.bDirect8x8Inference = OMX_FALSE;
1279 h264type.bDirectSpatialTemporal = OMX_FALSE;
1280 h264type.nCabacInitIdc = 0;
1281 }
1282
1283 if (h264type.nBFrames != 0) {
1284 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1285 }
1286
Andreas Huberea6a38c2009-11-16 15:43:38 -08001287 h264type.bEnableUEP = OMX_FALSE;
1288 h264type.bEnableFMO = OMX_FALSE;
1289 h264type.bEnableASO = OMX_FALSE;
1290 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001291 h264type.bFrameMBsOnly = OMX_TRUE;
1292 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001293 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1294
pgudadhe9c305322010-07-26 13:59:29 -07001295 if (!strcasecmp("OMX.Nvidia.h264.encoder", mComponentName)) {
1296 h264type.eLevel = OMX_VIDEO_AVCLevelMax;
1297 }
1298
Andreas Huberea6a38c2009-11-16 15:43:38 -08001299 err = mOMX->setParameter(
1300 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001301 CHECK_EQ(err, (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001302
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001303 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001304
1305 return OK;
1306}
1307
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001308status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001309 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001310 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001311
Andreas Huberbe06d262009-08-14 14:37:10 -07001312 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001313 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001314 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001315 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001316 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001317 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001318 compressionFormat = OMX_VIDEO_CodingH263;
1319 } else {
1320 LOGE("Not a supported video mime type: %s", mime);
1321 CHECK(!"Should not be here. Not a supported video mime type.");
1322 }
1323
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001324 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001325 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1326
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001327 if (err != OK) {
1328 return err;
1329 }
1330
Andreas Huberbe06d262009-08-14 14:37:10 -07001331#if 1
1332 {
1333 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001334 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001335 format.nPortIndex = kPortIndexOutput;
1336 format.nIndex = 0;
1337
Andreas Huber784202e2009-10-15 13:46:54 -07001338 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001339 mNode, OMX_IndexParamVideoPortFormat,
1340 &format, sizeof(format));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001341 CHECK_EQ(err, (status_t)OK);
1342 CHECK_EQ((int)format.eCompressionFormat, (int)OMX_VIDEO_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07001343
1344 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1345
1346 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1347 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1348 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1349 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1350
Andreas Huber784202e2009-10-15 13:46:54 -07001351 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001352 mNode, OMX_IndexParamVideoPortFormat,
1353 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001354
1355 if (err != OK) {
1356 return err;
1357 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001358 }
1359#endif
1360
1361 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001362 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001363 def.nPortIndex = kPortIndexInput;
1364
Andreas Huber4c483422009-09-02 16:05:36 -07001365 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1366
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001367 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001368 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1369
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001370 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001371
1372#if 1
1373 // XXX Need a (much) better heuristic to compute input buffer sizes.
1374 const size_t X = 64 * 1024;
1375 if (def.nBufferSize < X) {
1376 def.nBufferSize = X;
1377 }
1378#endif
1379
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001380 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -07001381
1382 video_def->nFrameWidth = width;
1383 video_def->nFrameHeight = height;
1384
Andreas Huberb482ce82009-10-29 12:02:48 -07001385 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001386 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1387
Andreas Huber784202e2009-10-15 13:46:54 -07001388 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001389 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001390
1391 if (err != OK) {
1392 return err;
1393 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001394
1395 ////////////////////////////////////////////////////////////////////////////
1396
Andreas Huber4c483422009-09-02 16:05:36 -07001397 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001398 def.nPortIndex = kPortIndexOutput;
1399
Andreas Huber784202e2009-10-15 13:46:54 -07001400 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001401 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001402 CHECK_EQ(err, (status_t)OK);
1403 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -07001404
1405#if 0
1406 def.nBufferSize =
1407 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1408#endif
1409
1410 video_def->nFrameWidth = width;
1411 video_def->nFrameHeight = height;
1412
Andreas Huber784202e2009-10-15 13:46:54 -07001413 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001414 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001415
1416 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001417}
1418
Andreas Huberbe06d262009-08-14 14:37:10 -07001419OMXCodec::OMXCodec(
1420 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001421 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001422 const char *mime,
1423 const char *componentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001424 const sp<MediaSource> &source,
1425 const sp<ANativeWindow> &nativeWindow)
Andreas Huberbe06d262009-08-14 14:37:10 -07001426 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001427 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001428 mNode(node),
1429 mQuirks(quirks),
1430 mIsEncoder(isEncoder),
1431 mMIME(strdup(mime)),
1432 mComponentName(strdup(componentName)),
1433 mSource(source),
1434 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001435 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001436 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001437 mSignalledEOS(false),
1438 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001439 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001440 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001441 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1442 mTargetTimeUs(-1),
Andreas Huberb9289832011-02-08 13:10:25 -08001443 mOutputPortSettingsChangedPending(false),
Andreas Huber1f24b302010-06-10 11:12:39 -07001444 mLeftOverBuffer(NULL),
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001445 mPaused(false),
1446 mNativeWindow(nativeWindow) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001447 mPortStatus[kPortIndexInput] = ENABLED;
1448 mPortStatus[kPortIndexOutput] = ENABLED;
1449
Andreas Huber4c483422009-09-02 16:05:36 -07001450 setComponentRole();
1451}
1452
Andreas Hubere6c40962009-09-10 14:13:30 -07001453// static
1454void OMXCodec::setComponentRole(
1455 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1456 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001457 struct MimeToRole {
1458 const char *mime;
1459 const char *decoderRole;
1460 const char *encoderRole;
1461 };
1462
1463 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001464 { MEDIA_MIMETYPE_AUDIO_MPEG,
1465 "audio_decoder.mp3", "audio_encoder.mp3" },
1466 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1467 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1468 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1469 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1470 { MEDIA_MIMETYPE_AUDIO_AAC,
1471 "audio_decoder.aac", "audio_encoder.aac" },
1472 { MEDIA_MIMETYPE_VIDEO_AVC,
1473 "video_decoder.avc", "video_encoder.avc" },
1474 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1475 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1476 { MEDIA_MIMETYPE_VIDEO_H263,
1477 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001478 };
1479
1480 static const size_t kNumMimeToRole =
1481 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1482
1483 size_t i;
1484 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001485 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001486 break;
1487 }
1488 }
1489
1490 if (i == kNumMimeToRole) {
1491 return;
1492 }
1493
1494 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001495 isEncoder ? kMimeToRole[i].encoderRole
1496 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001497
1498 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001499 OMX_PARAM_COMPONENTROLETYPE roleParams;
1500 InitOMXParams(&roleParams);
1501
1502 strncpy((char *)roleParams.cRole,
1503 role, OMX_MAX_STRINGNAME_SIZE - 1);
1504
1505 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1506
Andreas Huber784202e2009-10-15 13:46:54 -07001507 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001508 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001509 &roleParams, sizeof(roleParams));
1510
1511 if (err != OK) {
1512 LOGW("Failed to set standard component role '%s'.", role);
1513 }
1514 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001515}
1516
Andreas Hubere6c40962009-09-10 14:13:30 -07001517void OMXCodec::setComponentRole() {
1518 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1519}
1520
Andreas Huberbe06d262009-08-14 14:37:10 -07001521OMXCodec::~OMXCodec() {
Andreas Huberf98197a2010-09-17 11:49:39 -07001522 mSource.clear();
1523
Andreas Hubereec06d32011-01-06 10:26:36 -08001524 CHECK(mState == LOADED || mState == ERROR || mState == LOADED_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001525
Andreas Huber784202e2009-10-15 13:46:54 -07001526 status_t err = mOMX->freeNode(mNode);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001527 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001528
1529 mNode = NULL;
1530 setState(DEAD);
1531
1532 clearCodecSpecificData();
1533
1534 free(mComponentName);
1535 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001536
Andreas Huberbe06d262009-08-14 14:37:10 -07001537 free(mMIME);
1538 mMIME = NULL;
1539}
1540
1541status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001542 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001543
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001544 CHECK_EQ((int)mState, (int)LOADED);
Andreas Huberbe06d262009-08-14 14:37:10 -07001545
1546 status_t err;
1547 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001548 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001549 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001550 setState(LOADED_TO_IDLE);
1551 }
1552
1553 err = allocateBuffers();
Jamie Gennisfd8b75a2010-12-17 15:07:02 -08001554 if (err != (status_t)OK) {
1555 return err;
1556 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001557
1558 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001559 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001560 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001561
1562 setState(LOADED_TO_IDLE);
1563 }
1564
1565 while (mState != EXECUTING && mState != ERROR) {
1566 mAsyncCompletion.wait(mLock);
1567 }
1568
1569 return mState == ERROR ? UNKNOWN_ERROR : OK;
1570}
1571
1572// static
1573bool OMXCodec::isIntermediateState(State state) {
1574 return state == LOADED_TO_IDLE
1575 || state == IDLE_TO_EXECUTING
1576 || state == EXECUTING_TO_IDLE
1577 || state == IDLE_TO_LOADED
1578 || state == RECONFIGURING;
1579}
1580
1581status_t OMXCodec::allocateBuffers() {
1582 status_t err = allocateBuffersOnPort(kPortIndexInput);
1583
1584 if (err != OK) {
1585 return err;
1586 }
1587
1588 return allocateBuffersOnPort(kPortIndexOutput);
1589}
1590
1591status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
Jamie Gennisdbfb32e2010-10-20 15:53:59 -07001592 if (mNativeWindow != NULL && portIndex == kPortIndexOutput) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001593 return allocateOutputBuffersFromNativeWindow();
1594 }
1595
Jamie Gennis66380f72011-04-07 19:03:56 -07001596 if (mEnableGrallocUsageProtected && portIndex == kPortIndexOutput) {
1597 LOGE("protected output buffers must be stent to an ANativeWindow");
1598 return PERMISSION_DENIED;
1599 }
1600
Andreas Huberbe06d262009-08-14 14:37:10 -07001601 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001602 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001603 def.nPortIndex = portIndex;
1604
Andreas Huber784202e2009-10-15 13:46:54 -07001605 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001606 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1607
1608 if (err != OK) {
1609 return err;
1610 }
1611
James Dong05c2fd52010-11-02 13:20:11 -07001612 if (mIsMetaDataStoredInVideoBuffers && portIndex == kPortIndexInput) {
1613 err = mOMX->storeMetaDataInBuffers(mNode, kPortIndexInput, OMX_TRUE);
1614 if (err != OK) {
1615 LOGE("Storing meta data in video buffers is not supported");
1616 return err;
1617 }
1618 }
1619
Andreas Huber57648e42010-08-04 10:14:30 -07001620 CODEC_LOGI("allocating %lu buffers of size %lu on %s port",
1621 def.nBufferCountActual, def.nBufferSize,
1622 portIndex == kPortIndexInput ? "input" : "output");
1623
Andreas Huber5c0a9132009-08-20 11:16:40 -07001624 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001625 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001626
Andreas Huberbe06d262009-08-14 14:37:10 -07001627 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001628 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001629 CHECK(mem.get() != NULL);
1630
Andreas Huberc712b9f2010-01-20 15:05:46 -08001631 BufferInfo info;
1632 info.mData = NULL;
1633 info.mSize = def.nBufferSize;
1634
Andreas Huberbe06d262009-08-14 14:37:10 -07001635 IOMX::buffer_id buffer;
1636 if (portIndex == kPortIndexInput
1637 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001638 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001639 mem.clear();
1640
Andreas Huberf1fe0642010-01-15 15:28:19 -08001641 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001642 mNode, portIndex, def.nBufferSize, &buffer,
1643 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001644 } else {
1645 err = mOMX->allocateBufferWithBackup(
1646 mNode, portIndex, mem, &buffer);
1647 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001648 } else if (portIndex == kPortIndexOutput
1649 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001650 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001651 mem.clear();
1652
Andreas Huberf1fe0642010-01-15 15:28:19 -08001653 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001654 mNode, portIndex, def.nBufferSize, &buffer,
1655 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001656 } else {
1657 err = mOMX->allocateBufferWithBackup(
1658 mNode, portIndex, mem, &buffer);
1659 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001660 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001661 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001662 }
1663
1664 if (err != OK) {
1665 LOGE("allocate_buffer_with_backup failed");
1666 return err;
1667 }
1668
Andreas Huberc712b9f2010-01-20 15:05:46 -08001669 if (mem != NULL) {
1670 info.mData = mem->pointer();
1671 }
1672
Andreas Huberbe06d262009-08-14 14:37:10 -07001673 info.mBuffer = buffer;
Andreas Huberbbbcf652010-12-07 14:25:54 -08001674 info.mStatus = OWNED_BY_US;
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
Andreas Huber5e9dc942011-01-21 14:32:31 -08001701status_t OMXCodec::applyRotation() {
1702 sp<MetaData> meta = mSource->getFormat();
1703
1704 int32_t rotationDegrees;
1705 if (!meta->findInt32(kKeyRotation, &rotationDegrees)) {
1706 rotationDegrees = 0;
1707 }
1708
1709 uint32_t transform;
1710 switch (rotationDegrees) {
1711 case 0: transform = 0; break;
1712 case 90: transform = HAL_TRANSFORM_ROT_90; break;
1713 case 180: transform = HAL_TRANSFORM_ROT_180; break;
1714 case 270: transform = HAL_TRANSFORM_ROT_270; break;
1715 default: transform = 0; break;
1716 }
1717
1718 status_t err = OK;
1719
1720 if (transform) {
1721 err = native_window_set_buffers_transform(
1722 mNativeWindow.get(), transform);
1723 }
1724
1725 return err;
1726}
1727
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001728status_t OMXCodec::allocateOutputBuffersFromNativeWindow() {
1729 // Get the number of buffers needed.
1730 OMX_PARAM_PORTDEFINITIONTYPE def;
1731 InitOMXParams(&def);
1732 def.nPortIndex = kPortIndexOutput;
1733
1734 status_t err = mOMX->getParameter(
1735 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1736 if (err != OK) {
1737 return err;
1738 }
1739
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001740 err = native_window_set_buffers_geometry(
1741 mNativeWindow.get(),
1742 def.format.video.nFrameWidth,
1743 def.format.video.nFrameHeight,
Jamie Gennis044ace62010-10-29 15:19:29 -07001744 def.format.video.eColorFormat);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001745
1746 if (err != 0) {
1747 LOGE("native_window_set_buffers_geometry failed: %s (%d)",
1748 strerror(-err), -err);
1749 return err;
1750 }
1751
Andreas Huber5e9dc942011-01-21 14:32:31 -08001752 err = applyRotation();
1753 if (err != OK) {
1754 return err;
1755 }
1756
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001757 // Set up the native window.
Jamie Gennis94c59802011-02-24 12:48:17 -08001758 OMX_U32 usage = 0;
1759 err = mOMX->getGraphicBufferUsage(mNode, kPortIndexOutput, &usage);
1760 if (err != 0) {
1761 LOGW("querying usage flags from OMX IL component failed: %d", err);
1762 // XXX: Currently this error is logged, but not fatal.
1763 usage = 0;
1764 }
Glenn Kastenb8763f62011-01-28 12:37:51 -08001765 if (mEnableGrallocUsageProtected) {
1766 usage |= GRALLOC_USAGE_PROTECTED;
1767 }
Jamie Gennis94c59802011-02-24 12:48:17 -08001768
Jamie Gennis66380f72011-04-07 19:03:56 -07001769 // Make sure to check whether either Stagefright or the video decoder
1770 // requested protected buffers.
1771 if (usage & GRALLOC_USAGE_PROTECTED) {
1772 // Verify that the ANativeWindow sends images directly to
1773 // SurfaceFlinger.
1774 int queuesToNativeWindow = 0;
1775 err = mNativeWindow->query(
1776 mNativeWindow.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
1777 &queuesToNativeWindow);
1778 if (err != 0) {
1779 LOGE("error authenticating native window: %d", err);
1780 return err;
1781 }
1782 if (queuesToNativeWindow != 1) {
1783 LOGE("native window could not be authenticated");
1784 return PERMISSION_DENIED;
1785 }
1786 }
1787
Glenn Kastenb8763f62011-01-28 12:37:51 -08001788 LOGV("native_window_set_usage usage=0x%x", usage);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001789 err = native_window_set_usage(
Jamie Gennis94c59802011-02-24 12:48:17 -08001790 mNativeWindow.get(), usage | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001791 if (err != 0) {
1792 LOGE("native_window_set_usage failed: %s (%d)", strerror(-err), -err);
1793 return err;
1794 }
1795
Jamie Gennis01951fd2011-02-27 15:10:34 -08001796 int minUndequeuedBufs = 0;
1797 err = mNativeWindow->query(mNativeWindow.get(),
1798 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBufs);
1799 if (err != 0) {
1800 LOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d)",
1801 strerror(-err), -err);
1802 return err;
1803 }
1804
1805 // XXX: Is this the right logic to use? It's not clear to me what the OMX
1806 // buffer counts refer to - how do they account for the renderer holding on
1807 // to buffers?
1808 if (def.nBufferCountActual < def.nBufferCountMin + minUndequeuedBufs) {
1809 OMX_U32 newBufferCount = def.nBufferCountMin + minUndequeuedBufs;
1810 def.nBufferCountActual = newBufferCount;
1811 err = mOMX->setParameter(
1812 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1813 if (err != OK) {
1814 CODEC_LOGE("setting nBufferCountActual to %lu failed: %d",
1815 newBufferCount, err);
1816 return err;
1817 }
1818 }
1819
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001820 err = native_window_set_buffer_count(
1821 mNativeWindow.get(), def.nBufferCountActual);
1822 if (err != 0) {
1823 LOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
1824 -err);
1825 return err;
1826 }
1827
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001828 CODEC_LOGI("allocating %lu buffers from a native window of size %lu on "
1829 "output port", def.nBufferCountActual, def.nBufferSize);
1830
1831 // Dequeue buffers and send them to OMX
Jamie Gennis42024642011-02-22 18:33:06 -08001832 for (OMX_U32 i = 0; i < def.nBufferCountActual; i++) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001833 android_native_buffer_t* buf;
1834 err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf);
1835 if (err != 0) {
1836 LOGE("dequeueBuffer failed: %s (%d)", strerror(-err), -err);
1837 break;
1838 }
1839
1840 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false));
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001841 BufferInfo info;
1842 info.mData = NULL;
1843 info.mSize = def.nBufferSize;
Andreas Huberbbbcf652010-12-07 14:25:54 -08001844 info.mStatus = OWNED_BY_US;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001845 info.mMem = NULL;
1846 info.mMediaBuffer = new MediaBuffer(graphicBuffer);
1847 info.mMediaBuffer->setObserver(this);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001848 mPortBuffers[kPortIndexOutput].push(info);
Jamie Gennis42024642011-02-22 18:33:06 -08001849
1850 IOMX::buffer_id bufferId;
1851 err = mOMX->useGraphicBuffer(mNode, kPortIndexOutput, graphicBuffer,
1852 &bufferId);
1853 if (err != 0) {
1854 CODEC_LOGE("registering GraphicBuffer with OMX IL component "
1855 "failed: %d", err);
1856 break;
1857 }
1858
1859 mPortBuffers[kPortIndexOutput].editItemAt(i).mBuffer = bufferId;
1860
1861 CODEC_LOGV("registered graphic buffer with ID %p (pointer = %p)",
1862 bufferId, graphicBuffer.get());
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001863 }
1864
1865 OMX_U32 cancelStart;
1866 OMX_U32 cancelEnd;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001867 if (err != 0) {
1868 // If an error occurred while dequeuing we need to cancel any buffers
1869 // that were dequeued.
1870 cancelStart = 0;
Jamie Gennis42024642011-02-22 18:33:06 -08001871 cancelEnd = mPortBuffers[kPortIndexOutput].size();
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001872 } else {
1873 // Return the last two buffers to the native window.
Jamie Gennis01951fd2011-02-27 15:10:34 -08001874 cancelStart = def.nBufferCountActual - minUndequeuedBufs;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001875 cancelEnd = def.nBufferCountActual;
1876 }
1877
1878 for (OMX_U32 i = cancelStart; i < cancelEnd; i++) {
1879 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(i);
1880 cancelBufferToNativeWindow(info);
1881 }
1882
1883 return err;
1884}
1885
1886status_t OMXCodec::cancelBufferToNativeWindow(BufferInfo *info) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08001887 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001888 CODEC_LOGV("Calling cancelBuffer on buffer %p", info->mBuffer);
1889 int err = mNativeWindow->cancelBuffer(
1890 mNativeWindow.get(), info->mMediaBuffer->graphicBuffer().get());
1891 if (err != 0) {
1892 CODEC_LOGE("cancelBuffer failed w/ error 0x%08x", err);
1893
1894 setState(ERROR);
1895 return err;
1896 }
Andreas Huberbbbcf652010-12-07 14:25:54 -08001897 info->mStatus = OWNED_BY_NATIVE_WINDOW;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001898 return OK;
1899}
1900
1901OMXCodec::BufferInfo* OMXCodec::dequeueBufferFromNativeWindow() {
1902 // Dequeue the next buffer from the native window.
1903 android_native_buffer_t* buf;
1904 int err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf);
1905 if (err != 0) {
1906 CODEC_LOGE("dequeueBuffer failed w/ error 0x%08x", err);
1907
1908 setState(ERROR);
1909 return 0;
1910 }
1911
1912 // Determine which buffer we just dequeued.
1913 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1914 BufferInfo *bufInfo = 0;
1915 for (size_t i = 0; i < buffers->size(); i++) {
1916 sp<GraphicBuffer> graphicBuffer = buffers->itemAt(i).
1917 mMediaBuffer->graphicBuffer();
1918 if (graphicBuffer->handle == buf->handle) {
1919 bufInfo = &buffers->editItemAt(i);
1920 break;
1921 }
1922 }
1923
1924 if (bufInfo == 0) {
1925 CODEC_LOGE("dequeued unrecognized buffer: %p", buf);
1926
1927 setState(ERROR);
1928 return 0;
1929 }
1930
1931 // The native window no longer owns the buffer.
Andreas Huberbbbcf652010-12-07 14:25:54 -08001932 CHECK_EQ((int)bufInfo->mStatus, (int)OWNED_BY_NATIVE_WINDOW);
1933 bufInfo->mStatus = OWNED_BY_US;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001934
1935 return bufInfo;
1936}
1937
Andreas Huberbe06d262009-08-14 14:37:10 -07001938void OMXCodec::on_message(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001939 switch (msg.type) {
1940 case omx_message::EVENT:
1941 {
1942 onEvent(
1943 msg.u.event_data.event, msg.u.event_data.data1,
1944 msg.u.event_data.data2);
1945
1946 break;
1947 }
1948
1949 case omx_message::EMPTY_BUFFER_DONE:
1950 {
1951 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1952
Andreas Huber4c483422009-09-02 16:05:36 -07001953 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001954
1955 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1956 size_t i = 0;
1957 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1958 ++i;
1959 }
1960
1961 CHECK(i < buffers->size());
Andreas Huberbbbcf652010-12-07 14:25:54 -08001962 if ((*buffers)[i].mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001963 LOGW("We already own input buffer %p, yet received "
1964 "an EMPTY_BUFFER_DONE.", buffer);
1965 }
1966
James Dong05c2fd52010-11-02 13:20:11 -07001967 BufferInfo* info = &buffers->editItemAt(i);
Andreas Huberbbbcf652010-12-07 14:25:54 -08001968 info->mStatus = OWNED_BY_US;
James Dong05c2fd52010-11-02 13:20:11 -07001969
1970 // Buffer could not be released until empty buffer done is called.
1971 if (info->mMediaBuffer != NULL) {
James Dong31b9375f2010-11-10 21:11:41 -08001972 if (mIsEncoder &&
1973 (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
1974 // If zero-copy mode is enabled this will send the
1975 // input buffer back to the upstream source.
1976 restorePatchedDataPointer(info);
1977 }
1978
James Dong05c2fd52010-11-02 13:20:11 -07001979 info->mMediaBuffer->release();
1980 info->mMediaBuffer = NULL;
1981 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001982
1983 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001984 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001985
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07001986 status_t err = freeBuffer(kPortIndexInput, i);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001987 CHECK_EQ(err, (status_t)OK);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001988 } else if (mState != ERROR
1989 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001990 CHECK_EQ((int)mPortStatus[kPortIndexInput], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07001991 drainInputBuffer(&buffers->editItemAt(i));
1992 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001993 break;
1994 }
1995
1996 case omx_message::FILL_BUFFER_DONE:
1997 {
1998 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1999 OMX_U32 flags = msg.u.extended_buffer_data.flags;
2000
Andreas Huber2ea14e22009-12-16 09:30:55 -08002001 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07002002 buffer,
2003 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08002004 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07002005 msg.u.extended_buffer_data.timestamp,
2006 msg.u.extended_buffer_data.timestamp / 1E6);
2007
2008 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2009 size_t i = 0;
2010 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
2011 ++i;
2012 }
2013
2014 CHECK(i < buffers->size());
2015 BufferInfo *info = &buffers->editItemAt(i);
2016
Andreas Huberbbbcf652010-12-07 14:25:54 -08002017 if (info->mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002018 LOGW("We already own output buffer %p, yet received "
2019 "a FILL_BUFFER_DONE.", buffer);
2020 }
2021
Andreas Huberbbbcf652010-12-07 14:25:54 -08002022 info->mStatus = OWNED_BY_US;
Andreas Huberbe06d262009-08-14 14:37:10 -07002023
2024 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07002025 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07002026
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002027 status_t err = freeBuffer(kPortIndexOutput, i);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002028 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002029
Andreas Huber2ea14e22009-12-16 09:30:55 -08002030#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07002031 } else if (mPortStatus[kPortIndexOutput] == ENABLED
2032 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07002033 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002034 mNoMoreOutputData = true;
2035 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08002036#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07002037 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002038 CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07002039
Andreas Huber52733b82010-01-25 10:41:35 -08002040 if (info->mMediaBuffer == NULL) {
2041 CHECK(mOMXLivesLocally);
2042 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
2043 CHECK(mQuirks & kDefersOutputBufferAllocation);
2044
2045 // The qcom video decoders on Nexus don't actually allocate
2046 // output buffer memory on a call to OMX_AllocateBuffer
2047 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
2048 // structure is only filled in later.
2049
2050 info->mMediaBuffer = new MediaBuffer(
2051 msg.u.extended_buffer_data.data_ptr,
2052 info->mSize);
2053 info->mMediaBuffer->setObserver(this);
2054 }
2055
Andreas Huberbe06d262009-08-14 14:37:10 -07002056 MediaBuffer *buffer = info->mMediaBuffer;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002057 bool isGraphicBuffer = buffer->graphicBuffer() != NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -07002058
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002059 if (!isGraphicBuffer
2060 && msg.u.extended_buffer_data.range_offset
Andreas Huberf88f8442010-08-10 11:18:36 -07002061 + msg.u.extended_buffer_data.range_length
2062 > buffer->size()) {
2063 CODEC_LOGE(
2064 "Codec lied about its buffer size requirements, "
2065 "sending a buffer larger than the originally "
2066 "advertised size in FILL_BUFFER_DONE!");
2067 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002068 buffer->set_range(
2069 msg.u.extended_buffer_data.range_offset,
2070 msg.u.extended_buffer_data.range_length);
2071
2072 buffer->meta_data()->clear();
2073
Andreas Huberfa8de752009-10-08 10:07:49 -07002074 buffer->meta_data()->setInt64(
2075 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07002076
2077 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
2078 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
2079 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08002080 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
2081 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
2082 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002083
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002084 if (isGraphicBuffer || mQuirks & kOutputBuffersAreUnreadable) {
Andreas Huber1e194162010-10-06 16:43:57 -07002085 buffer->meta_data()->setInt32(kKeyIsUnreadable, true);
2086 }
2087
Andreas Huberbe06d262009-08-14 14:37:10 -07002088 buffer->meta_data()->setPointer(
2089 kKeyPlatformPrivate,
2090 msg.u.extended_buffer_data.platform_private);
2091
2092 buffer->meta_data()->setPointer(
2093 kKeyBufferID,
2094 msg.u.extended_buffer_data.buffer);
2095
Andreas Huber2ea14e22009-12-16 09:30:55 -08002096 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
2097 CODEC_LOGV("No more output data.");
2098 mNoMoreOutputData = true;
2099 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002100
2101 if (mTargetTimeUs >= 0) {
2102 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
2103
2104 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
2105 CODEC_LOGV(
2106 "skipping output buffer at timestamp %lld us",
2107 msg.u.extended_buffer_data.timestamp);
2108
2109 fillOutputBuffer(info);
2110 break;
2111 }
2112
2113 CODEC_LOGV(
2114 "returning output buffer at target timestamp "
2115 "%lld us",
2116 msg.u.extended_buffer_data.timestamp);
2117
2118 mTargetTimeUs = -1;
2119 }
2120
2121 mFilledBuffers.push_back(i);
2122 mBufferFilled.signal();
James Dongfc8b7c92010-12-07 14:37:27 -08002123 if (mIsEncoder) {
2124 sched_yield();
2125 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002126 }
2127
2128 break;
2129 }
2130
2131 default:
2132 {
2133 CHECK(!"should not be here.");
2134 break;
2135 }
2136 }
2137}
2138
Andreas Huberb1678602009-10-19 13:06:40 -07002139// Has the format changed in any way that the client would have to be aware of?
2140static bool formatHasNotablyChanged(
2141 const sp<MetaData> &from, const sp<MetaData> &to) {
2142 if (from.get() == NULL && to.get() == NULL) {
2143 return false;
2144 }
2145
Andreas Huberf68c1682009-10-21 14:01:30 -07002146 if ((from.get() == NULL && to.get() != NULL)
2147 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07002148 return true;
2149 }
2150
2151 const char *mime_from, *mime_to;
2152 CHECK(from->findCString(kKeyMIMEType, &mime_from));
2153 CHECK(to->findCString(kKeyMIMEType, &mime_to));
2154
2155 if (strcasecmp(mime_from, mime_to)) {
2156 return true;
2157 }
2158
2159 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
2160 int32_t colorFormat_from, colorFormat_to;
2161 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
2162 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
2163
2164 if (colorFormat_from != colorFormat_to) {
2165 return true;
2166 }
2167
2168 int32_t width_from, width_to;
2169 CHECK(from->findInt32(kKeyWidth, &width_from));
2170 CHECK(to->findInt32(kKeyWidth, &width_to));
2171
2172 if (width_from != width_to) {
2173 return true;
2174 }
2175
2176 int32_t height_from, height_to;
2177 CHECK(from->findInt32(kKeyHeight, &height_from));
2178 CHECK(to->findInt32(kKeyHeight, &height_to));
2179
2180 if (height_from != height_to) {
2181 return true;
2182 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002183
2184 int32_t left_from, top_from, right_from, bottom_from;
2185 CHECK(from->findRect(
2186 kKeyCropRect,
2187 &left_from, &top_from, &right_from, &bottom_from));
2188
2189 int32_t left_to, top_to, right_to, bottom_to;
2190 CHECK(to->findRect(
2191 kKeyCropRect,
2192 &left_to, &top_to, &right_to, &bottom_to));
2193
2194 if (left_to != left_from || top_to != top_from
2195 || right_to != right_from || bottom_to != bottom_from) {
2196 return true;
2197 }
Andreas Huberb1678602009-10-19 13:06:40 -07002198 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
2199 int32_t numChannels_from, numChannels_to;
2200 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
2201 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
2202
2203 if (numChannels_from != numChannels_to) {
2204 return true;
2205 }
2206
2207 int32_t sampleRate_from, sampleRate_to;
2208 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
2209 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
2210
2211 if (sampleRate_from != sampleRate_to) {
2212 return true;
2213 }
2214 }
2215
2216 return false;
2217}
2218
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002219void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
2220 switch (event) {
2221 case OMX_EventCmdComplete:
2222 {
2223 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
2224 break;
2225 }
2226
2227 case OMX_EventError:
2228 {
2229 CODEC_LOGE("ERROR(0x%08lx, %ld)", data1, data2);
2230
2231 setState(ERROR);
2232 break;
2233 }
2234
2235 case OMX_EventPortSettingsChanged:
2236 {
2237 CODEC_LOGV("OMX_EventPortSettingsChanged(port=%ld, data2=0x%08lx)",
2238 data1, data2);
2239
2240 if (data2 == 0 || data2 == OMX_IndexParamPortDefinition) {
2241 onPortSettingsChanged(data1);
2242 } else if (data1 == kPortIndexOutput
2243 && data2 == OMX_IndexConfigCommonOutputCrop) {
2244
2245 sp<MetaData> oldOutputFormat = mOutputFormat;
2246 initOutputFormat(mSource->getFormat());
2247
2248 if (formatHasNotablyChanged(oldOutputFormat, mOutputFormat)) {
2249 mOutputPortSettingsHaveChanged = true;
2250
2251 if (mNativeWindow != NULL) {
2252 int32_t left, top, right, bottom;
2253 CHECK(mOutputFormat->findRect(
2254 kKeyCropRect,
2255 &left, &top, &right, &bottom));
2256
2257 android_native_rect_t crop;
2258 crop.left = left;
2259 crop.top = top;
Andreas Huberb13c4a92011-03-29 10:00:20 -07002260 crop.right = right + 1;
2261 crop.bottom = bottom + 1;
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002262
Andreas Huber00d6c722011-01-26 12:02:35 -08002263 // We'll ignore any errors here, if the surface is
2264 // already invalid, we'll know soon enough.
2265 native_window_set_crop(mNativeWindow.get(), &crop);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002266 }
2267 }
2268 }
2269 break;
2270 }
2271
2272#if 0
2273 case OMX_EventBufferFlag:
2274 {
2275 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
2276
2277 if (data1 == kPortIndexOutput) {
2278 mNoMoreOutputData = true;
2279 }
2280 break;
2281 }
2282#endif
2283
2284 default:
2285 {
2286 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
2287 break;
2288 }
2289 }
2290}
2291
Andreas Huberbe06d262009-08-14 14:37:10 -07002292void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
2293 switch (cmd) {
2294 case OMX_CommandStateSet:
2295 {
2296 onStateChange((OMX_STATETYPE)data);
2297 break;
2298 }
2299
2300 case OMX_CommandPortDisable:
2301 {
2302 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07002303 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002304
2305 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002306 CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLING);
2307 CHECK_EQ(mPortBuffers[portIndex].size(), 0u);
Andreas Huberbe06d262009-08-14 14:37:10 -07002308
2309 mPortStatus[portIndex] = DISABLED;
2310
2311 if (mState == RECONFIGURING) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002312 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002313
Andreas Huberb1678602009-10-19 13:06:40 -07002314 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07002315 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07002316
2317 // Don't notify clients if the output port settings change
2318 // wasn't of importance to them, i.e. it may be that just the
2319 // number of buffers has changed and nothing else.
2320 mOutputPortSettingsHaveChanged =
2321 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07002322
Andreas Huberbe06d262009-08-14 14:37:10 -07002323 enablePortAsync(portIndex);
2324
2325 status_t err = allocateBuffersOnPort(portIndex);
Andreas Huberba1b16792011-01-19 09:20:58 -08002326
2327 if (err != OK) {
2328 CODEC_LOGE("allocateBuffersOnPort failed (err = %d)", err);
2329 setState(ERROR);
2330 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002331 }
2332 break;
2333 }
2334
2335 case OMX_CommandPortEnable:
2336 {
2337 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07002338 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002339
2340 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002341 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002342
2343 mPortStatus[portIndex] = ENABLED;
2344
2345 if (mState == RECONFIGURING) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002346 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002347
2348 setState(EXECUTING);
2349
2350 fillOutputBuffers();
2351 }
2352 break;
2353 }
2354
2355 case OMX_CommandFlush:
2356 {
2357 OMX_U32 portIndex = data;
2358
Andreas Huber4c483422009-09-02 16:05:36 -07002359 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002360
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002361 CHECK_EQ((int)mPortStatus[portIndex], (int)SHUTTING_DOWN);
Andreas Huberbe06d262009-08-14 14:37:10 -07002362 mPortStatus[portIndex] = ENABLED;
2363
2364 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
2365 mPortBuffers[portIndex].size());
2366
2367 if (mState == RECONFIGURING) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002368 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002369
2370 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002371 } else if (mState == EXECUTING_TO_IDLE) {
2372 if (mPortStatus[kPortIndexInput] == ENABLED
2373 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002374 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002375 "transition from EXECUTING to IDLE.");
2376
2377 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2378 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2379
2380 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002381 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002382 CHECK_EQ(err, (status_t)OK);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002383 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002384 } else {
2385 // We're flushing both ports in preparation for seeking.
2386
2387 if (mPortStatus[kPortIndexInput] == ENABLED
2388 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002389 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07002390 " seek-time.");
2391
Andreas Huber1f24b302010-06-10 11:12:39 -07002392 // We implicitly resume pulling on our upstream source.
2393 mPaused = false;
2394
Andreas Huberbe06d262009-08-14 14:37:10 -07002395 drainInputBuffers();
2396 fillOutputBuffers();
2397 }
Andreas Huberb9289832011-02-08 13:10:25 -08002398
2399 if (mOutputPortSettingsChangedPending) {
2400 CODEC_LOGV(
2401 "Honoring deferred output port settings change.");
2402
2403 mOutputPortSettingsChangedPending = false;
2404 onPortSettingsChanged(kPortIndexOutput);
2405 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002406 }
2407
2408 break;
2409 }
2410
2411 default:
2412 {
Andreas Huber4c483422009-09-02 16:05:36 -07002413 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07002414 break;
2415 }
2416 }
2417}
2418
2419void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002420 CODEC_LOGV("onStateChange %d", newState);
2421
Andreas Huberbe06d262009-08-14 14:37:10 -07002422 switch (newState) {
2423 case OMX_StateIdle:
2424 {
Andreas Huber4c483422009-09-02 16:05:36 -07002425 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002426 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07002427 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002428 mNode, OMX_CommandStateSet, OMX_StateExecuting);
2429
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002430 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002431
2432 setState(IDLE_TO_EXECUTING);
2433 } else {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002434 CHECK_EQ((int)mState, (int)EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002435
2436 CHECK_EQ(
2437 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
2438 mPortBuffers[kPortIndexInput].size());
2439
2440 CHECK_EQ(
2441 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
2442 mPortBuffers[kPortIndexOutput].size());
2443
Andreas Huber784202e2009-10-15 13:46:54 -07002444 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002445 mNode, OMX_CommandStateSet, OMX_StateLoaded);
2446
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002447 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002448
2449 err = freeBuffersOnPort(kPortIndexInput);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002450 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002451
2452 err = freeBuffersOnPort(kPortIndexOutput);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002453 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002454
2455 mPortStatus[kPortIndexInput] = ENABLED;
2456 mPortStatus[kPortIndexOutput] = ENABLED;
2457
2458 setState(IDLE_TO_LOADED);
2459 }
2460 break;
2461 }
2462
2463 case OMX_StateExecuting:
2464 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002465 CHECK_EQ((int)mState, (int)IDLE_TO_EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002466
Andreas Huber4c483422009-09-02 16:05:36 -07002467 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002468
Andreas Huberb9289832011-02-08 13:10:25 -08002469 mOutputPortSettingsChangedPending = false;
2470
Andreas Huberbe06d262009-08-14 14:37:10 -07002471 setState(EXECUTING);
2472
Andreas Huber42978e52009-08-27 10:08:39 -07002473 // Buffers will be submitted to the component in the first
2474 // call to OMXCodec::read as mInitialBufferSubmit is true at
2475 // this point. This ensures that this on_message call returns,
2476 // releases the lock and ::init can notice the state change and
2477 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07002478 break;
2479 }
2480
2481 case OMX_StateLoaded:
2482 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002483 CHECK_EQ((int)mState, (int)IDLE_TO_LOADED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002484
Andreas Huber4c483422009-09-02 16:05:36 -07002485 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002486
2487 setState(LOADED);
2488 break;
2489 }
2490
Andreas Huberc712b9f2010-01-20 15:05:46 -08002491 case OMX_StateInvalid:
2492 {
2493 setState(ERROR);
2494 break;
2495 }
2496
Andreas Huberbe06d262009-08-14 14:37:10 -07002497 default:
2498 {
2499 CHECK(!"should not be here.");
2500 break;
2501 }
2502 }
2503}
2504
2505// static
2506size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
2507 size_t n = 0;
2508 for (size_t i = 0; i < buffers.size(); ++i) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002509 if (buffers[i].mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002510 ++n;
2511 }
2512 }
2513
2514 return n;
2515}
2516
2517status_t OMXCodec::freeBuffersOnPort(
2518 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2519 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2520
2521 status_t stickyErr = OK;
2522
2523 for (size_t i = buffers->size(); i-- > 0;) {
2524 BufferInfo *info = &buffers->editItemAt(i);
2525
Andreas Huberbbbcf652010-12-07 14:25:54 -08002526 if (onlyThoseWeOwn && info->mStatus == OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002527 continue;
2528 }
2529
Andreas Huberbbbcf652010-12-07 14:25:54 -08002530 CHECK(info->mStatus == OWNED_BY_US
2531 || info->mStatus == OWNED_BY_NATIVE_WINDOW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002532
Andreas Huber92022852009-09-14 15:24:14 -07002533 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2534
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002535 status_t err = freeBuffer(portIndex, i);
Andreas Huberbe06d262009-08-14 14:37:10 -07002536
2537 if (err != OK) {
2538 stickyErr = err;
2539 }
2540
Andreas Huberbe06d262009-08-14 14:37:10 -07002541 }
2542
2543 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2544
2545 return stickyErr;
2546}
2547
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002548status_t OMXCodec::freeBuffer(OMX_U32 portIndex, size_t bufIndex) {
2549 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2550
2551 BufferInfo *info = &buffers->editItemAt(bufIndex);
2552
2553 status_t err = mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
2554
2555 if (err == OK && info->mMediaBuffer != NULL) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002556 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002557 info->mMediaBuffer->setObserver(NULL);
2558
2559 // Make sure nobody but us owns this buffer at this point.
2560 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2561
2562 // Cancel the buffer if it belongs to an ANativeWindow.
2563 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer();
Andreas Huberbbbcf652010-12-07 14:25:54 -08002564 if (info->mStatus == OWNED_BY_US && graphicBuffer != 0) {
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002565 err = cancelBufferToNativeWindow(info);
2566 }
2567
2568 info->mMediaBuffer->release();
James Dong31b9375f2010-11-10 21:11:41 -08002569 info->mMediaBuffer = NULL;
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002570 }
2571
2572 if (err == OK) {
2573 buffers->removeAt(bufIndex);
2574 }
2575
2576 return err;
2577}
2578
Andreas Huberbe06d262009-08-14 14:37:10 -07002579void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002580 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002581
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002582 CHECK_EQ((int)mState, (int)EXECUTING);
2583 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberb9289832011-02-08 13:10:25 -08002584 CHECK(!mOutputPortSettingsChangedPending);
2585
2586 if (mPortStatus[kPortIndexOutput] != ENABLED) {
2587 CODEC_LOGV("Deferring output port settings change.");
2588 mOutputPortSettingsChangedPending = true;
2589 return;
2590 }
2591
Andreas Huberbe06d262009-08-14 14:37:10 -07002592 setState(RECONFIGURING);
2593
2594 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002595 if (!flushPortAsync(portIndex)) {
2596 onCmdComplete(OMX_CommandFlush, portIndex);
2597 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002598 } else {
2599 disablePortAsync(portIndex);
2600 }
2601}
2602
Andreas Huber404cc412009-08-25 14:26:05 -07002603bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002604 CHECK(mState == EXECUTING || mState == RECONFIGURING
2605 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002606
Andreas Huber4c483422009-09-02 16:05:36 -07002607 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002608 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2609 mPortBuffers[portIndex].size());
2610
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002611 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002612 mPortStatus[portIndex] = SHUTTING_DOWN;
2613
Andreas Huber404cc412009-08-25 14:26:05 -07002614 if ((mQuirks & kRequiresFlushCompleteEmulation)
2615 && countBuffersWeOwn(mPortBuffers[portIndex])
2616 == mPortBuffers[portIndex].size()) {
2617 // No flush is necessary and this component fails to send a
2618 // flush-complete event in this case.
2619
2620 return false;
2621 }
2622
Andreas Huberbe06d262009-08-14 14:37:10 -07002623 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002624 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002625 CHECK_EQ(err, (status_t)OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002626
2627 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002628}
2629
2630void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2631 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2632
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002633 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002634 mPortStatus[portIndex] = DISABLING;
2635
Andreas Huberd222c842010-08-26 14:29:34 -07002636 CODEC_LOGV("sending OMX_CommandPortDisable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002637 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002638 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002639 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002640
2641 freeBuffersOnPort(portIndex, true);
2642}
2643
2644void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2645 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2646
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002647 CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002648 mPortStatus[portIndex] = ENABLING;
2649
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002650 CODEC_LOGV("sending OMX_CommandPortEnable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002651 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002652 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002653 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002654}
2655
2656void OMXCodec::fillOutputBuffers() {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002657 CHECK_EQ((int)mState, (int)EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002658
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002659 // This is a workaround for some decoders not properly reporting
2660 // end-of-output-stream. If we own all input buffers and also own
2661 // all output buffers and we already signalled end-of-input-stream,
2662 // the end-of-output-stream is implied.
2663 if (mSignalledEOS
2664 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2665 == mPortBuffers[kPortIndexInput].size()
2666 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2667 == mPortBuffers[kPortIndexOutput].size()) {
2668 mNoMoreOutputData = true;
2669 mBufferFilled.signal();
2670
2671 return;
2672 }
2673
Andreas Huberbe06d262009-08-14 14:37:10 -07002674 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2675 for (size_t i = 0; i < buffers->size(); ++i) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002676 BufferInfo *info = &buffers->editItemAt(i);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002677 if (info->mStatus == OWNED_BY_US) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002678 fillOutputBuffer(&buffers->editItemAt(i));
2679 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002680 }
2681}
2682
2683void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002684 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002685
2686 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2687 for (size_t i = 0; i < buffers->size(); ++i) {
James Dong5f3ab062011-01-25 16:31:28 -08002688 BufferInfo *info = &buffers->editItemAt(i);
2689
2690 if (info->mStatus != OWNED_BY_US) {
2691 continue;
2692 }
2693
2694 if (!drainInputBuffer(info)) {
2695 break;
2696 }
2697
2698 if (mOnlySubmitOneBufferAtOneTime) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002699 break;
2700 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002701 }
2702}
2703
Andreas Huberbbbcf652010-12-07 14:25:54 -08002704bool OMXCodec::drainInputBuffer(BufferInfo *info) {
2705 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Andreas Huberbe06d262009-08-14 14:37:10 -07002706
2707 if (mSignalledEOS) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002708 return false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002709 }
2710
2711 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2712 const CodecSpecificData *specific =
2713 mCodecSpecificData[mCodecSpecificDataIndex];
2714
2715 size_t size = specific->mSize;
2716
Andreas Hubere6c40962009-09-10 14:13:30 -07002717 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002718 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002719 static const uint8_t kNALStartCode[4] =
2720 { 0x00, 0x00, 0x00, 0x01 };
2721
Andreas Huberc712b9f2010-01-20 15:05:46 -08002722 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002723
2724 size += 4;
2725
Andreas Huberc712b9f2010-01-20 15:05:46 -08002726 memcpy(info->mData, kNALStartCode, 4);
2727 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002728 specific->mData, specific->mSize);
2729 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002730 CHECK(info->mSize >= specific->mSize);
2731 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002732 }
2733
Andreas Huber2ea14e22009-12-16 09:30:55 -08002734 mNoMoreOutputData = false;
2735
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002736 CODEC_LOGV("calling emptyBuffer with codec specific data");
2737
Andreas Huber784202e2009-10-15 13:46:54 -07002738 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002739 mNode, info->mBuffer, 0, size,
2740 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2741 0);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002742 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002743
Andreas Huberbbbcf652010-12-07 14:25:54 -08002744 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberbe06d262009-08-14 14:37:10 -07002745
2746 ++mCodecSpecificDataIndex;
Andreas Huberbbbcf652010-12-07 14:25:54 -08002747 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002748 }
2749
Andreas Huber1f24b302010-06-10 11:12:39 -07002750 if (mPaused) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002751 return false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002752 }
2753
Andreas Huberbe06d262009-08-14 14:37:10 -07002754 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002755
Andreas Hubera4357ad2010-04-02 12:49:54 -07002756 bool signalEOS = false;
2757 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002758
Andreas Hubera4357ad2010-04-02 12:49:54 -07002759 size_t offset = 0;
2760 int32_t n = 0;
Andreas Huberbbbcf652010-12-07 14:25:54 -08002761
Andreas Hubera4357ad2010-04-02 12:49:54 -07002762 for (;;) {
2763 MediaBuffer *srcBuffer;
2764 if (mSeekTimeUs >= 0) {
2765 if (mLeftOverBuffer) {
2766 mLeftOverBuffer->release();
2767 mLeftOverBuffer = NULL;
2768 }
James Dong2144f632010-12-11 10:43:41 -08002769
2770 MediaSource::ReadOptions options;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002771 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002772
2773 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002774 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002775 mBufferFilled.signal();
2776
2777 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002778
2779 if (err == OK) {
2780 int64_t targetTimeUs;
2781 if (srcBuffer->meta_data()->findInt64(
2782 kKeyTargetTime, &targetTimeUs)
2783 && targetTimeUs >= 0) {
Andreas Huberb9289832011-02-08 13:10:25 -08002784 CODEC_LOGV("targetTimeUs = %lld us", targetTimeUs);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002785 mTargetTimeUs = targetTimeUs;
2786 } else {
2787 mTargetTimeUs = -1;
2788 }
2789 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002790 } else if (mLeftOverBuffer) {
2791 srcBuffer = mLeftOverBuffer;
2792 mLeftOverBuffer = NULL;
2793
2794 err = OK;
2795 } else {
James Dong2144f632010-12-11 10:43:41 -08002796 err = mSource->read(&srcBuffer);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002797 }
2798
2799 if (err != OK) {
2800 signalEOS = true;
2801 mFinalStatus = err;
2802 mSignalledEOS = true;
Andreas Huber94bced12010-12-14 09:50:46 -08002803 mBufferFilled.signal();
Andreas Hubera4357ad2010-04-02 12:49:54 -07002804 break;
2805 }
2806
2807 size_t remainingBytes = info->mSize - offset;
2808
2809 if (srcBuffer->range_length() > remainingBytes) {
2810 if (offset == 0) {
2811 CODEC_LOGE(
2812 "Codec's input buffers are too small to accomodate "
2813 "buffer read from source (info->mSize = %d, srcLength = %d)",
2814 info->mSize, srcBuffer->range_length());
2815
2816 srcBuffer->release();
2817 srcBuffer = NULL;
2818
2819 setState(ERROR);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002820 return false;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002821 }
2822
2823 mLeftOverBuffer = srcBuffer;
2824 break;
2825 }
2826
James Dong05c2fd52010-11-02 13:20:11 -07002827 bool releaseBuffer = true;
James Dong4f501f02010-06-07 14:41:41 -07002828 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2829 CHECK(mOMXLivesLocally && offset == 0);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002830
2831 OMX_BUFFERHEADERTYPE *header =
2832 (OMX_BUFFERHEADERTYPE *)info->mBuffer;
2833
James Dong31b9375f2010-11-10 21:11:41 -08002834 CHECK(header->pBuffer == info->mData);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002835
2836 header->pBuffer =
2837 (OMX_U8 *)srcBuffer->data() + srcBuffer->range_offset();
2838
James Dong05c2fd52010-11-02 13:20:11 -07002839 releaseBuffer = false;
2840 info->mMediaBuffer = srcBuffer;
James Dong4f501f02010-06-07 14:41:41 -07002841 } else {
James Dong05c2fd52010-11-02 13:20:11 -07002842 if (mIsMetaDataStoredInVideoBuffers) {
2843 releaseBuffer = false;
2844 info->mMediaBuffer = srcBuffer;
2845 }
James Dong4f501f02010-06-07 14:41:41 -07002846 memcpy((uint8_t *)info->mData + offset,
Andreas Huberbbbcf652010-12-07 14:25:54 -08002847 (const uint8_t *)srcBuffer->data()
2848 + srcBuffer->range_offset(),
James Dong4f501f02010-06-07 14:41:41 -07002849 srcBuffer->range_length());
2850 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002851
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002852 int64_t lastBufferTimeUs;
2853 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002854 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002855
Andreas Hubera4357ad2010-04-02 12:49:54 -07002856 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002857 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002858 }
2859
2860 offset += srcBuffer->range_length();
2861
James Dong05c2fd52010-11-02 13:20:11 -07002862 if (releaseBuffer) {
2863 srcBuffer->release();
2864 srcBuffer = NULL;
2865 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002866
2867 ++n;
2868
2869 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2870 break;
2871 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002872
2873 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2874
2875 if (coalescedDurationUs > 250000ll) {
2876 // Don't coalesce more than 250ms worth of encoded data at once.
2877 break;
2878 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002879 }
2880
2881 if (n > 1) {
2882 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002883 }
2884
2885 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002886
Andreas Hubera4357ad2010-04-02 12:49:54 -07002887 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002888 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002889 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002890 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002891 }
2892
Andreas Hubera4357ad2010-04-02 12:49:54 -07002893 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2894 "timestamp %lld us (%.2f secs)",
2895 info->mBuffer, offset,
2896 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002897
Andreas Huber784202e2009-10-15 13:46:54 -07002898 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002899 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002900 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002901
2902 if (err != OK) {
2903 setState(ERROR);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002904 return false;
Andreas Huber3f427072009-10-08 11:02:27 -07002905 }
2906
Andreas Huberbbbcf652010-12-07 14:25:54 -08002907 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002908
2909 // This component does not ever signal the EOS flag on output buffers,
2910 // Thanks for nothing.
2911 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2912 mNoMoreOutputData = true;
2913 mBufferFilled.signal();
2914 }
Andreas Huberbbbcf652010-12-07 14:25:54 -08002915
2916 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002917}
2918
2919void OMXCodec::fillOutputBuffer(BufferInfo *info) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002920 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Andreas Huberbe06d262009-08-14 14:37:10 -07002921
Andreas Huber404cc412009-08-25 14:26:05 -07002922 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002923 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002924 "calling fillOutputBuffer");
2925 return;
2926 }
2927
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002928 if (info->mMediaBuffer != NULL) {
2929 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer();
2930 if (graphicBuffer != 0) {
2931 // When using a native buffer we need to lock the buffer before
2932 // giving it to OMX.
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002933 CODEC_LOGV("Calling lockBuffer on %p", info->mBuffer);
2934 int err = mNativeWindow->lockBuffer(mNativeWindow.get(),
2935 graphicBuffer.get());
2936 if (err != 0) {
2937 CODEC_LOGE("lockBuffer failed w/ error 0x%08x", err);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002938
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002939 setState(ERROR);
2940 return;
2941 }
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002942 }
2943 }
2944
2945 CODEC_LOGV("Calling fillBuffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002946 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002947
2948 if (err != OK) {
2949 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2950
2951 setState(ERROR);
2952 return;
2953 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002954
Andreas Huberbbbcf652010-12-07 14:25:54 -08002955 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberbe06d262009-08-14 14:37:10 -07002956}
2957
Andreas Huberbbbcf652010-12-07 14:25:54 -08002958bool OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002959 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2960 for (size_t i = 0; i < buffers->size(); ++i) {
2961 if ((*buffers)[i].mBuffer == buffer) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002962 return drainInputBuffer(&buffers->editItemAt(i));
Andreas Huberbe06d262009-08-14 14:37:10 -07002963 }
2964 }
2965
2966 CHECK(!"should not be here.");
Andreas Huberbbbcf652010-12-07 14:25:54 -08002967
2968 return false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002969}
2970
2971void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2972 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2973 for (size_t i = 0; i < buffers->size(); ++i) {
2974 if ((*buffers)[i].mBuffer == buffer) {
2975 fillOutputBuffer(&buffers->editItemAt(i));
2976 return;
2977 }
2978 }
2979
2980 CHECK(!"should not be here.");
2981}
2982
2983void OMXCodec::setState(State newState) {
2984 mState = newState;
2985 mAsyncCompletion.signal();
2986
2987 // This may cause some spurious wakeups but is necessary to
2988 // unblock the reader if we enter ERROR state.
2989 mBufferFilled.signal();
2990}
2991
Andreas Huberda050cf22009-09-02 14:01:43 -07002992void OMXCodec::setRawAudioFormat(
2993 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002994
2995 // port definition
2996 OMX_PARAM_PORTDEFINITIONTYPE def;
2997 InitOMXParams(&def);
2998 def.nPortIndex = portIndex;
2999 status_t err = mOMX->getParameter(
3000 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003001 CHECK_EQ(err, (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003002 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
3003 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003004 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003005
3006 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07003007 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07003008 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07003009 pcmParams.nPortIndex = portIndex;
3010
James Dongabed93a2010-04-22 17:27:04 -07003011 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003012 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
3013
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003014 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07003015
3016 pcmParams.nChannels = numChannels;
3017 pcmParams.eNumData = OMX_NumericalDataSigned;
3018 pcmParams.bInterleaved = OMX_TRUE;
3019 pcmParams.nBitPerSample = 16;
3020 pcmParams.nSamplingRate = sampleRate;
3021 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
3022
3023 if (numChannels == 1) {
3024 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
3025 } else {
3026 CHECK_EQ(numChannels, 2);
3027
3028 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
3029 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
3030 }
3031
Andreas Huber784202e2009-10-15 13:46:54 -07003032 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003033 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
3034
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003035 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07003036}
3037
James Dong17299ab2010-05-14 15:45:22 -07003038static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
3039 if (isAMRWB) {
3040 if (bps <= 6600) {
3041 return OMX_AUDIO_AMRBandModeWB0;
3042 } else if (bps <= 8850) {
3043 return OMX_AUDIO_AMRBandModeWB1;
3044 } else if (bps <= 12650) {
3045 return OMX_AUDIO_AMRBandModeWB2;
3046 } else if (bps <= 14250) {
3047 return OMX_AUDIO_AMRBandModeWB3;
3048 } else if (bps <= 15850) {
3049 return OMX_AUDIO_AMRBandModeWB4;
3050 } else if (bps <= 18250) {
3051 return OMX_AUDIO_AMRBandModeWB5;
3052 } else if (bps <= 19850) {
3053 return OMX_AUDIO_AMRBandModeWB6;
3054 } else if (bps <= 23050) {
3055 return OMX_AUDIO_AMRBandModeWB7;
3056 }
3057
3058 // 23850 bps
3059 return OMX_AUDIO_AMRBandModeWB8;
3060 } else { // AMRNB
3061 if (bps <= 4750) {
3062 return OMX_AUDIO_AMRBandModeNB0;
3063 } else if (bps <= 5150) {
3064 return OMX_AUDIO_AMRBandModeNB1;
3065 } else if (bps <= 5900) {
3066 return OMX_AUDIO_AMRBandModeNB2;
3067 } else if (bps <= 6700) {
3068 return OMX_AUDIO_AMRBandModeNB3;
3069 } else if (bps <= 7400) {
3070 return OMX_AUDIO_AMRBandModeNB4;
3071 } else if (bps <= 7950) {
3072 return OMX_AUDIO_AMRBandModeNB5;
3073 } else if (bps <= 10200) {
3074 return OMX_AUDIO_AMRBandModeNB6;
3075 }
3076
3077 // 12200 bps
3078 return OMX_AUDIO_AMRBandModeNB7;
3079 }
3080}
3081
3082void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08003083 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003084
Andreas Huber8768f2c2009-12-01 15:26:54 -08003085 OMX_AUDIO_PARAM_AMRTYPE def;
3086 InitOMXParams(&def);
3087 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07003088
Andreas Huber8768f2c2009-12-01 15:26:54 -08003089 status_t err =
3090 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07003091
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003092 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003093
Andreas Huber8768f2c2009-12-01 15:26:54 -08003094 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07003095
James Dong17299ab2010-05-14 15:45:22 -07003096 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08003097 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003098 CHECK_EQ(err, (status_t)OK);
Andreas Huberee606e62009-09-08 10:19:21 -07003099
3100 ////////////////////////
3101
3102 if (mIsEncoder) {
3103 sp<MetaData> format = mSource->getFormat();
3104 int32_t sampleRate;
3105 int32_t numChannels;
3106 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
3107 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
3108
3109 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
3110 }
3111}
3112
James Dong17299ab2010-05-14 15:45:22 -07003113void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07003114 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07003115 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07003116 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07003117 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07003118
3119 //////////////// output port ////////////////////
3120 // format
3121 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
3122 format.nPortIndex = kPortIndexOutput;
3123 format.nIndex = 0;
3124 status_t err = OMX_ErrorNone;
3125 while (OMX_ErrorNone == err) {
3126 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003127 &format, sizeof(format)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003128 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
3129 break;
3130 }
3131 format.nIndex++;
3132 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003133 CHECK_EQ((status_t)OK, err);
James Dongabed93a2010-04-22 17:27:04 -07003134 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003135 &format, sizeof(format)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003136
3137 // port definition
3138 OMX_PARAM_PORTDEFINITIONTYPE def;
3139 InitOMXParams(&def);
3140 def.nPortIndex = kPortIndexOutput;
3141 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003142 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003143 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
3144 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
3145 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003146 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003147
3148 // profile
3149 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
3150 InitOMXParams(&profile);
3151 profile.nPortIndex = kPortIndexOutput;
3152 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003153 &profile, sizeof(profile)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003154 profile.nChannels = numChannels;
3155 profile.eChannelMode = (numChannels == 1?
3156 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
3157 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07003158 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003159 profile.nAudioBandWidth = 0;
3160 profile.nFrameLength = 0;
3161 profile.nAACtools = OMX_AUDIO_AACToolAll;
3162 profile.nAACERtools = OMX_AUDIO_AACERNone;
3163 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
3164 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
3165 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003166 &profile, sizeof(profile)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003167
Andreas Huberda050cf22009-09-02 14:01:43 -07003168 } else {
3169 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07003170 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07003171 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003172
Andreas Huber784202e2009-10-15 13:46:54 -07003173 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003174 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003175 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003176
Andreas Huberda050cf22009-09-02 14:01:43 -07003177 profile.nChannels = numChannels;
3178 profile.nSampleRate = sampleRate;
3179 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07003180
Andreas Huber784202e2009-10-15 13:46:54 -07003181 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003182 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003183 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07003184 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003185}
3186
3187void OMXCodec::setImageOutputFormat(
3188 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07003189 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07003190
3191#if 0
3192 OMX_INDEXTYPE index;
3193 status_t err = mOMX->get_extension_index(
3194 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003195 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003196
3197 err = mOMX->set_config(mNode, index, &format, sizeof(format));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003198 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003199#endif
3200
3201 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003202 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003203 def.nPortIndex = kPortIndexOutput;
3204
Andreas Huber784202e2009-10-15 13:46:54 -07003205 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003206 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003207 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003208
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003209 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage);
Andreas Huberbe06d262009-08-14 14:37:10 -07003210
3211 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07003212
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003213 CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07003214 imageDef->eColorFormat = format;
3215 imageDef->nFrameWidth = width;
3216 imageDef->nFrameHeight = height;
3217
3218 switch (format) {
3219 case OMX_COLOR_FormatYUV420PackedPlanar:
3220 case OMX_COLOR_FormatYUV411Planar:
3221 {
3222 def.nBufferSize = (width * height * 3) / 2;
3223 break;
3224 }
3225
3226 case OMX_COLOR_FormatCbYCrY:
3227 {
3228 def.nBufferSize = width * height * 2;
3229 break;
3230 }
3231
3232 case OMX_COLOR_Format32bitARGB8888:
3233 {
3234 def.nBufferSize = width * height * 4;
3235 break;
3236 }
3237
Andreas Huber201511c2009-09-08 14:01:44 -07003238 case OMX_COLOR_Format16bitARGB4444:
3239 case OMX_COLOR_Format16bitARGB1555:
3240 case OMX_COLOR_Format16bitRGB565:
3241 case OMX_COLOR_Format16bitBGR565:
3242 {
3243 def.nBufferSize = width * height * 2;
3244 break;
3245 }
3246
Andreas Huberbe06d262009-08-14 14:37:10 -07003247 default:
3248 CHECK(!"Should not be here. Unknown color format.");
3249 break;
3250 }
3251
Andreas Huber5c0a9132009-08-20 11:16:40 -07003252 def.nBufferCountActual = def.nBufferCountMin;
3253
Andreas Huber784202e2009-10-15 13:46:54 -07003254 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003255 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003256 CHECK_EQ(err, (status_t)OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07003257}
Andreas Huberbe06d262009-08-14 14:37:10 -07003258
Andreas Huber5c0a9132009-08-20 11:16:40 -07003259void OMXCodec::setJPEGInputFormat(
3260 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
3261 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003262 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003263 def.nPortIndex = kPortIndexInput;
3264
Andreas Huber784202e2009-10-15 13:46:54 -07003265 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003266 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003267 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003268
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003269 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage);
Andreas Huber5c0a9132009-08-20 11:16:40 -07003270 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3271
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003272 CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingJPEG);
Andreas Huberbe06d262009-08-14 14:37:10 -07003273 imageDef->nFrameWidth = width;
3274 imageDef->nFrameHeight = height;
3275
Andreas Huber5c0a9132009-08-20 11:16:40 -07003276 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07003277 def.nBufferCountActual = def.nBufferCountMin;
3278
Andreas Huber784202e2009-10-15 13:46:54 -07003279 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003280 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003281 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003282}
3283
3284void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
3285 CodecSpecificData *specific =
3286 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
3287
3288 specific->mSize = size;
3289 memcpy(specific->mData, data, size);
3290
3291 mCodecSpecificData.push(specific);
3292}
3293
3294void OMXCodec::clearCodecSpecificData() {
3295 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
3296 free(mCodecSpecificData.editItemAt(i));
3297 }
3298 mCodecSpecificData.clear();
3299 mCodecSpecificDataIndex = 0;
3300}
3301
James Dong36e573b2010-06-19 09:04:18 -07003302status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07003303 Mutex::Autolock autoLock(mLock);
3304
Andreas Huberbe06d262009-08-14 14:37:10 -07003305 if (mState != LOADED) {
3306 return UNKNOWN_ERROR;
3307 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07003308
Andreas Huberbe06d262009-08-14 14:37:10 -07003309 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07003310 if (mQuirks & kWantsNALFragments) {
3311 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07003312 }
James Dong36e573b2010-06-19 09:04:18 -07003313 if (meta) {
3314 int64_t startTimeUs = 0;
3315 int64_t timeUs;
3316 if (meta->findInt64(kKeyTime, &timeUs)) {
3317 startTimeUs = timeUs;
3318 }
3319 params->setInt64(kKeyTime, startTimeUs);
3320 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003321 status_t err = mSource->start(params.get());
3322
3323 if (err != OK) {
3324 return err;
3325 }
3326
3327 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07003328 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07003329 mSignalledEOS = false;
3330 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07003331 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003332 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003333 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
3334 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07003335 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07003336 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003337
3338 return init();
3339}
3340
3341status_t OMXCodec::stop() {
Andreas Huber7ff89392011-02-15 15:34:11 -08003342 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07003343
3344 Mutex::Autolock autoLock(mLock);
3345
3346 while (isIntermediateState(mState)) {
3347 mAsyncCompletion.wait(mLock);
3348 }
3349
3350 switch (mState) {
3351 case LOADED:
3352 case ERROR:
3353 break;
3354
3355 case EXECUTING:
3356 {
3357 setState(EXECUTING_TO_IDLE);
3358
Andreas Huber127fcdc2009-08-26 16:27:02 -07003359 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07003360 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07003361 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07003362
Andreas Huber127fcdc2009-08-26 16:27:02 -07003363 bool emulateInputFlushCompletion =
3364 !flushPortAsync(kPortIndexInput);
3365
3366 bool emulateOutputFlushCompletion =
3367 !flushPortAsync(kPortIndexOutput);
3368
3369 if (emulateInputFlushCompletion) {
3370 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3371 }
3372
3373 if (emulateOutputFlushCompletion) {
3374 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3375 }
3376 } else {
3377 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
3378 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
3379
3380 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07003381 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003382 CHECK_EQ(err, (status_t)OK);
Andreas Huber127fcdc2009-08-26 16:27:02 -07003383 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003384
3385 while (mState != LOADED && mState != ERROR) {
3386 mAsyncCompletion.wait(mLock);
3387 }
3388
3389 break;
3390 }
3391
3392 default:
3393 {
3394 CHECK(!"should not be here.");
3395 break;
3396 }
3397 }
3398
Andreas Hubera4357ad2010-04-02 12:49:54 -07003399 if (mLeftOverBuffer) {
3400 mLeftOverBuffer->release();
3401 mLeftOverBuffer = NULL;
3402 }
3403
Andreas Huberbe06d262009-08-14 14:37:10 -07003404 mSource->stop();
3405
Andreas Huber06be3b12011-01-25 14:55:00 -08003406 CODEC_LOGI("stopped in state %d", mState);
Andreas Huber4a9375e2010-02-09 11:54:33 -08003407
Andreas Huberbe06d262009-08-14 14:37:10 -07003408 return OK;
3409}
3410
3411sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07003412 Mutex::Autolock autoLock(mLock);
3413
Andreas Huberbe06d262009-08-14 14:37:10 -07003414 return mOutputFormat;
3415}
3416
3417status_t OMXCodec::read(
3418 MediaBuffer **buffer, const ReadOptions *options) {
3419 *buffer = NULL;
3420
3421 Mutex::Autolock autoLock(mLock);
3422
Andreas Huberd06e5b82009-08-28 13:18:14 -07003423 if (mState != EXECUTING && mState != RECONFIGURING) {
3424 return UNKNOWN_ERROR;
3425 }
3426
Andreas Hubere981c332009-10-22 13:49:30 -07003427 bool seeking = false;
3428 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003429 ReadOptions::SeekMode seekMode;
3430 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07003431 seeking = true;
3432 }
3433
Andreas Huber42978e52009-08-27 10:08:39 -07003434 if (mInitialBufferSubmit) {
3435 mInitialBufferSubmit = false;
3436
Andreas Hubere981c332009-10-22 13:49:30 -07003437 if (seeking) {
3438 CHECK(seekTimeUs >= 0);
3439 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003440 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07003441
3442 // There's no reason to trigger the code below, there's
3443 // nothing to flush yet.
3444 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07003445 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07003446 }
3447
Andreas Huber42978e52009-08-27 10:08:39 -07003448 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07003449
Andreas Huberd06e5b82009-08-28 13:18:14 -07003450 if (mState == EXECUTING) {
3451 // Otherwise mState == RECONFIGURING and this code will trigger
3452 // after the output port is reenabled.
3453 fillOutputBuffers();
3454 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003455 }
3456
Andreas Hubere981c332009-10-22 13:49:30 -07003457 if (seeking) {
Andreas Huberb9289832011-02-08 13:10:25 -08003458 while (mState == RECONFIGURING) {
3459 mBufferFilled.wait(mLock);
3460 }
3461
3462 if (mState != EXECUTING) {
3463 return UNKNOWN_ERROR;
3464 }
3465
Andreas Huber4c483422009-09-02 16:05:36 -07003466 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07003467
3468 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003469
3470 CHECK(seekTimeUs >= 0);
3471 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003472 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07003473
3474 mFilledBuffers.clear();
3475
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003476 CHECK_EQ((int)mState, (int)EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07003477
Andreas Huber404cc412009-08-25 14:26:05 -07003478 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
3479 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
3480
3481 if (emulateInputFlushCompletion) {
3482 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3483 }
3484
3485 if (emulateOutputFlushCompletion) {
3486 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3487 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08003488
3489 while (mSeekTimeUs >= 0) {
3490 mBufferFilled.wait(mLock);
3491 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003492 }
3493
3494 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
James Dong74556302010-12-20 21:29:12 -08003495 if (mIsEncoder) {
3496 if (NO_ERROR != mBufferFilled.waitRelative(mLock, 3000000000LL)) {
3497 LOGW("Timed out waiting for buffers from video encoder: %d/%d",
3498 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
3499 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]));
3500 }
3501 } else {
3502 mBufferFilled.wait(mLock);
3503 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003504 }
3505
3506 if (mState == ERROR) {
3507 return UNKNOWN_ERROR;
3508 }
3509
3510 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08003511 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07003512 }
3513
Andreas Hubercfd55572009-10-09 14:11:28 -07003514 if (mOutputPortSettingsHaveChanged) {
3515 mOutputPortSettingsHaveChanged = false;
3516
3517 return INFO_FORMAT_CHANGED;
3518 }
3519
Andreas Huberbe06d262009-08-14 14:37:10 -07003520 size_t index = *mFilledBuffers.begin();
3521 mFilledBuffers.erase(mFilledBuffers.begin());
3522
3523 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
Andreas Huberbbbcf652010-12-07 14:25:54 -08003524 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
3525 info->mStatus = OWNED_BY_CLIENT;
3526
Andreas Huberbe06d262009-08-14 14:37:10 -07003527 info->mMediaBuffer->add_ref();
3528 *buffer = info->mMediaBuffer;
3529
3530 return OK;
3531}
3532
3533void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
3534 Mutex::Autolock autoLock(mLock);
3535
3536 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
3537 for (size_t i = 0; i < buffers->size(); ++i) {
3538 BufferInfo *info = &buffers->editItemAt(i);
3539
3540 if (info->mMediaBuffer == buffer) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003541 CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED);
Andreas Huberbbbcf652010-12-07 14:25:54 -08003542 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_CLIENT);
3543
3544 info->mStatus = OWNED_BY_US;
3545
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003546 if (buffer->graphicBuffer() == 0) {
3547 fillOutputBuffer(info);
3548 } else {
3549 sp<MetaData> metaData = info->mMediaBuffer->meta_data();
3550 int32_t rendered = 0;
3551 if (!metaData->findInt32(kKeyRendered, &rendered)) {
3552 rendered = 0;
3553 }
3554 if (!rendered) {
3555 status_t err = cancelBufferToNativeWindow(info);
3556 if (err < 0) {
3557 return;
3558 }
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003559 }
3560
Andreas Huberbbbcf652010-12-07 14:25:54 -08003561 info->mStatus = OWNED_BY_NATIVE_WINDOW;
3562
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003563 // Dequeue the next buffer from the native window.
3564 BufferInfo *nextBufInfo = dequeueBufferFromNativeWindow();
3565 if (nextBufInfo == 0) {
3566 return;
3567 }
3568
3569 // Give the buffer to the OMX node to fill.
3570 fillOutputBuffer(nextBufInfo);
3571 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003572 return;
3573 }
3574 }
3575
3576 CHECK(!"should not be here.");
3577}
3578
3579static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
3580 static const char *kNames[] = {
3581 "OMX_IMAGE_CodingUnused",
3582 "OMX_IMAGE_CodingAutoDetect",
3583 "OMX_IMAGE_CodingJPEG",
3584 "OMX_IMAGE_CodingJPEG2K",
3585 "OMX_IMAGE_CodingEXIF",
3586 "OMX_IMAGE_CodingTIFF",
3587 "OMX_IMAGE_CodingGIF",
3588 "OMX_IMAGE_CodingPNG",
3589 "OMX_IMAGE_CodingLZW",
3590 "OMX_IMAGE_CodingBMP",
3591 };
3592
3593 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3594
3595 if (type < 0 || (size_t)type >= numNames) {
3596 return "UNKNOWN";
3597 } else {
3598 return kNames[type];
3599 }
3600}
3601
3602static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
3603 static const char *kNames[] = {
3604 "OMX_COLOR_FormatUnused",
3605 "OMX_COLOR_FormatMonochrome",
3606 "OMX_COLOR_Format8bitRGB332",
3607 "OMX_COLOR_Format12bitRGB444",
3608 "OMX_COLOR_Format16bitARGB4444",
3609 "OMX_COLOR_Format16bitARGB1555",
3610 "OMX_COLOR_Format16bitRGB565",
3611 "OMX_COLOR_Format16bitBGR565",
3612 "OMX_COLOR_Format18bitRGB666",
3613 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003614 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07003615 "OMX_COLOR_Format24bitRGB888",
3616 "OMX_COLOR_Format24bitBGR888",
3617 "OMX_COLOR_Format24bitARGB1887",
3618 "OMX_COLOR_Format25bitARGB1888",
3619 "OMX_COLOR_Format32bitBGRA8888",
3620 "OMX_COLOR_Format32bitARGB8888",
3621 "OMX_COLOR_FormatYUV411Planar",
3622 "OMX_COLOR_FormatYUV411PackedPlanar",
3623 "OMX_COLOR_FormatYUV420Planar",
3624 "OMX_COLOR_FormatYUV420PackedPlanar",
3625 "OMX_COLOR_FormatYUV420SemiPlanar",
3626 "OMX_COLOR_FormatYUV422Planar",
3627 "OMX_COLOR_FormatYUV422PackedPlanar",
3628 "OMX_COLOR_FormatYUV422SemiPlanar",
3629 "OMX_COLOR_FormatYCbYCr",
3630 "OMX_COLOR_FormatYCrYCb",
3631 "OMX_COLOR_FormatCbYCrY",
3632 "OMX_COLOR_FormatCrYCbY",
3633 "OMX_COLOR_FormatYUV444Interleaved",
3634 "OMX_COLOR_FormatRawBayer8bit",
3635 "OMX_COLOR_FormatRawBayer10bit",
3636 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003637 "OMX_COLOR_FormatL2",
3638 "OMX_COLOR_FormatL4",
3639 "OMX_COLOR_FormatL8",
3640 "OMX_COLOR_FormatL16",
3641 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07003642 "OMX_COLOR_FormatL32",
3643 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
3644 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
3645 "OMX_COLOR_Format18BitBGR666",
3646 "OMX_COLOR_Format24BitARGB6666",
3647 "OMX_COLOR_Format24BitABGR6666",
3648 };
3649
3650 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3651
Andreas Huberbe06d262009-08-14 14:37:10 -07003652 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3653 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3654 } else if (type < 0 || (size_t)type >= numNames) {
3655 return "UNKNOWN";
3656 } else {
3657 return kNames[type];
3658 }
3659}
3660
3661static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3662 static const char *kNames[] = {
3663 "OMX_VIDEO_CodingUnused",
3664 "OMX_VIDEO_CodingAutoDetect",
3665 "OMX_VIDEO_CodingMPEG2",
3666 "OMX_VIDEO_CodingH263",
3667 "OMX_VIDEO_CodingMPEG4",
3668 "OMX_VIDEO_CodingWMV",
3669 "OMX_VIDEO_CodingRV",
3670 "OMX_VIDEO_CodingAVC",
3671 "OMX_VIDEO_CodingMJPEG",
3672 };
3673
3674 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3675
3676 if (type < 0 || (size_t)type >= numNames) {
3677 return "UNKNOWN";
3678 } else {
3679 return kNames[type];
3680 }
3681}
3682
3683static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3684 static const char *kNames[] = {
3685 "OMX_AUDIO_CodingUnused",
3686 "OMX_AUDIO_CodingAutoDetect",
3687 "OMX_AUDIO_CodingPCM",
3688 "OMX_AUDIO_CodingADPCM",
3689 "OMX_AUDIO_CodingAMR",
3690 "OMX_AUDIO_CodingGSMFR",
3691 "OMX_AUDIO_CodingGSMEFR",
3692 "OMX_AUDIO_CodingGSMHR",
3693 "OMX_AUDIO_CodingPDCFR",
3694 "OMX_AUDIO_CodingPDCEFR",
3695 "OMX_AUDIO_CodingPDCHR",
3696 "OMX_AUDIO_CodingTDMAFR",
3697 "OMX_AUDIO_CodingTDMAEFR",
3698 "OMX_AUDIO_CodingQCELP8",
3699 "OMX_AUDIO_CodingQCELP13",
3700 "OMX_AUDIO_CodingEVRC",
3701 "OMX_AUDIO_CodingSMV",
3702 "OMX_AUDIO_CodingG711",
3703 "OMX_AUDIO_CodingG723",
3704 "OMX_AUDIO_CodingG726",
3705 "OMX_AUDIO_CodingG729",
3706 "OMX_AUDIO_CodingAAC",
3707 "OMX_AUDIO_CodingMP3",
3708 "OMX_AUDIO_CodingSBC",
3709 "OMX_AUDIO_CodingVORBIS",
3710 "OMX_AUDIO_CodingWMA",
3711 "OMX_AUDIO_CodingRA",
3712 "OMX_AUDIO_CodingMIDI",
3713 };
3714
3715 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3716
3717 if (type < 0 || (size_t)type >= numNames) {
3718 return "UNKNOWN";
3719 } else {
3720 return kNames[type];
3721 }
3722}
3723
3724static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3725 static const char *kNames[] = {
3726 "OMX_AUDIO_PCMModeLinear",
3727 "OMX_AUDIO_PCMModeALaw",
3728 "OMX_AUDIO_PCMModeMULaw",
3729 };
3730
3731 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3732
3733 if (type < 0 || (size_t)type >= numNames) {
3734 return "UNKNOWN";
3735 } else {
3736 return kNames[type];
3737 }
3738}
3739
Andreas Huber7ae02c82009-09-09 16:29:47 -07003740static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3741 static const char *kNames[] = {
3742 "OMX_AUDIO_AMRBandModeUnused",
3743 "OMX_AUDIO_AMRBandModeNB0",
3744 "OMX_AUDIO_AMRBandModeNB1",
3745 "OMX_AUDIO_AMRBandModeNB2",
3746 "OMX_AUDIO_AMRBandModeNB3",
3747 "OMX_AUDIO_AMRBandModeNB4",
3748 "OMX_AUDIO_AMRBandModeNB5",
3749 "OMX_AUDIO_AMRBandModeNB6",
3750 "OMX_AUDIO_AMRBandModeNB7",
3751 "OMX_AUDIO_AMRBandModeWB0",
3752 "OMX_AUDIO_AMRBandModeWB1",
3753 "OMX_AUDIO_AMRBandModeWB2",
3754 "OMX_AUDIO_AMRBandModeWB3",
3755 "OMX_AUDIO_AMRBandModeWB4",
3756 "OMX_AUDIO_AMRBandModeWB5",
3757 "OMX_AUDIO_AMRBandModeWB6",
3758 "OMX_AUDIO_AMRBandModeWB7",
3759 "OMX_AUDIO_AMRBandModeWB8",
3760 };
3761
3762 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3763
3764 if (type < 0 || (size_t)type >= numNames) {
3765 return "UNKNOWN";
3766 } else {
3767 return kNames[type];
3768 }
3769}
3770
3771static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3772 static const char *kNames[] = {
3773 "OMX_AUDIO_AMRFrameFormatConformance",
3774 "OMX_AUDIO_AMRFrameFormatIF1",
3775 "OMX_AUDIO_AMRFrameFormatIF2",
3776 "OMX_AUDIO_AMRFrameFormatFSF",
3777 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3778 "OMX_AUDIO_AMRFrameFormatITU",
3779 };
3780
3781 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3782
3783 if (type < 0 || (size_t)type >= numNames) {
3784 return "UNKNOWN";
3785 } else {
3786 return kNames[type];
3787 }
3788}
Andreas Huberbe06d262009-08-14 14:37:10 -07003789
3790void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3791 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003792 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003793 def.nPortIndex = portIndex;
3794
Andreas Huber784202e2009-10-15 13:46:54 -07003795 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003796 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003797 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003798
3799 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3800
3801 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3802 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3803
3804 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3805 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3806 printf(" nBufferSize = %ld\n", def.nBufferSize);
3807
3808 switch (def.eDomain) {
3809 case OMX_PortDomainImage:
3810 {
3811 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3812
3813 printf("\n");
3814 printf(" // Image\n");
3815 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3816 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3817 printf(" nStride = %ld\n", imageDef->nStride);
3818
3819 printf(" eCompressionFormat = %s\n",
3820 imageCompressionFormatString(imageDef->eCompressionFormat));
3821
3822 printf(" eColorFormat = %s\n",
3823 colorFormatString(imageDef->eColorFormat));
3824
3825 break;
3826 }
3827
3828 case OMX_PortDomainVideo:
3829 {
3830 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3831
3832 printf("\n");
3833 printf(" // Video\n");
3834 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3835 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3836 printf(" nStride = %ld\n", videoDef->nStride);
3837
3838 printf(" eCompressionFormat = %s\n",
3839 videoCompressionFormatString(videoDef->eCompressionFormat));
3840
3841 printf(" eColorFormat = %s\n",
3842 colorFormatString(videoDef->eColorFormat));
3843
3844 break;
3845 }
3846
3847 case OMX_PortDomainAudio:
3848 {
3849 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3850
3851 printf("\n");
3852 printf(" // Audio\n");
3853 printf(" eEncoding = %s\n",
3854 audioCodingTypeString(audioDef->eEncoding));
3855
3856 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3857 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003858 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003859 params.nPortIndex = portIndex;
3860
Andreas Huber784202e2009-10-15 13:46:54 -07003861 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003862 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003863 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003864
3865 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3866 printf(" nChannels = %ld\n", params.nChannels);
3867 printf(" bInterleaved = %d\n", params.bInterleaved);
3868 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3869
3870 printf(" eNumData = %s\n",
3871 params.eNumData == OMX_NumericalDataSigned
3872 ? "signed" : "unsigned");
3873
3874 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003875 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3876 OMX_AUDIO_PARAM_AMRTYPE amr;
3877 InitOMXParams(&amr);
3878 amr.nPortIndex = portIndex;
3879
Andreas Huber784202e2009-10-15 13:46:54 -07003880 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003881 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003882 CHECK_EQ(err, (status_t)OK);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003883
3884 printf(" nChannels = %ld\n", amr.nChannels);
3885 printf(" eAMRBandMode = %s\n",
3886 amrBandModeString(amr.eAMRBandMode));
3887 printf(" eAMRFrameFormat = %s\n",
3888 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003889 }
3890
3891 break;
3892 }
3893
3894 default:
3895 {
3896 printf(" // Unknown\n");
3897 break;
3898 }
3899 }
3900
3901 printf("}\n");
3902}
3903
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003904status_t OMXCodec::initNativeWindow() {
3905 // Enable use of a GraphicBuffer as the output for this node. This must
3906 // happen before getting the IndexParamPortDefinition parameter because it
3907 // will affect the pixel format that the node reports.
3908 status_t err = mOMX->enableGraphicBuffers(mNode, kPortIndexOutput, OMX_TRUE);
3909 if (err != 0) {
3910 return err;
3911 }
3912
3913 return OK;
3914}
3915
Andreas Huberbe06d262009-08-14 14:37:10 -07003916void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3917 mOutputFormat = new MetaData;
3918 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003919 if (mIsEncoder) {
3920 int32_t timeScale;
3921 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3922 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3923 }
3924 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003925
3926 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003927 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003928 def.nPortIndex = kPortIndexOutput;
3929
Andreas Huber784202e2009-10-15 13:46:54 -07003930 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003931 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003932 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003933
3934 switch (def.eDomain) {
3935 case OMX_PortDomainImage:
3936 {
3937 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003938 CHECK_EQ((int)imageDef->eCompressionFormat,
3939 (int)OMX_IMAGE_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07003940
Andreas Hubere6c40962009-09-10 14:13:30 -07003941 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003942 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3943 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3944 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3945 break;
3946 }
3947
3948 case OMX_PortDomainAudio:
3949 {
3950 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3951
Andreas Huberda050cf22009-09-02 14:01:43 -07003952 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3953 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003954 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003955 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003956
Andreas Huber784202e2009-10-15 13:46:54 -07003957 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003958 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003959 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003960
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003961 CHECK_EQ((int)params.eNumData, (int)OMX_NumericalDataSigned);
3962 CHECK_EQ(params.nBitPerSample, 16u);
3963 CHECK_EQ((int)params.ePCMMode, (int)OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003964
Andreas Huberda050cf22009-09-02 14:01:43 -07003965 int32_t numChannels, sampleRate;
3966 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3967 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003968
Andreas Huberda050cf22009-09-02 14:01:43 -07003969 if ((OMX_U32)numChannels != params.nChannels) {
3970 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003971 "the input stream contains (contains %d channels, "
3972 "codec outputs %ld channels).",
3973 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003974 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003975
Andreas Hubere6c40962009-09-10 14:13:30 -07003976 mOutputFormat->setCString(
3977 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003978
3979 // Use the codec-advertised number of channels, as some
3980 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003981 // mono. If we know the codec lies about this information,
3982 // use the actual number of channels instead.
3983 mOutputFormat->setInt32(
3984 kKeyChannelCount,
3985 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3986 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003987
3988 // The codec-reported sampleRate is not reliable...
3989 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3990 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003991 OMX_AUDIO_PARAM_AMRTYPE amr;
3992 InitOMXParams(&amr);
3993 amr.nPortIndex = kPortIndexOutput;
3994
Andreas Huber784202e2009-10-15 13:46:54 -07003995 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003996 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003997 CHECK_EQ(err, (status_t)OK);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003998
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003999 CHECK_EQ(amr.nChannels, 1u);
Andreas Huber7ae02c82009-09-09 16:29:47 -07004000 mOutputFormat->setInt32(kKeyChannelCount, 1);
4001
4002 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
4003 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07004004 mOutputFormat->setCString(
4005 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07004006 mOutputFormat->setInt32(kKeySampleRate, 8000);
4007 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
4008 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07004009 mOutputFormat->setCString(
4010 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07004011 mOutputFormat->setInt32(kKeySampleRate, 16000);
4012 } else {
4013 CHECK(!"Unknown AMR band mode.");
4014 }
Andreas Huberda050cf22009-09-02 14:01:43 -07004015 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07004016 mOutputFormat->setCString(
4017 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07004018 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07004019 inputFormat->findInt32(kKeyChannelCount, &numChannels);
4020 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07004021 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07004022 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
4023 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07004024 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07004025 } else {
4026 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07004027 }
Andreas Huberbe06d262009-08-14 14:37:10 -07004028 break;
4029 }
4030
4031 case OMX_PortDomainVideo:
4032 {
4033 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
4034
4035 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07004036 mOutputFormat->setCString(
4037 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07004038 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07004039 mOutputFormat->setCString(
4040 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07004041 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07004042 mOutputFormat->setCString(
4043 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07004044 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07004045 mOutputFormat->setCString(
4046 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07004047 } else {
4048 CHECK(!"Unknown compression format.");
4049 }
4050
James Dong5592bcc2010-10-22 17:10:43 -07004051 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
4052 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
Andreas Huberbe06d262009-08-14 14:37:10 -07004053 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004054
James Dong0c9153d2010-11-23 11:30:21 -08004055 if (!mIsEncoder) {
4056 OMX_CONFIG_RECTTYPE rect;
James Donga1735412011-01-07 14:56:34 -08004057 InitOMXParams(&rect);
4058 rect.nPortIndex = kPortIndexOutput;
James Dong0c9153d2010-11-23 11:30:21 -08004059 status_t err =
4060 mOMX->getConfig(
4061 mNode, OMX_IndexConfigCommonOutputCrop,
4062 &rect, sizeof(rect));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004063
James Dong0c9153d2010-11-23 11:30:21 -08004064 if (err == OK) {
4065 CHECK_GE(rect.nLeft, 0);
4066 CHECK_GE(rect.nTop, 0);
4067 CHECK_GE(rect.nWidth, 0u);
4068 CHECK_GE(rect.nHeight, 0u);
4069 CHECK_LE(rect.nLeft + rect.nWidth - 1, video_def->nFrameWidth);
4070 CHECK_LE(rect.nTop + rect.nHeight - 1, video_def->nFrameHeight);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004071
James Dong0c9153d2010-11-23 11:30:21 -08004072 mOutputFormat->setRect(
4073 kKeyCropRect,
4074 rect.nLeft,
4075 rect.nTop,
4076 rect.nLeft + rect.nWidth - 1,
4077 rect.nTop + rect.nHeight - 1);
4078 } else {
4079 mOutputFormat->setRect(
4080 kKeyCropRect,
4081 0, 0,
4082 video_def->nFrameWidth - 1,
4083 video_def->nFrameHeight - 1);
4084 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004085 }
4086
Andreas Huberbe06d262009-08-14 14:37:10 -07004087 break;
4088 }
4089
4090 default:
4091 {
4092 CHECK(!"should not be here, neither audio nor video.");
4093 break;
4094 }
4095 }
4096}
4097
Andreas Huber1f24b302010-06-10 11:12:39 -07004098status_t OMXCodec::pause() {
4099 Mutex::Autolock autoLock(mLock);
4100
4101 mPaused = true;
4102
4103 return OK;
4104}
4105
Andreas Hubere6c40962009-09-10 14:13:30 -07004106////////////////////////////////////////////////////////////////////////////////
4107
4108status_t QueryCodecs(
4109 const sp<IOMX> &omx,
4110 const char *mime, bool queryDecoders,
4111 Vector<CodecCapabilities> *results) {
4112 results->clear();
4113
4114 for (int index = 0;; ++index) {
4115 const char *componentName;
4116
4117 if (!queryDecoders) {
4118 componentName = GetCodec(
4119 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
4120 mime, index);
4121 } else {
4122 componentName = GetCodec(
4123 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
4124 mime, index);
4125 }
4126
4127 if (!componentName) {
4128 return OK;
4129 }
4130
Andreas Huber1a189a82010-03-24 13:49:20 -07004131 if (strncmp(componentName, "OMX.", 4)) {
4132 // Not an OpenMax component but a software codec.
4133
4134 results->push();
4135 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
4136 caps->mComponentName = componentName;
4137
4138 continue;
4139 }
4140
Andreas Huber784202e2009-10-15 13:46:54 -07004141 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07004142 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07004143 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07004144
4145 if (err != OK) {
4146 continue;
4147 }
4148
James Dong722d5912010-04-13 10:56:59 -07004149 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07004150
4151 results->push();
4152 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
4153 caps->mComponentName = componentName;
4154
4155 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
4156 InitOMXParams(&param);
4157
4158 param.nPortIndex = queryDecoders ? 0 : 1;
4159
4160 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07004161 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07004162 node, OMX_IndexParamVideoProfileLevelQuerySupported,
4163 &param, sizeof(param));
4164
4165 if (err != OK) {
4166 break;
4167 }
4168
4169 CodecProfileLevel profileLevel;
4170 profileLevel.mProfile = param.eProfile;
4171 profileLevel.mLevel = param.eLevel;
4172
4173 caps->mProfileLevels.push(profileLevel);
4174 }
4175
James Dongd7810892010-11-11 00:33:05 -08004176 // Color format query
4177 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
4178 InitOMXParams(&portFormat);
4179 portFormat.nPortIndex = queryDecoders ? 1 : 0;
4180 for (portFormat.nIndex = 0;; ++portFormat.nIndex) {
4181 err = omx->getParameter(
4182 node, OMX_IndexParamVideoPortFormat,
4183 &portFormat, sizeof(portFormat));
4184 if (err != OK) {
4185 break;
4186 }
4187 caps->mColorFormats.push(portFormat.eColorFormat);
4188 }
4189
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004190 CHECK_EQ(omx->freeNode(node), (status_t)OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07004191 }
4192}
4193
James Dong31b9375f2010-11-10 21:11:41 -08004194void OMXCodec::restorePatchedDataPointer(BufferInfo *info) {
4195 CHECK(mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames));
4196 CHECK(mOMXLivesLocally);
4197
4198 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *)info->mBuffer;
4199 header->pBuffer = (OMX_U8 *)info->mData;
4200}
4201
Andreas Huberbe06d262009-08-14 14:37:10 -07004202} // namespace android