blob: 4a94e0d6acb12720baa9383a62630794bf862151 [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
Andreas Huber4c19bf92010-09-08 14:32:20 -0700531 if (!(flags & kIgnoreCodecSpecificData)) {
532 uint32_t type;
533 const void *data;
534 size_t size;
535 if (meta->findData(kKeyESDS, &type, &data, &size)) {
536 ESDS esds((const char *)data, size);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800537 CHECK_EQ(esds.InitCheck(), (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700538
Andreas Huber4c19bf92010-09-08 14:32:20 -0700539 const void *codec_specific_data;
540 size_t codec_specific_data_size;
541 esds.getCodecSpecificInfo(
542 &codec_specific_data, &codec_specific_data_size);
Andreas Huberbe06d262009-08-14 14:37:10 -0700543
Andreas Huber4c19bf92010-09-08 14:32:20 -0700544 addCodecSpecificData(
545 codec_specific_data, codec_specific_data_size);
546 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
547 // Parse the AVCDecoderConfigurationRecord
Andreas Huberebf66ea2009-08-19 13:32:58 -0700548
Andreas Huber4c19bf92010-09-08 14:32:20 -0700549 const uint8_t *ptr = (const uint8_t *)data;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700550
Andreas Huber4c19bf92010-09-08 14:32:20 -0700551 CHECK(size >= 7);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800552 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1
Andreas Huber4c19bf92010-09-08 14:32:20 -0700553 uint8_t profile = ptr[1];
554 uint8_t level = ptr[3];
Andreas Huberebf66ea2009-08-19 13:32:58 -0700555
Andreas Huber4c19bf92010-09-08 14:32:20 -0700556 // There is decodable content out there that fails the following
557 // assertion, let's be lenient for now...
558 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700559
Andreas Huber4c19bf92010-09-08 14:32:20 -0700560 size_t lengthSize = 1 + (ptr[4] & 3);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700561
Andreas Huber4c19bf92010-09-08 14:32:20 -0700562 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
563 // violates it...
564 // CHECK((ptr[5] >> 5) == 7); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700565
Andreas Huber4c19bf92010-09-08 14:32:20 -0700566 size_t numSeqParameterSets = ptr[5] & 31;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700567
Andreas Huber4c19bf92010-09-08 14:32:20 -0700568 ptr += 6;
569 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700570
Andreas Huber4c19bf92010-09-08 14:32:20 -0700571 for (size_t i = 0; i < numSeqParameterSets; ++i) {
572 CHECK(size >= 2);
573 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700574
Andreas Huber4c19bf92010-09-08 14:32:20 -0700575 ptr += 2;
576 size -= 2;
Andreas Huberbe06d262009-08-14 14:37:10 -0700577
Andreas Huber4c19bf92010-09-08 14:32:20 -0700578 CHECK(size >= length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700579
Andreas Huber4c19bf92010-09-08 14:32:20 -0700580 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700581
Andreas Huber4c19bf92010-09-08 14:32:20 -0700582 ptr += length;
583 size -= length;
584 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700585
Andreas Huber4c19bf92010-09-08 14:32:20 -0700586 CHECK(size >= 1);
587 size_t numPictureParameterSets = *ptr;
588 ++ptr;
589 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700590
Andreas Huber4c19bf92010-09-08 14:32:20 -0700591 for (size_t i = 0; i < numPictureParameterSets; ++i) {
592 CHECK(size >= 2);
593 size_t length = U16_AT(ptr);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700594
Andreas Huber4c19bf92010-09-08 14:32:20 -0700595 ptr += 2;
596 size -= 2;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700597
Andreas Huber4c19bf92010-09-08 14:32:20 -0700598 CHECK(size >= length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700599
Andreas Huber4c19bf92010-09-08 14:32:20 -0700600 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700601
Andreas Huber4c19bf92010-09-08 14:32:20 -0700602 ptr += length;
603 size -= length;
604 }
Andreas Huberebf66ea2009-08-19 13:32:58 -0700605
Andreas Huber6ac2cb22010-11-18 14:06:07 -0800606 CODEC_LOGI(
Andreas Huber4c19bf92010-09-08 14:32:20 -0700607 "AVC profile = %d (%s), level = %d",
608 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700609
Andreas Huber4c19bf92010-09-08 14:32:20 -0700610 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
611 && (profile != kAVCProfileBaseline || level > 30)) {
612 // This stream exceeds the decoder's capabilities. The decoder
613 // does not handle this gracefully and would clobber the heap
614 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700615
Andreas Huber4c19bf92010-09-08 14:32:20 -0700616 LOGE("Profile and/or level exceed the decoder's capabilities.");
617 return ERROR_UNSUPPORTED;
618 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700619 }
620 }
621
James Dong17299ab2010-05-14 15:45:22 -0700622 int32_t bitRate = 0;
623 if (mIsEncoder) {
624 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
625 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700626 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700627 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700628 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700629 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700630 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700631 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700632 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700633 int32_t numChannels, sampleRate;
634 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
635 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
636
James Dong17299ab2010-05-14 15:45:22 -0700637 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700638 }
James Dongabed93a2010-04-22 17:27:04 -0700639
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700640 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700641
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700642 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700643 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700644 } else {
James Dong1244eab2010-06-08 11:58:53 -0700645 int32_t width, height;
646 bool success = meta->findInt32(kKeyWidth, &width);
647 success = success && meta->findInt32(kKeyHeight, &height);
648 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700649 status_t err = setVideoOutputFormat(
650 mMIME, width, height);
651
652 if (err != OK) {
653 return err;
654 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700655 }
656 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700657
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700658 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
659 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700660 OMX_COLOR_FORMATTYPE format =
661 OMX_COLOR_Format32bitARGB8888;
662 // OMX_COLOR_FormatYUV420PackedPlanar;
663 // OMX_COLOR_FormatCbYCrY;
664 // OMX_COLOR_FormatYUV411Planar;
665
666 int32_t width, height;
667 bool success = meta->findInt32(kKeyWidth, &width);
668 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700669
670 int32_t compressedSize;
671 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700672 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700673
674 CHECK(success);
675 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700676
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700677 setImageOutputFormat(format, width, height);
678 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700679 }
680
Andreas Huberda050cf22009-09-02 14:01:43 -0700681 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800682 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700683 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700684 }
685
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700686 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700687 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
688 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700689 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700690 }
691
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700692 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700693
Andreas Huber5a40e392010-10-18 09:57:42 -0700694 if ((flags & kClientNeedsFramebuffer)
695 && !strncmp(mComponentName, "OMX.SEC.", 8)) {
696 OMX_INDEXTYPE index;
697
698 status_t err =
699 mOMX->getExtensionIndex(
700 mNode,
701 "OMX.SEC.index.ThumbnailMode",
702 &index);
703
704 if (err != OK) {
705 return err;
706 }
707
708 OMX_BOOL enable = OMX_TRUE;
709 err = mOMX->setConfig(mNode, index, &enable, sizeof(enable));
710
711 if (err != OK) {
712 CODEC_LOGE("setConfig('OMX.SEC.index.ThumbnailMode') "
713 "returned error 0x%08x", err);
714
715 return err;
716 }
717
718 mQuirks &= ~kOutputBuffersAreUnreadable;
719 }
720
Jamie Gennisdbfb32e2010-10-20 15:53:59 -0700721 if (mNativeWindow != NULL
722 && !mIsEncoder
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700723 && !strncasecmp(mMIME, "video/", 6)
724 && !strncmp(mComponentName, "OMX.", 4)) {
725 status_t err = initNativeWindow();
726 if (err != OK) {
727 return err;
728 }
729 }
730
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700731 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700732}
733
Andreas Huberda050cf22009-09-02 14:01:43 -0700734void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
735 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700736 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700737 def.nPortIndex = portIndex;
738
Andreas Huber784202e2009-10-15 13:46:54 -0700739 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700740 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800741 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -0700742
Andreas Huberb8de9572010-02-22 14:58:45 -0800743 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
744 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700745 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700746 }
747
Andreas Huber784202e2009-10-15 13:46:54 -0700748 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700749 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800750 CHECK_EQ(err, (status_t)OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800751
752 err = mOMX->getParameter(
753 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800754 CHECK_EQ(err, (status_t)OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800755
756 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800757 if (portIndex == kPortIndexInput
758 && (mQuirks & kInputBufferSizesAreBogus)) {
759 CHECK_EQ(def.nBufferSize, size);
760 } else {
761 CHECK(def.nBufferSize >= size);
762 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700763}
764
Andreas Huberbe06d262009-08-14 14:37:10 -0700765status_t OMXCodec::setVideoPortFormatType(
766 OMX_U32 portIndex,
767 OMX_VIDEO_CODINGTYPE compressionFormat,
768 OMX_COLOR_FORMATTYPE colorFormat) {
769 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700770 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700771 format.nPortIndex = portIndex;
772 format.nIndex = 0;
773 bool found = false;
774
775 OMX_U32 index = 0;
776 for (;;) {
777 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700778 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700779 mNode, OMX_IndexParamVideoPortFormat,
780 &format, sizeof(format));
781
782 if (err != OK) {
783 return err;
784 }
785
786 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700787 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700788
789#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700790 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700791 portIndex,
792 index, format.eCompressionFormat, format.eColorFormat);
793#endif
794
795 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
796 if (portIndex == kPortIndexInput
797 && colorFormat == format.eColorFormat) {
798 // eCompressionFormat does not seem right.
799 found = true;
800 break;
801 }
802 if (portIndex == kPortIndexOutput
803 && compressionFormat == format.eCompressionFormat) {
804 // eColorFormat does not seem right.
805 found = true;
806 break;
807 }
808 }
809
810 if (format.eCompressionFormat == compressionFormat
811 && format.eColorFormat == colorFormat) {
812 found = true;
813 break;
814 }
815
816 ++index;
817 }
818
819 if (!found) {
820 return UNKNOWN_ERROR;
821 }
822
Andreas Huber53a76bd2009-10-06 16:20:44 -0700823 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700824 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700825 mNode, OMX_IndexParamVideoPortFormat,
826 &format, sizeof(format));
827
828 return err;
829}
830
Andreas Huberb482ce82009-10-29 12:02:48 -0700831static size_t getFrameSize(
832 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
833 switch (colorFormat) {
834 case OMX_COLOR_FormatYCbYCr:
835 case OMX_COLOR_FormatCbYCrY:
836 return width * height * 2;
837
Andreas Huber71c27d92010-03-19 11:43:15 -0700838 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700839 case OMX_COLOR_FormatYUV420SemiPlanar:
840 return (width * height * 3) / 2;
841
842 default:
843 CHECK(!"Should not be here. Unsupported color format.");
844 break;
845 }
846}
847
James Dongafd97e82010-08-03 17:19:23 -0700848status_t OMXCodec::findTargetColorFormat(
849 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) {
850 LOGV("findTargetColorFormat");
851 CHECK(mIsEncoder);
852
853 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
854 int32_t targetColorFormat;
855 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) {
856 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat;
857 } else {
858 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
859 *colorFormat = OMX_COLOR_FormatYCbYCr;
860 }
861 }
862
863 // Check whether the target color format is supported.
864 return isColorFormatSupported(*colorFormat, kPortIndexInput);
865}
866
867status_t OMXCodec::isColorFormatSupported(
868 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) {
869 LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat));
870
871 // Enumerate all the color formats supported by
872 // the omx component to see whether the given
873 // color format is supported.
874 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
875 InitOMXParams(&portFormat);
876 portFormat.nPortIndex = portIndex;
877 OMX_U32 index = 0;
878 portFormat.nIndex = index;
879 while (true) {
880 if (OMX_ErrorNone != mOMX->getParameter(
881 mNode, OMX_IndexParamVideoPortFormat,
882 &portFormat, sizeof(portFormat))) {
James Dongb5024da2010-09-13 16:30:51 -0700883 break;
James Dongafd97e82010-08-03 17:19:23 -0700884 }
885 // Make sure that omx component does not overwrite
886 // the incremented index (bug 2897413).
887 CHECK_EQ(index, portFormat.nIndex);
888 if ((portFormat.eColorFormat == colorFormat)) {
889 LOGV("Found supported color format: %d", portFormat.eColorFormat);
890 return OK; // colorFormat is supported!
891 }
892 ++index;
893 portFormat.nIndex = index;
894
895 // OMX Spec defines less than 50 color formats
896 // 1000 is more than enough for us to tell whether the omx
897 // component in question is buggy or not.
898 if (index >= 1000) {
899 LOGE("More than %ld color formats are supported???", index);
900 break;
901 }
902 }
James Dongb5024da2010-09-13 16:30:51 -0700903
904 LOGE("color format %d is not supported", colorFormat);
James Dongafd97e82010-08-03 17:19:23 -0700905 return UNKNOWN_ERROR;
906}
907
Andreas Huberbe06d262009-08-14 14:37:10 -0700908void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700909 const char *mime, const sp<MetaData>& meta) {
910
911 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
912 bool success = meta->findInt32(kKeyWidth, &width);
913 success = success && meta->findInt32(kKeyHeight, &height);
James Dongaac193c2010-11-10 20:43:53 -0800914 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -0700915 success = success && meta->findInt32(kKeyBitRate, &bitRate);
916 success = success && meta->findInt32(kKeyStride, &stride);
917 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
918 CHECK(success);
919 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700920
921 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700922 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700923 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700924 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700925 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700926 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700927 compressionFormat = OMX_VIDEO_CodingH263;
928 } else {
929 LOGE("Not a supported video mime type: %s", mime);
930 CHECK(!"Should not be here. Not a supported video mime type.");
931 }
932
James Dongafd97e82010-08-03 17:19:23 -0700933 OMX_COLOR_FORMATTYPE colorFormat;
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800934 CHECK_EQ((status_t)OK, findTargetColorFormat(meta, &colorFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -0700935
James Dongb00e2462010-04-26 17:48:26 -0700936 status_t err;
937 OMX_PARAM_PORTDEFINITIONTYPE def;
938 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
939
940 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700941 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700942 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800943 colorFormat), (status_t)OK);
James Dong4f501f02010-06-07 14:41:41 -0700944
James Dongb00e2462010-04-26 17:48:26 -0700945 InitOMXParams(&def);
946 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700947
James Dongb00e2462010-04-26 17:48:26 -0700948 err = mOMX->getParameter(
949 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800950 CHECK_EQ(err, (status_t)OK);
James Dongb00e2462010-04-26 17:48:26 -0700951
James Dong1244eab2010-06-08 11:58:53 -0700952 def.nBufferSize = getFrameSize(colorFormat,
953 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700954
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800955 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
James Dongb00e2462010-04-26 17:48:26 -0700956
957 video_def->nFrameWidth = width;
958 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700959 video_def->nStride = stride;
960 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700961 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700962 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
963 video_def->eColorFormat = colorFormat;
964
James Dongb00e2462010-04-26 17:48:26 -0700965 err = mOMX->setParameter(
966 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800967 CHECK_EQ(err, (status_t)OK);
James Dongb00e2462010-04-26 17:48:26 -0700968
969 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700970 CHECK_EQ(setVideoPortFormatType(
971 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800972 (status_t)OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700973 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700974 def.nPortIndex = kPortIndexOutput;
975
James Dongb00e2462010-04-26 17:48:26 -0700976 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700977 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
978
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800979 CHECK_EQ(err, (status_t)OK);
980 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -0700981
982 video_def->nFrameWidth = width;
983 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700984 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700985 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700986 video_def->eCompressionFormat = compressionFormat;
987 video_def->eColorFormat = OMX_COLOR_FormatUnused;
James Dong90862e22010-08-26 19:12:59 -0700988 if (mQuirks & kRequiresLargerEncoderOutputBuffer) {
989 // Increases the output buffer size
990 def.nBufferSize = ((def.nBufferSize * 3) >> 1);
991 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700992
Andreas Huber784202e2009-10-15 13:46:54 -0700993 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700994 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800995 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700996
James Dongb00e2462010-04-26 17:48:26 -0700997 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700998 switch (compressionFormat) {
999 case OMX_VIDEO_CodingMPEG4:
1000 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001001 CHECK_EQ(setupMPEG4EncoderParameters(meta), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001002 break;
1003 }
1004
1005 case OMX_VIDEO_CodingH263:
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001006 CHECK_EQ(setupH263EncoderParameters(meta), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001007 break;
1008
Andreas Huberea6a38c2009-11-16 15:43:38 -08001009 case OMX_VIDEO_CodingAVC:
1010 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001011 CHECK_EQ(setupAVCEncoderParameters(meta), (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001012 break;
1013 }
1014
Andreas Huberb482ce82009-10-29 12:02:48 -07001015 default:
1016 CHECK(!"Support for this compressionFormat to be implemented.");
1017 break;
1018 }
1019}
1020
James Dong1244eab2010-06-08 11:58:53 -07001021static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
1022 if (iFramesInterval < 0) {
1023 return 0xFFFFFFFF;
1024 } else if (iFramesInterval == 0) {
1025 return 0;
1026 }
1027 OMX_U32 ret = frameRate * iFramesInterval;
1028 CHECK(ret > 1);
1029 return ret;
1030}
1031
James Dongc0ab2a62010-06-29 16:29:19 -07001032status_t OMXCodec::setupErrorCorrectionParameters() {
1033 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
1034 InitOMXParams(&errorCorrectionType);
1035 errorCorrectionType.nPortIndex = kPortIndexOutput;
1036
1037 status_t err = mOMX->getParameter(
1038 mNode, OMX_IndexParamVideoErrorCorrection,
1039 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001040 if (err != OK) {
1041 LOGW("Error correction param query is not supported");
1042 return OK; // Optional feature. Ignore this failure
1043 }
James Dongc0ab2a62010-06-29 16:29:19 -07001044
1045 errorCorrectionType.bEnableHEC = OMX_FALSE;
1046 errorCorrectionType.bEnableResync = OMX_TRUE;
1047 errorCorrectionType.nResynchMarkerSpacing = 256;
1048 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
1049 errorCorrectionType.bEnableRVLC = OMX_FALSE;
1050
1051 err = mOMX->setParameter(
1052 mNode, OMX_IndexParamVideoErrorCorrection,
1053 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001054 if (err != OK) {
1055 LOGW("Error correction param configuration is not supported");
1056 }
1057
1058 // Optional feature. Ignore the failure.
James Dongc0ab2a62010-06-29 16:29:19 -07001059 return OK;
1060}
1061
1062status_t OMXCodec::setupBitRate(int32_t bitRate) {
1063 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
1064 InitOMXParams(&bitrateType);
1065 bitrateType.nPortIndex = kPortIndexOutput;
1066
1067 status_t err = mOMX->getParameter(
1068 mNode, OMX_IndexParamVideoBitrate,
1069 &bitrateType, sizeof(bitrateType));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001070 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001071
1072 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
1073 bitrateType.nTargetBitrate = bitRate;
1074
1075 err = mOMX->setParameter(
1076 mNode, OMX_IndexParamVideoBitrate,
1077 &bitrateType, sizeof(bitrateType));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001078 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001079 return OK;
1080}
1081
James Dong81c929a2010-07-01 15:02:14 -07001082status_t OMXCodec::getVideoProfileLevel(
1083 const sp<MetaData>& meta,
1084 const CodecProfileLevel& defaultProfileLevel,
1085 CodecProfileLevel &profileLevel) {
1086 CODEC_LOGV("Default profile: %ld, level %ld",
1087 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
1088
1089 // Are the default profile and level overwriten?
1090 int32_t profile, level;
1091 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
1092 profile = defaultProfileLevel.mProfile;
1093 }
1094 if (!meta->findInt32(kKeyVideoLevel, &level)) {
1095 level = defaultProfileLevel.mLevel;
1096 }
1097 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
1098
1099 // Are the target profile and level supported by the encoder?
1100 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
1101 InitOMXParams(&param);
1102 param.nPortIndex = kPortIndexOutput;
1103 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
1104 status_t err = mOMX->getParameter(
1105 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
1106 &param, sizeof(param));
1107
James Dongdfb89912010-09-15 21:07:52 -07001108 if (err != OK) break;
James Dong81c929a2010-07-01 15:02:14 -07001109
1110 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
1111 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -07001112 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -07001113 supportedProfile, supportedLevel);
1114
1115 if (profile == supportedProfile &&
James Dongdfb89912010-09-15 21:07:52 -07001116 level <= supportedLevel) {
1117 // We can further check whether the level is a valid
1118 // value; but we will leave that to the omx encoder component
1119 // via OMX_SetParameter call.
James Dong81c929a2010-07-01 15:02:14 -07001120 profileLevel.mProfile = profile;
1121 profileLevel.mLevel = level;
1122 return OK;
1123 }
1124 }
1125
1126 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
1127 profile, level);
1128 return BAD_VALUE;
1129}
1130
James Dongc0ab2a62010-06-29 16:29:19 -07001131status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
1132 int32_t iFramesInterval, frameRate, bitRate;
1133 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001134 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dongc0ab2a62010-06-29 16:29:19 -07001135 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1136 CHECK(success);
1137 OMX_VIDEO_PARAM_H263TYPE h263type;
1138 InitOMXParams(&h263type);
1139 h263type.nPortIndex = kPortIndexOutput;
1140
1141 status_t err = mOMX->getParameter(
1142 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001143 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001144
1145 h263type.nAllowedPictureTypes =
1146 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1147
1148 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1149 if (h263type.nPFrames == 0) {
1150 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1151 }
1152 h263type.nBFrames = 0;
1153
James Dong81c929a2010-07-01 15:02:14 -07001154 // Check profile and level parameters
1155 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001156 defaultProfileLevel.mProfile = h263type.eProfile;
1157 defaultProfileLevel.mLevel = h263type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001158 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1159 if (err != OK) return err;
1160 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1161 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001162
1163 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1164 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1165 h263type.nPictureHeaderRepetition = 0;
1166 h263type.nGOBHeaderInterval = 0;
1167
1168 err = mOMX->setParameter(
1169 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001170 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001171
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001172 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
1173 CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001174
1175 return OK;
1176}
1177
James Dong1244eab2010-06-08 11:58:53 -07001178status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1179 int32_t iFramesInterval, frameRate, bitRate;
1180 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001181 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -07001182 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1183 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001184 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1185 InitOMXParams(&mpeg4type);
1186 mpeg4type.nPortIndex = kPortIndexOutput;
1187
1188 status_t err = mOMX->getParameter(
1189 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001190 CHECK_EQ(err, (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001191
1192 mpeg4type.nSliceHeaderSpacing = 0;
1193 mpeg4type.bSVH = OMX_FALSE;
1194 mpeg4type.bGov = OMX_FALSE;
1195
1196 mpeg4type.nAllowedPictureTypes =
1197 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1198
James Dong1244eab2010-06-08 11:58:53 -07001199 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1200 if (mpeg4type.nPFrames == 0) {
1201 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1202 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001203 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001204 mpeg4type.nIDCVLCThreshold = 0;
1205 mpeg4type.bACPred = OMX_TRUE;
1206 mpeg4type.nMaxPacketSize = 256;
1207 mpeg4type.nTimeIncRes = 1000;
1208 mpeg4type.nHeaderExtension = 0;
1209 mpeg4type.bReversibleVLC = OMX_FALSE;
1210
James Dong81c929a2010-07-01 15:02:14 -07001211 // Check profile and level parameters
1212 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001213 defaultProfileLevel.mProfile = mpeg4type.eProfile;
1214 defaultProfileLevel.mLevel = mpeg4type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001215 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1216 if (err != OK) return err;
1217 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1218 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001219
1220 err = mOMX->setParameter(
1221 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001222 CHECK_EQ(err, (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001223
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001224 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
1225 CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001226
1227 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001228}
1229
James Dong1244eab2010-06-08 11:58:53 -07001230status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1231 int32_t iFramesInterval, frameRate, bitRate;
1232 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001233 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -07001234 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1235 CHECK(success);
1236
Andreas Huberea6a38c2009-11-16 15:43:38 -08001237 OMX_VIDEO_PARAM_AVCTYPE h264type;
1238 InitOMXParams(&h264type);
1239 h264type.nPortIndex = kPortIndexOutput;
1240
1241 status_t err = mOMX->getParameter(
1242 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001243 CHECK_EQ(err, (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001244
1245 h264type.nAllowedPictureTypes =
1246 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1247
1248 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001249 h264type.nBFrames = 0; // No B frames support yet
1250 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1251 if (h264type.nPFrames == 0) {
1252 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1253 }
James Dong81c929a2010-07-01 15:02:14 -07001254
1255 // Check profile and level parameters
1256 CodecProfileLevel defaultProfileLevel, profileLevel;
1257 defaultProfileLevel.mProfile = h264type.eProfile;
1258 defaultProfileLevel.mLevel = h264type.eLevel;
1259 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1260 if (err != OK) return err;
1261 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1262 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1263
1264 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1265 h264type.bUseHadamard = OMX_TRUE;
1266 h264type.nRefFrames = 1;
1267 h264type.nRefIdx10ActiveMinus1 = 0;
1268 h264type.nRefIdx11ActiveMinus1 = 0;
1269 h264type.bEntropyCodingCABAC = OMX_FALSE;
1270 h264type.bWeightedPPrediction = OMX_FALSE;
1271 h264type.bconstIpred = OMX_FALSE;
1272 h264type.bDirect8x8Inference = OMX_FALSE;
1273 h264type.bDirectSpatialTemporal = OMX_FALSE;
1274 h264type.nCabacInitIdc = 0;
1275 }
1276
1277 if (h264type.nBFrames != 0) {
1278 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1279 }
1280
Andreas Huberea6a38c2009-11-16 15:43:38 -08001281 h264type.bEnableUEP = OMX_FALSE;
1282 h264type.bEnableFMO = OMX_FALSE;
1283 h264type.bEnableASO = OMX_FALSE;
1284 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001285 h264type.bFrameMBsOnly = OMX_TRUE;
1286 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001287 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1288
pgudadhe9c305322010-07-26 13:59:29 -07001289 if (!strcasecmp("OMX.Nvidia.h264.encoder", mComponentName)) {
1290 h264type.eLevel = OMX_VIDEO_AVCLevelMax;
1291 }
1292
Andreas Huberea6a38c2009-11-16 15:43:38 -08001293 err = mOMX->setParameter(
1294 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001295 CHECK_EQ(err, (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001296
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001297 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001298
1299 return OK;
1300}
1301
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001302status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001303 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001304 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001305
Andreas Huberbe06d262009-08-14 14:37:10 -07001306 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001307 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001308 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001309 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001310 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001311 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001312 compressionFormat = OMX_VIDEO_CodingH263;
1313 } else {
1314 LOGE("Not a supported video mime type: %s", mime);
1315 CHECK(!"Should not be here. Not a supported video mime type.");
1316 }
1317
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001318 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001319 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1320
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001321 if (err != OK) {
1322 return err;
1323 }
1324
Andreas Huberbe06d262009-08-14 14:37:10 -07001325#if 1
1326 {
1327 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001328 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001329 format.nPortIndex = kPortIndexOutput;
1330 format.nIndex = 0;
1331
Andreas Huber784202e2009-10-15 13:46:54 -07001332 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001333 mNode, OMX_IndexParamVideoPortFormat,
1334 &format, sizeof(format));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001335 CHECK_EQ(err, (status_t)OK);
1336 CHECK_EQ((int)format.eCompressionFormat, (int)OMX_VIDEO_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07001337
1338 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1339
1340 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1341 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1342 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1343 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1344
Andreas Huber784202e2009-10-15 13:46:54 -07001345 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001346 mNode, OMX_IndexParamVideoPortFormat,
1347 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001348
1349 if (err != OK) {
1350 return err;
1351 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001352 }
1353#endif
1354
1355 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001356 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001357 def.nPortIndex = kPortIndexInput;
1358
Andreas Huber4c483422009-09-02 16:05:36 -07001359 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1360
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001361 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001362 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1363
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001364 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001365
1366#if 1
1367 // XXX Need a (much) better heuristic to compute input buffer sizes.
1368 const size_t X = 64 * 1024;
1369 if (def.nBufferSize < X) {
1370 def.nBufferSize = X;
1371 }
1372#endif
1373
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001374 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -07001375
1376 video_def->nFrameWidth = width;
1377 video_def->nFrameHeight = height;
1378
Andreas Huberb482ce82009-10-29 12:02:48 -07001379 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001380 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1381
Andreas Huber784202e2009-10-15 13:46:54 -07001382 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001383 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001384
1385 if (err != OK) {
1386 return err;
1387 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001388
1389 ////////////////////////////////////////////////////////////////////////////
1390
Andreas Huber4c483422009-09-02 16:05:36 -07001391 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001392 def.nPortIndex = kPortIndexOutput;
1393
Andreas Huber784202e2009-10-15 13:46:54 -07001394 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001395 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001396 CHECK_EQ(err, (status_t)OK);
1397 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -07001398
1399#if 0
1400 def.nBufferSize =
1401 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1402#endif
1403
1404 video_def->nFrameWidth = width;
1405 video_def->nFrameHeight = height;
1406
Andreas Huber784202e2009-10-15 13:46:54 -07001407 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001408 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001409
1410 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001411}
1412
Andreas Huberbe06d262009-08-14 14:37:10 -07001413OMXCodec::OMXCodec(
1414 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001415 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001416 const char *mime,
1417 const char *componentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001418 const sp<MediaSource> &source,
1419 const sp<ANativeWindow> &nativeWindow)
Andreas Huberbe06d262009-08-14 14:37:10 -07001420 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001421 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001422 mNode(node),
1423 mQuirks(quirks),
1424 mIsEncoder(isEncoder),
1425 mMIME(strdup(mime)),
1426 mComponentName(strdup(componentName)),
1427 mSource(source),
1428 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001429 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001430 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001431 mSignalledEOS(false),
1432 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001433 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001434 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001435 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1436 mTargetTimeUs(-1),
Andreas Huberb9289832011-02-08 13:10:25 -08001437 mOutputPortSettingsChangedPending(false),
Andreas Huber1f24b302010-06-10 11:12:39 -07001438 mLeftOverBuffer(NULL),
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001439 mPaused(false),
1440 mNativeWindow(nativeWindow) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001441 mPortStatus[kPortIndexInput] = ENABLED;
1442 mPortStatus[kPortIndexOutput] = ENABLED;
1443
Andreas Huber4c483422009-09-02 16:05:36 -07001444 setComponentRole();
1445}
1446
Andreas Hubere6c40962009-09-10 14:13:30 -07001447// static
1448void OMXCodec::setComponentRole(
1449 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1450 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001451 struct MimeToRole {
1452 const char *mime;
1453 const char *decoderRole;
1454 const char *encoderRole;
1455 };
1456
1457 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001458 { MEDIA_MIMETYPE_AUDIO_MPEG,
1459 "audio_decoder.mp3", "audio_encoder.mp3" },
1460 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1461 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1462 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1463 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1464 { MEDIA_MIMETYPE_AUDIO_AAC,
1465 "audio_decoder.aac", "audio_encoder.aac" },
1466 { MEDIA_MIMETYPE_VIDEO_AVC,
1467 "video_decoder.avc", "video_encoder.avc" },
1468 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1469 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1470 { MEDIA_MIMETYPE_VIDEO_H263,
1471 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001472 };
1473
1474 static const size_t kNumMimeToRole =
1475 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1476
1477 size_t i;
1478 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001479 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001480 break;
1481 }
1482 }
1483
1484 if (i == kNumMimeToRole) {
1485 return;
1486 }
1487
1488 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001489 isEncoder ? kMimeToRole[i].encoderRole
1490 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001491
1492 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001493 OMX_PARAM_COMPONENTROLETYPE roleParams;
1494 InitOMXParams(&roleParams);
1495
1496 strncpy((char *)roleParams.cRole,
1497 role, OMX_MAX_STRINGNAME_SIZE - 1);
1498
1499 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1500
Andreas Huber784202e2009-10-15 13:46:54 -07001501 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001502 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001503 &roleParams, sizeof(roleParams));
1504
1505 if (err != OK) {
1506 LOGW("Failed to set standard component role '%s'.", role);
1507 }
1508 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001509}
1510
Andreas Hubere6c40962009-09-10 14:13:30 -07001511void OMXCodec::setComponentRole() {
1512 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1513}
1514
Andreas Huberbe06d262009-08-14 14:37:10 -07001515OMXCodec::~OMXCodec() {
Andreas Huberf98197a2010-09-17 11:49:39 -07001516 mSource.clear();
1517
Andreas Hubereec06d32011-01-06 10:26:36 -08001518 CHECK(mState == LOADED || mState == ERROR || mState == LOADED_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001519
Andreas Huber784202e2009-10-15 13:46:54 -07001520 status_t err = mOMX->freeNode(mNode);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001521 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001522
1523 mNode = NULL;
1524 setState(DEAD);
1525
1526 clearCodecSpecificData();
1527
1528 free(mComponentName);
1529 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001530
Andreas Huberbe06d262009-08-14 14:37:10 -07001531 free(mMIME);
1532 mMIME = NULL;
1533}
1534
1535status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001536 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001537
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001538 CHECK_EQ((int)mState, (int)LOADED);
Andreas Huberbe06d262009-08-14 14:37:10 -07001539
1540 status_t err;
1541 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001542 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001543 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001544 setState(LOADED_TO_IDLE);
1545 }
1546
1547 err = allocateBuffers();
Jamie Gennisfd8b75a2010-12-17 15:07:02 -08001548 if (err != (status_t)OK) {
1549 return err;
1550 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001551
1552 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001553 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001554 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001555
1556 setState(LOADED_TO_IDLE);
1557 }
1558
1559 while (mState != EXECUTING && mState != ERROR) {
1560 mAsyncCompletion.wait(mLock);
1561 }
1562
1563 return mState == ERROR ? UNKNOWN_ERROR : OK;
1564}
1565
1566// static
1567bool OMXCodec::isIntermediateState(State state) {
1568 return state == LOADED_TO_IDLE
1569 || state == IDLE_TO_EXECUTING
1570 || state == EXECUTING_TO_IDLE
1571 || state == IDLE_TO_LOADED
1572 || state == RECONFIGURING;
1573}
1574
1575status_t OMXCodec::allocateBuffers() {
1576 status_t err = allocateBuffersOnPort(kPortIndexInput);
1577
1578 if (err != OK) {
1579 return err;
1580 }
1581
1582 return allocateBuffersOnPort(kPortIndexOutput);
1583}
1584
1585status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
Jamie Gennisdbfb32e2010-10-20 15:53:59 -07001586 if (mNativeWindow != NULL && portIndex == kPortIndexOutput) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001587 return allocateOutputBuffersFromNativeWindow();
1588 }
1589
Andreas Huberbe06d262009-08-14 14:37:10 -07001590 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001591 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001592 def.nPortIndex = portIndex;
1593
Andreas Huber784202e2009-10-15 13:46:54 -07001594 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001595 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1596
1597 if (err != OK) {
1598 return err;
1599 }
1600
James Dong05c2fd52010-11-02 13:20:11 -07001601 if (mIsMetaDataStoredInVideoBuffers && portIndex == kPortIndexInput) {
1602 err = mOMX->storeMetaDataInBuffers(mNode, kPortIndexInput, OMX_TRUE);
1603 if (err != OK) {
1604 LOGE("Storing meta data in video buffers is not supported");
1605 return err;
1606 }
1607 }
1608
Andreas Huber57648e42010-08-04 10:14:30 -07001609 CODEC_LOGI("allocating %lu buffers of size %lu on %s port",
1610 def.nBufferCountActual, def.nBufferSize,
1611 portIndex == kPortIndexInput ? "input" : "output");
1612
Andreas Huber5c0a9132009-08-20 11:16:40 -07001613 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001614 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001615
Andreas Huberbe06d262009-08-14 14:37:10 -07001616 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001617 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001618 CHECK(mem.get() != NULL);
1619
Andreas Huberc712b9f2010-01-20 15:05:46 -08001620 BufferInfo info;
1621 info.mData = NULL;
1622 info.mSize = def.nBufferSize;
1623
Andreas Huberbe06d262009-08-14 14:37:10 -07001624 IOMX::buffer_id buffer;
1625 if (portIndex == kPortIndexInput
1626 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001627 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001628 mem.clear();
1629
Andreas Huberf1fe0642010-01-15 15:28:19 -08001630 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001631 mNode, portIndex, def.nBufferSize, &buffer,
1632 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001633 } else {
1634 err = mOMX->allocateBufferWithBackup(
1635 mNode, portIndex, mem, &buffer);
1636 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001637 } else if (portIndex == kPortIndexOutput
1638 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001639 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001640 mem.clear();
1641
Andreas Huberf1fe0642010-01-15 15:28:19 -08001642 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001643 mNode, portIndex, def.nBufferSize, &buffer,
1644 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001645 } else {
1646 err = mOMX->allocateBufferWithBackup(
1647 mNode, portIndex, mem, &buffer);
1648 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001649 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001650 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001651 }
1652
1653 if (err != OK) {
1654 LOGE("allocate_buffer_with_backup failed");
1655 return err;
1656 }
1657
Andreas Huberc712b9f2010-01-20 15:05:46 -08001658 if (mem != NULL) {
1659 info.mData = mem->pointer();
1660 }
1661
Andreas Huberbe06d262009-08-14 14:37:10 -07001662 info.mBuffer = buffer;
Andreas Huberbbbcf652010-12-07 14:25:54 -08001663 info.mStatus = OWNED_BY_US;
Andreas Huberbe06d262009-08-14 14:37:10 -07001664 info.mMem = mem;
1665 info.mMediaBuffer = NULL;
1666
1667 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001668 if (!(mOMXLivesLocally
1669 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1670 && (mQuirks & kDefersOutputBufferAllocation))) {
1671 // If the node does not fill in the buffer ptr at this time,
1672 // we will defer creating the MediaBuffer until receiving
1673 // the first FILL_BUFFER_DONE notification instead.
1674 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1675 info.mMediaBuffer->setObserver(this);
1676 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001677 }
1678
1679 mPortBuffers[portIndex].push(info);
1680
Andreas Huber4c483422009-09-02 16:05:36 -07001681 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001682 portIndex == kPortIndexInput ? "input" : "output");
1683 }
1684
Andreas Huber2ea14e22009-12-16 09:30:55 -08001685 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001686
1687 return OK;
1688}
1689
Andreas Huber5e9dc942011-01-21 14:32:31 -08001690status_t OMXCodec::applyRotation() {
1691 sp<MetaData> meta = mSource->getFormat();
1692
1693 int32_t rotationDegrees;
1694 if (!meta->findInt32(kKeyRotation, &rotationDegrees)) {
1695 rotationDegrees = 0;
1696 }
1697
1698 uint32_t transform;
1699 switch (rotationDegrees) {
1700 case 0: transform = 0; break;
1701 case 90: transform = HAL_TRANSFORM_ROT_90; break;
1702 case 180: transform = HAL_TRANSFORM_ROT_180; break;
1703 case 270: transform = HAL_TRANSFORM_ROT_270; break;
1704 default: transform = 0; break;
1705 }
1706
1707 status_t err = OK;
1708
1709 if (transform) {
1710 err = native_window_set_buffers_transform(
1711 mNativeWindow.get(), transform);
1712 }
1713
1714 return err;
1715}
1716
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001717status_t OMXCodec::allocateOutputBuffersFromNativeWindow() {
1718 // Get the number of buffers needed.
1719 OMX_PARAM_PORTDEFINITIONTYPE def;
1720 InitOMXParams(&def);
1721 def.nPortIndex = kPortIndexOutput;
1722
1723 status_t err = mOMX->getParameter(
1724 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1725 if (err != OK) {
1726 return err;
1727 }
1728
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001729 err = native_window_set_buffers_geometry(
1730 mNativeWindow.get(),
1731 def.format.video.nFrameWidth,
1732 def.format.video.nFrameHeight,
Jamie Gennis044ace62010-10-29 15:19:29 -07001733 def.format.video.eColorFormat);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001734
1735 if (err != 0) {
1736 LOGE("native_window_set_buffers_geometry failed: %s (%d)",
1737 strerror(-err), -err);
1738 return err;
1739 }
1740
Andreas Huber5e9dc942011-01-21 14:32:31 -08001741 err = applyRotation();
1742 if (err != OK) {
1743 return err;
1744 }
1745
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001746 // Set up the native window.
Jamie Gennis94c59802011-02-24 12:48:17 -08001747 OMX_U32 usage = 0;
1748 err = mOMX->getGraphicBufferUsage(mNode, kPortIndexOutput, &usage);
1749 if (err != 0) {
1750 LOGW("querying usage flags from OMX IL component failed: %d", err);
1751 // XXX: Currently this error is logged, but not fatal.
1752 usage = 0;
1753 }
1754
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001755 err = native_window_set_usage(
Jamie Gennis94c59802011-02-24 12:48:17 -08001756 mNativeWindow.get(), usage | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001757 if (err != 0) {
1758 LOGE("native_window_set_usage failed: %s (%d)", strerror(-err), -err);
1759 return err;
1760 }
1761
Jamie Gennis01951fd2011-02-27 15:10:34 -08001762 int minUndequeuedBufs = 0;
1763 err = mNativeWindow->query(mNativeWindow.get(),
1764 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBufs);
1765 if (err != 0) {
1766 LOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d)",
1767 strerror(-err), -err);
1768 return err;
1769 }
1770
1771 // XXX: Is this the right logic to use? It's not clear to me what the OMX
1772 // buffer counts refer to - how do they account for the renderer holding on
1773 // to buffers?
1774 if (def.nBufferCountActual < def.nBufferCountMin + minUndequeuedBufs) {
1775 OMX_U32 newBufferCount = def.nBufferCountMin + minUndequeuedBufs;
1776 def.nBufferCountActual = newBufferCount;
1777 err = mOMX->setParameter(
1778 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1779 if (err != OK) {
1780 CODEC_LOGE("setting nBufferCountActual to %lu failed: %d",
1781 newBufferCount, err);
1782 return err;
1783 }
1784 }
1785
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001786 err = native_window_set_buffer_count(
1787 mNativeWindow.get(), def.nBufferCountActual);
1788 if (err != 0) {
1789 LOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
1790 -err);
1791 return err;
1792 }
1793
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001794 CODEC_LOGI("allocating %lu buffers from a native window of size %lu on "
1795 "output port", def.nBufferCountActual, def.nBufferSize);
1796
1797 // Dequeue buffers and send them to OMX
Jamie Gennis42024642011-02-22 18:33:06 -08001798 for (OMX_U32 i = 0; i < def.nBufferCountActual; i++) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001799 android_native_buffer_t* buf;
1800 err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf);
1801 if (err != 0) {
1802 LOGE("dequeueBuffer failed: %s (%d)", strerror(-err), -err);
1803 break;
1804 }
1805
1806 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false));
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001807 BufferInfo info;
1808 info.mData = NULL;
1809 info.mSize = def.nBufferSize;
Andreas Huberbbbcf652010-12-07 14:25:54 -08001810 info.mStatus = OWNED_BY_US;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001811 info.mMem = NULL;
1812 info.mMediaBuffer = new MediaBuffer(graphicBuffer);
1813 info.mMediaBuffer->setObserver(this);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001814 mPortBuffers[kPortIndexOutput].push(info);
Jamie Gennis42024642011-02-22 18:33:06 -08001815
1816 IOMX::buffer_id bufferId;
1817 err = mOMX->useGraphicBuffer(mNode, kPortIndexOutput, graphicBuffer,
1818 &bufferId);
1819 if (err != 0) {
1820 CODEC_LOGE("registering GraphicBuffer with OMX IL component "
1821 "failed: %d", err);
1822 break;
1823 }
1824
1825 mPortBuffers[kPortIndexOutput].editItemAt(i).mBuffer = bufferId;
1826
1827 CODEC_LOGV("registered graphic buffer with ID %p (pointer = %p)",
1828 bufferId, graphicBuffer.get());
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001829 }
1830
1831 OMX_U32 cancelStart;
1832 OMX_U32 cancelEnd;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001833 if (err != 0) {
1834 // If an error occurred while dequeuing we need to cancel any buffers
1835 // that were dequeued.
1836 cancelStart = 0;
Jamie Gennis42024642011-02-22 18:33:06 -08001837 cancelEnd = mPortBuffers[kPortIndexOutput].size();
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001838 } else {
1839 // Return the last two buffers to the native window.
Jamie Gennis01951fd2011-02-27 15:10:34 -08001840 cancelStart = def.nBufferCountActual - minUndequeuedBufs;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001841 cancelEnd = def.nBufferCountActual;
1842 }
1843
1844 for (OMX_U32 i = cancelStart; i < cancelEnd; i++) {
1845 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(i);
1846 cancelBufferToNativeWindow(info);
1847 }
1848
1849 return err;
1850}
1851
1852status_t OMXCodec::cancelBufferToNativeWindow(BufferInfo *info) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08001853 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001854 CODEC_LOGV("Calling cancelBuffer on buffer %p", info->mBuffer);
1855 int err = mNativeWindow->cancelBuffer(
1856 mNativeWindow.get(), info->mMediaBuffer->graphicBuffer().get());
1857 if (err != 0) {
1858 CODEC_LOGE("cancelBuffer failed w/ error 0x%08x", err);
1859
1860 setState(ERROR);
1861 return err;
1862 }
Andreas Huberbbbcf652010-12-07 14:25:54 -08001863 info->mStatus = OWNED_BY_NATIVE_WINDOW;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001864 return OK;
1865}
1866
1867OMXCodec::BufferInfo* OMXCodec::dequeueBufferFromNativeWindow() {
1868 // Dequeue the next buffer from the native window.
1869 android_native_buffer_t* buf;
1870 int err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf);
1871 if (err != 0) {
1872 CODEC_LOGE("dequeueBuffer failed w/ error 0x%08x", err);
1873
1874 setState(ERROR);
1875 return 0;
1876 }
1877
1878 // Determine which buffer we just dequeued.
1879 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1880 BufferInfo *bufInfo = 0;
1881 for (size_t i = 0; i < buffers->size(); i++) {
1882 sp<GraphicBuffer> graphicBuffer = buffers->itemAt(i).
1883 mMediaBuffer->graphicBuffer();
1884 if (graphicBuffer->handle == buf->handle) {
1885 bufInfo = &buffers->editItemAt(i);
1886 break;
1887 }
1888 }
1889
1890 if (bufInfo == 0) {
1891 CODEC_LOGE("dequeued unrecognized buffer: %p", buf);
1892
1893 setState(ERROR);
1894 return 0;
1895 }
1896
1897 // The native window no longer owns the buffer.
Andreas Huberbbbcf652010-12-07 14:25:54 -08001898 CHECK_EQ((int)bufInfo->mStatus, (int)OWNED_BY_NATIVE_WINDOW);
1899 bufInfo->mStatus = OWNED_BY_US;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001900
1901 return bufInfo;
1902}
1903
Andreas Huberbe06d262009-08-14 14:37:10 -07001904void OMXCodec::on_message(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001905 switch (msg.type) {
1906 case omx_message::EVENT:
1907 {
1908 onEvent(
1909 msg.u.event_data.event, msg.u.event_data.data1,
1910 msg.u.event_data.data2);
1911
1912 break;
1913 }
1914
1915 case omx_message::EMPTY_BUFFER_DONE:
1916 {
1917 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1918
Andreas Huber4c483422009-09-02 16:05:36 -07001919 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001920
1921 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1922 size_t i = 0;
1923 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1924 ++i;
1925 }
1926
1927 CHECK(i < buffers->size());
Andreas Huberbbbcf652010-12-07 14:25:54 -08001928 if ((*buffers)[i].mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001929 LOGW("We already own input buffer %p, yet received "
1930 "an EMPTY_BUFFER_DONE.", buffer);
1931 }
1932
James Dong05c2fd52010-11-02 13:20:11 -07001933 BufferInfo* info = &buffers->editItemAt(i);
Andreas Huberbbbcf652010-12-07 14:25:54 -08001934 info->mStatus = OWNED_BY_US;
James Dong05c2fd52010-11-02 13:20:11 -07001935
1936 // Buffer could not be released until empty buffer done is called.
1937 if (info->mMediaBuffer != NULL) {
James Dong31b9375f2010-11-10 21:11:41 -08001938 if (mIsEncoder &&
1939 (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
1940 // If zero-copy mode is enabled this will send the
1941 // input buffer back to the upstream source.
1942 restorePatchedDataPointer(info);
1943 }
1944
James Dong05c2fd52010-11-02 13:20:11 -07001945 info->mMediaBuffer->release();
1946 info->mMediaBuffer = NULL;
1947 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001948
1949 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001950 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001951
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07001952 status_t err = freeBuffer(kPortIndexInput, i);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001953 CHECK_EQ(err, (status_t)OK);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001954 } else if (mState != ERROR
1955 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001956 CHECK_EQ((int)mPortStatus[kPortIndexInput], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07001957 drainInputBuffer(&buffers->editItemAt(i));
1958 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001959 break;
1960 }
1961
1962 case omx_message::FILL_BUFFER_DONE:
1963 {
1964 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1965 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1966
Andreas Huber2ea14e22009-12-16 09:30:55 -08001967 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001968 buffer,
1969 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001970 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001971 msg.u.extended_buffer_data.timestamp,
1972 msg.u.extended_buffer_data.timestamp / 1E6);
1973
1974 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1975 size_t i = 0;
1976 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1977 ++i;
1978 }
1979
1980 CHECK(i < buffers->size());
1981 BufferInfo *info = &buffers->editItemAt(i);
1982
Andreas Huberbbbcf652010-12-07 14:25:54 -08001983 if (info->mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001984 LOGW("We already own output buffer %p, yet received "
1985 "a FILL_BUFFER_DONE.", buffer);
1986 }
1987
Andreas Huberbbbcf652010-12-07 14:25:54 -08001988 info->mStatus = OWNED_BY_US;
Andreas Huberbe06d262009-08-14 14:37:10 -07001989
1990 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001991 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001992
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07001993 status_t err = freeBuffer(kPortIndexOutput, i);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001994 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001995
Andreas Huber2ea14e22009-12-16 09:30:55 -08001996#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001997 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1998 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001999 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002000 mNoMoreOutputData = true;
2001 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08002002#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07002003 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002004 CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07002005
Andreas Huber52733b82010-01-25 10:41:35 -08002006 if (info->mMediaBuffer == NULL) {
2007 CHECK(mOMXLivesLocally);
2008 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
2009 CHECK(mQuirks & kDefersOutputBufferAllocation);
2010
2011 // The qcom video decoders on Nexus don't actually allocate
2012 // output buffer memory on a call to OMX_AllocateBuffer
2013 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
2014 // structure is only filled in later.
2015
2016 info->mMediaBuffer = new MediaBuffer(
2017 msg.u.extended_buffer_data.data_ptr,
2018 info->mSize);
2019 info->mMediaBuffer->setObserver(this);
2020 }
2021
Andreas Huberbe06d262009-08-14 14:37:10 -07002022 MediaBuffer *buffer = info->mMediaBuffer;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002023 bool isGraphicBuffer = buffer->graphicBuffer() != NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -07002024
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002025 if (!isGraphicBuffer
2026 && msg.u.extended_buffer_data.range_offset
Andreas Huberf88f8442010-08-10 11:18:36 -07002027 + msg.u.extended_buffer_data.range_length
2028 > buffer->size()) {
2029 CODEC_LOGE(
2030 "Codec lied about its buffer size requirements, "
2031 "sending a buffer larger than the originally "
2032 "advertised size in FILL_BUFFER_DONE!");
2033 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002034 buffer->set_range(
2035 msg.u.extended_buffer_data.range_offset,
2036 msg.u.extended_buffer_data.range_length);
2037
2038 buffer->meta_data()->clear();
2039
Andreas Huberfa8de752009-10-08 10:07:49 -07002040 buffer->meta_data()->setInt64(
2041 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07002042
2043 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
2044 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
2045 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08002046 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
2047 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
2048 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002049
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002050 if (isGraphicBuffer || mQuirks & kOutputBuffersAreUnreadable) {
Andreas Huber1e194162010-10-06 16:43:57 -07002051 buffer->meta_data()->setInt32(kKeyIsUnreadable, true);
2052 }
2053
Andreas Huberbe06d262009-08-14 14:37:10 -07002054 buffer->meta_data()->setPointer(
2055 kKeyPlatformPrivate,
2056 msg.u.extended_buffer_data.platform_private);
2057
2058 buffer->meta_data()->setPointer(
2059 kKeyBufferID,
2060 msg.u.extended_buffer_data.buffer);
2061
Andreas Huber2ea14e22009-12-16 09:30:55 -08002062 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
2063 CODEC_LOGV("No more output data.");
2064 mNoMoreOutputData = true;
2065 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002066
2067 if (mTargetTimeUs >= 0) {
2068 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
2069
2070 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
2071 CODEC_LOGV(
2072 "skipping output buffer at timestamp %lld us",
2073 msg.u.extended_buffer_data.timestamp);
2074
2075 fillOutputBuffer(info);
2076 break;
2077 }
2078
2079 CODEC_LOGV(
2080 "returning output buffer at target timestamp "
2081 "%lld us",
2082 msg.u.extended_buffer_data.timestamp);
2083
2084 mTargetTimeUs = -1;
2085 }
2086
2087 mFilledBuffers.push_back(i);
2088 mBufferFilled.signal();
James Dongfc8b7c92010-12-07 14:37:27 -08002089 if (mIsEncoder) {
2090 sched_yield();
2091 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002092 }
2093
2094 break;
2095 }
2096
2097 default:
2098 {
2099 CHECK(!"should not be here.");
2100 break;
2101 }
2102 }
2103}
2104
Andreas Huberb1678602009-10-19 13:06:40 -07002105// Has the format changed in any way that the client would have to be aware of?
2106static bool formatHasNotablyChanged(
2107 const sp<MetaData> &from, const sp<MetaData> &to) {
2108 if (from.get() == NULL && to.get() == NULL) {
2109 return false;
2110 }
2111
Andreas Huberf68c1682009-10-21 14:01:30 -07002112 if ((from.get() == NULL && to.get() != NULL)
2113 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07002114 return true;
2115 }
2116
2117 const char *mime_from, *mime_to;
2118 CHECK(from->findCString(kKeyMIMEType, &mime_from));
2119 CHECK(to->findCString(kKeyMIMEType, &mime_to));
2120
2121 if (strcasecmp(mime_from, mime_to)) {
2122 return true;
2123 }
2124
2125 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
2126 int32_t colorFormat_from, colorFormat_to;
2127 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
2128 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
2129
2130 if (colorFormat_from != colorFormat_to) {
2131 return true;
2132 }
2133
2134 int32_t width_from, width_to;
2135 CHECK(from->findInt32(kKeyWidth, &width_from));
2136 CHECK(to->findInt32(kKeyWidth, &width_to));
2137
2138 if (width_from != width_to) {
2139 return true;
2140 }
2141
2142 int32_t height_from, height_to;
2143 CHECK(from->findInt32(kKeyHeight, &height_from));
2144 CHECK(to->findInt32(kKeyHeight, &height_to));
2145
2146 if (height_from != height_to) {
2147 return true;
2148 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002149
2150 int32_t left_from, top_from, right_from, bottom_from;
2151 CHECK(from->findRect(
2152 kKeyCropRect,
2153 &left_from, &top_from, &right_from, &bottom_from));
2154
2155 int32_t left_to, top_to, right_to, bottom_to;
2156 CHECK(to->findRect(
2157 kKeyCropRect,
2158 &left_to, &top_to, &right_to, &bottom_to));
2159
2160 if (left_to != left_from || top_to != top_from
2161 || right_to != right_from || bottom_to != bottom_from) {
2162 return true;
2163 }
Andreas Huberb1678602009-10-19 13:06:40 -07002164 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
2165 int32_t numChannels_from, numChannels_to;
2166 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
2167 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
2168
2169 if (numChannels_from != numChannels_to) {
2170 return true;
2171 }
2172
2173 int32_t sampleRate_from, sampleRate_to;
2174 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
2175 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
2176
2177 if (sampleRate_from != sampleRate_to) {
2178 return true;
2179 }
2180 }
2181
2182 return false;
2183}
2184
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002185void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
2186 switch (event) {
2187 case OMX_EventCmdComplete:
2188 {
2189 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
2190 break;
2191 }
2192
2193 case OMX_EventError:
2194 {
2195 CODEC_LOGE("ERROR(0x%08lx, %ld)", data1, data2);
2196
2197 setState(ERROR);
2198 break;
2199 }
2200
2201 case OMX_EventPortSettingsChanged:
2202 {
2203 CODEC_LOGV("OMX_EventPortSettingsChanged(port=%ld, data2=0x%08lx)",
2204 data1, data2);
2205
2206 if (data2 == 0 || data2 == OMX_IndexParamPortDefinition) {
2207 onPortSettingsChanged(data1);
2208 } else if (data1 == kPortIndexOutput
2209 && data2 == OMX_IndexConfigCommonOutputCrop) {
2210
2211 sp<MetaData> oldOutputFormat = mOutputFormat;
2212 initOutputFormat(mSource->getFormat());
2213
2214 if (formatHasNotablyChanged(oldOutputFormat, mOutputFormat)) {
2215 mOutputPortSettingsHaveChanged = true;
2216
2217 if (mNativeWindow != NULL) {
2218 int32_t left, top, right, bottom;
2219 CHECK(mOutputFormat->findRect(
2220 kKeyCropRect,
2221 &left, &top, &right, &bottom));
2222
2223 android_native_rect_t crop;
2224 crop.left = left;
2225 crop.top = top;
2226 crop.right = right;
2227 crop.bottom = bottom;
2228
Andreas Huber00d6c722011-01-26 12:02:35 -08002229 // We'll ignore any errors here, if the surface is
2230 // already invalid, we'll know soon enough.
2231 native_window_set_crop(mNativeWindow.get(), &crop);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002232 }
2233 }
2234 }
2235 break;
2236 }
2237
2238#if 0
2239 case OMX_EventBufferFlag:
2240 {
2241 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
2242
2243 if (data1 == kPortIndexOutput) {
2244 mNoMoreOutputData = true;
2245 }
2246 break;
2247 }
2248#endif
2249
2250 default:
2251 {
2252 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
2253 break;
2254 }
2255 }
2256}
2257
Andreas Huberbe06d262009-08-14 14:37:10 -07002258void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
2259 switch (cmd) {
2260 case OMX_CommandStateSet:
2261 {
2262 onStateChange((OMX_STATETYPE)data);
2263 break;
2264 }
2265
2266 case OMX_CommandPortDisable:
2267 {
2268 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07002269 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002270
2271 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002272 CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLING);
2273 CHECK_EQ(mPortBuffers[portIndex].size(), 0u);
Andreas Huberbe06d262009-08-14 14:37:10 -07002274
2275 mPortStatus[portIndex] = DISABLED;
2276
2277 if (mState == RECONFIGURING) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002278 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002279
Andreas Huberb1678602009-10-19 13:06:40 -07002280 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07002281 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07002282
2283 // Don't notify clients if the output port settings change
2284 // wasn't of importance to them, i.e. it may be that just the
2285 // number of buffers has changed and nothing else.
2286 mOutputPortSettingsHaveChanged =
2287 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07002288
Andreas Huberbe06d262009-08-14 14:37:10 -07002289 enablePortAsync(portIndex);
2290
2291 status_t err = allocateBuffersOnPort(portIndex);
Andreas Huberba1b16792011-01-19 09:20:58 -08002292
2293 if (err != OK) {
2294 CODEC_LOGE("allocateBuffersOnPort failed (err = %d)", err);
2295 setState(ERROR);
2296 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002297 }
2298 break;
2299 }
2300
2301 case OMX_CommandPortEnable:
2302 {
2303 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07002304 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002305
2306 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002307 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002308
2309 mPortStatus[portIndex] = ENABLED;
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
2314 setState(EXECUTING);
2315
2316 fillOutputBuffers();
2317 }
2318 break;
2319 }
2320
2321 case OMX_CommandFlush:
2322 {
2323 OMX_U32 portIndex = data;
2324
Andreas Huber4c483422009-09-02 16:05:36 -07002325 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002326
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002327 CHECK_EQ((int)mPortStatus[portIndex], (int)SHUTTING_DOWN);
Andreas Huberbe06d262009-08-14 14:37:10 -07002328 mPortStatus[portIndex] = ENABLED;
2329
2330 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
2331 mPortBuffers[portIndex].size());
2332
2333 if (mState == RECONFIGURING) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002334 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002335
2336 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002337 } else if (mState == EXECUTING_TO_IDLE) {
2338 if (mPortStatus[kPortIndexInput] == ENABLED
2339 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002340 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002341 "transition from EXECUTING to IDLE.");
2342
2343 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2344 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2345
2346 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002347 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002348 CHECK_EQ(err, (status_t)OK);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002349 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002350 } else {
2351 // We're flushing both ports in preparation for seeking.
2352
2353 if (mPortStatus[kPortIndexInput] == ENABLED
2354 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002355 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07002356 " seek-time.");
2357
Andreas Huber1f24b302010-06-10 11:12:39 -07002358 // We implicitly resume pulling on our upstream source.
2359 mPaused = false;
2360
Andreas Huberbe06d262009-08-14 14:37:10 -07002361 drainInputBuffers();
2362 fillOutputBuffers();
2363 }
Andreas Huberb9289832011-02-08 13:10:25 -08002364
2365 if (mOutputPortSettingsChangedPending) {
2366 CODEC_LOGV(
2367 "Honoring deferred output port settings change.");
2368
2369 mOutputPortSettingsChangedPending = false;
2370 onPortSettingsChanged(kPortIndexOutput);
2371 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002372 }
2373
2374 break;
2375 }
2376
2377 default:
2378 {
Andreas Huber4c483422009-09-02 16:05:36 -07002379 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07002380 break;
2381 }
2382 }
2383}
2384
2385void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002386 CODEC_LOGV("onStateChange %d", newState);
2387
Andreas Huberbe06d262009-08-14 14:37:10 -07002388 switch (newState) {
2389 case OMX_StateIdle:
2390 {
Andreas Huber4c483422009-09-02 16:05:36 -07002391 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002392 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07002393 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002394 mNode, OMX_CommandStateSet, OMX_StateExecuting);
2395
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002396 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002397
2398 setState(IDLE_TO_EXECUTING);
2399 } else {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002400 CHECK_EQ((int)mState, (int)EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002401
2402 CHECK_EQ(
2403 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
2404 mPortBuffers[kPortIndexInput].size());
2405
2406 CHECK_EQ(
2407 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
2408 mPortBuffers[kPortIndexOutput].size());
2409
Andreas Huber784202e2009-10-15 13:46:54 -07002410 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002411 mNode, OMX_CommandStateSet, OMX_StateLoaded);
2412
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002413 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002414
2415 err = freeBuffersOnPort(kPortIndexInput);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002416 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002417
2418 err = freeBuffersOnPort(kPortIndexOutput);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002419 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002420
2421 mPortStatus[kPortIndexInput] = ENABLED;
2422 mPortStatus[kPortIndexOutput] = ENABLED;
2423
2424 setState(IDLE_TO_LOADED);
2425 }
2426 break;
2427 }
2428
2429 case OMX_StateExecuting:
2430 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002431 CHECK_EQ((int)mState, (int)IDLE_TO_EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002432
Andreas Huber4c483422009-09-02 16:05:36 -07002433 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002434
Andreas Huberb9289832011-02-08 13:10:25 -08002435 mOutputPortSettingsChangedPending = false;
2436
Andreas Huberbe06d262009-08-14 14:37:10 -07002437 setState(EXECUTING);
2438
Andreas Huber42978e52009-08-27 10:08:39 -07002439 // Buffers will be submitted to the component in the first
2440 // call to OMXCodec::read as mInitialBufferSubmit is true at
2441 // this point. This ensures that this on_message call returns,
2442 // releases the lock and ::init can notice the state change and
2443 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07002444 break;
2445 }
2446
2447 case OMX_StateLoaded:
2448 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002449 CHECK_EQ((int)mState, (int)IDLE_TO_LOADED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002450
Andreas Huber4c483422009-09-02 16:05:36 -07002451 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002452
2453 setState(LOADED);
2454 break;
2455 }
2456
Andreas Huberc712b9f2010-01-20 15:05:46 -08002457 case OMX_StateInvalid:
2458 {
2459 setState(ERROR);
2460 break;
2461 }
2462
Andreas Huberbe06d262009-08-14 14:37:10 -07002463 default:
2464 {
2465 CHECK(!"should not be here.");
2466 break;
2467 }
2468 }
2469}
2470
2471// static
2472size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
2473 size_t n = 0;
2474 for (size_t i = 0; i < buffers.size(); ++i) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002475 if (buffers[i].mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002476 ++n;
2477 }
2478 }
2479
2480 return n;
2481}
2482
2483status_t OMXCodec::freeBuffersOnPort(
2484 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2485 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2486
2487 status_t stickyErr = OK;
2488
2489 for (size_t i = buffers->size(); i-- > 0;) {
2490 BufferInfo *info = &buffers->editItemAt(i);
2491
Andreas Huberbbbcf652010-12-07 14:25:54 -08002492 if (onlyThoseWeOwn && info->mStatus == OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002493 continue;
2494 }
2495
Andreas Huberbbbcf652010-12-07 14:25:54 -08002496 CHECK(info->mStatus == OWNED_BY_US
2497 || info->mStatus == OWNED_BY_NATIVE_WINDOW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002498
Andreas Huber92022852009-09-14 15:24:14 -07002499 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2500
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002501 status_t err = freeBuffer(portIndex, i);
Andreas Huberbe06d262009-08-14 14:37:10 -07002502
2503 if (err != OK) {
2504 stickyErr = err;
2505 }
2506
Andreas Huberbe06d262009-08-14 14:37:10 -07002507 }
2508
2509 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2510
2511 return stickyErr;
2512}
2513
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002514status_t OMXCodec::freeBuffer(OMX_U32 portIndex, size_t bufIndex) {
2515 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2516
2517 BufferInfo *info = &buffers->editItemAt(bufIndex);
2518
2519 status_t err = mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
2520
2521 if (err == OK && info->mMediaBuffer != NULL) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002522 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002523 info->mMediaBuffer->setObserver(NULL);
2524
2525 // Make sure nobody but us owns this buffer at this point.
2526 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2527
2528 // Cancel the buffer if it belongs to an ANativeWindow.
2529 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer();
Andreas Huberbbbcf652010-12-07 14:25:54 -08002530 if (info->mStatus == OWNED_BY_US && graphicBuffer != 0) {
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002531 err = cancelBufferToNativeWindow(info);
2532 }
2533
2534 info->mMediaBuffer->release();
James Dong31b9375f2010-11-10 21:11:41 -08002535 info->mMediaBuffer = NULL;
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002536 }
2537
2538 if (err == OK) {
2539 buffers->removeAt(bufIndex);
2540 }
2541
2542 return err;
2543}
2544
Andreas Huberbe06d262009-08-14 14:37:10 -07002545void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002546 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002547
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002548 CHECK_EQ((int)mState, (int)EXECUTING);
2549 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberb9289832011-02-08 13:10:25 -08002550 CHECK(!mOutputPortSettingsChangedPending);
2551
2552 if (mPortStatus[kPortIndexOutput] != ENABLED) {
2553 CODEC_LOGV("Deferring output port settings change.");
2554 mOutputPortSettingsChangedPending = true;
2555 return;
2556 }
2557
Andreas Huberbe06d262009-08-14 14:37:10 -07002558 setState(RECONFIGURING);
2559
2560 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002561 if (!flushPortAsync(portIndex)) {
2562 onCmdComplete(OMX_CommandFlush, portIndex);
2563 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002564 } else {
2565 disablePortAsync(portIndex);
2566 }
2567}
2568
Andreas Huber404cc412009-08-25 14:26:05 -07002569bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002570 CHECK(mState == EXECUTING || mState == RECONFIGURING
2571 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002572
Andreas Huber4c483422009-09-02 16:05:36 -07002573 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002574 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2575 mPortBuffers[portIndex].size());
2576
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002577 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002578 mPortStatus[portIndex] = SHUTTING_DOWN;
2579
Andreas Huber404cc412009-08-25 14:26:05 -07002580 if ((mQuirks & kRequiresFlushCompleteEmulation)
2581 && countBuffersWeOwn(mPortBuffers[portIndex])
2582 == mPortBuffers[portIndex].size()) {
2583 // No flush is necessary and this component fails to send a
2584 // flush-complete event in this case.
2585
2586 return false;
2587 }
2588
Andreas Huberbe06d262009-08-14 14:37:10 -07002589 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002590 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002591 CHECK_EQ(err, (status_t)OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002592
2593 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002594}
2595
2596void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2597 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2598
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002599 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002600 mPortStatus[portIndex] = DISABLING;
2601
Andreas Huberd222c842010-08-26 14:29:34 -07002602 CODEC_LOGV("sending OMX_CommandPortDisable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002603 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002604 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002605 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002606
2607 freeBuffersOnPort(portIndex, true);
2608}
2609
2610void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2611 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2612
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002613 CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002614 mPortStatus[portIndex] = ENABLING;
2615
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002616 CODEC_LOGV("sending OMX_CommandPortEnable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002617 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002618 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002619 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002620}
2621
2622void OMXCodec::fillOutputBuffers() {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002623 CHECK_EQ((int)mState, (int)EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002624
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002625 // This is a workaround for some decoders not properly reporting
2626 // end-of-output-stream. If we own all input buffers and also own
2627 // all output buffers and we already signalled end-of-input-stream,
2628 // the end-of-output-stream is implied.
2629 if (mSignalledEOS
2630 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2631 == mPortBuffers[kPortIndexInput].size()
2632 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2633 == mPortBuffers[kPortIndexOutput].size()) {
2634 mNoMoreOutputData = true;
2635 mBufferFilled.signal();
2636
2637 return;
2638 }
2639
Andreas Huberbe06d262009-08-14 14:37:10 -07002640 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2641 for (size_t i = 0; i < buffers->size(); ++i) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002642 BufferInfo *info = &buffers->editItemAt(i);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002643 if (info->mStatus == OWNED_BY_US) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002644 fillOutputBuffer(&buffers->editItemAt(i));
2645 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002646 }
2647}
2648
2649void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002650 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002651
2652 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2653 for (size_t i = 0; i < buffers->size(); ++i) {
James Dong5f3ab062011-01-25 16:31:28 -08002654 BufferInfo *info = &buffers->editItemAt(i);
2655
2656 if (info->mStatus != OWNED_BY_US) {
2657 continue;
2658 }
2659
2660 if (!drainInputBuffer(info)) {
2661 break;
2662 }
2663
2664 if (mOnlySubmitOneBufferAtOneTime) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002665 break;
2666 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002667 }
2668}
2669
Andreas Huberbbbcf652010-12-07 14:25:54 -08002670bool OMXCodec::drainInputBuffer(BufferInfo *info) {
2671 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Andreas Huberbe06d262009-08-14 14:37:10 -07002672
2673 if (mSignalledEOS) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002674 return false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002675 }
2676
2677 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2678 const CodecSpecificData *specific =
2679 mCodecSpecificData[mCodecSpecificDataIndex];
2680
2681 size_t size = specific->mSize;
2682
Andreas Hubere6c40962009-09-10 14:13:30 -07002683 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002684 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002685 static const uint8_t kNALStartCode[4] =
2686 { 0x00, 0x00, 0x00, 0x01 };
2687
Andreas Huberc712b9f2010-01-20 15:05:46 -08002688 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002689
2690 size += 4;
2691
Andreas Huberc712b9f2010-01-20 15:05:46 -08002692 memcpy(info->mData, kNALStartCode, 4);
2693 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002694 specific->mData, specific->mSize);
2695 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002696 CHECK(info->mSize >= specific->mSize);
2697 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002698 }
2699
Andreas Huber2ea14e22009-12-16 09:30:55 -08002700 mNoMoreOutputData = false;
2701
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002702 CODEC_LOGV("calling emptyBuffer with codec specific data");
2703
Andreas Huber784202e2009-10-15 13:46:54 -07002704 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002705 mNode, info->mBuffer, 0, size,
2706 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2707 0);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002708 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002709
Andreas Huberbbbcf652010-12-07 14:25:54 -08002710 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberbe06d262009-08-14 14:37:10 -07002711
2712 ++mCodecSpecificDataIndex;
Andreas Huberbbbcf652010-12-07 14:25:54 -08002713 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002714 }
2715
Andreas Huber1f24b302010-06-10 11:12:39 -07002716 if (mPaused) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002717 return false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002718 }
2719
Andreas Huberbe06d262009-08-14 14:37:10 -07002720 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002721
Andreas Hubera4357ad2010-04-02 12:49:54 -07002722 bool signalEOS = false;
2723 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002724
Andreas Hubera4357ad2010-04-02 12:49:54 -07002725 size_t offset = 0;
2726 int32_t n = 0;
Andreas Huberbbbcf652010-12-07 14:25:54 -08002727
Andreas Hubera4357ad2010-04-02 12:49:54 -07002728 for (;;) {
2729 MediaBuffer *srcBuffer;
2730 if (mSeekTimeUs >= 0) {
2731 if (mLeftOverBuffer) {
2732 mLeftOverBuffer->release();
2733 mLeftOverBuffer = NULL;
2734 }
James Dong2144f632010-12-11 10:43:41 -08002735
2736 MediaSource::ReadOptions options;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002737 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002738
2739 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002740 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002741 mBufferFilled.signal();
2742
2743 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002744
2745 if (err == OK) {
2746 int64_t targetTimeUs;
2747 if (srcBuffer->meta_data()->findInt64(
2748 kKeyTargetTime, &targetTimeUs)
2749 && targetTimeUs >= 0) {
Andreas Huberb9289832011-02-08 13:10:25 -08002750 CODEC_LOGV("targetTimeUs = %lld us", targetTimeUs);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002751 mTargetTimeUs = targetTimeUs;
2752 } else {
2753 mTargetTimeUs = -1;
2754 }
2755 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002756 } else if (mLeftOverBuffer) {
2757 srcBuffer = mLeftOverBuffer;
2758 mLeftOverBuffer = NULL;
2759
2760 err = OK;
2761 } else {
James Dong2144f632010-12-11 10:43:41 -08002762 err = mSource->read(&srcBuffer);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002763 }
2764
2765 if (err != OK) {
2766 signalEOS = true;
2767 mFinalStatus = err;
2768 mSignalledEOS = true;
Andreas Huber94bced12010-12-14 09:50:46 -08002769 mBufferFilled.signal();
Andreas Hubera4357ad2010-04-02 12:49:54 -07002770 break;
2771 }
2772
2773 size_t remainingBytes = info->mSize - offset;
2774
2775 if (srcBuffer->range_length() > remainingBytes) {
2776 if (offset == 0) {
2777 CODEC_LOGE(
2778 "Codec's input buffers are too small to accomodate "
2779 "buffer read from source (info->mSize = %d, srcLength = %d)",
2780 info->mSize, srcBuffer->range_length());
2781
2782 srcBuffer->release();
2783 srcBuffer = NULL;
2784
2785 setState(ERROR);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002786 return false;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002787 }
2788
2789 mLeftOverBuffer = srcBuffer;
2790 break;
2791 }
2792
James Dong05c2fd52010-11-02 13:20:11 -07002793 bool releaseBuffer = true;
James Dong4f501f02010-06-07 14:41:41 -07002794 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2795 CHECK(mOMXLivesLocally && offset == 0);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002796
2797 OMX_BUFFERHEADERTYPE *header =
2798 (OMX_BUFFERHEADERTYPE *)info->mBuffer;
2799
James Dong31b9375f2010-11-10 21:11:41 -08002800 CHECK(header->pBuffer == info->mData);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002801
2802 header->pBuffer =
2803 (OMX_U8 *)srcBuffer->data() + srcBuffer->range_offset();
2804
James Dong05c2fd52010-11-02 13:20:11 -07002805 releaseBuffer = false;
2806 info->mMediaBuffer = srcBuffer;
James Dong4f501f02010-06-07 14:41:41 -07002807 } else {
James Dong05c2fd52010-11-02 13:20:11 -07002808 if (mIsMetaDataStoredInVideoBuffers) {
2809 releaseBuffer = false;
2810 info->mMediaBuffer = srcBuffer;
2811 }
James Dong4f501f02010-06-07 14:41:41 -07002812 memcpy((uint8_t *)info->mData + offset,
Andreas Huberbbbcf652010-12-07 14:25:54 -08002813 (const uint8_t *)srcBuffer->data()
2814 + srcBuffer->range_offset(),
James Dong4f501f02010-06-07 14:41:41 -07002815 srcBuffer->range_length());
2816 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002817
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002818 int64_t lastBufferTimeUs;
2819 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002820 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002821
Andreas Hubera4357ad2010-04-02 12:49:54 -07002822 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002823 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002824 }
2825
2826 offset += srcBuffer->range_length();
2827
James Dong05c2fd52010-11-02 13:20:11 -07002828 if (releaseBuffer) {
2829 srcBuffer->release();
2830 srcBuffer = NULL;
2831 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002832
2833 ++n;
2834
2835 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2836 break;
2837 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002838
2839 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2840
2841 if (coalescedDurationUs > 250000ll) {
2842 // Don't coalesce more than 250ms worth of encoded data at once.
2843 break;
2844 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002845 }
2846
2847 if (n > 1) {
2848 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002849 }
2850
2851 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002852
Andreas Hubera4357ad2010-04-02 12:49:54 -07002853 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002854 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002855 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002856 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002857 }
2858
Andreas Hubera4357ad2010-04-02 12:49:54 -07002859 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2860 "timestamp %lld us (%.2f secs)",
2861 info->mBuffer, offset,
2862 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002863
Andreas Huber784202e2009-10-15 13:46:54 -07002864 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002865 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002866 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002867
2868 if (err != OK) {
2869 setState(ERROR);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002870 return false;
Andreas Huber3f427072009-10-08 11:02:27 -07002871 }
2872
Andreas Huberbbbcf652010-12-07 14:25:54 -08002873 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002874
2875 // This component does not ever signal the EOS flag on output buffers,
2876 // Thanks for nothing.
2877 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2878 mNoMoreOutputData = true;
2879 mBufferFilled.signal();
2880 }
Andreas Huberbbbcf652010-12-07 14:25:54 -08002881
2882 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002883}
2884
2885void OMXCodec::fillOutputBuffer(BufferInfo *info) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002886 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Andreas Huberbe06d262009-08-14 14:37:10 -07002887
Andreas Huber404cc412009-08-25 14:26:05 -07002888 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002889 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002890 "calling fillOutputBuffer");
2891 return;
2892 }
2893
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002894 if (info->mMediaBuffer != NULL) {
2895 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer();
2896 if (graphicBuffer != 0) {
2897 // When using a native buffer we need to lock the buffer before
2898 // giving it to OMX.
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002899 CODEC_LOGV("Calling lockBuffer on %p", info->mBuffer);
2900 int err = mNativeWindow->lockBuffer(mNativeWindow.get(),
2901 graphicBuffer.get());
2902 if (err != 0) {
2903 CODEC_LOGE("lockBuffer failed w/ error 0x%08x", err);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002904
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002905 setState(ERROR);
2906 return;
2907 }
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002908 }
2909 }
2910
2911 CODEC_LOGV("Calling fillBuffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002912 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002913
2914 if (err != OK) {
2915 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2916
2917 setState(ERROR);
2918 return;
2919 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002920
Andreas Huberbbbcf652010-12-07 14:25:54 -08002921 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberbe06d262009-08-14 14:37:10 -07002922}
2923
Andreas Huberbbbcf652010-12-07 14:25:54 -08002924bool OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002925 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2926 for (size_t i = 0; i < buffers->size(); ++i) {
2927 if ((*buffers)[i].mBuffer == buffer) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002928 return drainInputBuffer(&buffers->editItemAt(i));
Andreas Huberbe06d262009-08-14 14:37:10 -07002929 }
2930 }
2931
2932 CHECK(!"should not be here.");
Andreas Huberbbbcf652010-12-07 14:25:54 -08002933
2934 return false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002935}
2936
2937void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2938 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2939 for (size_t i = 0; i < buffers->size(); ++i) {
2940 if ((*buffers)[i].mBuffer == buffer) {
2941 fillOutputBuffer(&buffers->editItemAt(i));
2942 return;
2943 }
2944 }
2945
2946 CHECK(!"should not be here.");
2947}
2948
2949void OMXCodec::setState(State newState) {
2950 mState = newState;
2951 mAsyncCompletion.signal();
2952
2953 // This may cause some spurious wakeups but is necessary to
2954 // unblock the reader if we enter ERROR state.
2955 mBufferFilled.signal();
2956}
2957
Andreas Huberda050cf22009-09-02 14:01:43 -07002958void OMXCodec::setRawAudioFormat(
2959 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002960
2961 // port definition
2962 OMX_PARAM_PORTDEFINITIONTYPE def;
2963 InitOMXParams(&def);
2964 def.nPortIndex = portIndex;
2965 status_t err = mOMX->getParameter(
2966 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002967 CHECK_EQ(err, (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07002968 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2969 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002970 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07002971
2972 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002973 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002974 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002975 pcmParams.nPortIndex = portIndex;
2976
James Dongabed93a2010-04-22 17:27:04 -07002977 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002978 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2979
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002980 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07002981
2982 pcmParams.nChannels = numChannels;
2983 pcmParams.eNumData = OMX_NumericalDataSigned;
2984 pcmParams.bInterleaved = OMX_TRUE;
2985 pcmParams.nBitPerSample = 16;
2986 pcmParams.nSamplingRate = sampleRate;
2987 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2988
2989 if (numChannels == 1) {
2990 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2991 } else {
2992 CHECK_EQ(numChannels, 2);
2993
2994 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2995 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2996 }
2997
Andreas Huber784202e2009-10-15 13:46:54 -07002998 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002999 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
3000
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003001 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07003002}
3003
James Dong17299ab2010-05-14 15:45:22 -07003004static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
3005 if (isAMRWB) {
3006 if (bps <= 6600) {
3007 return OMX_AUDIO_AMRBandModeWB0;
3008 } else if (bps <= 8850) {
3009 return OMX_AUDIO_AMRBandModeWB1;
3010 } else if (bps <= 12650) {
3011 return OMX_AUDIO_AMRBandModeWB2;
3012 } else if (bps <= 14250) {
3013 return OMX_AUDIO_AMRBandModeWB3;
3014 } else if (bps <= 15850) {
3015 return OMX_AUDIO_AMRBandModeWB4;
3016 } else if (bps <= 18250) {
3017 return OMX_AUDIO_AMRBandModeWB5;
3018 } else if (bps <= 19850) {
3019 return OMX_AUDIO_AMRBandModeWB6;
3020 } else if (bps <= 23050) {
3021 return OMX_AUDIO_AMRBandModeWB7;
3022 }
3023
3024 // 23850 bps
3025 return OMX_AUDIO_AMRBandModeWB8;
3026 } else { // AMRNB
3027 if (bps <= 4750) {
3028 return OMX_AUDIO_AMRBandModeNB0;
3029 } else if (bps <= 5150) {
3030 return OMX_AUDIO_AMRBandModeNB1;
3031 } else if (bps <= 5900) {
3032 return OMX_AUDIO_AMRBandModeNB2;
3033 } else if (bps <= 6700) {
3034 return OMX_AUDIO_AMRBandModeNB3;
3035 } else if (bps <= 7400) {
3036 return OMX_AUDIO_AMRBandModeNB4;
3037 } else if (bps <= 7950) {
3038 return OMX_AUDIO_AMRBandModeNB5;
3039 } else if (bps <= 10200) {
3040 return OMX_AUDIO_AMRBandModeNB6;
3041 }
3042
3043 // 12200 bps
3044 return OMX_AUDIO_AMRBandModeNB7;
3045 }
3046}
3047
3048void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08003049 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003050
Andreas Huber8768f2c2009-12-01 15:26:54 -08003051 OMX_AUDIO_PARAM_AMRTYPE def;
3052 InitOMXParams(&def);
3053 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07003054
Andreas Huber8768f2c2009-12-01 15:26:54 -08003055 status_t err =
3056 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07003057
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003058 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003059
Andreas Huber8768f2c2009-12-01 15:26:54 -08003060 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07003061
James Dong17299ab2010-05-14 15:45:22 -07003062 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08003063 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003064 CHECK_EQ(err, (status_t)OK);
Andreas Huberee606e62009-09-08 10:19:21 -07003065
3066 ////////////////////////
3067
3068 if (mIsEncoder) {
3069 sp<MetaData> format = mSource->getFormat();
3070 int32_t sampleRate;
3071 int32_t numChannels;
3072 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
3073 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
3074
3075 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
3076 }
3077}
3078
James Dong17299ab2010-05-14 15:45:22 -07003079void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07003080 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07003081 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07003082 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07003083 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07003084
3085 //////////////// output port ////////////////////
3086 // format
3087 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
3088 format.nPortIndex = kPortIndexOutput;
3089 format.nIndex = 0;
3090 status_t err = OMX_ErrorNone;
3091 while (OMX_ErrorNone == err) {
3092 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003093 &format, sizeof(format)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003094 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
3095 break;
3096 }
3097 format.nIndex++;
3098 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003099 CHECK_EQ((status_t)OK, err);
James Dongabed93a2010-04-22 17:27:04 -07003100 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003101 &format, sizeof(format)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003102
3103 // port definition
3104 OMX_PARAM_PORTDEFINITIONTYPE def;
3105 InitOMXParams(&def);
3106 def.nPortIndex = kPortIndexOutput;
3107 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003108 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003109 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
3110 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
3111 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003112 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003113
3114 // profile
3115 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
3116 InitOMXParams(&profile);
3117 profile.nPortIndex = kPortIndexOutput;
3118 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003119 &profile, sizeof(profile)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003120 profile.nChannels = numChannels;
3121 profile.eChannelMode = (numChannels == 1?
3122 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
3123 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07003124 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003125 profile.nAudioBandWidth = 0;
3126 profile.nFrameLength = 0;
3127 profile.nAACtools = OMX_AUDIO_AACToolAll;
3128 profile.nAACERtools = OMX_AUDIO_AACERNone;
3129 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
3130 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
3131 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003132 &profile, sizeof(profile)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003133
Andreas Huberda050cf22009-09-02 14:01:43 -07003134 } else {
3135 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07003136 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07003137 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003138
Andreas Huber784202e2009-10-15 13:46:54 -07003139 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003140 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003141 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003142
Andreas Huberda050cf22009-09-02 14:01:43 -07003143 profile.nChannels = numChannels;
3144 profile.nSampleRate = sampleRate;
3145 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07003146
Andreas Huber784202e2009-10-15 13:46:54 -07003147 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003148 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003149 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07003150 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003151}
3152
3153void OMXCodec::setImageOutputFormat(
3154 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07003155 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07003156
3157#if 0
3158 OMX_INDEXTYPE index;
3159 status_t err = mOMX->get_extension_index(
3160 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003161 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003162
3163 err = mOMX->set_config(mNode, index, &format, sizeof(format));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003164 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003165#endif
3166
3167 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003168 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003169 def.nPortIndex = kPortIndexOutput;
3170
Andreas Huber784202e2009-10-15 13:46:54 -07003171 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003172 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003173 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003174
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003175 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage);
Andreas Huberbe06d262009-08-14 14:37:10 -07003176
3177 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07003178
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003179 CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07003180 imageDef->eColorFormat = format;
3181 imageDef->nFrameWidth = width;
3182 imageDef->nFrameHeight = height;
3183
3184 switch (format) {
3185 case OMX_COLOR_FormatYUV420PackedPlanar:
3186 case OMX_COLOR_FormatYUV411Planar:
3187 {
3188 def.nBufferSize = (width * height * 3) / 2;
3189 break;
3190 }
3191
3192 case OMX_COLOR_FormatCbYCrY:
3193 {
3194 def.nBufferSize = width * height * 2;
3195 break;
3196 }
3197
3198 case OMX_COLOR_Format32bitARGB8888:
3199 {
3200 def.nBufferSize = width * height * 4;
3201 break;
3202 }
3203
Andreas Huber201511c2009-09-08 14:01:44 -07003204 case OMX_COLOR_Format16bitARGB4444:
3205 case OMX_COLOR_Format16bitARGB1555:
3206 case OMX_COLOR_Format16bitRGB565:
3207 case OMX_COLOR_Format16bitBGR565:
3208 {
3209 def.nBufferSize = width * height * 2;
3210 break;
3211 }
3212
Andreas Huberbe06d262009-08-14 14:37:10 -07003213 default:
3214 CHECK(!"Should not be here. Unknown color format.");
3215 break;
3216 }
3217
Andreas Huber5c0a9132009-08-20 11:16:40 -07003218 def.nBufferCountActual = def.nBufferCountMin;
3219
Andreas Huber784202e2009-10-15 13:46:54 -07003220 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003221 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003222 CHECK_EQ(err, (status_t)OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07003223}
Andreas Huberbe06d262009-08-14 14:37:10 -07003224
Andreas Huber5c0a9132009-08-20 11:16:40 -07003225void OMXCodec::setJPEGInputFormat(
3226 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
3227 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003228 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003229 def.nPortIndex = kPortIndexInput;
3230
Andreas Huber784202e2009-10-15 13:46:54 -07003231 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003232 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003233 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003234
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003235 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage);
Andreas Huber5c0a9132009-08-20 11:16:40 -07003236 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3237
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003238 CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingJPEG);
Andreas Huberbe06d262009-08-14 14:37:10 -07003239 imageDef->nFrameWidth = width;
3240 imageDef->nFrameHeight = height;
3241
Andreas Huber5c0a9132009-08-20 11:16:40 -07003242 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07003243 def.nBufferCountActual = def.nBufferCountMin;
3244
Andreas Huber784202e2009-10-15 13:46:54 -07003245 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003246 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003247 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003248}
3249
3250void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
3251 CodecSpecificData *specific =
3252 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
3253
3254 specific->mSize = size;
3255 memcpy(specific->mData, data, size);
3256
3257 mCodecSpecificData.push(specific);
3258}
3259
3260void OMXCodec::clearCodecSpecificData() {
3261 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
3262 free(mCodecSpecificData.editItemAt(i));
3263 }
3264 mCodecSpecificData.clear();
3265 mCodecSpecificDataIndex = 0;
3266}
3267
James Dong36e573b2010-06-19 09:04:18 -07003268status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07003269 Mutex::Autolock autoLock(mLock);
3270
Andreas Huberbe06d262009-08-14 14:37:10 -07003271 if (mState != LOADED) {
3272 return UNKNOWN_ERROR;
3273 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07003274
Andreas Huberbe06d262009-08-14 14:37:10 -07003275 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07003276 if (mQuirks & kWantsNALFragments) {
3277 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07003278 }
James Dong36e573b2010-06-19 09:04:18 -07003279 if (meta) {
3280 int64_t startTimeUs = 0;
3281 int64_t timeUs;
3282 if (meta->findInt64(kKeyTime, &timeUs)) {
3283 startTimeUs = timeUs;
3284 }
3285 params->setInt64(kKeyTime, startTimeUs);
3286 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003287 status_t err = mSource->start(params.get());
3288
3289 if (err != OK) {
3290 return err;
3291 }
3292
3293 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07003294 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07003295 mSignalledEOS = false;
3296 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07003297 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003298 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003299 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
3300 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07003301 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07003302 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003303
3304 return init();
3305}
3306
3307status_t OMXCodec::stop() {
Andreas Huber7ff89392011-02-15 15:34:11 -08003308 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07003309
3310 Mutex::Autolock autoLock(mLock);
3311
3312 while (isIntermediateState(mState)) {
3313 mAsyncCompletion.wait(mLock);
3314 }
3315
3316 switch (mState) {
3317 case LOADED:
3318 case ERROR:
3319 break;
3320
3321 case EXECUTING:
3322 {
3323 setState(EXECUTING_TO_IDLE);
3324
Andreas Huber127fcdc2009-08-26 16:27:02 -07003325 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07003326 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07003327 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07003328
Andreas Huber127fcdc2009-08-26 16:27:02 -07003329 bool emulateInputFlushCompletion =
3330 !flushPortAsync(kPortIndexInput);
3331
3332 bool emulateOutputFlushCompletion =
3333 !flushPortAsync(kPortIndexOutput);
3334
3335 if (emulateInputFlushCompletion) {
3336 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3337 }
3338
3339 if (emulateOutputFlushCompletion) {
3340 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3341 }
3342 } else {
3343 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
3344 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
3345
3346 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07003347 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003348 CHECK_EQ(err, (status_t)OK);
Andreas Huber127fcdc2009-08-26 16:27:02 -07003349 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003350
3351 while (mState != LOADED && mState != ERROR) {
3352 mAsyncCompletion.wait(mLock);
3353 }
3354
3355 break;
3356 }
3357
3358 default:
3359 {
3360 CHECK(!"should not be here.");
3361 break;
3362 }
3363 }
3364
Andreas Hubera4357ad2010-04-02 12:49:54 -07003365 if (mLeftOverBuffer) {
3366 mLeftOverBuffer->release();
3367 mLeftOverBuffer = NULL;
3368 }
3369
Andreas Huberbe06d262009-08-14 14:37:10 -07003370 mSource->stop();
3371
Andreas Huber06be3b12011-01-25 14:55:00 -08003372 CODEC_LOGI("stopped in state %d", mState);
Andreas Huber4a9375e2010-02-09 11:54:33 -08003373
Andreas Huberbe06d262009-08-14 14:37:10 -07003374 return OK;
3375}
3376
3377sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07003378 Mutex::Autolock autoLock(mLock);
3379
Andreas Huberbe06d262009-08-14 14:37:10 -07003380 return mOutputFormat;
3381}
3382
3383status_t OMXCodec::read(
3384 MediaBuffer **buffer, const ReadOptions *options) {
3385 *buffer = NULL;
3386
3387 Mutex::Autolock autoLock(mLock);
3388
Andreas Huberd06e5b82009-08-28 13:18:14 -07003389 if (mState != EXECUTING && mState != RECONFIGURING) {
3390 return UNKNOWN_ERROR;
3391 }
3392
Andreas Hubere981c332009-10-22 13:49:30 -07003393 bool seeking = false;
3394 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003395 ReadOptions::SeekMode seekMode;
3396 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07003397 seeking = true;
3398 }
3399
Andreas Huber42978e52009-08-27 10:08:39 -07003400 if (mInitialBufferSubmit) {
3401 mInitialBufferSubmit = false;
3402
Andreas Hubere981c332009-10-22 13:49:30 -07003403 if (seeking) {
3404 CHECK(seekTimeUs >= 0);
3405 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003406 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07003407
3408 // There's no reason to trigger the code below, there's
3409 // nothing to flush yet.
3410 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07003411 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07003412 }
3413
Andreas Huber42978e52009-08-27 10:08:39 -07003414 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07003415
Andreas Huberd06e5b82009-08-28 13:18:14 -07003416 if (mState == EXECUTING) {
3417 // Otherwise mState == RECONFIGURING and this code will trigger
3418 // after the output port is reenabled.
3419 fillOutputBuffers();
3420 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003421 }
3422
Andreas Hubere981c332009-10-22 13:49:30 -07003423 if (seeking) {
Andreas Huberb9289832011-02-08 13:10:25 -08003424 while (mState == RECONFIGURING) {
3425 mBufferFilled.wait(mLock);
3426 }
3427
3428 if (mState != EXECUTING) {
3429 return UNKNOWN_ERROR;
3430 }
3431
Andreas Huber4c483422009-09-02 16:05:36 -07003432 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07003433
3434 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003435
3436 CHECK(seekTimeUs >= 0);
3437 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003438 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07003439
3440 mFilledBuffers.clear();
3441
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003442 CHECK_EQ((int)mState, (int)EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07003443
Andreas Huber404cc412009-08-25 14:26:05 -07003444 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
3445 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
3446
3447 if (emulateInputFlushCompletion) {
3448 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3449 }
3450
3451 if (emulateOutputFlushCompletion) {
3452 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3453 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08003454
3455 while (mSeekTimeUs >= 0) {
3456 mBufferFilled.wait(mLock);
3457 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003458 }
3459
3460 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
James Dong74556302010-12-20 21:29:12 -08003461 if (mIsEncoder) {
3462 if (NO_ERROR != mBufferFilled.waitRelative(mLock, 3000000000LL)) {
3463 LOGW("Timed out waiting for buffers from video encoder: %d/%d",
3464 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
3465 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]));
3466 }
3467 } else {
3468 mBufferFilled.wait(mLock);
3469 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003470 }
3471
3472 if (mState == ERROR) {
3473 return UNKNOWN_ERROR;
3474 }
3475
3476 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08003477 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07003478 }
3479
Andreas Hubercfd55572009-10-09 14:11:28 -07003480 if (mOutputPortSettingsHaveChanged) {
3481 mOutputPortSettingsHaveChanged = false;
3482
3483 return INFO_FORMAT_CHANGED;
3484 }
3485
Andreas Huberbe06d262009-08-14 14:37:10 -07003486 size_t index = *mFilledBuffers.begin();
3487 mFilledBuffers.erase(mFilledBuffers.begin());
3488
3489 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
Andreas Huberbbbcf652010-12-07 14:25:54 -08003490 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
3491 info->mStatus = OWNED_BY_CLIENT;
3492
Andreas Huberbe06d262009-08-14 14:37:10 -07003493 info->mMediaBuffer->add_ref();
3494 *buffer = info->mMediaBuffer;
3495
3496 return OK;
3497}
3498
3499void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
3500 Mutex::Autolock autoLock(mLock);
3501
3502 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
3503 for (size_t i = 0; i < buffers->size(); ++i) {
3504 BufferInfo *info = &buffers->editItemAt(i);
3505
3506 if (info->mMediaBuffer == buffer) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003507 CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED);
Andreas Huberbbbcf652010-12-07 14:25:54 -08003508 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_CLIENT);
3509
3510 info->mStatus = OWNED_BY_US;
3511
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003512 if (buffer->graphicBuffer() == 0) {
3513 fillOutputBuffer(info);
3514 } else {
3515 sp<MetaData> metaData = info->mMediaBuffer->meta_data();
3516 int32_t rendered = 0;
3517 if (!metaData->findInt32(kKeyRendered, &rendered)) {
3518 rendered = 0;
3519 }
3520 if (!rendered) {
3521 status_t err = cancelBufferToNativeWindow(info);
3522 if (err < 0) {
3523 return;
3524 }
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003525 }
3526
Andreas Huberbbbcf652010-12-07 14:25:54 -08003527 info->mStatus = OWNED_BY_NATIVE_WINDOW;
3528
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003529 // Dequeue the next buffer from the native window.
3530 BufferInfo *nextBufInfo = dequeueBufferFromNativeWindow();
3531 if (nextBufInfo == 0) {
3532 return;
3533 }
3534
3535 // Give the buffer to the OMX node to fill.
3536 fillOutputBuffer(nextBufInfo);
3537 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003538 return;
3539 }
3540 }
3541
3542 CHECK(!"should not be here.");
3543}
3544
3545static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
3546 static const char *kNames[] = {
3547 "OMX_IMAGE_CodingUnused",
3548 "OMX_IMAGE_CodingAutoDetect",
3549 "OMX_IMAGE_CodingJPEG",
3550 "OMX_IMAGE_CodingJPEG2K",
3551 "OMX_IMAGE_CodingEXIF",
3552 "OMX_IMAGE_CodingTIFF",
3553 "OMX_IMAGE_CodingGIF",
3554 "OMX_IMAGE_CodingPNG",
3555 "OMX_IMAGE_CodingLZW",
3556 "OMX_IMAGE_CodingBMP",
3557 };
3558
3559 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3560
3561 if (type < 0 || (size_t)type >= numNames) {
3562 return "UNKNOWN";
3563 } else {
3564 return kNames[type];
3565 }
3566}
3567
3568static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
3569 static const char *kNames[] = {
3570 "OMX_COLOR_FormatUnused",
3571 "OMX_COLOR_FormatMonochrome",
3572 "OMX_COLOR_Format8bitRGB332",
3573 "OMX_COLOR_Format12bitRGB444",
3574 "OMX_COLOR_Format16bitARGB4444",
3575 "OMX_COLOR_Format16bitARGB1555",
3576 "OMX_COLOR_Format16bitRGB565",
3577 "OMX_COLOR_Format16bitBGR565",
3578 "OMX_COLOR_Format18bitRGB666",
3579 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003580 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07003581 "OMX_COLOR_Format24bitRGB888",
3582 "OMX_COLOR_Format24bitBGR888",
3583 "OMX_COLOR_Format24bitARGB1887",
3584 "OMX_COLOR_Format25bitARGB1888",
3585 "OMX_COLOR_Format32bitBGRA8888",
3586 "OMX_COLOR_Format32bitARGB8888",
3587 "OMX_COLOR_FormatYUV411Planar",
3588 "OMX_COLOR_FormatYUV411PackedPlanar",
3589 "OMX_COLOR_FormatYUV420Planar",
3590 "OMX_COLOR_FormatYUV420PackedPlanar",
3591 "OMX_COLOR_FormatYUV420SemiPlanar",
3592 "OMX_COLOR_FormatYUV422Planar",
3593 "OMX_COLOR_FormatYUV422PackedPlanar",
3594 "OMX_COLOR_FormatYUV422SemiPlanar",
3595 "OMX_COLOR_FormatYCbYCr",
3596 "OMX_COLOR_FormatYCrYCb",
3597 "OMX_COLOR_FormatCbYCrY",
3598 "OMX_COLOR_FormatCrYCbY",
3599 "OMX_COLOR_FormatYUV444Interleaved",
3600 "OMX_COLOR_FormatRawBayer8bit",
3601 "OMX_COLOR_FormatRawBayer10bit",
3602 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003603 "OMX_COLOR_FormatL2",
3604 "OMX_COLOR_FormatL4",
3605 "OMX_COLOR_FormatL8",
3606 "OMX_COLOR_FormatL16",
3607 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07003608 "OMX_COLOR_FormatL32",
3609 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
3610 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
3611 "OMX_COLOR_Format18BitBGR666",
3612 "OMX_COLOR_Format24BitARGB6666",
3613 "OMX_COLOR_Format24BitABGR6666",
3614 };
3615
3616 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3617
Andreas Huberbe06d262009-08-14 14:37:10 -07003618 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3619 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3620 } else if (type < 0 || (size_t)type >= numNames) {
3621 return "UNKNOWN";
3622 } else {
3623 return kNames[type];
3624 }
3625}
3626
3627static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3628 static const char *kNames[] = {
3629 "OMX_VIDEO_CodingUnused",
3630 "OMX_VIDEO_CodingAutoDetect",
3631 "OMX_VIDEO_CodingMPEG2",
3632 "OMX_VIDEO_CodingH263",
3633 "OMX_VIDEO_CodingMPEG4",
3634 "OMX_VIDEO_CodingWMV",
3635 "OMX_VIDEO_CodingRV",
3636 "OMX_VIDEO_CodingAVC",
3637 "OMX_VIDEO_CodingMJPEG",
3638 };
3639
3640 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3641
3642 if (type < 0 || (size_t)type >= numNames) {
3643 return "UNKNOWN";
3644 } else {
3645 return kNames[type];
3646 }
3647}
3648
3649static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3650 static const char *kNames[] = {
3651 "OMX_AUDIO_CodingUnused",
3652 "OMX_AUDIO_CodingAutoDetect",
3653 "OMX_AUDIO_CodingPCM",
3654 "OMX_AUDIO_CodingADPCM",
3655 "OMX_AUDIO_CodingAMR",
3656 "OMX_AUDIO_CodingGSMFR",
3657 "OMX_AUDIO_CodingGSMEFR",
3658 "OMX_AUDIO_CodingGSMHR",
3659 "OMX_AUDIO_CodingPDCFR",
3660 "OMX_AUDIO_CodingPDCEFR",
3661 "OMX_AUDIO_CodingPDCHR",
3662 "OMX_AUDIO_CodingTDMAFR",
3663 "OMX_AUDIO_CodingTDMAEFR",
3664 "OMX_AUDIO_CodingQCELP8",
3665 "OMX_AUDIO_CodingQCELP13",
3666 "OMX_AUDIO_CodingEVRC",
3667 "OMX_AUDIO_CodingSMV",
3668 "OMX_AUDIO_CodingG711",
3669 "OMX_AUDIO_CodingG723",
3670 "OMX_AUDIO_CodingG726",
3671 "OMX_AUDIO_CodingG729",
3672 "OMX_AUDIO_CodingAAC",
3673 "OMX_AUDIO_CodingMP3",
3674 "OMX_AUDIO_CodingSBC",
3675 "OMX_AUDIO_CodingVORBIS",
3676 "OMX_AUDIO_CodingWMA",
3677 "OMX_AUDIO_CodingRA",
3678 "OMX_AUDIO_CodingMIDI",
3679 };
3680
3681 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3682
3683 if (type < 0 || (size_t)type >= numNames) {
3684 return "UNKNOWN";
3685 } else {
3686 return kNames[type];
3687 }
3688}
3689
3690static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3691 static const char *kNames[] = {
3692 "OMX_AUDIO_PCMModeLinear",
3693 "OMX_AUDIO_PCMModeALaw",
3694 "OMX_AUDIO_PCMModeMULaw",
3695 };
3696
3697 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3698
3699 if (type < 0 || (size_t)type >= numNames) {
3700 return "UNKNOWN";
3701 } else {
3702 return kNames[type];
3703 }
3704}
3705
Andreas Huber7ae02c82009-09-09 16:29:47 -07003706static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3707 static const char *kNames[] = {
3708 "OMX_AUDIO_AMRBandModeUnused",
3709 "OMX_AUDIO_AMRBandModeNB0",
3710 "OMX_AUDIO_AMRBandModeNB1",
3711 "OMX_AUDIO_AMRBandModeNB2",
3712 "OMX_AUDIO_AMRBandModeNB3",
3713 "OMX_AUDIO_AMRBandModeNB4",
3714 "OMX_AUDIO_AMRBandModeNB5",
3715 "OMX_AUDIO_AMRBandModeNB6",
3716 "OMX_AUDIO_AMRBandModeNB7",
3717 "OMX_AUDIO_AMRBandModeWB0",
3718 "OMX_AUDIO_AMRBandModeWB1",
3719 "OMX_AUDIO_AMRBandModeWB2",
3720 "OMX_AUDIO_AMRBandModeWB3",
3721 "OMX_AUDIO_AMRBandModeWB4",
3722 "OMX_AUDIO_AMRBandModeWB5",
3723 "OMX_AUDIO_AMRBandModeWB6",
3724 "OMX_AUDIO_AMRBandModeWB7",
3725 "OMX_AUDIO_AMRBandModeWB8",
3726 };
3727
3728 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3729
3730 if (type < 0 || (size_t)type >= numNames) {
3731 return "UNKNOWN";
3732 } else {
3733 return kNames[type];
3734 }
3735}
3736
3737static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3738 static const char *kNames[] = {
3739 "OMX_AUDIO_AMRFrameFormatConformance",
3740 "OMX_AUDIO_AMRFrameFormatIF1",
3741 "OMX_AUDIO_AMRFrameFormatIF2",
3742 "OMX_AUDIO_AMRFrameFormatFSF",
3743 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3744 "OMX_AUDIO_AMRFrameFormatITU",
3745 };
3746
3747 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3748
3749 if (type < 0 || (size_t)type >= numNames) {
3750 return "UNKNOWN";
3751 } else {
3752 return kNames[type];
3753 }
3754}
Andreas Huberbe06d262009-08-14 14:37:10 -07003755
3756void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3757 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003758 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003759 def.nPortIndex = portIndex;
3760
Andreas Huber784202e2009-10-15 13:46:54 -07003761 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003762 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003763 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003764
3765 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3766
3767 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3768 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3769
3770 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3771 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3772 printf(" nBufferSize = %ld\n", def.nBufferSize);
3773
3774 switch (def.eDomain) {
3775 case OMX_PortDomainImage:
3776 {
3777 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3778
3779 printf("\n");
3780 printf(" // Image\n");
3781 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3782 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3783 printf(" nStride = %ld\n", imageDef->nStride);
3784
3785 printf(" eCompressionFormat = %s\n",
3786 imageCompressionFormatString(imageDef->eCompressionFormat));
3787
3788 printf(" eColorFormat = %s\n",
3789 colorFormatString(imageDef->eColorFormat));
3790
3791 break;
3792 }
3793
3794 case OMX_PortDomainVideo:
3795 {
3796 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3797
3798 printf("\n");
3799 printf(" // Video\n");
3800 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3801 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3802 printf(" nStride = %ld\n", videoDef->nStride);
3803
3804 printf(" eCompressionFormat = %s\n",
3805 videoCompressionFormatString(videoDef->eCompressionFormat));
3806
3807 printf(" eColorFormat = %s\n",
3808 colorFormatString(videoDef->eColorFormat));
3809
3810 break;
3811 }
3812
3813 case OMX_PortDomainAudio:
3814 {
3815 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3816
3817 printf("\n");
3818 printf(" // Audio\n");
3819 printf(" eEncoding = %s\n",
3820 audioCodingTypeString(audioDef->eEncoding));
3821
3822 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3823 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003824 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003825 params.nPortIndex = portIndex;
3826
Andreas Huber784202e2009-10-15 13:46:54 -07003827 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003828 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003829 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003830
3831 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3832 printf(" nChannels = %ld\n", params.nChannels);
3833 printf(" bInterleaved = %d\n", params.bInterleaved);
3834 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3835
3836 printf(" eNumData = %s\n",
3837 params.eNumData == OMX_NumericalDataSigned
3838 ? "signed" : "unsigned");
3839
3840 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003841 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3842 OMX_AUDIO_PARAM_AMRTYPE amr;
3843 InitOMXParams(&amr);
3844 amr.nPortIndex = portIndex;
3845
Andreas Huber784202e2009-10-15 13:46:54 -07003846 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003847 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003848 CHECK_EQ(err, (status_t)OK);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003849
3850 printf(" nChannels = %ld\n", amr.nChannels);
3851 printf(" eAMRBandMode = %s\n",
3852 amrBandModeString(amr.eAMRBandMode));
3853 printf(" eAMRFrameFormat = %s\n",
3854 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003855 }
3856
3857 break;
3858 }
3859
3860 default:
3861 {
3862 printf(" // Unknown\n");
3863 break;
3864 }
3865 }
3866
3867 printf("}\n");
3868}
3869
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003870status_t OMXCodec::initNativeWindow() {
3871 // Enable use of a GraphicBuffer as the output for this node. This must
3872 // happen before getting the IndexParamPortDefinition parameter because it
3873 // will affect the pixel format that the node reports.
3874 status_t err = mOMX->enableGraphicBuffers(mNode, kPortIndexOutput, OMX_TRUE);
3875 if (err != 0) {
3876 return err;
3877 }
3878
3879 return OK;
3880}
3881
Andreas Huberbe06d262009-08-14 14:37:10 -07003882void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3883 mOutputFormat = new MetaData;
3884 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003885 if (mIsEncoder) {
3886 int32_t timeScale;
3887 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3888 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3889 }
3890 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003891
3892 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003893 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003894 def.nPortIndex = kPortIndexOutput;
3895
Andreas Huber784202e2009-10-15 13:46:54 -07003896 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003897 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003898 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003899
3900 switch (def.eDomain) {
3901 case OMX_PortDomainImage:
3902 {
3903 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003904 CHECK_EQ((int)imageDef->eCompressionFormat,
3905 (int)OMX_IMAGE_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07003906
Andreas Hubere6c40962009-09-10 14:13:30 -07003907 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003908 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3909 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3910 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3911 break;
3912 }
3913
3914 case OMX_PortDomainAudio:
3915 {
3916 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3917
Andreas Huberda050cf22009-09-02 14:01:43 -07003918 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3919 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003920 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003921 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003922
Andreas Huber784202e2009-10-15 13:46:54 -07003923 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003924 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003925 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003926
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003927 CHECK_EQ((int)params.eNumData, (int)OMX_NumericalDataSigned);
3928 CHECK_EQ(params.nBitPerSample, 16u);
3929 CHECK_EQ((int)params.ePCMMode, (int)OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003930
Andreas Huberda050cf22009-09-02 14:01:43 -07003931 int32_t numChannels, sampleRate;
3932 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3933 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003934
Andreas Huberda050cf22009-09-02 14:01:43 -07003935 if ((OMX_U32)numChannels != params.nChannels) {
3936 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003937 "the input stream contains (contains %d channels, "
3938 "codec outputs %ld channels).",
3939 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003940 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003941
Andreas Hubere6c40962009-09-10 14:13:30 -07003942 mOutputFormat->setCString(
3943 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003944
3945 // Use the codec-advertised number of channels, as some
3946 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003947 // mono. If we know the codec lies about this information,
3948 // use the actual number of channels instead.
3949 mOutputFormat->setInt32(
3950 kKeyChannelCount,
3951 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3952 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003953
3954 // The codec-reported sampleRate is not reliable...
3955 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3956 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003957 OMX_AUDIO_PARAM_AMRTYPE amr;
3958 InitOMXParams(&amr);
3959 amr.nPortIndex = kPortIndexOutput;
3960
Andreas Huber784202e2009-10-15 13:46:54 -07003961 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003962 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003963 CHECK_EQ(err, (status_t)OK);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003964
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003965 CHECK_EQ(amr.nChannels, 1u);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003966 mOutputFormat->setInt32(kKeyChannelCount, 1);
3967
3968 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3969 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003970 mOutputFormat->setCString(
3971 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003972 mOutputFormat->setInt32(kKeySampleRate, 8000);
3973 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3974 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003975 mOutputFormat->setCString(
3976 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003977 mOutputFormat->setInt32(kKeySampleRate, 16000);
3978 } else {
3979 CHECK(!"Unknown AMR band mode.");
3980 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003981 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003982 mOutputFormat->setCString(
3983 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003984 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003985 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3986 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003987 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003988 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3989 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003990 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003991 } else {
3992 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003993 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003994 break;
3995 }
3996
3997 case OMX_PortDomainVideo:
3998 {
3999 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
4000
4001 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07004002 mOutputFormat->setCString(
4003 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07004004 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07004005 mOutputFormat->setCString(
4006 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07004007 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07004008 mOutputFormat->setCString(
4009 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07004010 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07004011 mOutputFormat->setCString(
4012 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07004013 } else {
4014 CHECK(!"Unknown compression format.");
4015 }
4016
James Dong5592bcc2010-10-22 17:10:43 -07004017 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
4018 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
Andreas Huberbe06d262009-08-14 14:37:10 -07004019 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004020
James Dong0c9153d2010-11-23 11:30:21 -08004021 if (!mIsEncoder) {
4022 OMX_CONFIG_RECTTYPE rect;
James Donga1735412011-01-07 14:56:34 -08004023 InitOMXParams(&rect);
4024 rect.nPortIndex = kPortIndexOutput;
James Dong0c9153d2010-11-23 11:30:21 -08004025 status_t err =
4026 mOMX->getConfig(
4027 mNode, OMX_IndexConfigCommonOutputCrop,
4028 &rect, sizeof(rect));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004029
James Dong0c9153d2010-11-23 11:30:21 -08004030 if (err == OK) {
4031 CHECK_GE(rect.nLeft, 0);
4032 CHECK_GE(rect.nTop, 0);
4033 CHECK_GE(rect.nWidth, 0u);
4034 CHECK_GE(rect.nHeight, 0u);
4035 CHECK_LE(rect.nLeft + rect.nWidth - 1, video_def->nFrameWidth);
4036 CHECK_LE(rect.nTop + rect.nHeight - 1, video_def->nFrameHeight);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004037
James Dong0c9153d2010-11-23 11:30:21 -08004038 mOutputFormat->setRect(
4039 kKeyCropRect,
4040 rect.nLeft,
4041 rect.nTop,
4042 rect.nLeft + rect.nWidth - 1,
4043 rect.nTop + rect.nHeight - 1);
4044 } else {
4045 mOutputFormat->setRect(
4046 kKeyCropRect,
4047 0, 0,
4048 video_def->nFrameWidth - 1,
4049 video_def->nFrameHeight - 1);
4050 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004051 }
4052
Andreas Huberbe06d262009-08-14 14:37:10 -07004053 break;
4054 }
4055
4056 default:
4057 {
4058 CHECK(!"should not be here, neither audio nor video.");
4059 break;
4060 }
4061 }
4062}
4063
Andreas Huber1f24b302010-06-10 11:12:39 -07004064status_t OMXCodec::pause() {
4065 Mutex::Autolock autoLock(mLock);
4066
4067 mPaused = true;
4068
4069 return OK;
4070}
4071
Andreas Hubere6c40962009-09-10 14:13:30 -07004072////////////////////////////////////////////////////////////////////////////////
4073
4074status_t QueryCodecs(
4075 const sp<IOMX> &omx,
4076 const char *mime, bool queryDecoders,
4077 Vector<CodecCapabilities> *results) {
4078 results->clear();
4079
4080 for (int index = 0;; ++index) {
4081 const char *componentName;
4082
4083 if (!queryDecoders) {
4084 componentName = GetCodec(
4085 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
4086 mime, index);
4087 } else {
4088 componentName = GetCodec(
4089 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
4090 mime, index);
4091 }
4092
4093 if (!componentName) {
4094 return OK;
4095 }
4096
Andreas Huber1a189a82010-03-24 13:49:20 -07004097 if (strncmp(componentName, "OMX.", 4)) {
4098 // Not an OpenMax component but a software codec.
4099
4100 results->push();
4101 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
4102 caps->mComponentName = componentName;
4103
4104 continue;
4105 }
4106
Andreas Huber784202e2009-10-15 13:46:54 -07004107 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07004108 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07004109 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07004110
4111 if (err != OK) {
4112 continue;
4113 }
4114
James Dong722d5912010-04-13 10:56:59 -07004115 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07004116
4117 results->push();
4118 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
4119 caps->mComponentName = componentName;
4120
4121 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
4122 InitOMXParams(&param);
4123
4124 param.nPortIndex = queryDecoders ? 0 : 1;
4125
4126 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07004127 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07004128 node, OMX_IndexParamVideoProfileLevelQuerySupported,
4129 &param, sizeof(param));
4130
4131 if (err != OK) {
4132 break;
4133 }
4134
4135 CodecProfileLevel profileLevel;
4136 profileLevel.mProfile = param.eProfile;
4137 profileLevel.mLevel = param.eLevel;
4138
4139 caps->mProfileLevels.push(profileLevel);
4140 }
4141
James Dongd7810892010-11-11 00:33:05 -08004142 // Color format query
4143 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
4144 InitOMXParams(&portFormat);
4145 portFormat.nPortIndex = queryDecoders ? 1 : 0;
4146 for (portFormat.nIndex = 0;; ++portFormat.nIndex) {
4147 err = omx->getParameter(
4148 node, OMX_IndexParamVideoPortFormat,
4149 &portFormat, sizeof(portFormat));
4150 if (err != OK) {
4151 break;
4152 }
4153 caps->mColorFormats.push(portFormat.eColorFormat);
4154 }
4155
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004156 CHECK_EQ(omx->freeNode(node), (status_t)OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07004157 }
4158}
4159
James Dong31b9375f2010-11-10 21:11:41 -08004160void OMXCodec::restorePatchedDataPointer(BufferInfo *info) {
4161 CHECK(mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames));
4162 CHECK(mOMXLivesLocally);
4163
4164 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *)info->mBuffer;
4165 header->pBuffer = (OMX_U8 *)info->mData;
4166}
4167
Andreas Huberbe06d262009-08-14 14:37:10 -07004168} // namespace android