blob: 802eaa98d9222f65640d3c9aaec991b1dea825fa [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 }
Andreas Huber4c19bf92010-09-08 14:32:20 -0700525 if (!(flags & kIgnoreCodecSpecificData)) {
526 uint32_t type;
527 const void *data;
528 size_t size;
529 if (meta->findData(kKeyESDS, &type, &data, &size)) {
530 ESDS esds((const char *)data, size);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800531 CHECK_EQ(esds.InitCheck(), (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700532
Andreas Huber4c19bf92010-09-08 14:32:20 -0700533 const void *codec_specific_data;
534 size_t codec_specific_data_size;
535 esds.getCodecSpecificInfo(
536 &codec_specific_data, &codec_specific_data_size);
Andreas Huberbe06d262009-08-14 14:37:10 -0700537
Andreas Huber4c19bf92010-09-08 14:32:20 -0700538 addCodecSpecificData(
539 codec_specific_data, codec_specific_data_size);
540 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
541 // Parse the AVCDecoderConfigurationRecord
Andreas Huberebf66ea2009-08-19 13:32:58 -0700542
Andreas Huber4c19bf92010-09-08 14:32:20 -0700543 const uint8_t *ptr = (const uint8_t *)data;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700544
Andreas Huber4c19bf92010-09-08 14:32:20 -0700545 CHECK(size >= 7);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800546 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1
Andreas Huber4c19bf92010-09-08 14:32:20 -0700547 uint8_t profile = ptr[1];
548 uint8_t level = ptr[3];
Andreas Huberebf66ea2009-08-19 13:32:58 -0700549
Andreas Huber4c19bf92010-09-08 14:32:20 -0700550 // There is decodable content out there that fails the following
551 // assertion, let's be lenient for now...
552 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700553
Andreas Huber4c19bf92010-09-08 14:32:20 -0700554 size_t lengthSize = 1 + (ptr[4] & 3);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700555
Andreas Huber4c19bf92010-09-08 14:32:20 -0700556 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
557 // violates it...
558 // CHECK((ptr[5] >> 5) == 7); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700559
Andreas Huber4c19bf92010-09-08 14:32:20 -0700560 size_t numSeqParameterSets = ptr[5] & 31;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700561
Andreas Huber4c19bf92010-09-08 14:32:20 -0700562 ptr += 6;
563 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700564
Andreas Huber4c19bf92010-09-08 14:32:20 -0700565 for (size_t i = 0; i < numSeqParameterSets; ++i) {
566 CHECK(size >= 2);
567 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700568
Andreas Huber4c19bf92010-09-08 14:32:20 -0700569 ptr += 2;
570 size -= 2;
Andreas Huberbe06d262009-08-14 14:37:10 -0700571
Andreas Huber4c19bf92010-09-08 14:32:20 -0700572 CHECK(size >= length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700573
Andreas Huber4c19bf92010-09-08 14:32:20 -0700574 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700575
Andreas Huber4c19bf92010-09-08 14:32:20 -0700576 ptr += length;
577 size -= length;
578 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700579
Andreas Huber4c19bf92010-09-08 14:32:20 -0700580 CHECK(size >= 1);
581 size_t numPictureParameterSets = *ptr;
582 ++ptr;
583 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700584
Andreas Huber4c19bf92010-09-08 14:32:20 -0700585 for (size_t i = 0; i < numPictureParameterSets; ++i) {
586 CHECK(size >= 2);
587 size_t length = U16_AT(ptr);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700588
Andreas Huber4c19bf92010-09-08 14:32:20 -0700589 ptr += 2;
590 size -= 2;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700591
Andreas Huber4c19bf92010-09-08 14:32:20 -0700592 CHECK(size >= length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700593
Andreas Huber4c19bf92010-09-08 14:32:20 -0700594 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700595
Andreas Huber4c19bf92010-09-08 14:32:20 -0700596 ptr += length;
597 size -= length;
598 }
Andreas Huberebf66ea2009-08-19 13:32:58 -0700599
Andreas Huber6ac2cb22010-11-18 14:06:07 -0800600 CODEC_LOGI(
Andreas Huber4c19bf92010-09-08 14:32:20 -0700601 "AVC profile = %d (%s), level = %d",
602 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700603
Andreas Huber4c19bf92010-09-08 14:32:20 -0700604 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
605 && (profile != kAVCProfileBaseline || level > 30)) {
606 // This stream exceeds the decoder's capabilities. The decoder
607 // does not handle this gracefully and would clobber the heap
608 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700609
Andreas Huber4c19bf92010-09-08 14:32:20 -0700610 LOGE("Profile and/or level exceed the decoder's capabilities.");
611 return ERROR_UNSUPPORTED;
612 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700613 }
614 }
615
James Dong17299ab2010-05-14 15:45:22 -0700616 int32_t bitRate = 0;
617 if (mIsEncoder) {
618 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
619 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700620 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700621 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700622 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700623 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700624 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700625 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700626 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700627 int32_t numChannels, sampleRate;
628 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
629 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
630
James Dong17299ab2010-05-14 15:45:22 -0700631 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700632 }
James Dongabed93a2010-04-22 17:27:04 -0700633
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700634 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700635
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700636 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700637 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700638 } else {
James Dong1244eab2010-06-08 11:58:53 -0700639 int32_t width, height;
640 bool success = meta->findInt32(kKeyWidth, &width);
641 success = success && meta->findInt32(kKeyHeight, &height);
642 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700643 status_t err = setVideoOutputFormat(
644 mMIME, width, height);
645
646 if (err != OK) {
647 return err;
648 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700649 }
650 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700651
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700652 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
653 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700654 OMX_COLOR_FORMATTYPE format =
655 OMX_COLOR_Format32bitARGB8888;
656 // OMX_COLOR_FormatYUV420PackedPlanar;
657 // OMX_COLOR_FormatCbYCrY;
658 // OMX_COLOR_FormatYUV411Planar;
659
660 int32_t width, height;
661 bool success = meta->findInt32(kKeyWidth, &width);
662 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700663
664 int32_t compressedSize;
665 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700666 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700667
668 CHECK(success);
669 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700670
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700671 setImageOutputFormat(format, width, height);
672 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700673 }
674
Andreas Huberda050cf22009-09-02 14:01:43 -0700675 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800676 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700677 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700678 }
679
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700680 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700681 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
682 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700683 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700684 }
685
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700686 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700687
Andreas Huber5a40e392010-10-18 09:57:42 -0700688 if ((flags & kClientNeedsFramebuffer)
689 && !strncmp(mComponentName, "OMX.SEC.", 8)) {
690 OMX_INDEXTYPE index;
691
692 status_t err =
693 mOMX->getExtensionIndex(
694 mNode,
695 "OMX.SEC.index.ThumbnailMode",
696 &index);
697
698 if (err != OK) {
699 return err;
700 }
701
702 OMX_BOOL enable = OMX_TRUE;
703 err = mOMX->setConfig(mNode, index, &enable, sizeof(enable));
704
705 if (err != OK) {
706 CODEC_LOGE("setConfig('OMX.SEC.index.ThumbnailMode') "
707 "returned error 0x%08x", err);
708
709 return err;
710 }
711
712 mQuirks &= ~kOutputBuffersAreUnreadable;
713 }
714
Jamie Gennisdbfb32e2010-10-20 15:53:59 -0700715 if (mNativeWindow != NULL
716 && !mIsEncoder
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700717 && !strncasecmp(mMIME, "video/", 6)
718 && !strncmp(mComponentName, "OMX.", 4)) {
719 status_t err = initNativeWindow();
720 if (err != OK) {
721 return err;
722 }
723 }
724
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700725 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700726}
727
Andreas Huberda050cf22009-09-02 14:01:43 -0700728void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
729 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700730 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700731 def.nPortIndex = portIndex;
732
Andreas Huber784202e2009-10-15 13:46:54 -0700733 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700734 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800735 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -0700736
Andreas Huberb8de9572010-02-22 14:58:45 -0800737 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
738 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700739 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700740 }
741
Andreas Huber784202e2009-10-15 13:46:54 -0700742 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700743 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800744 CHECK_EQ(err, (status_t)OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800745
746 err = mOMX->getParameter(
747 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800748 CHECK_EQ(err, (status_t)OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800749
750 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800751 if (portIndex == kPortIndexInput
752 && (mQuirks & kInputBufferSizesAreBogus)) {
753 CHECK_EQ(def.nBufferSize, size);
754 } else {
755 CHECK(def.nBufferSize >= size);
756 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700757}
758
Andreas Huberbe06d262009-08-14 14:37:10 -0700759status_t OMXCodec::setVideoPortFormatType(
760 OMX_U32 portIndex,
761 OMX_VIDEO_CODINGTYPE compressionFormat,
762 OMX_COLOR_FORMATTYPE colorFormat) {
763 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700764 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700765 format.nPortIndex = portIndex;
766 format.nIndex = 0;
767 bool found = false;
768
769 OMX_U32 index = 0;
770 for (;;) {
771 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700772 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700773 mNode, OMX_IndexParamVideoPortFormat,
774 &format, sizeof(format));
775
776 if (err != OK) {
777 return err;
778 }
779
780 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700781 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700782
783#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700784 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700785 portIndex,
786 index, format.eCompressionFormat, format.eColorFormat);
787#endif
788
789 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
790 if (portIndex == kPortIndexInput
791 && colorFormat == format.eColorFormat) {
792 // eCompressionFormat does not seem right.
793 found = true;
794 break;
795 }
796 if (portIndex == kPortIndexOutput
797 && compressionFormat == format.eCompressionFormat) {
798 // eColorFormat does not seem right.
799 found = true;
800 break;
801 }
802 }
803
804 if (format.eCompressionFormat == compressionFormat
805 && format.eColorFormat == colorFormat) {
806 found = true;
807 break;
808 }
809
810 ++index;
811 }
812
813 if (!found) {
814 return UNKNOWN_ERROR;
815 }
816
Andreas Huber53a76bd2009-10-06 16:20:44 -0700817 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700818 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700819 mNode, OMX_IndexParamVideoPortFormat,
820 &format, sizeof(format));
821
822 return err;
823}
824
Andreas Huberb482ce82009-10-29 12:02:48 -0700825static size_t getFrameSize(
826 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
827 switch (colorFormat) {
828 case OMX_COLOR_FormatYCbYCr:
829 case OMX_COLOR_FormatCbYCrY:
830 return width * height * 2;
831
Andreas Huber71c27d92010-03-19 11:43:15 -0700832 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700833 case OMX_COLOR_FormatYUV420SemiPlanar:
834 return (width * height * 3) / 2;
835
836 default:
837 CHECK(!"Should not be here. Unsupported color format.");
838 break;
839 }
840}
841
James Dongafd97e82010-08-03 17:19:23 -0700842status_t OMXCodec::findTargetColorFormat(
843 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) {
844 LOGV("findTargetColorFormat");
845 CHECK(mIsEncoder);
846
847 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
848 int32_t targetColorFormat;
849 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) {
850 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat;
851 } else {
852 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
853 *colorFormat = OMX_COLOR_FormatYCbYCr;
854 }
855 }
856
857 // Check whether the target color format is supported.
858 return isColorFormatSupported(*colorFormat, kPortIndexInput);
859}
860
861status_t OMXCodec::isColorFormatSupported(
862 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) {
863 LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat));
864
865 // Enumerate all the color formats supported by
866 // the omx component to see whether the given
867 // color format is supported.
868 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
869 InitOMXParams(&portFormat);
870 portFormat.nPortIndex = portIndex;
871 OMX_U32 index = 0;
872 portFormat.nIndex = index;
873 while (true) {
874 if (OMX_ErrorNone != mOMX->getParameter(
875 mNode, OMX_IndexParamVideoPortFormat,
876 &portFormat, sizeof(portFormat))) {
James Dongb5024da2010-09-13 16:30:51 -0700877 break;
James Dongafd97e82010-08-03 17:19:23 -0700878 }
879 // Make sure that omx component does not overwrite
880 // the incremented index (bug 2897413).
881 CHECK_EQ(index, portFormat.nIndex);
882 if ((portFormat.eColorFormat == colorFormat)) {
883 LOGV("Found supported color format: %d", portFormat.eColorFormat);
884 return OK; // colorFormat is supported!
885 }
886 ++index;
887 portFormat.nIndex = index;
888
889 // OMX Spec defines less than 50 color formats
890 // 1000 is more than enough for us to tell whether the omx
891 // component in question is buggy or not.
892 if (index >= 1000) {
893 LOGE("More than %ld color formats are supported???", index);
894 break;
895 }
896 }
James Dongb5024da2010-09-13 16:30:51 -0700897
898 LOGE("color format %d is not supported", colorFormat);
James Dongafd97e82010-08-03 17:19:23 -0700899 return UNKNOWN_ERROR;
900}
901
Andreas Huberbe06d262009-08-14 14:37:10 -0700902void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700903 const char *mime, const sp<MetaData>& meta) {
904
905 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
906 bool success = meta->findInt32(kKeyWidth, &width);
907 success = success && meta->findInt32(kKeyHeight, &height);
James Dongaac193c2010-11-10 20:43:53 -0800908 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -0700909 success = success && meta->findInt32(kKeyBitRate, &bitRate);
910 success = success && meta->findInt32(kKeyStride, &stride);
911 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
912 CHECK(success);
913 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700914
915 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700916 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700917 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700918 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700919 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700920 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700921 compressionFormat = OMX_VIDEO_CodingH263;
922 } else {
923 LOGE("Not a supported video mime type: %s", mime);
924 CHECK(!"Should not be here. Not a supported video mime type.");
925 }
926
James Dongafd97e82010-08-03 17:19:23 -0700927 OMX_COLOR_FORMATTYPE colorFormat;
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800928 CHECK_EQ((status_t)OK, findTargetColorFormat(meta, &colorFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -0700929
James Dongb00e2462010-04-26 17:48:26 -0700930 status_t err;
931 OMX_PARAM_PORTDEFINITIONTYPE def;
932 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
933
934 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700935 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700936 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800937 colorFormat), (status_t)OK);
James Dong4f501f02010-06-07 14:41:41 -0700938
James Dongb00e2462010-04-26 17:48:26 -0700939 InitOMXParams(&def);
940 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700941
James Dongb00e2462010-04-26 17:48:26 -0700942 err = mOMX->getParameter(
943 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800944 CHECK_EQ(err, (status_t)OK);
James Dongb00e2462010-04-26 17:48:26 -0700945
James Dong1244eab2010-06-08 11:58:53 -0700946 def.nBufferSize = getFrameSize(colorFormat,
947 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700948
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800949 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
James Dongb00e2462010-04-26 17:48:26 -0700950
951 video_def->nFrameWidth = width;
952 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700953 video_def->nStride = stride;
954 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700955 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700956 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
957 video_def->eColorFormat = colorFormat;
958
James Dongb00e2462010-04-26 17:48:26 -0700959 err = mOMX->setParameter(
960 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800961 CHECK_EQ(err, (status_t)OK);
James Dongb00e2462010-04-26 17:48:26 -0700962
963 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700964 CHECK_EQ(setVideoPortFormatType(
965 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800966 (status_t)OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700967 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700968 def.nPortIndex = kPortIndexOutput;
969
James Dongb00e2462010-04-26 17:48:26 -0700970 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700971 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
972
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800973 CHECK_EQ(err, (status_t)OK);
974 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -0700975
976 video_def->nFrameWidth = width;
977 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700978 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700979 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700980 video_def->eCompressionFormat = compressionFormat;
981 video_def->eColorFormat = OMX_COLOR_FormatUnused;
James Dong90862e22010-08-26 19:12:59 -0700982 if (mQuirks & kRequiresLargerEncoderOutputBuffer) {
983 // Increases the output buffer size
984 def.nBufferSize = ((def.nBufferSize * 3) >> 1);
985 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700986
Andreas Huber784202e2009-10-15 13:46:54 -0700987 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700988 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800989 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700990
James Dongb00e2462010-04-26 17:48:26 -0700991 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700992 switch (compressionFormat) {
993 case OMX_VIDEO_CodingMPEG4:
994 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800995 CHECK_EQ(setupMPEG4EncoderParameters(meta), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700996 break;
997 }
998
999 case OMX_VIDEO_CodingH263:
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001000 CHECK_EQ(setupH263EncoderParameters(meta), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001001 break;
1002
Andreas Huberea6a38c2009-11-16 15:43:38 -08001003 case OMX_VIDEO_CodingAVC:
1004 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001005 CHECK_EQ(setupAVCEncoderParameters(meta), (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001006 break;
1007 }
1008
Andreas Huberb482ce82009-10-29 12:02:48 -07001009 default:
1010 CHECK(!"Support for this compressionFormat to be implemented.");
1011 break;
1012 }
1013}
1014
James Dong1244eab2010-06-08 11:58:53 -07001015static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
1016 if (iFramesInterval < 0) {
1017 return 0xFFFFFFFF;
1018 } else if (iFramesInterval == 0) {
1019 return 0;
1020 }
1021 OMX_U32 ret = frameRate * iFramesInterval;
1022 CHECK(ret > 1);
1023 return ret;
1024}
1025
James Dongc0ab2a62010-06-29 16:29:19 -07001026status_t OMXCodec::setupErrorCorrectionParameters() {
1027 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
1028 InitOMXParams(&errorCorrectionType);
1029 errorCorrectionType.nPortIndex = kPortIndexOutput;
1030
1031 status_t err = mOMX->getParameter(
1032 mNode, OMX_IndexParamVideoErrorCorrection,
1033 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001034 if (err != OK) {
1035 LOGW("Error correction param query is not supported");
1036 return OK; // Optional feature. Ignore this failure
1037 }
James Dongc0ab2a62010-06-29 16:29:19 -07001038
1039 errorCorrectionType.bEnableHEC = OMX_FALSE;
1040 errorCorrectionType.bEnableResync = OMX_TRUE;
1041 errorCorrectionType.nResynchMarkerSpacing = 256;
1042 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
1043 errorCorrectionType.bEnableRVLC = OMX_FALSE;
1044
1045 err = mOMX->setParameter(
1046 mNode, OMX_IndexParamVideoErrorCorrection,
1047 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001048 if (err != OK) {
1049 LOGW("Error correction param configuration is not supported");
1050 }
1051
1052 // Optional feature. Ignore the failure.
James Dongc0ab2a62010-06-29 16:29:19 -07001053 return OK;
1054}
1055
1056status_t OMXCodec::setupBitRate(int32_t bitRate) {
1057 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
1058 InitOMXParams(&bitrateType);
1059 bitrateType.nPortIndex = kPortIndexOutput;
1060
1061 status_t err = mOMX->getParameter(
1062 mNode, OMX_IndexParamVideoBitrate,
1063 &bitrateType, sizeof(bitrateType));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001064 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001065
1066 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
1067 bitrateType.nTargetBitrate = bitRate;
1068
1069 err = mOMX->setParameter(
1070 mNode, OMX_IndexParamVideoBitrate,
1071 &bitrateType, sizeof(bitrateType));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001072 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001073 return OK;
1074}
1075
James Dong81c929a2010-07-01 15:02:14 -07001076status_t OMXCodec::getVideoProfileLevel(
1077 const sp<MetaData>& meta,
1078 const CodecProfileLevel& defaultProfileLevel,
1079 CodecProfileLevel &profileLevel) {
1080 CODEC_LOGV("Default profile: %ld, level %ld",
1081 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
1082
1083 // Are the default profile and level overwriten?
1084 int32_t profile, level;
1085 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
1086 profile = defaultProfileLevel.mProfile;
1087 }
1088 if (!meta->findInt32(kKeyVideoLevel, &level)) {
1089 level = defaultProfileLevel.mLevel;
1090 }
1091 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
1092
1093 // Are the target profile and level supported by the encoder?
1094 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
1095 InitOMXParams(&param);
1096 param.nPortIndex = kPortIndexOutput;
1097 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
1098 status_t err = mOMX->getParameter(
1099 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
1100 &param, sizeof(param));
1101
James Dongdfb89912010-09-15 21:07:52 -07001102 if (err != OK) break;
James Dong81c929a2010-07-01 15:02:14 -07001103
1104 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
1105 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -07001106 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -07001107 supportedProfile, supportedLevel);
1108
1109 if (profile == supportedProfile &&
James Dongdfb89912010-09-15 21:07:52 -07001110 level <= supportedLevel) {
1111 // We can further check whether the level is a valid
1112 // value; but we will leave that to the omx encoder component
1113 // via OMX_SetParameter call.
James Dong81c929a2010-07-01 15:02:14 -07001114 profileLevel.mProfile = profile;
1115 profileLevel.mLevel = level;
1116 return OK;
1117 }
1118 }
1119
1120 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
1121 profile, level);
1122 return BAD_VALUE;
1123}
1124
James Dongc0ab2a62010-06-29 16:29:19 -07001125status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
1126 int32_t iFramesInterval, frameRate, bitRate;
1127 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001128 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dongc0ab2a62010-06-29 16:29:19 -07001129 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1130 CHECK(success);
1131 OMX_VIDEO_PARAM_H263TYPE h263type;
1132 InitOMXParams(&h263type);
1133 h263type.nPortIndex = kPortIndexOutput;
1134
1135 status_t err = mOMX->getParameter(
1136 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001137 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001138
1139 h263type.nAllowedPictureTypes =
1140 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1141
1142 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1143 if (h263type.nPFrames == 0) {
1144 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1145 }
1146 h263type.nBFrames = 0;
1147
James Dong81c929a2010-07-01 15:02:14 -07001148 // Check profile and level parameters
1149 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001150 defaultProfileLevel.mProfile = h263type.eProfile;
1151 defaultProfileLevel.mLevel = h263type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001152 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1153 if (err != OK) return err;
1154 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1155 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001156
1157 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1158 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1159 h263type.nPictureHeaderRepetition = 0;
1160 h263type.nGOBHeaderInterval = 0;
1161
1162 err = mOMX->setParameter(
1163 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001164 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001165
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001166 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
1167 CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001168
1169 return OK;
1170}
1171
James Dong1244eab2010-06-08 11:58:53 -07001172status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1173 int32_t iFramesInterval, frameRate, bitRate;
1174 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001175 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -07001176 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1177 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001178 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1179 InitOMXParams(&mpeg4type);
1180 mpeg4type.nPortIndex = kPortIndexOutput;
1181
1182 status_t err = mOMX->getParameter(
1183 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001184 CHECK_EQ(err, (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001185
1186 mpeg4type.nSliceHeaderSpacing = 0;
1187 mpeg4type.bSVH = OMX_FALSE;
1188 mpeg4type.bGov = OMX_FALSE;
1189
1190 mpeg4type.nAllowedPictureTypes =
1191 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1192
James Dong1244eab2010-06-08 11:58:53 -07001193 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1194 if (mpeg4type.nPFrames == 0) {
1195 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1196 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001197 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001198 mpeg4type.nIDCVLCThreshold = 0;
1199 mpeg4type.bACPred = OMX_TRUE;
1200 mpeg4type.nMaxPacketSize = 256;
1201 mpeg4type.nTimeIncRes = 1000;
1202 mpeg4type.nHeaderExtension = 0;
1203 mpeg4type.bReversibleVLC = OMX_FALSE;
1204
James Dong81c929a2010-07-01 15:02:14 -07001205 // Check profile and level parameters
1206 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001207 defaultProfileLevel.mProfile = mpeg4type.eProfile;
1208 defaultProfileLevel.mLevel = mpeg4type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001209 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1210 if (err != OK) return err;
1211 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1212 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001213
1214 err = mOMX->setParameter(
1215 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001216 CHECK_EQ(err, (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001217
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001218 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
1219 CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001220
1221 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001222}
1223
James Dong1244eab2010-06-08 11:58:53 -07001224status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1225 int32_t iFramesInterval, frameRate, bitRate;
1226 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001227 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -07001228 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1229 CHECK(success);
1230
Andreas Huberea6a38c2009-11-16 15:43:38 -08001231 OMX_VIDEO_PARAM_AVCTYPE h264type;
1232 InitOMXParams(&h264type);
1233 h264type.nPortIndex = kPortIndexOutput;
1234
1235 status_t err = mOMX->getParameter(
1236 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001237 CHECK_EQ(err, (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001238
1239 h264type.nAllowedPictureTypes =
1240 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1241
1242 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001243 h264type.nBFrames = 0; // No B frames support yet
1244 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1245 if (h264type.nPFrames == 0) {
1246 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1247 }
James Dong81c929a2010-07-01 15:02:14 -07001248
1249 // Check profile and level parameters
1250 CodecProfileLevel defaultProfileLevel, profileLevel;
1251 defaultProfileLevel.mProfile = h264type.eProfile;
1252 defaultProfileLevel.mLevel = h264type.eLevel;
1253 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1254 if (err != OK) return err;
1255 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1256 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1257
1258 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1259 h264type.bUseHadamard = OMX_TRUE;
1260 h264type.nRefFrames = 1;
1261 h264type.nRefIdx10ActiveMinus1 = 0;
1262 h264type.nRefIdx11ActiveMinus1 = 0;
1263 h264type.bEntropyCodingCABAC = OMX_FALSE;
1264 h264type.bWeightedPPrediction = OMX_FALSE;
1265 h264type.bconstIpred = OMX_FALSE;
1266 h264type.bDirect8x8Inference = OMX_FALSE;
1267 h264type.bDirectSpatialTemporal = OMX_FALSE;
1268 h264type.nCabacInitIdc = 0;
1269 }
1270
1271 if (h264type.nBFrames != 0) {
1272 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1273 }
1274
Andreas Huberea6a38c2009-11-16 15:43:38 -08001275 h264type.bEnableUEP = OMX_FALSE;
1276 h264type.bEnableFMO = OMX_FALSE;
1277 h264type.bEnableASO = OMX_FALSE;
1278 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001279 h264type.bFrameMBsOnly = OMX_TRUE;
1280 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001281 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1282
pgudadhe9c305322010-07-26 13:59:29 -07001283 if (!strcasecmp("OMX.Nvidia.h264.encoder", mComponentName)) {
1284 h264type.eLevel = OMX_VIDEO_AVCLevelMax;
1285 }
1286
Andreas Huberea6a38c2009-11-16 15:43:38 -08001287 err = mOMX->setParameter(
1288 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001289 CHECK_EQ(err, (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001290
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001291 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001292
1293 return OK;
1294}
1295
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001296status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001297 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001298 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001299
Andreas Huberbe06d262009-08-14 14:37:10 -07001300 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001301 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001302 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001303 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001304 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001305 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001306 compressionFormat = OMX_VIDEO_CodingH263;
1307 } else {
1308 LOGE("Not a supported video mime type: %s", mime);
1309 CHECK(!"Should not be here. Not a supported video mime type.");
1310 }
1311
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001312 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001313 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1314
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001315 if (err != OK) {
1316 return err;
1317 }
1318
Andreas Huberbe06d262009-08-14 14:37:10 -07001319#if 1
1320 {
1321 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001322 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001323 format.nPortIndex = kPortIndexOutput;
1324 format.nIndex = 0;
1325
Andreas Huber784202e2009-10-15 13:46:54 -07001326 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001327 mNode, OMX_IndexParamVideoPortFormat,
1328 &format, sizeof(format));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001329 CHECK_EQ(err, (status_t)OK);
1330 CHECK_EQ((int)format.eCompressionFormat, (int)OMX_VIDEO_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07001331
1332 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1333
1334 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1335 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1336 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1337 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1338
Andreas Huber784202e2009-10-15 13:46:54 -07001339 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001340 mNode, OMX_IndexParamVideoPortFormat,
1341 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001342
1343 if (err != OK) {
1344 return err;
1345 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001346 }
1347#endif
1348
1349 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001350 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001351 def.nPortIndex = kPortIndexInput;
1352
Andreas Huber4c483422009-09-02 16:05:36 -07001353 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1354
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001355 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001356 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1357
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001358 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001359
1360#if 1
1361 // XXX Need a (much) better heuristic to compute input buffer sizes.
1362 const size_t X = 64 * 1024;
1363 if (def.nBufferSize < X) {
1364 def.nBufferSize = X;
1365 }
1366#endif
1367
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001368 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -07001369
1370 video_def->nFrameWidth = width;
1371 video_def->nFrameHeight = height;
1372
Andreas Huberb482ce82009-10-29 12:02:48 -07001373 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001374 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1375
Andreas Huber784202e2009-10-15 13:46:54 -07001376 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001377 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001378
1379 if (err != OK) {
1380 return err;
1381 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001382
1383 ////////////////////////////////////////////////////////////////////////////
1384
Andreas Huber4c483422009-09-02 16:05:36 -07001385 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001386 def.nPortIndex = kPortIndexOutput;
1387
Andreas Huber784202e2009-10-15 13:46:54 -07001388 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001389 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001390 CHECK_EQ(err, (status_t)OK);
1391 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -07001392
1393#if 0
1394 def.nBufferSize =
1395 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1396#endif
1397
1398 video_def->nFrameWidth = width;
1399 video_def->nFrameHeight = height;
1400
Andreas Huber784202e2009-10-15 13:46:54 -07001401 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001402 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001403
1404 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001405}
1406
Andreas Huberbe06d262009-08-14 14:37:10 -07001407OMXCodec::OMXCodec(
1408 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001409 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001410 const char *mime,
1411 const char *componentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001412 const sp<MediaSource> &source,
1413 const sp<ANativeWindow> &nativeWindow)
Andreas Huberbe06d262009-08-14 14:37:10 -07001414 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001415 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001416 mNode(node),
1417 mQuirks(quirks),
1418 mIsEncoder(isEncoder),
1419 mMIME(strdup(mime)),
1420 mComponentName(strdup(componentName)),
1421 mSource(source),
1422 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001423 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001424 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001425 mSignalledEOS(false),
1426 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001427 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001428 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001429 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1430 mTargetTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001431 mLeftOverBuffer(NULL),
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001432 mPaused(false),
1433 mNativeWindow(nativeWindow) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001434 mPortStatus[kPortIndexInput] = ENABLED;
1435 mPortStatus[kPortIndexOutput] = ENABLED;
1436
Andreas Huber4c483422009-09-02 16:05:36 -07001437 setComponentRole();
1438}
1439
Andreas Hubere6c40962009-09-10 14:13:30 -07001440// static
1441void OMXCodec::setComponentRole(
1442 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1443 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001444 struct MimeToRole {
1445 const char *mime;
1446 const char *decoderRole;
1447 const char *encoderRole;
1448 };
1449
1450 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001451 { MEDIA_MIMETYPE_AUDIO_MPEG,
1452 "audio_decoder.mp3", "audio_encoder.mp3" },
1453 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1454 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1455 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1456 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1457 { MEDIA_MIMETYPE_AUDIO_AAC,
1458 "audio_decoder.aac", "audio_encoder.aac" },
1459 { MEDIA_MIMETYPE_VIDEO_AVC,
1460 "video_decoder.avc", "video_encoder.avc" },
1461 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1462 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1463 { MEDIA_MIMETYPE_VIDEO_H263,
1464 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001465 };
1466
1467 static const size_t kNumMimeToRole =
1468 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1469
1470 size_t i;
1471 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001472 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001473 break;
1474 }
1475 }
1476
1477 if (i == kNumMimeToRole) {
1478 return;
1479 }
1480
1481 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001482 isEncoder ? kMimeToRole[i].encoderRole
1483 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001484
1485 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001486 OMX_PARAM_COMPONENTROLETYPE roleParams;
1487 InitOMXParams(&roleParams);
1488
1489 strncpy((char *)roleParams.cRole,
1490 role, OMX_MAX_STRINGNAME_SIZE - 1);
1491
1492 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1493
Andreas Huber784202e2009-10-15 13:46:54 -07001494 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001495 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001496 &roleParams, sizeof(roleParams));
1497
1498 if (err != OK) {
1499 LOGW("Failed to set standard component role '%s'.", role);
1500 }
1501 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001502}
1503
Andreas Hubere6c40962009-09-10 14:13:30 -07001504void OMXCodec::setComponentRole() {
1505 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1506}
1507
Andreas Huberbe06d262009-08-14 14:37:10 -07001508OMXCodec::~OMXCodec() {
Andreas Huberf98197a2010-09-17 11:49:39 -07001509 mSource.clear();
1510
Andreas Hubereec06d32011-01-06 10:26:36 -08001511 CHECK(mState == LOADED || mState == ERROR || mState == LOADED_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001512
Andreas Huber784202e2009-10-15 13:46:54 -07001513 status_t err = mOMX->freeNode(mNode);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001514 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001515
1516 mNode = NULL;
1517 setState(DEAD);
1518
1519 clearCodecSpecificData();
1520
1521 free(mComponentName);
1522 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001523
Andreas Huberbe06d262009-08-14 14:37:10 -07001524 free(mMIME);
1525 mMIME = NULL;
1526}
1527
1528status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001529 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001530
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001531 CHECK_EQ((int)mState, (int)LOADED);
Andreas Huberbe06d262009-08-14 14:37:10 -07001532
1533 status_t err;
1534 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001535 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001536 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001537 setState(LOADED_TO_IDLE);
1538 }
1539
1540 err = allocateBuffers();
Jamie Gennisfd8b75a2010-12-17 15:07:02 -08001541 if (err != (status_t)OK) {
1542 return err;
1543 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001544
1545 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001546 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001547 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001548
1549 setState(LOADED_TO_IDLE);
1550 }
1551
1552 while (mState != EXECUTING && mState != ERROR) {
1553 mAsyncCompletion.wait(mLock);
1554 }
1555
1556 return mState == ERROR ? UNKNOWN_ERROR : OK;
1557}
1558
1559// static
1560bool OMXCodec::isIntermediateState(State state) {
1561 return state == LOADED_TO_IDLE
1562 || state == IDLE_TO_EXECUTING
1563 || state == EXECUTING_TO_IDLE
1564 || state == IDLE_TO_LOADED
1565 || state == RECONFIGURING;
1566}
1567
1568status_t OMXCodec::allocateBuffers() {
1569 status_t err = allocateBuffersOnPort(kPortIndexInput);
1570
1571 if (err != OK) {
1572 return err;
1573 }
1574
1575 return allocateBuffersOnPort(kPortIndexOutput);
1576}
1577
1578status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
Jamie Gennisdbfb32e2010-10-20 15:53:59 -07001579 if (mNativeWindow != NULL && portIndex == kPortIndexOutput) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001580 return allocateOutputBuffersFromNativeWindow();
1581 }
1582
Andreas Huberbe06d262009-08-14 14:37:10 -07001583 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001584 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001585 def.nPortIndex = portIndex;
1586
Andreas Huber784202e2009-10-15 13:46:54 -07001587 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001588 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1589
1590 if (err != OK) {
1591 return err;
1592 }
1593
James Dong05c2fd52010-11-02 13:20:11 -07001594 if (mIsMetaDataStoredInVideoBuffers && portIndex == kPortIndexInput) {
1595 err = mOMX->storeMetaDataInBuffers(mNode, kPortIndexInput, OMX_TRUE);
1596 if (err != OK) {
1597 LOGE("Storing meta data in video buffers is not supported");
1598 return err;
1599 }
1600 }
1601
Andreas Huber57648e42010-08-04 10:14:30 -07001602 CODEC_LOGI("allocating %lu buffers of size %lu on %s port",
1603 def.nBufferCountActual, def.nBufferSize,
1604 portIndex == kPortIndexInput ? "input" : "output");
1605
Andreas Huber5c0a9132009-08-20 11:16:40 -07001606 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001607 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001608
Andreas Huberbe06d262009-08-14 14:37:10 -07001609 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001610 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001611 CHECK(mem.get() != NULL);
1612
Andreas Huberc712b9f2010-01-20 15:05:46 -08001613 BufferInfo info;
1614 info.mData = NULL;
1615 info.mSize = def.nBufferSize;
1616
Andreas Huberbe06d262009-08-14 14:37:10 -07001617 IOMX::buffer_id buffer;
1618 if (portIndex == kPortIndexInput
1619 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001620 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001621 mem.clear();
1622
Andreas Huberf1fe0642010-01-15 15:28:19 -08001623 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001624 mNode, portIndex, def.nBufferSize, &buffer,
1625 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001626 } else {
1627 err = mOMX->allocateBufferWithBackup(
1628 mNode, portIndex, mem, &buffer);
1629 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001630 } else if (portIndex == kPortIndexOutput
1631 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001632 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001633 mem.clear();
1634
Andreas Huberf1fe0642010-01-15 15:28:19 -08001635 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001636 mNode, portIndex, def.nBufferSize, &buffer,
1637 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001638 } else {
1639 err = mOMX->allocateBufferWithBackup(
1640 mNode, portIndex, mem, &buffer);
1641 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001642 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001643 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001644 }
1645
1646 if (err != OK) {
1647 LOGE("allocate_buffer_with_backup failed");
1648 return err;
1649 }
1650
Andreas Huberc712b9f2010-01-20 15:05:46 -08001651 if (mem != NULL) {
1652 info.mData = mem->pointer();
1653 }
1654
Andreas Huberbe06d262009-08-14 14:37:10 -07001655 info.mBuffer = buffer;
Andreas Huberbbbcf652010-12-07 14:25:54 -08001656 info.mStatus = OWNED_BY_US;
Andreas Huberbe06d262009-08-14 14:37:10 -07001657 info.mMem = mem;
1658 info.mMediaBuffer = NULL;
1659
1660 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001661 if (!(mOMXLivesLocally
1662 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1663 && (mQuirks & kDefersOutputBufferAllocation))) {
1664 // If the node does not fill in the buffer ptr at this time,
1665 // we will defer creating the MediaBuffer until receiving
1666 // the first FILL_BUFFER_DONE notification instead.
1667 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1668 info.mMediaBuffer->setObserver(this);
1669 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001670 }
1671
1672 mPortBuffers[portIndex].push(info);
1673
Andreas Huber4c483422009-09-02 16:05:36 -07001674 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001675 portIndex == kPortIndexInput ? "input" : "output");
1676 }
1677
Andreas Huber2ea14e22009-12-16 09:30:55 -08001678 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001679
1680 return OK;
1681}
1682
Andreas Huber5e9dc942011-01-21 14:32:31 -08001683status_t OMXCodec::applyRotation() {
1684 sp<MetaData> meta = mSource->getFormat();
1685
1686 int32_t rotationDegrees;
1687 if (!meta->findInt32(kKeyRotation, &rotationDegrees)) {
1688 rotationDegrees = 0;
1689 }
1690
1691 uint32_t transform;
1692 switch (rotationDegrees) {
1693 case 0: transform = 0; break;
1694 case 90: transform = HAL_TRANSFORM_ROT_90; break;
1695 case 180: transform = HAL_TRANSFORM_ROT_180; break;
1696 case 270: transform = HAL_TRANSFORM_ROT_270; break;
1697 default: transform = 0; break;
1698 }
1699
1700 status_t err = OK;
1701
1702 if (transform) {
1703 err = native_window_set_buffers_transform(
1704 mNativeWindow.get(), transform);
1705 }
1706
1707 return err;
1708}
1709
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001710status_t OMXCodec::allocateOutputBuffersFromNativeWindow() {
1711 // Get the number of buffers needed.
1712 OMX_PARAM_PORTDEFINITIONTYPE def;
1713 InitOMXParams(&def);
1714 def.nPortIndex = kPortIndexOutput;
1715
1716 status_t err = mOMX->getParameter(
1717 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1718 if (err != OK) {
1719 return err;
1720 }
1721
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001722 err = native_window_set_buffers_geometry(
1723 mNativeWindow.get(),
1724 def.format.video.nFrameWidth,
1725 def.format.video.nFrameHeight,
Jamie Gennis044ace62010-10-29 15:19:29 -07001726 def.format.video.eColorFormat);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001727
1728 if (err != 0) {
1729 LOGE("native_window_set_buffers_geometry failed: %s (%d)",
1730 strerror(-err), -err);
1731 return err;
1732 }
1733
1734 // Increase the buffer count by one to allow for the ANativeWindow to hold
1735 // on to one of the buffers.
1736 def.nBufferCountActual++;
1737 err = mOMX->setParameter(
1738 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1739 if (err != OK) {
1740 return err;
1741 }
1742
Andreas Huber5e9dc942011-01-21 14:32:31 -08001743 err = applyRotation();
1744 if (err != OK) {
1745 return err;
1746 }
1747
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001748 // Set up the native window.
1749 // XXX TODO: Get the gralloc usage flags from the OMX plugin!
1750 err = native_window_set_usage(
Jamie Gennisb04c15032010-11-17 18:57:13 -08001751 mNativeWindow.get(), GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001752 if (err != 0) {
1753 LOGE("native_window_set_usage failed: %s (%d)", strerror(-err), -err);
1754 return err;
1755 }
1756
1757 err = native_window_set_buffer_count(
1758 mNativeWindow.get(), def.nBufferCountActual);
1759 if (err != 0) {
1760 LOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
1761 -err);
1762 return err;
1763 }
1764
1765 // XXX TODO: Do something so the ANativeWindow knows that we'll need to get
1766 // the same set of buffers.
1767
1768 CODEC_LOGI("allocating %lu buffers from a native window of size %lu on "
1769 "output port", def.nBufferCountActual, def.nBufferSize);
1770
1771 // Dequeue buffers and send them to OMX
1772 OMX_U32 i;
1773 for (i = 0; i < def.nBufferCountActual; i++) {
1774 android_native_buffer_t* buf;
1775 err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf);
1776 if (err != 0) {
1777 LOGE("dequeueBuffer failed: %s (%d)", strerror(-err), -err);
1778 break;
1779 }
1780
1781 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false));
1782 IOMX::buffer_id bufferId;
1783 err = mOMX->useGraphicBuffer(mNode, kPortIndexOutput, graphicBuffer,
1784 &bufferId);
1785 if (err != 0) {
1786 break;
1787 }
1788
1789 CODEC_LOGV("registered graphic buffer with ID %p (pointer = %p)",
1790 bufferId, graphicBuffer.get());
1791
1792 BufferInfo info;
1793 info.mData = NULL;
1794 info.mSize = def.nBufferSize;
1795 info.mBuffer = bufferId;
Andreas Huberbbbcf652010-12-07 14:25:54 -08001796 info.mStatus = OWNED_BY_US;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001797 info.mMem = NULL;
1798 info.mMediaBuffer = new MediaBuffer(graphicBuffer);
1799 info.mMediaBuffer->setObserver(this);
1800
1801 mPortBuffers[kPortIndexOutput].push(info);
1802 }
1803
1804 OMX_U32 cancelStart;
1805 OMX_U32 cancelEnd;
1806
1807 if (err != 0) {
1808 // If an error occurred while dequeuing we need to cancel any buffers
1809 // that were dequeued.
1810 cancelStart = 0;
1811 cancelEnd = i;
1812 } else {
1813 // Return the last two buffers to the native window.
1814 // XXX TODO: The number of buffers the native window owns should probably be
1815 // queried from it when we put the native window in fixed buffer pool mode
1816 // (which needs to be implemented). Currently it's hard-coded to 2.
1817 cancelStart = def.nBufferCountActual - 2;
1818 cancelEnd = def.nBufferCountActual;
1819 }
1820
1821 for (OMX_U32 i = cancelStart; i < cancelEnd; i++) {
1822 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(i);
1823 cancelBufferToNativeWindow(info);
1824 }
1825
1826 return err;
1827}
1828
1829status_t OMXCodec::cancelBufferToNativeWindow(BufferInfo *info) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08001830 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001831 CODEC_LOGV("Calling cancelBuffer on buffer %p", info->mBuffer);
1832 int err = mNativeWindow->cancelBuffer(
1833 mNativeWindow.get(), info->mMediaBuffer->graphicBuffer().get());
1834 if (err != 0) {
1835 CODEC_LOGE("cancelBuffer failed w/ error 0x%08x", err);
1836
1837 setState(ERROR);
1838 return err;
1839 }
Andreas Huberbbbcf652010-12-07 14:25:54 -08001840 info->mStatus = OWNED_BY_NATIVE_WINDOW;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001841 return OK;
1842}
1843
1844OMXCodec::BufferInfo* OMXCodec::dequeueBufferFromNativeWindow() {
1845 // Dequeue the next buffer from the native window.
1846 android_native_buffer_t* buf;
1847 int err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf);
1848 if (err != 0) {
1849 CODEC_LOGE("dequeueBuffer failed w/ error 0x%08x", err);
1850
1851 setState(ERROR);
1852 return 0;
1853 }
1854
1855 // Determine which buffer we just dequeued.
1856 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1857 BufferInfo *bufInfo = 0;
1858 for (size_t i = 0; i < buffers->size(); i++) {
1859 sp<GraphicBuffer> graphicBuffer = buffers->itemAt(i).
1860 mMediaBuffer->graphicBuffer();
1861 if (graphicBuffer->handle == buf->handle) {
1862 bufInfo = &buffers->editItemAt(i);
1863 break;
1864 }
1865 }
1866
1867 if (bufInfo == 0) {
1868 CODEC_LOGE("dequeued unrecognized buffer: %p", buf);
1869
1870 setState(ERROR);
1871 return 0;
1872 }
1873
1874 // The native window no longer owns the buffer.
Andreas Huberbbbcf652010-12-07 14:25:54 -08001875 CHECK_EQ((int)bufInfo->mStatus, (int)OWNED_BY_NATIVE_WINDOW);
1876 bufInfo->mStatus = OWNED_BY_US;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001877
1878 return bufInfo;
1879}
1880
Andreas Huberbe06d262009-08-14 14:37:10 -07001881void OMXCodec::on_message(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001882 switch (msg.type) {
1883 case omx_message::EVENT:
1884 {
1885 onEvent(
1886 msg.u.event_data.event, msg.u.event_data.data1,
1887 msg.u.event_data.data2);
1888
1889 break;
1890 }
1891
1892 case omx_message::EMPTY_BUFFER_DONE:
1893 {
1894 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1895
Andreas Huber4c483422009-09-02 16:05:36 -07001896 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001897
1898 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1899 size_t i = 0;
1900 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1901 ++i;
1902 }
1903
1904 CHECK(i < buffers->size());
Andreas Huberbbbcf652010-12-07 14:25:54 -08001905 if ((*buffers)[i].mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001906 LOGW("We already own input buffer %p, yet received "
1907 "an EMPTY_BUFFER_DONE.", buffer);
1908 }
1909
James Dong05c2fd52010-11-02 13:20:11 -07001910 BufferInfo* info = &buffers->editItemAt(i);
Andreas Huberbbbcf652010-12-07 14:25:54 -08001911 info->mStatus = OWNED_BY_US;
James Dong05c2fd52010-11-02 13:20:11 -07001912
1913 // Buffer could not be released until empty buffer done is called.
1914 if (info->mMediaBuffer != NULL) {
James Dong31b9375f2010-11-10 21:11:41 -08001915 if (mIsEncoder &&
1916 (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
1917 // If zero-copy mode is enabled this will send the
1918 // input buffer back to the upstream source.
1919 restorePatchedDataPointer(info);
1920 }
1921
James Dong05c2fd52010-11-02 13:20:11 -07001922 info->mMediaBuffer->release();
1923 info->mMediaBuffer = NULL;
1924 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001925
1926 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001927 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001928
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07001929 status_t err = freeBuffer(kPortIndexInput, i);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001930 CHECK_EQ(err, (status_t)OK);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001931 } else if (mState != ERROR
1932 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001933 CHECK_EQ((int)mPortStatus[kPortIndexInput], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07001934 drainInputBuffer(&buffers->editItemAt(i));
1935 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001936 break;
1937 }
1938
1939 case omx_message::FILL_BUFFER_DONE:
1940 {
1941 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1942 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1943
Andreas Huber2ea14e22009-12-16 09:30:55 -08001944 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001945 buffer,
1946 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001947 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001948 msg.u.extended_buffer_data.timestamp,
1949 msg.u.extended_buffer_data.timestamp / 1E6);
1950
1951 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1952 size_t i = 0;
1953 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1954 ++i;
1955 }
1956
1957 CHECK(i < buffers->size());
1958 BufferInfo *info = &buffers->editItemAt(i);
1959
Andreas Huberbbbcf652010-12-07 14:25:54 -08001960 if (info->mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001961 LOGW("We already own output buffer %p, yet received "
1962 "a FILL_BUFFER_DONE.", buffer);
1963 }
1964
Andreas Huberbbbcf652010-12-07 14:25:54 -08001965 info->mStatus = OWNED_BY_US;
Andreas Huberbe06d262009-08-14 14:37:10 -07001966
1967 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001968 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001969
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07001970 status_t err = freeBuffer(kPortIndexOutput, i);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001971 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001972
Andreas Huber2ea14e22009-12-16 09:30:55 -08001973#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001974 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1975 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001976 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001977 mNoMoreOutputData = true;
1978 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001979#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001980 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001981 CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001982
Andreas Huber52733b82010-01-25 10:41:35 -08001983 if (info->mMediaBuffer == NULL) {
1984 CHECK(mOMXLivesLocally);
1985 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1986 CHECK(mQuirks & kDefersOutputBufferAllocation);
1987
1988 // The qcom video decoders on Nexus don't actually allocate
1989 // output buffer memory on a call to OMX_AllocateBuffer
1990 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1991 // structure is only filled in later.
1992
1993 info->mMediaBuffer = new MediaBuffer(
1994 msg.u.extended_buffer_data.data_ptr,
1995 info->mSize);
1996 info->mMediaBuffer->setObserver(this);
1997 }
1998
Andreas Huberbe06d262009-08-14 14:37:10 -07001999 MediaBuffer *buffer = info->mMediaBuffer;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002000 bool isGraphicBuffer = buffer->graphicBuffer() != NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -07002001
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002002 if (!isGraphicBuffer
2003 && msg.u.extended_buffer_data.range_offset
Andreas Huberf88f8442010-08-10 11:18:36 -07002004 + msg.u.extended_buffer_data.range_length
2005 > buffer->size()) {
2006 CODEC_LOGE(
2007 "Codec lied about its buffer size requirements, "
2008 "sending a buffer larger than the originally "
2009 "advertised size in FILL_BUFFER_DONE!");
2010 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002011 buffer->set_range(
2012 msg.u.extended_buffer_data.range_offset,
2013 msg.u.extended_buffer_data.range_length);
2014
2015 buffer->meta_data()->clear();
2016
Andreas Huberfa8de752009-10-08 10:07:49 -07002017 buffer->meta_data()->setInt64(
2018 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07002019
2020 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
2021 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
2022 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08002023 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
2024 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
2025 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002026
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002027 if (isGraphicBuffer || mQuirks & kOutputBuffersAreUnreadable) {
Andreas Huber1e194162010-10-06 16:43:57 -07002028 buffer->meta_data()->setInt32(kKeyIsUnreadable, true);
2029 }
2030
Andreas Huberbe06d262009-08-14 14:37:10 -07002031 buffer->meta_data()->setPointer(
2032 kKeyPlatformPrivate,
2033 msg.u.extended_buffer_data.platform_private);
2034
2035 buffer->meta_data()->setPointer(
2036 kKeyBufferID,
2037 msg.u.extended_buffer_data.buffer);
2038
Andreas Huber2ea14e22009-12-16 09:30:55 -08002039 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
2040 CODEC_LOGV("No more output data.");
2041 mNoMoreOutputData = true;
2042 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002043
2044 if (mTargetTimeUs >= 0) {
2045 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
2046
2047 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
2048 CODEC_LOGV(
2049 "skipping output buffer at timestamp %lld us",
2050 msg.u.extended_buffer_data.timestamp);
2051
2052 fillOutputBuffer(info);
2053 break;
2054 }
2055
2056 CODEC_LOGV(
2057 "returning output buffer at target timestamp "
2058 "%lld us",
2059 msg.u.extended_buffer_data.timestamp);
2060
2061 mTargetTimeUs = -1;
2062 }
2063
2064 mFilledBuffers.push_back(i);
2065 mBufferFilled.signal();
James Dongfc8b7c92010-12-07 14:37:27 -08002066 if (mIsEncoder) {
2067 sched_yield();
2068 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002069 }
2070
2071 break;
2072 }
2073
2074 default:
2075 {
2076 CHECK(!"should not be here.");
2077 break;
2078 }
2079 }
2080}
2081
Andreas Huberb1678602009-10-19 13:06:40 -07002082// Has the format changed in any way that the client would have to be aware of?
2083static bool formatHasNotablyChanged(
2084 const sp<MetaData> &from, const sp<MetaData> &to) {
2085 if (from.get() == NULL && to.get() == NULL) {
2086 return false;
2087 }
2088
Andreas Huberf68c1682009-10-21 14:01:30 -07002089 if ((from.get() == NULL && to.get() != NULL)
2090 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07002091 return true;
2092 }
2093
2094 const char *mime_from, *mime_to;
2095 CHECK(from->findCString(kKeyMIMEType, &mime_from));
2096 CHECK(to->findCString(kKeyMIMEType, &mime_to));
2097
2098 if (strcasecmp(mime_from, mime_to)) {
2099 return true;
2100 }
2101
2102 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
2103 int32_t colorFormat_from, colorFormat_to;
2104 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
2105 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
2106
2107 if (colorFormat_from != colorFormat_to) {
2108 return true;
2109 }
2110
2111 int32_t width_from, width_to;
2112 CHECK(from->findInt32(kKeyWidth, &width_from));
2113 CHECK(to->findInt32(kKeyWidth, &width_to));
2114
2115 if (width_from != width_to) {
2116 return true;
2117 }
2118
2119 int32_t height_from, height_to;
2120 CHECK(from->findInt32(kKeyHeight, &height_from));
2121 CHECK(to->findInt32(kKeyHeight, &height_to));
2122
2123 if (height_from != height_to) {
2124 return true;
2125 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002126
2127 int32_t left_from, top_from, right_from, bottom_from;
2128 CHECK(from->findRect(
2129 kKeyCropRect,
2130 &left_from, &top_from, &right_from, &bottom_from));
2131
2132 int32_t left_to, top_to, right_to, bottom_to;
2133 CHECK(to->findRect(
2134 kKeyCropRect,
2135 &left_to, &top_to, &right_to, &bottom_to));
2136
2137 if (left_to != left_from || top_to != top_from
2138 || right_to != right_from || bottom_to != bottom_from) {
2139 return true;
2140 }
Andreas Huberb1678602009-10-19 13:06:40 -07002141 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
2142 int32_t numChannels_from, numChannels_to;
2143 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
2144 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
2145
2146 if (numChannels_from != numChannels_to) {
2147 return true;
2148 }
2149
2150 int32_t sampleRate_from, sampleRate_to;
2151 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
2152 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
2153
2154 if (sampleRate_from != sampleRate_to) {
2155 return true;
2156 }
2157 }
2158
2159 return false;
2160}
2161
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002162void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
2163 switch (event) {
2164 case OMX_EventCmdComplete:
2165 {
2166 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
2167 break;
2168 }
2169
2170 case OMX_EventError:
2171 {
2172 CODEC_LOGE("ERROR(0x%08lx, %ld)", data1, data2);
2173
2174 setState(ERROR);
2175 break;
2176 }
2177
2178 case OMX_EventPortSettingsChanged:
2179 {
2180 CODEC_LOGV("OMX_EventPortSettingsChanged(port=%ld, data2=0x%08lx)",
2181 data1, data2);
2182
2183 if (data2 == 0 || data2 == OMX_IndexParamPortDefinition) {
2184 onPortSettingsChanged(data1);
2185 } else if (data1 == kPortIndexOutput
2186 && data2 == OMX_IndexConfigCommonOutputCrop) {
2187
2188 sp<MetaData> oldOutputFormat = mOutputFormat;
2189 initOutputFormat(mSource->getFormat());
2190
2191 if (formatHasNotablyChanged(oldOutputFormat, mOutputFormat)) {
2192 mOutputPortSettingsHaveChanged = true;
2193
2194 if (mNativeWindow != NULL) {
2195 int32_t left, top, right, bottom;
2196 CHECK(mOutputFormat->findRect(
2197 kKeyCropRect,
2198 &left, &top, &right, &bottom));
2199
2200 android_native_rect_t crop;
2201 crop.left = left;
2202 crop.top = top;
2203 crop.right = right;
2204 crop.bottom = bottom;
2205
Andreas Huber00d6c722011-01-26 12:02:35 -08002206 // We'll ignore any errors here, if the surface is
2207 // already invalid, we'll know soon enough.
2208 native_window_set_crop(mNativeWindow.get(), &crop);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002209 }
2210 }
2211 }
2212 break;
2213 }
2214
2215#if 0
2216 case OMX_EventBufferFlag:
2217 {
2218 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
2219
2220 if (data1 == kPortIndexOutput) {
2221 mNoMoreOutputData = true;
2222 }
2223 break;
2224 }
2225#endif
2226
2227 default:
2228 {
2229 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
2230 break;
2231 }
2232 }
2233}
2234
Andreas Huberbe06d262009-08-14 14:37:10 -07002235void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
2236 switch (cmd) {
2237 case OMX_CommandStateSet:
2238 {
2239 onStateChange((OMX_STATETYPE)data);
2240 break;
2241 }
2242
2243 case OMX_CommandPortDisable:
2244 {
2245 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07002246 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002247
2248 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002249 CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLING);
2250 CHECK_EQ(mPortBuffers[portIndex].size(), 0u);
Andreas Huberbe06d262009-08-14 14:37:10 -07002251
2252 mPortStatus[portIndex] = DISABLED;
2253
2254 if (mState == RECONFIGURING) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002255 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002256
Andreas Huberb1678602009-10-19 13:06:40 -07002257 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07002258 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07002259
2260 // Don't notify clients if the output port settings change
2261 // wasn't of importance to them, i.e. it may be that just the
2262 // number of buffers has changed and nothing else.
2263 mOutputPortSettingsHaveChanged =
2264 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07002265
Andreas Huberbe06d262009-08-14 14:37:10 -07002266 enablePortAsync(portIndex);
2267
2268 status_t err = allocateBuffersOnPort(portIndex);
Andreas Huberba1b16792011-01-19 09:20:58 -08002269
2270 if (err != OK) {
2271 CODEC_LOGE("allocateBuffersOnPort failed (err = %d)", err);
2272 setState(ERROR);
2273 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002274 }
2275 break;
2276 }
2277
2278 case OMX_CommandPortEnable:
2279 {
2280 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07002281 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002282
2283 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002284 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002285
2286 mPortStatus[portIndex] = ENABLED;
2287
2288 if (mState == RECONFIGURING) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002289 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002290
2291 setState(EXECUTING);
2292
2293 fillOutputBuffers();
2294 }
2295 break;
2296 }
2297
2298 case OMX_CommandFlush:
2299 {
2300 OMX_U32 portIndex = data;
2301
Andreas Huber4c483422009-09-02 16:05:36 -07002302 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002303
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002304 CHECK_EQ((int)mPortStatus[portIndex], (int)SHUTTING_DOWN);
Andreas Huberbe06d262009-08-14 14:37:10 -07002305 mPortStatus[portIndex] = ENABLED;
2306
2307 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
2308 mPortBuffers[portIndex].size());
2309
2310 if (mState == RECONFIGURING) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002311 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002312
2313 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002314 } else if (mState == EXECUTING_TO_IDLE) {
2315 if (mPortStatus[kPortIndexInput] == ENABLED
2316 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002317 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002318 "transition from EXECUTING to IDLE.");
2319
2320 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2321 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2322
2323 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002324 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002325 CHECK_EQ(err, (status_t)OK);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002326 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002327 } else {
2328 // We're flushing both ports in preparation for seeking.
2329
2330 if (mPortStatus[kPortIndexInput] == ENABLED
2331 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002332 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07002333 " seek-time.");
2334
Andreas Huber1f24b302010-06-10 11:12:39 -07002335 // We implicitly resume pulling on our upstream source.
2336 mPaused = false;
2337
Andreas Huberbe06d262009-08-14 14:37:10 -07002338 drainInputBuffers();
2339 fillOutputBuffers();
2340 }
2341 }
2342
2343 break;
2344 }
2345
2346 default:
2347 {
Andreas Huber4c483422009-09-02 16:05:36 -07002348 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07002349 break;
2350 }
2351 }
2352}
2353
2354void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002355 CODEC_LOGV("onStateChange %d", newState);
2356
Andreas Huberbe06d262009-08-14 14:37:10 -07002357 switch (newState) {
2358 case OMX_StateIdle:
2359 {
Andreas Huber4c483422009-09-02 16:05:36 -07002360 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002361 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07002362 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002363 mNode, OMX_CommandStateSet, OMX_StateExecuting);
2364
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002365 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002366
2367 setState(IDLE_TO_EXECUTING);
2368 } else {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002369 CHECK_EQ((int)mState, (int)EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002370
2371 CHECK_EQ(
2372 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
2373 mPortBuffers[kPortIndexInput].size());
2374
2375 CHECK_EQ(
2376 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
2377 mPortBuffers[kPortIndexOutput].size());
2378
Andreas Huber784202e2009-10-15 13:46:54 -07002379 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002380 mNode, OMX_CommandStateSet, OMX_StateLoaded);
2381
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002382 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002383
2384 err = freeBuffersOnPort(kPortIndexInput);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002385 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002386
2387 err = freeBuffersOnPort(kPortIndexOutput);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002388 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002389
2390 mPortStatus[kPortIndexInput] = ENABLED;
2391 mPortStatus[kPortIndexOutput] = ENABLED;
2392
2393 setState(IDLE_TO_LOADED);
2394 }
2395 break;
2396 }
2397
2398 case OMX_StateExecuting:
2399 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002400 CHECK_EQ((int)mState, (int)IDLE_TO_EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002401
Andreas Huber4c483422009-09-02 16:05:36 -07002402 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002403
2404 setState(EXECUTING);
2405
Andreas Huber42978e52009-08-27 10:08:39 -07002406 // Buffers will be submitted to the component in the first
2407 // call to OMXCodec::read as mInitialBufferSubmit is true at
2408 // this point. This ensures that this on_message call returns,
2409 // releases the lock and ::init can notice the state change and
2410 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07002411 break;
2412 }
2413
2414 case OMX_StateLoaded:
2415 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002416 CHECK_EQ((int)mState, (int)IDLE_TO_LOADED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002417
Andreas Huber4c483422009-09-02 16:05:36 -07002418 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002419
2420 setState(LOADED);
2421 break;
2422 }
2423
Andreas Huberc712b9f2010-01-20 15:05:46 -08002424 case OMX_StateInvalid:
2425 {
2426 setState(ERROR);
2427 break;
2428 }
2429
Andreas Huberbe06d262009-08-14 14:37:10 -07002430 default:
2431 {
2432 CHECK(!"should not be here.");
2433 break;
2434 }
2435 }
2436}
2437
2438// static
2439size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
2440 size_t n = 0;
2441 for (size_t i = 0; i < buffers.size(); ++i) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002442 if (buffers[i].mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002443 ++n;
2444 }
2445 }
2446
2447 return n;
2448}
2449
2450status_t OMXCodec::freeBuffersOnPort(
2451 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2452 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2453
2454 status_t stickyErr = OK;
2455
2456 for (size_t i = buffers->size(); i-- > 0;) {
2457 BufferInfo *info = &buffers->editItemAt(i);
2458
Andreas Huberbbbcf652010-12-07 14:25:54 -08002459 if (onlyThoseWeOwn && info->mStatus == OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002460 continue;
2461 }
2462
Andreas Huberbbbcf652010-12-07 14:25:54 -08002463 CHECK(info->mStatus == OWNED_BY_US
2464 || info->mStatus == OWNED_BY_NATIVE_WINDOW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002465
Andreas Huber92022852009-09-14 15:24:14 -07002466 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2467
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002468 status_t err = freeBuffer(portIndex, i);
Andreas Huberbe06d262009-08-14 14:37:10 -07002469
2470 if (err != OK) {
2471 stickyErr = err;
2472 }
2473
Andreas Huberbe06d262009-08-14 14:37:10 -07002474 }
2475
2476 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2477
2478 return stickyErr;
2479}
2480
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002481status_t OMXCodec::freeBuffer(OMX_U32 portIndex, size_t bufIndex) {
2482 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2483
2484 BufferInfo *info = &buffers->editItemAt(bufIndex);
2485
2486 status_t err = mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
2487
2488 if (err == OK && info->mMediaBuffer != NULL) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002489 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002490 info->mMediaBuffer->setObserver(NULL);
2491
2492 // Make sure nobody but us owns this buffer at this point.
2493 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2494
2495 // Cancel the buffer if it belongs to an ANativeWindow.
2496 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer();
Andreas Huberbbbcf652010-12-07 14:25:54 -08002497 if (info->mStatus == OWNED_BY_US && graphicBuffer != 0) {
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002498 err = cancelBufferToNativeWindow(info);
2499 }
2500
2501 info->mMediaBuffer->release();
James Dong31b9375f2010-11-10 21:11:41 -08002502 info->mMediaBuffer = NULL;
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002503 }
2504
2505 if (err == OK) {
2506 buffers->removeAt(bufIndex);
2507 }
2508
2509 return err;
2510}
2511
Andreas Huberbe06d262009-08-14 14:37:10 -07002512void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002513 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002514
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002515 CHECK_EQ((int)mState, (int)EXECUTING);
2516 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002517 setState(RECONFIGURING);
2518
2519 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002520 if (!flushPortAsync(portIndex)) {
2521 onCmdComplete(OMX_CommandFlush, portIndex);
2522 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002523 } else {
2524 disablePortAsync(portIndex);
2525 }
2526}
2527
Andreas Huber404cc412009-08-25 14:26:05 -07002528bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002529 CHECK(mState == EXECUTING || mState == RECONFIGURING
2530 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002531
Andreas Huber4c483422009-09-02 16:05:36 -07002532 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002533 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2534 mPortBuffers[portIndex].size());
2535
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002536 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002537 mPortStatus[portIndex] = SHUTTING_DOWN;
2538
Andreas Huber404cc412009-08-25 14:26:05 -07002539 if ((mQuirks & kRequiresFlushCompleteEmulation)
2540 && countBuffersWeOwn(mPortBuffers[portIndex])
2541 == mPortBuffers[portIndex].size()) {
2542 // No flush is necessary and this component fails to send a
2543 // flush-complete event in this case.
2544
2545 return false;
2546 }
2547
Andreas Huberbe06d262009-08-14 14:37:10 -07002548 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002549 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002550 CHECK_EQ(err, (status_t)OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002551
2552 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002553}
2554
2555void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2556 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2557
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002558 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002559 mPortStatus[portIndex] = DISABLING;
2560
Andreas Huberd222c842010-08-26 14:29:34 -07002561 CODEC_LOGV("sending OMX_CommandPortDisable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002562 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002563 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002564 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002565
2566 freeBuffersOnPort(portIndex, true);
2567}
2568
2569void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2570 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2571
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002572 CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002573 mPortStatus[portIndex] = ENABLING;
2574
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002575 CODEC_LOGV("sending OMX_CommandPortEnable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002576 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002577 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002578 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002579}
2580
2581void OMXCodec::fillOutputBuffers() {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002582 CHECK_EQ((int)mState, (int)EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002583
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002584 // This is a workaround for some decoders not properly reporting
2585 // end-of-output-stream. If we own all input buffers and also own
2586 // all output buffers and we already signalled end-of-input-stream,
2587 // the end-of-output-stream is implied.
2588 if (mSignalledEOS
2589 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2590 == mPortBuffers[kPortIndexInput].size()
2591 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2592 == mPortBuffers[kPortIndexOutput].size()) {
2593 mNoMoreOutputData = true;
2594 mBufferFilled.signal();
2595
2596 return;
2597 }
2598
Andreas Huberbe06d262009-08-14 14:37:10 -07002599 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2600 for (size_t i = 0; i < buffers->size(); ++i) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002601 BufferInfo *info = &buffers->editItemAt(i);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002602 if (info->mStatus == OWNED_BY_US) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002603 fillOutputBuffer(&buffers->editItemAt(i));
2604 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002605 }
2606}
2607
2608void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002609 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002610
2611 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2612 for (size_t i = 0; i < buffers->size(); ++i) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002613 if (!drainInputBuffer(&buffers->editItemAt(i))) {
2614 break;
2615 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002616 }
2617}
2618
Andreas Huberbbbcf652010-12-07 14:25:54 -08002619bool OMXCodec::drainInputBuffer(BufferInfo *info) {
2620 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Andreas Huberbe06d262009-08-14 14:37:10 -07002621
2622 if (mSignalledEOS) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002623 return false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002624 }
2625
2626 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2627 const CodecSpecificData *specific =
2628 mCodecSpecificData[mCodecSpecificDataIndex];
2629
2630 size_t size = specific->mSize;
2631
Andreas Hubere6c40962009-09-10 14:13:30 -07002632 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002633 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002634 static const uint8_t kNALStartCode[4] =
2635 { 0x00, 0x00, 0x00, 0x01 };
2636
Andreas Huberc712b9f2010-01-20 15:05:46 -08002637 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002638
2639 size += 4;
2640
Andreas Huberc712b9f2010-01-20 15:05:46 -08002641 memcpy(info->mData, kNALStartCode, 4);
2642 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002643 specific->mData, specific->mSize);
2644 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002645 CHECK(info->mSize >= specific->mSize);
2646 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002647 }
2648
Andreas Huber2ea14e22009-12-16 09:30:55 -08002649 mNoMoreOutputData = false;
2650
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002651 CODEC_LOGV("calling emptyBuffer with codec specific data");
2652
Andreas Huber784202e2009-10-15 13:46:54 -07002653 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002654 mNode, info->mBuffer, 0, size,
2655 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2656 0);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002657 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002658
Andreas Huberbbbcf652010-12-07 14:25:54 -08002659 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberbe06d262009-08-14 14:37:10 -07002660
2661 ++mCodecSpecificDataIndex;
Andreas Huberbbbcf652010-12-07 14:25:54 -08002662 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002663 }
2664
Andreas Huber1f24b302010-06-10 11:12:39 -07002665 if (mPaused) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002666 return false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002667 }
2668
Andreas Huberbe06d262009-08-14 14:37:10 -07002669 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002670
Andreas Hubera4357ad2010-04-02 12:49:54 -07002671 bool signalEOS = false;
2672 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002673
Andreas Hubera4357ad2010-04-02 12:49:54 -07002674 size_t offset = 0;
2675 int32_t n = 0;
Andreas Huberbbbcf652010-12-07 14:25:54 -08002676
Andreas Hubera4357ad2010-04-02 12:49:54 -07002677 for (;;) {
2678 MediaBuffer *srcBuffer;
2679 if (mSeekTimeUs >= 0) {
2680 if (mLeftOverBuffer) {
2681 mLeftOverBuffer->release();
2682 mLeftOverBuffer = NULL;
2683 }
James Dong2144f632010-12-11 10:43:41 -08002684
2685 MediaSource::ReadOptions options;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002686 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002687
2688 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002689 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002690 mBufferFilled.signal();
2691
2692 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002693
2694 if (err == OK) {
2695 int64_t targetTimeUs;
2696 if (srcBuffer->meta_data()->findInt64(
2697 kKeyTargetTime, &targetTimeUs)
2698 && targetTimeUs >= 0) {
2699 mTargetTimeUs = targetTimeUs;
2700 } else {
2701 mTargetTimeUs = -1;
2702 }
2703 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002704 } else if (mLeftOverBuffer) {
2705 srcBuffer = mLeftOverBuffer;
2706 mLeftOverBuffer = NULL;
2707
2708 err = OK;
2709 } else {
James Dong2144f632010-12-11 10:43:41 -08002710 err = mSource->read(&srcBuffer);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002711 }
2712
2713 if (err != OK) {
2714 signalEOS = true;
2715 mFinalStatus = err;
2716 mSignalledEOS = true;
Andreas Huber94bced12010-12-14 09:50:46 -08002717 mBufferFilled.signal();
Andreas Hubera4357ad2010-04-02 12:49:54 -07002718 break;
2719 }
2720
2721 size_t remainingBytes = info->mSize - offset;
2722
2723 if (srcBuffer->range_length() > remainingBytes) {
2724 if (offset == 0) {
2725 CODEC_LOGE(
2726 "Codec's input buffers are too small to accomodate "
2727 "buffer read from source (info->mSize = %d, srcLength = %d)",
2728 info->mSize, srcBuffer->range_length());
2729
2730 srcBuffer->release();
2731 srcBuffer = NULL;
2732
2733 setState(ERROR);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002734 return false;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002735 }
2736
2737 mLeftOverBuffer = srcBuffer;
2738 break;
2739 }
2740
James Dong05c2fd52010-11-02 13:20:11 -07002741 bool releaseBuffer = true;
James Dong4f501f02010-06-07 14:41:41 -07002742 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2743 CHECK(mOMXLivesLocally && offset == 0);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002744
2745 OMX_BUFFERHEADERTYPE *header =
2746 (OMX_BUFFERHEADERTYPE *)info->mBuffer;
2747
James Dong31b9375f2010-11-10 21:11:41 -08002748 CHECK(header->pBuffer == info->mData);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002749
2750 header->pBuffer =
2751 (OMX_U8 *)srcBuffer->data() + srcBuffer->range_offset();
2752
James Dong05c2fd52010-11-02 13:20:11 -07002753 releaseBuffer = false;
2754 info->mMediaBuffer = srcBuffer;
James Dong4f501f02010-06-07 14:41:41 -07002755 } else {
James Dong05c2fd52010-11-02 13:20:11 -07002756 if (mIsMetaDataStoredInVideoBuffers) {
2757 releaseBuffer = false;
2758 info->mMediaBuffer = srcBuffer;
2759 }
James Dong4f501f02010-06-07 14:41:41 -07002760 memcpy((uint8_t *)info->mData + offset,
Andreas Huberbbbcf652010-12-07 14:25:54 -08002761 (const uint8_t *)srcBuffer->data()
2762 + srcBuffer->range_offset(),
James Dong4f501f02010-06-07 14:41:41 -07002763 srcBuffer->range_length());
2764 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002765
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002766 int64_t lastBufferTimeUs;
2767 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002768 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002769
Andreas Hubera4357ad2010-04-02 12:49:54 -07002770 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002771 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002772 }
2773
2774 offset += srcBuffer->range_length();
2775
James Dong05c2fd52010-11-02 13:20:11 -07002776 if (releaseBuffer) {
2777 srcBuffer->release();
2778 srcBuffer = NULL;
2779 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002780
2781 ++n;
2782
2783 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2784 break;
2785 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002786
2787 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2788
2789 if (coalescedDurationUs > 250000ll) {
2790 // Don't coalesce more than 250ms worth of encoded data at once.
2791 break;
2792 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002793 }
2794
2795 if (n > 1) {
2796 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002797 }
2798
2799 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002800
Andreas Hubera4357ad2010-04-02 12:49:54 -07002801 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002802 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002803 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002804 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002805 }
2806
Andreas Hubera4357ad2010-04-02 12:49:54 -07002807 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2808 "timestamp %lld us (%.2f secs)",
2809 info->mBuffer, offset,
2810 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002811
Andreas Huber784202e2009-10-15 13:46:54 -07002812 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002813 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002814 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002815
2816 if (err != OK) {
2817 setState(ERROR);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002818 return false;
Andreas Huber3f427072009-10-08 11:02:27 -07002819 }
2820
Andreas Huberbbbcf652010-12-07 14:25:54 -08002821 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002822
2823 // This component does not ever signal the EOS flag on output buffers,
2824 // Thanks for nothing.
2825 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2826 mNoMoreOutputData = true;
2827 mBufferFilled.signal();
2828 }
Andreas Huberbbbcf652010-12-07 14:25:54 -08002829
2830 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002831}
2832
2833void OMXCodec::fillOutputBuffer(BufferInfo *info) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002834 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Andreas Huberbe06d262009-08-14 14:37:10 -07002835
Andreas Huber404cc412009-08-25 14:26:05 -07002836 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002837 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002838 "calling fillOutputBuffer");
2839 return;
2840 }
2841
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002842 if (info->mMediaBuffer != NULL) {
2843 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer();
2844 if (graphicBuffer != 0) {
2845 // When using a native buffer we need to lock the buffer before
2846 // giving it to OMX.
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002847 CODEC_LOGV("Calling lockBuffer on %p", info->mBuffer);
2848 int err = mNativeWindow->lockBuffer(mNativeWindow.get(),
2849 graphicBuffer.get());
2850 if (err != 0) {
2851 CODEC_LOGE("lockBuffer failed w/ error 0x%08x", err);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002852
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002853 setState(ERROR);
2854 return;
2855 }
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002856 }
2857 }
2858
2859 CODEC_LOGV("Calling fillBuffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002860 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002861
2862 if (err != OK) {
2863 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2864
2865 setState(ERROR);
2866 return;
2867 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002868
Andreas Huberbbbcf652010-12-07 14:25:54 -08002869 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberbe06d262009-08-14 14:37:10 -07002870}
2871
Andreas Huberbbbcf652010-12-07 14:25:54 -08002872bool OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002873 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2874 for (size_t i = 0; i < buffers->size(); ++i) {
2875 if ((*buffers)[i].mBuffer == buffer) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002876 return drainInputBuffer(&buffers->editItemAt(i));
Andreas Huberbe06d262009-08-14 14:37:10 -07002877 }
2878 }
2879
2880 CHECK(!"should not be here.");
Andreas Huberbbbcf652010-12-07 14:25:54 -08002881
2882 return false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002883}
2884
2885void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2886 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2887 for (size_t i = 0; i < buffers->size(); ++i) {
2888 if ((*buffers)[i].mBuffer == buffer) {
2889 fillOutputBuffer(&buffers->editItemAt(i));
2890 return;
2891 }
2892 }
2893
2894 CHECK(!"should not be here.");
2895}
2896
2897void OMXCodec::setState(State newState) {
2898 mState = newState;
2899 mAsyncCompletion.signal();
2900
2901 // This may cause some spurious wakeups but is necessary to
2902 // unblock the reader if we enter ERROR state.
2903 mBufferFilled.signal();
2904}
2905
Andreas Huberda050cf22009-09-02 14:01:43 -07002906void OMXCodec::setRawAudioFormat(
2907 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002908
2909 // port definition
2910 OMX_PARAM_PORTDEFINITIONTYPE def;
2911 InitOMXParams(&def);
2912 def.nPortIndex = portIndex;
2913 status_t err = mOMX->getParameter(
2914 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002915 CHECK_EQ(err, (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07002916 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2917 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002918 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07002919
2920 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002921 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002922 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002923 pcmParams.nPortIndex = portIndex;
2924
James Dongabed93a2010-04-22 17:27:04 -07002925 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002926 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2927
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002928 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07002929
2930 pcmParams.nChannels = numChannels;
2931 pcmParams.eNumData = OMX_NumericalDataSigned;
2932 pcmParams.bInterleaved = OMX_TRUE;
2933 pcmParams.nBitPerSample = 16;
2934 pcmParams.nSamplingRate = sampleRate;
2935 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2936
2937 if (numChannels == 1) {
2938 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2939 } else {
2940 CHECK_EQ(numChannels, 2);
2941
2942 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2943 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2944 }
2945
Andreas Huber784202e2009-10-15 13:46:54 -07002946 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002947 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2948
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002949 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07002950}
2951
James Dong17299ab2010-05-14 15:45:22 -07002952static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2953 if (isAMRWB) {
2954 if (bps <= 6600) {
2955 return OMX_AUDIO_AMRBandModeWB0;
2956 } else if (bps <= 8850) {
2957 return OMX_AUDIO_AMRBandModeWB1;
2958 } else if (bps <= 12650) {
2959 return OMX_AUDIO_AMRBandModeWB2;
2960 } else if (bps <= 14250) {
2961 return OMX_AUDIO_AMRBandModeWB3;
2962 } else if (bps <= 15850) {
2963 return OMX_AUDIO_AMRBandModeWB4;
2964 } else if (bps <= 18250) {
2965 return OMX_AUDIO_AMRBandModeWB5;
2966 } else if (bps <= 19850) {
2967 return OMX_AUDIO_AMRBandModeWB6;
2968 } else if (bps <= 23050) {
2969 return OMX_AUDIO_AMRBandModeWB7;
2970 }
2971
2972 // 23850 bps
2973 return OMX_AUDIO_AMRBandModeWB8;
2974 } else { // AMRNB
2975 if (bps <= 4750) {
2976 return OMX_AUDIO_AMRBandModeNB0;
2977 } else if (bps <= 5150) {
2978 return OMX_AUDIO_AMRBandModeNB1;
2979 } else if (bps <= 5900) {
2980 return OMX_AUDIO_AMRBandModeNB2;
2981 } else if (bps <= 6700) {
2982 return OMX_AUDIO_AMRBandModeNB3;
2983 } else if (bps <= 7400) {
2984 return OMX_AUDIO_AMRBandModeNB4;
2985 } else if (bps <= 7950) {
2986 return OMX_AUDIO_AMRBandModeNB5;
2987 } else if (bps <= 10200) {
2988 return OMX_AUDIO_AMRBandModeNB6;
2989 }
2990
2991 // 12200 bps
2992 return OMX_AUDIO_AMRBandModeNB7;
2993 }
2994}
2995
2996void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002997 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002998
Andreas Huber8768f2c2009-12-01 15:26:54 -08002999 OMX_AUDIO_PARAM_AMRTYPE def;
3000 InitOMXParams(&def);
3001 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07003002
Andreas Huber8768f2c2009-12-01 15:26:54 -08003003 status_t err =
3004 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07003005
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003006 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003007
Andreas Huber8768f2c2009-12-01 15:26:54 -08003008 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07003009
James Dong17299ab2010-05-14 15:45:22 -07003010 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08003011 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003012 CHECK_EQ(err, (status_t)OK);
Andreas Huberee606e62009-09-08 10:19:21 -07003013
3014 ////////////////////////
3015
3016 if (mIsEncoder) {
3017 sp<MetaData> format = mSource->getFormat();
3018 int32_t sampleRate;
3019 int32_t numChannels;
3020 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
3021 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
3022
3023 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
3024 }
3025}
3026
James Dong17299ab2010-05-14 15:45:22 -07003027void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07003028 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07003029 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07003030 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07003031 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07003032
3033 //////////////// output port ////////////////////
3034 // format
3035 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
3036 format.nPortIndex = kPortIndexOutput;
3037 format.nIndex = 0;
3038 status_t err = OMX_ErrorNone;
3039 while (OMX_ErrorNone == err) {
3040 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003041 &format, sizeof(format)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003042 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
3043 break;
3044 }
3045 format.nIndex++;
3046 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003047 CHECK_EQ((status_t)OK, err);
James Dongabed93a2010-04-22 17:27:04 -07003048 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003049 &format, sizeof(format)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003050
3051 // port definition
3052 OMX_PARAM_PORTDEFINITIONTYPE def;
3053 InitOMXParams(&def);
3054 def.nPortIndex = kPortIndexOutput;
3055 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003056 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003057 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
3058 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
3059 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003060 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003061
3062 // profile
3063 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
3064 InitOMXParams(&profile);
3065 profile.nPortIndex = kPortIndexOutput;
3066 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003067 &profile, sizeof(profile)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003068 profile.nChannels = numChannels;
3069 profile.eChannelMode = (numChannels == 1?
3070 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
3071 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07003072 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003073 profile.nAudioBandWidth = 0;
3074 profile.nFrameLength = 0;
3075 profile.nAACtools = OMX_AUDIO_AACToolAll;
3076 profile.nAACERtools = OMX_AUDIO_AACERNone;
3077 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
3078 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
3079 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003080 &profile, sizeof(profile)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003081
Andreas Huberda050cf22009-09-02 14:01:43 -07003082 } else {
3083 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07003084 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07003085 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003086
Andreas Huber784202e2009-10-15 13:46:54 -07003087 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003088 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003089 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003090
Andreas Huberda050cf22009-09-02 14:01:43 -07003091 profile.nChannels = numChannels;
3092 profile.nSampleRate = sampleRate;
3093 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07003094
Andreas Huber784202e2009-10-15 13:46:54 -07003095 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003096 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003097 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07003098 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003099}
3100
3101void OMXCodec::setImageOutputFormat(
3102 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07003103 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07003104
3105#if 0
3106 OMX_INDEXTYPE index;
3107 status_t err = mOMX->get_extension_index(
3108 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003109 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003110
3111 err = mOMX->set_config(mNode, index, &format, sizeof(format));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003112 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003113#endif
3114
3115 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003116 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003117 def.nPortIndex = kPortIndexOutput;
3118
Andreas Huber784202e2009-10-15 13:46:54 -07003119 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003120 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003121 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003122
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003123 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage);
Andreas Huberbe06d262009-08-14 14:37:10 -07003124
3125 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07003126
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003127 CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07003128 imageDef->eColorFormat = format;
3129 imageDef->nFrameWidth = width;
3130 imageDef->nFrameHeight = height;
3131
3132 switch (format) {
3133 case OMX_COLOR_FormatYUV420PackedPlanar:
3134 case OMX_COLOR_FormatYUV411Planar:
3135 {
3136 def.nBufferSize = (width * height * 3) / 2;
3137 break;
3138 }
3139
3140 case OMX_COLOR_FormatCbYCrY:
3141 {
3142 def.nBufferSize = width * height * 2;
3143 break;
3144 }
3145
3146 case OMX_COLOR_Format32bitARGB8888:
3147 {
3148 def.nBufferSize = width * height * 4;
3149 break;
3150 }
3151
Andreas Huber201511c2009-09-08 14:01:44 -07003152 case OMX_COLOR_Format16bitARGB4444:
3153 case OMX_COLOR_Format16bitARGB1555:
3154 case OMX_COLOR_Format16bitRGB565:
3155 case OMX_COLOR_Format16bitBGR565:
3156 {
3157 def.nBufferSize = width * height * 2;
3158 break;
3159 }
3160
Andreas Huberbe06d262009-08-14 14:37:10 -07003161 default:
3162 CHECK(!"Should not be here. Unknown color format.");
3163 break;
3164 }
3165
Andreas Huber5c0a9132009-08-20 11:16:40 -07003166 def.nBufferCountActual = def.nBufferCountMin;
3167
Andreas Huber784202e2009-10-15 13:46:54 -07003168 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003169 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003170 CHECK_EQ(err, (status_t)OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07003171}
Andreas Huberbe06d262009-08-14 14:37:10 -07003172
Andreas Huber5c0a9132009-08-20 11:16:40 -07003173void OMXCodec::setJPEGInputFormat(
3174 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
3175 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003176 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003177 def.nPortIndex = kPortIndexInput;
3178
Andreas Huber784202e2009-10-15 13:46:54 -07003179 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003180 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003181 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003182
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003183 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage);
Andreas Huber5c0a9132009-08-20 11:16:40 -07003184 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3185
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003186 CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingJPEG);
Andreas Huberbe06d262009-08-14 14:37:10 -07003187 imageDef->nFrameWidth = width;
3188 imageDef->nFrameHeight = height;
3189
Andreas Huber5c0a9132009-08-20 11:16:40 -07003190 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07003191 def.nBufferCountActual = def.nBufferCountMin;
3192
Andreas Huber784202e2009-10-15 13:46:54 -07003193 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003194 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003195 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003196}
3197
3198void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
3199 CodecSpecificData *specific =
3200 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
3201
3202 specific->mSize = size;
3203 memcpy(specific->mData, data, size);
3204
3205 mCodecSpecificData.push(specific);
3206}
3207
3208void OMXCodec::clearCodecSpecificData() {
3209 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
3210 free(mCodecSpecificData.editItemAt(i));
3211 }
3212 mCodecSpecificData.clear();
3213 mCodecSpecificDataIndex = 0;
3214}
3215
James Dong36e573b2010-06-19 09:04:18 -07003216status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07003217 Mutex::Autolock autoLock(mLock);
3218
Andreas Huberbe06d262009-08-14 14:37:10 -07003219 if (mState != LOADED) {
3220 return UNKNOWN_ERROR;
3221 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07003222
Andreas Huberbe06d262009-08-14 14:37:10 -07003223 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07003224 if (mQuirks & kWantsNALFragments) {
3225 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07003226 }
James Dong36e573b2010-06-19 09:04:18 -07003227 if (meta) {
3228 int64_t startTimeUs = 0;
3229 int64_t timeUs;
3230 if (meta->findInt64(kKeyTime, &timeUs)) {
3231 startTimeUs = timeUs;
3232 }
3233 params->setInt64(kKeyTime, startTimeUs);
3234 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003235 status_t err = mSource->start(params.get());
3236
3237 if (err != OK) {
3238 return err;
3239 }
3240
3241 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07003242 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07003243 mSignalledEOS = false;
3244 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07003245 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003246 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003247 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
3248 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07003249 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07003250 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003251
3252 return init();
3253}
3254
3255status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08003256 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07003257
3258 Mutex::Autolock autoLock(mLock);
3259
3260 while (isIntermediateState(mState)) {
3261 mAsyncCompletion.wait(mLock);
3262 }
3263
3264 switch (mState) {
3265 case LOADED:
3266 case ERROR:
3267 break;
3268
3269 case EXECUTING:
3270 {
3271 setState(EXECUTING_TO_IDLE);
3272
Andreas Huber127fcdc2009-08-26 16:27:02 -07003273 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07003274 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07003275 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07003276
Andreas Huber127fcdc2009-08-26 16:27:02 -07003277 bool emulateInputFlushCompletion =
3278 !flushPortAsync(kPortIndexInput);
3279
3280 bool emulateOutputFlushCompletion =
3281 !flushPortAsync(kPortIndexOutput);
3282
3283 if (emulateInputFlushCompletion) {
3284 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3285 }
3286
3287 if (emulateOutputFlushCompletion) {
3288 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3289 }
3290 } else {
3291 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
3292 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
3293
3294 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07003295 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003296 CHECK_EQ(err, (status_t)OK);
Andreas Huber127fcdc2009-08-26 16:27:02 -07003297 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003298
3299 while (mState != LOADED && mState != ERROR) {
3300 mAsyncCompletion.wait(mLock);
3301 }
3302
3303 break;
3304 }
3305
3306 default:
3307 {
3308 CHECK(!"should not be here.");
3309 break;
3310 }
3311 }
3312
Andreas Hubera4357ad2010-04-02 12:49:54 -07003313 if (mLeftOverBuffer) {
3314 mLeftOverBuffer->release();
3315 mLeftOverBuffer = NULL;
3316 }
3317
Andreas Huberbe06d262009-08-14 14:37:10 -07003318 mSource->stop();
3319
Andreas Huber4a9375e2010-02-09 11:54:33 -08003320 CODEC_LOGV("stopped");
3321
Andreas Huberbe06d262009-08-14 14:37:10 -07003322 return OK;
3323}
3324
3325sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07003326 Mutex::Autolock autoLock(mLock);
3327
Andreas Huberbe06d262009-08-14 14:37:10 -07003328 return mOutputFormat;
3329}
3330
3331status_t OMXCodec::read(
3332 MediaBuffer **buffer, const ReadOptions *options) {
3333 *buffer = NULL;
3334
3335 Mutex::Autolock autoLock(mLock);
3336
Andreas Huberd06e5b82009-08-28 13:18:14 -07003337 if (mState != EXECUTING && mState != RECONFIGURING) {
3338 return UNKNOWN_ERROR;
3339 }
3340
Andreas Hubere981c332009-10-22 13:49:30 -07003341 bool seeking = false;
3342 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003343 ReadOptions::SeekMode seekMode;
3344 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07003345 seeking = true;
3346 }
3347
Andreas Huber42978e52009-08-27 10:08:39 -07003348 if (mInitialBufferSubmit) {
3349 mInitialBufferSubmit = false;
3350
Andreas Hubere981c332009-10-22 13:49:30 -07003351 if (seeking) {
3352 CHECK(seekTimeUs >= 0);
3353 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003354 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07003355
3356 // There's no reason to trigger the code below, there's
3357 // nothing to flush yet.
3358 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07003359 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07003360 }
3361
Andreas Huber42978e52009-08-27 10:08:39 -07003362 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07003363
Andreas Huberd06e5b82009-08-28 13:18:14 -07003364 if (mState == EXECUTING) {
3365 // Otherwise mState == RECONFIGURING and this code will trigger
3366 // after the output port is reenabled.
3367 fillOutputBuffers();
3368 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003369 }
3370
Andreas Hubere981c332009-10-22 13:49:30 -07003371 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07003372 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07003373
3374 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003375
3376 CHECK(seekTimeUs >= 0);
3377 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003378 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07003379
3380 mFilledBuffers.clear();
3381
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003382 CHECK_EQ((int)mState, (int)EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07003383
Andreas Huber404cc412009-08-25 14:26:05 -07003384 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
3385 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
3386
3387 if (emulateInputFlushCompletion) {
3388 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3389 }
3390
3391 if (emulateOutputFlushCompletion) {
3392 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3393 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08003394
3395 while (mSeekTimeUs >= 0) {
3396 mBufferFilled.wait(mLock);
3397 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003398 }
3399
3400 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
James Dong74556302010-12-20 21:29:12 -08003401 if (mIsEncoder) {
3402 if (NO_ERROR != mBufferFilled.waitRelative(mLock, 3000000000LL)) {
3403 LOGW("Timed out waiting for buffers from video encoder: %d/%d",
3404 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
3405 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]));
3406 }
3407 } else {
3408 mBufferFilled.wait(mLock);
3409 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003410 }
3411
3412 if (mState == ERROR) {
3413 return UNKNOWN_ERROR;
3414 }
3415
3416 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08003417 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07003418 }
3419
Andreas Hubercfd55572009-10-09 14:11:28 -07003420 if (mOutputPortSettingsHaveChanged) {
3421 mOutputPortSettingsHaveChanged = false;
3422
3423 return INFO_FORMAT_CHANGED;
3424 }
3425
Andreas Huberbe06d262009-08-14 14:37:10 -07003426 size_t index = *mFilledBuffers.begin();
3427 mFilledBuffers.erase(mFilledBuffers.begin());
3428
3429 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
Andreas Huberbbbcf652010-12-07 14:25:54 -08003430 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
3431 info->mStatus = OWNED_BY_CLIENT;
3432
Andreas Huberbe06d262009-08-14 14:37:10 -07003433 info->mMediaBuffer->add_ref();
3434 *buffer = info->mMediaBuffer;
3435
3436 return OK;
3437}
3438
3439void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
3440 Mutex::Autolock autoLock(mLock);
3441
3442 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
3443 for (size_t i = 0; i < buffers->size(); ++i) {
3444 BufferInfo *info = &buffers->editItemAt(i);
3445
3446 if (info->mMediaBuffer == buffer) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003447 CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED);
Andreas Huberbbbcf652010-12-07 14:25:54 -08003448 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_CLIENT);
3449
3450 info->mStatus = OWNED_BY_US;
3451
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003452 if (buffer->graphicBuffer() == 0) {
3453 fillOutputBuffer(info);
3454 } else {
3455 sp<MetaData> metaData = info->mMediaBuffer->meta_data();
3456 int32_t rendered = 0;
3457 if (!metaData->findInt32(kKeyRendered, &rendered)) {
3458 rendered = 0;
3459 }
3460 if (!rendered) {
3461 status_t err = cancelBufferToNativeWindow(info);
3462 if (err < 0) {
3463 return;
3464 }
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003465 }
3466
Andreas Huberbbbcf652010-12-07 14:25:54 -08003467 info->mStatus = OWNED_BY_NATIVE_WINDOW;
3468
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003469 // Dequeue the next buffer from the native window.
3470 BufferInfo *nextBufInfo = dequeueBufferFromNativeWindow();
3471 if (nextBufInfo == 0) {
3472 return;
3473 }
3474
3475 // Give the buffer to the OMX node to fill.
3476 fillOutputBuffer(nextBufInfo);
3477 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003478 return;
3479 }
3480 }
3481
3482 CHECK(!"should not be here.");
3483}
3484
3485static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
3486 static const char *kNames[] = {
3487 "OMX_IMAGE_CodingUnused",
3488 "OMX_IMAGE_CodingAutoDetect",
3489 "OMX_IMAGE_CodingJPEG",
3490 "OMX_IMAGE_CodingJPEG2K",
3491 "OMX_IMAGE_CodingEXIF",
3492 "OMX_IMAGE_CodingTIFF",
3493 "OMX_IMAGE_CodingGIF",
3494 "OMX_IMAGE_CodingPNG",
3495 "OMX_IMAGE_CodingLZW",
3496 "OMX_IMAGE_CodingBMP",
3497 };
3498
3499 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3500
3501 if (type < 0 || (size_t)type >= numNames) {
3502 return "UNKNOWN";
3503 } else {
3504 return kNames[type];
3505 }
3506}
3507
3508static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
3509 static const char *kNames[] = {
3510 "OMX_COLOR_FormatUnused",
3511 "OMX_COLOR_FormatMonochrome",
3512 "OMX_COLOR_Format8bitRGB332",
3513 "OMX_COLOR_Format12bitRGB444",
3514 "OMX_COLOR_Format16bitARGB4444",
3515 "OMX_COLOR_Format16bitARGB1555",
3516 "OMX_COLOR_Format16bitRGB565",
3517 "OMX_COLOR_Format16bitBGR565",
3518 "OMX_COLOR_Format18bitRGB666",
3519 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003520 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07003521 "OMX_COLOR_Format24bitRGB888",
3522 "OMX_COLOR_Format24bitBGR888",
3523 "OMX_COLOR_Format24bitARGB1887",
3524 "OMX_COLOR_Format25bitARGB1888",
3525 "OMX_COLOR_Format32bitBGRA8888",
3526 "OMX_COLOR_Format32bitARGB8888",
3527 "OMX_COLOR_FormatYUV411Planar",
3528 "OMX_COLOR_FormatYUV411PackedPlanar",
3529 "OMX_COLOR_FormatYUV420Planar",
3530 "OMX_COLOR_FormatYUV420PackedPlanar",
3531 "OMX_COLOR_FormatYUV420SemiPlanar",
3532 "OMX_COLOR_FormatYUV422Planar",
3533 "OMX_COLOR_FormatYUV422PackedPlanar",
3534 "OMX_COLOR_FormatYUV422SemiPlanar",
3535 "OMX_COLOR_FormatYCbYCr",
3536 "OMX_COLOR_FormatYCrYCb",
3537 "OMX_COLOR_FormatCbYCrY",
3538 "OMX_COLOR_FormatCrYCbY",
3539 "OMX_COLOR_FormatYUV444Interleaved",
3540 "OMX_COLOR_FormatRawBayer8bit",
3541 "OMX_COLOR_FormatRawBayer10bit",
3542 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003543 "OMX_COLOR_FormatL2",
3544 "OMX_COLOR_FormatL4",
3545 "OMX_COLOR_FormatL8",
3546 "OMX_COLOR_FormatL16",
3547 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07003548 "OMX_COLOR_FormatL32",
3549 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
3550 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
3551 "OMX_COLOR_Format18BitBGR666",
3552 "OMX_COLOR_Format24BitARGB6666",
3553 "OMX_COLOR_Format24BitABGR6666",
3554 };
3555
3556 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3557
Andreas Huberbe06d262009-08-14 14:37:10 -07003558 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3559 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3560 } else if (type < 0 || (size_t)type >= numNames) {
3561 return "UNKNOWN";
3562 } else {
3563 return kNames[type];
3564 }
3565}
3566
3567static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3568 static const char *kNames[] = {
3569 "OMX_VIDEO_CodingUnused",
3570 "OMX_VIDEO_CodingAutoDetect",
3571 "OMX_VIDEO_CodingMPEG2",
3572 "OMX_VIDEO_CodingH263",
3573 "OMX_VIDEO_CodingMPEG4",
3574 "OMX_VIDEO_CodingWMV",
3575 "OMX_VIDEO_CodingRV",
3576 "OMX_VIDEO_CodingAVC",
3577 "OMX_VIDEO_CodingMJPEG",
3578 };
3579
3580 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3581
3582 if (type < 0 || (size_t)type >= numNames) {
3583 return "UNKNOWN";
3584 } else {
3585 return kNames[type];
3586 }
3587}
3588
3589static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3590 static const char *kNames[] = {
3591 "OMX_AUDIO_CodingUnused",
3592 "OMX_AUDIO_CodingAutoDetect",
3593 "OMX_AUDIO_CodingPCM",
3594 "OMX_AUDIO_CodingADPCM",
3595 "OMX_AUDIO_CodingAMR",
3596 "OMX_AUDIO_CodingGSMFR",
3597 "OMX_AUDIO_CodingGSMEFR",
3598 "OMX_AUDIO_CodingGSMHR",
3599 "OMX_AUDIO_CodingPDCFR",
3600 "OMX_AUDIO_CodingPDCEFR",
3601 "OMX_AUDIO_CodingPDCHR",
3602 "OMX_AUDIO_CodingTDMAFR",
3603 "OMX_AUDIO_CodingTDMAEFR",
3604 "OMX_AUDIO_CodingQCELP8",
3605 "OMX_AUDIO_CodingQCELP13",
3606 "OMX_AUDIO_CodingEVRC",
3607 "OMX_AUDIO_CodingSMV",
3608 "OMX_AUDIO_CodingG711",
3609 "OMX_AUDIO_CodingG723",
3610 "OMX_AUDIO_CodingG726",
3611 "OMX_AUDIO_CodingG729",
3612 "OMX_AUDIO_CodingAAC",
3613 "OMX_AUDIO_CodingMP3",
3614 "OMX_AUDIO_CodingSBC",
3615 "OMX_AUDIO_CodingVORBIS",
3616 "OMX_AUDIO_CodingWMA",
3617 "OMX_AUDIO_CodingRA",
3618 "OMX_AUDIO_CodingMIDI",
3619 };
3620
3621 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3622
3623 if (type < 0 || (size_t)type >= numNames) {
3624 return "UNKNOWN";
3625 } else {
3626 return kNames[type];
3627 }
3628}
3629
3630static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3631 static const char *kNames[] = {
3632 "OMX_AUDIO_PCMModeLinear",
3633 "OMX_AUDIO_PCMModeALaw",
3634 "OMX_AUDIO_PCMModeMULaw",
3635 };
3636
3637 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3638
3639 if (type < 0 || (size_t)type >= numNames) {
3640 return "UNKNOWN";
3641 } else {
3642 return kNames[type];
3643 }
3644}
3645
Andreas Huber7ae02c82009-09-09 16:29:47 -07003646static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3647 static const char *kNames[] = {
3648 "OMX_AUDIO_AMRBandModeUnused",
3649 "OMX_AUDIO_AMRBandModeNB0",
3650 "OMX_AUDIO_AMRBandModeNB1",
3651 "OMX_AUDIO_AMRBandModeNB2",
3652 "OMX_AUDIO_AMRBandModeNB3",
3653 "OMX_AUDIO_AMRBandModeNB4",
3654 "OMX_AUDIO_AMRBandModeNB5",
3655 "OMX_AUDIO_AMRBandModeNB6",
3656 "OMX_AUDIO_AMRBandModeNB7",
3657 "OMX_AUDIO_AMRBandModeWB0",
3658 "OMX_AUDIO_AMRBandModeWB1",
3659 "OMX_AUDIO_AMRBandModeWB2",
3660 "OMX_AUDIO_AMRBandModeWB3",
3661 "OMX_AUDIO_AMRBandModeWB4",
3662 "OMX_AUDIO_AMRBandModeWB5",
3663 "OMX_AUDIO_AMRBandModeWB6",
3664 "OMX_AUDIO_AMRBandModeWB7",
3665 "OMX_AUDIO_AMRBandModeWB8",
3666 };
3667
3668 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3669
3670 if (type < 0 || (size_t)type >= numNames) {
3671 return "UNKNOWN";
3672 } else {
3673 return kNames[type];
3674 }
3675}
3676
3677static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3678 static const char *kNames[] = {
3679 "OMX_AUDIO_AMRFrameFormatConformance",
3680 "OMX_AUDIO_AMRFrameFormatIF1",
3681 "OMX_AUDIO_AMRFrameFormatIF2",
3682 "OMX_AUDIO_AMRFrameFormatFSF",
3683 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3684 "OMX_AUDIO_AMRFrameFormatITU",
3685 };
3686
3687 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3688
3689 if (type < 0 || (size_t)type >= numNames) {
3690 return "UNKNOWN";
3691 } else {
3692 return kNames[type];
3693 }
3694}
Andreas Huberbe06d262009-08-14 14:37:10 -07003695
3696void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3697 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003698 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003699 def.nPortIndex = portIndex;
3700
Andreas Huber784202e2009-10-15 13:46:54 -07003701 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003702 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003703 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003704
3705 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3706
3707 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3708 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3709
3710 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3711 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3712 printf(" nBufferSize = %ld\n", def.nBufferSize);
3713
3714 switch (def.eDomain) {
3715 case OMX_PortDomainImage:
3716 {
3717 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3718
3719 printf("\n");
3720 printf(" // Image\n");
3721 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3722 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3723 printf(" nStride = %ld\n", imageDef->nStride);
3724
3725 printf(" eCompressionFormat = %s\n",
3726 imageCompressionFormatString(imageDef->eCompressionFormat));
3727
3728 printf(" eColorFormat = %s\n",
3729 colorFormatString(imageDef->eColorFormat));
3730
3731 break;
3732 }
3733
3734 case OMX_PortDomainVideo:
3735 {
3736 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3737
3738 printf("\n");
3739 printf(" // Video\n");
3740 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3741 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3742 printf(" nStride = %ld\n", videoDef->nStride);
3743
3744 printf(" eCompressionFormat = %s\n",
3745 videoCompressionFormatString(videoDef->eCompressionFormat));
3746
3747 printf(" eColorFormat = %s\n",
3748 colorFormatString(videoDef->eColorFormat));
3749
3750 break;
3751 }
3752
3753 case OMX_PortDomainAudio:
3754 {
3755 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3756
3757 printf("\n");
3758 printf(" // Audio\n");
3759 printf(" eEncoding = %s\n",
3760 audioCodingTypeString(audioDef->eEncoding));
3761
3762 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3763 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003764 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003765 params.nPortIndex = portIndex;
3766
Andreas Huber784202e2009-10-15 13:46:54 -07003767 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003768 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003769 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003770
3771 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3772 printf(" nChannels = %ld\n", params.nChannels);
3773 printf(" bInterleaved = %d\n", params.bInterleaved);
3774 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3775
3776 printf(" eNumData = %s\n",
3777 params.eNumData == OMX_NumericalDataSigned
3778 ? "signed" : "unsigned");
3779
3780 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003781 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3782 OMX_AUDIO_PARAM_AMRTYPE amr;
3783 InitOMXParams(&amr);
3784 amr.nPortIndex = portIndex;
3785
Andreas Huber784202e2009-10-15 13:46:54 -07003786 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003787 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003788 CHECK_EQ(err, (status_t)OK);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003789
3790 printf(" nChannels = %ld\n", amr.nChannels);
3791 printf(" eAMRBandMode = %s\n",
3792 amrBandModeString(amr.eAMRBandMode));
3793 printf(" eAMRFrameFormat = %s\n",
3794 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003795 }
3796
3797 break;
3798 }
3799
3800 default:
3801 {
3802 printf(" // Unknown\n");
3803 break;
3804 }
3805 }
3806
3807 printf("}\n");
3808}
3809
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003810status_t OMXCodec::initNativeWindow() {
3811 // Enable use of a GraphicBuffer as the output for this node. This must
3812 // happen before getting the IndexParamPortDefinition parameter because it
3813 // will affect the pixel format that the node reports.
3814 status_t err = mOMX->enableGraphicBuffers(mNode, kPortIndexOutput, OMX_TRUE);
3815 if (err != 0) {
3816 return err;
3817 }
3818
3819 return OK;
3820}
3821
Andreas Huberbe06d262009-08-14 14:37:10 -07003822void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3823 mOutputFormat = new MetaData;
3824 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003825 if (mIsEncoder) {
3826 int32_t timeScale;
3827 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3828 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3829 }
3830 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003831
3832 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003833 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003834 def.nPortIndex = kPortIndexOutput;
3835
Andreas Huber784202e2009-10-15 13:46:54 -07003836 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003837 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003838 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003839
3840 switch (def.eDomain) {
3841 case OMX_PortDomainImage:
3842 {
3843 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003844 CHECK_EQ((int)imageDef->eCompressionFormat,
3845 (int)OMX_IMAGE_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07003846
Andreas Hubere6c40962009-09-10 14:13:30 -07003847 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003848 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3849 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3850 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3851 break;
3852 }
3853
3854 case OMX_PortDomainAudio:
3855 {
3856 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3857
Andreas Huberda050cf22009-09-02 14:01:43 -07003858 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3859 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003860 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003861 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003862
Andreas Huber784202e2009-10-15 13:46:54 -07003863 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003864 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003865 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003866
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003867 CHECK_EQ((int)params.eNumData, (int)OMX_NumericalDataSigned);
3868 CHECK_EQ(params.nBitPerSample, 16u);
3869 CHECK_EQ((int)params.ePCMMode, (int)OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003870
Andreas Huberda050cf22009-09-02 14:01:43 -07003871 int32_t numChannels, sampleRate;
3872 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3873 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003874
Andreas Huberda050cf22009-09-02 14:01:43 -07003875 if ((OMX_U32)numChannels != params.nChannels) {
3876 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003877 "the input stream contains (contains %d channels, "
3878 "codec outputs %ld channels).",
3879 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003880 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003881
Andreas Hubere6c40962009-09-10 14:13:30 -07003882 mOutputFormat->setCString(
3883 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003884
3885 // Use the codec-advertised number of channels, as some
3886 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003887 // mono. If we know the codec lies about this information,
3888 // use the actual number of channels instead.
3889 mOutputFormat->setInt32(
3890 kKeyChannelCount,
3891 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3892 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003893
3894 // The codec-reported sampleRate is not reliable...
3895 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3896 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003897 OMX_AUDIO_PARAM_AMRTYPE amr;
3898 InitOMXParams(&amr);
3899 amr.nPortIndex = kPortIndexOutput;
3900
Andreas Huber784202e2009-10-15 13:46:54 -07003901 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003902 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003903 CHECK_EQ(err, (status_t)OK);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003904
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003905 CHECK_EQ(amr.nChannels, 1u);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003906 mOutputFormat->setInt32(kKeyChannelCount, 1);
3907
3908 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3909 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003910 mOutputFormat->setCString(
3911 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003912 mOutputFormat->setInt32(kKeySampleRate, 8000);
3913 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3914 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003915 mOutputFormat->setCString(
3916 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003917 mOutputFormat->setInt32(kKeySampleRate, 16000);
3918 } else {
3919 CHECK(!"Unknown AMR band mode.");
3920 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003921 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003922 mOutputFormat->setCString(
3923 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003924 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003925 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3926 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003927 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003928 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3929 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003930 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003931 } else {
3932 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003933 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003934 break;
3935 }
3936
3937 case OMX_PortDomainVideo:
3938 {
3939 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3940
3941 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003942 mOutputFormat->setCString(
3943 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003944 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003945 mOutputFormat->setCString(
3946 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003947 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003948 mOutputFormat->setCString(
3949 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003950 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003951 mOutputFormat->setCString(
3952 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003953 } else {
3954 CHECK(!"Unknown compression format.");
3955 }
3956
James Dong5592bcc2010-10-22 17:10:43 -07003957 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3958 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
Andreas Huberbe06d262009-08-14 14:37:10 -07003959 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003960
James Dong0c9153d2010-11-23 11:30:21 -08003961 if (!mIsEncoder) {
3962 OMX_CONFIG_RECTTYPE rect;
James Donga1735412011-01-07 14:56:34 -08003963 InitOMXParams(&rect);
3964 rect.nPortIndex = kPortIndexOutput;
James Dong0c9153d2010-11-23 11:30:21 -08003965 status_t err =
3966 mOMX->getConfig(
3967 mNode, OMX_IndexConfigCommonOutputCrop,
3968 &rect, sizeof(rect));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003969
James Dong0c9153d2010-11-23 11:30:21 -08003970 if (err == OK) {
3971 CHECK_GE(rect.nLeft, 0);
3972 CHECK_GE(rect.nTop, 0);
3973 CHECK_GE(rect.nWidth, 0u);
3974 CHECK_GE(rect.nHeight, 0u);
3975 CHECK_LE(rect.nLeft + rect.nWidth - 1, video_def->nFrameWidth);
3976 CHECK_LE(rect.nTop + rect.nHeight - 1, video_def->nFrameHeight);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003977
James Dong0c9153d2010-11-23 11:30:21 -08003978 mOutputFormat->setRect(
3979 kKeyCropRect,
3980 rect.nLeft,
3981 rect.nTop,
3982 rect.nLeft + rect.nWidth - 1,
3983 rect.nTop + rect.nHeight - 1);
3984 } else {
3985 mOutputFormat->setRect(
3986 kKeyCropRect,
3987 0, 0,
3988 video_def->nFrameWidth - 1,
3989 video_def->nFrameHeight - 1);
3990 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003991 }
3992
Andreas Huberbe06d262009-08-14 14:37:10 -07003993 break;
3994 }
3995
3996 default:
3997 {
3998 CHECK(!"should not be here, neither audio nor video.");
3999 break;
4000 }
4001 }
4002}
4003
Andreas Huber1f24b302010-06-10 11:12:39 -07004004status_t OMXCodec::pause() {
4005 Mutex::Autolock autoLock(mLock);
4006
4007 mPaused = true;
4008
4009 return OK;
4010}
4011
Andreas Hubere6c40962009-09-10 14:13:30 -07004012////////////////////////////////////////////////////////////////////////////////
4013
4014status_t QueryCodecs(
4015 const sp<IOMX> &omx,
4016 const char *mime, bool queryDecoders,
4017 Vector<CodecCapabilities> *results) {
4018 results->clear();
4019
4020 for (int index = 0;; ++index) {
4021 const char *componentName;
4022
4023 if (!queryDecoders) {
4024 componentName = GetCodec(
4025 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
4026 mime, index);
4027 } else {
4028 componentName = GetCodec(
4029 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
4030 mime, index);
4031 }
4032
4033 if (!componentName) {
4034 return OK;
4035 }
4036
Andreas Huber1a189a82010-03-24 13:49:20 -07004037 if (strncmp(componentName, "OMX.", 4)) {
4038 // Not an OpenMax component but a software codec.
4039
4040 results->push();
4041 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
4042 caps->mComponentName = componentName;
4043
4044 continue;
4045 }
4046
Andreas Huber784202e2009-10-15 13:46:54 -07004047 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07004048 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07004049 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07004050
4051 if (err != OK) {
4052 continue;
4053 }
4054
James Dong722d5912010-04-13 10:56:59 -07004055 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07004056
4057 results->push();
4058 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
4059 caps->mComponentName = componentName;
4060
4061 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
4062 InitOMXParams(&param);
4063
4064 param.nPortIndex = queryDecoders ? 0 : 1;
4065
4066 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07004067 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07004068 node, OMX_IndexParamVideoProfileLevelQuerySupported,
4069 &param, sizeof(param));
4070
4071 if (err != OK) {
4072 break;
4073 }
4074
4075 CodecProfileLevel profileLevel;
4076 profileLevel.mProfile = param.eProfile;
4077 profileLevel.mLevel = param.eLevel;
4078
4079 caps->mProfileLevels.push(profileLevel);
4080 }
4081
James Dongd7810892010-11-11 00:33:05 -08004082 // Color format query
4083 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
4084 InitOMXParams(&portFormat);
4085 portFormat.nPortIndex = queryDecoders ? 1 : 0;
4086 for (portFormat.nIndex = 0;; ++portFormat.nIndex) {
4087 err = omx->getParameter(
4088 node, OMX_IndexParamVideoPortFormat,
4089 &portFormat, sizeof(portFormat));
4090 if (err != OK) {
4091 break;
4092 }
4093 caps->mColorFormats.push(portFormat.eColorFormat);
4094 }
4095
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004096 CHECK_EQ(omx->freeNode(node), (status_t)OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07004097 }
4098}
4099
James Dong31b9375f2010-11-10 21:11:41 -08004100void OMXCodec::restorePatchedDataPointer(BufferInfo *info) {
4101 CHECK(mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames));
4102 CHECK(mOMXLivesLocally);
4103
4104 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *)info->mBuffer;
4105 header->pBuffer = (OMX_U8 *)info->mData;
4106}
4107
Andreas Huberbe06d262009-08-14 14:37:10 -07004108} // namespace android