blob: 4bf922f2cc8193ad08c0d2657181b422bfe4e65c [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) {
238 codec->on_message(msg);
239 }
240 }
241
242protected:
243 virtual ~OMXCodecObserver() {}
244
245private:
246 wp<OMXCodec> mTarget;
247
248 OMXCodecObserver(const OMXCodecObserver &);
249 OMXCodecObserver &operator=(const OMXCodecObserver &);
250};
251
252static const char *GetCodec(const CodecInfo *info, size_t numInfos,
253 const char *mime, int index) {
254 CHECK(index >= 0);
255 for(size_t i = 0; i < numInfos; ++i) {
256 if (!strcasecmp(mime, info[i].mime)) {
257 if (index == 0) {
258 return info[i].codec;
259 }
260
261 --index;
262 }
263 }
264
265 return NULL;
266}
267
Andreas Huber4c483422009-09-02 16:05:36 -0700268template<class T>
269static void InitOMXParams(T *params) {
270 params->nSize = sizeof(T);
271 params->nVersion.s.nVersionMajor = 1;
272 params->nVersion.s.nVersionMinor = 0;
273 params->nVersion.s.nRevision = 0;
274 params->nVersion.s.nStep = 0;
275}
276
Andreas Hubere13526a2009-10-22 10:43:34 -0700277static bool IsSoftwareCodec(const char *componentName) {
James Dong5592bcc2010-10-22 17:10:43 -0700278 if (!strncmp("OMX.", componentName, 4)) {
279 return false;
Andreas Huberbe06d262009-08-14 14:37:10 -0700280 }
281
James Dong5592bcc2010-10-22 17:10:43 -0700282 return true;
Andreas Hubere13526a2009-10-22 10:43:34 -0700283}
284
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800285// A sort order in which non-OMX components are first,
James Dong5592bcc2010-10-22 17:10:43 -0700286// followed by software codecs, and followed by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700287static int CompareSoftwareCodecsFirst(
288 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800289 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
290 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
291
292 if (isNotOMX1) {
293 if (isNotOMX2) { return 0; }
294 return -1;
295 }
296 if (isNotOMX2) {
297 return 1;
298 }
299
Andreas Hubere13526a2009-10-22 10:43:34 -0700300 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
301 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
302
303 if (isSoftwareCodec1) {
304 if (isSoftwareCodec2) { return 0; }
305 return -1;
306 }
307
308 if (isSoftwareCodec2) {
309 return 1;
310 }
311
312 return 0;
313}
314
315// static
Andreas Huber1e194162010-10-06 16:43:57 -0700316uint32_t OMXCodec::getComponentQuirks(
317 const char *componentName, bool isEncoder) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700318 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700319
Dima Zavin30ba6cb2010-08-23 11:10:03 -0700320 if (!strcmp(componentName, "OMX.Nvidia.amr.decoder") ||
321 !strcmp(componentName, "OMX.Nvidia.amrwb.decoder") ||
322 !strcmp(componentName, "OMX.Nvidia.aac.decoder") ||
323 !strcmp(componentName, "OMX.Nvidia.mp3.decoder")) {
324 quirks |= kDecoderLiesAboutNumberOfChannels;
325 }
326
Andreas Huberbe06d262009-08-14 14:37:10 -0700327 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
328 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800329 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700330 }
331 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
332 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700333 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700334 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700335 }
336 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
337 quirks |= kRequiresLoadedToIdleAfterAllocation;
338 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700339 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dong90862e22010-08-26 19:12:59 -0700340 if (!strncmp(componentName, "OMX.qcom.video.encoder.avc", 26)) {
341
342 // The AVC encoder advertises the size of output buffers
343 // based on the input video resolution and assumes
344 // the worst/least compression ratio is 0.5. It is found that
345 // sometimes, the output buffer size is larger than
346 // size advertised by the encoder.
347 quirks |= kRequiresLargerEncoderOutputBuffer;
348 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700349 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700350 if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) {
351 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700352 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700353 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800354 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700355 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700356 if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) {
357 quirks |= kRequiresAllocateBufferOnInputPorts;
358 quirks |= kRequiresAllocateBufferOnOutputPorts;
359 quirks |= kDefersOutputBufferAllocation;
360 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700361
Andreas Huber2dc64d82009-09-11 12:58:53 -0700362 if (!strncmp(componentName, "OMX.TI.", 7)) {
363 // Apparently I must not use OMX_UseBuffer on either input or
364 // output ports on any of the TI components or quote:
365 // "(I) may have unexpected problem (sic) which can be timing related
366 // and hard to reproduce."
367
368 quirks |= kRequiresAllocateBufferOnInputPorts;
369 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dongdca66e12010-06-14 11:14:38 -0700370 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) {
James Dong4f501f02010-06-07 14:41:41 -0700371 quirks |= kAvoidMemcopyInputRecordingFrames;
372 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700373 }
374
Andreas Huberb8de9572010-02-22 14:58:45 -0800375 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
376 quirks |= kInputBufferSizesAreBogus;
377 }
378
Andreas Huber1e194162010-10-06 16:43:57 -0700379 if (!strncmp(componentName, "OMX.SEC.", 8) && !isEncoder) {
380 // These output buffers contain no video data, just some
381 // opaque information that allows the overlay to display their
382 // contents.
383 quirks |= kOutputBuffersAreUnreadable;
384 }
385
Andreas Hubere13526a2009-10-22 10:43:34 -0700386 return quirks;
387}
388
389// static
390void OMXCodec::findMatchingCodecs(
391 const char *mime,
392 bool createEncoder, const char *matchComponentName,
393 uint32_t flags,
394 Vector<String8> *matchingCodecs) {
395 matchingCodecs->clear();
396
397 for (int index = 0;; ++index) {
398 const char *componentName;
399
400 if (createEncoder) {
401 componentName = GetCodec(
402 kEncoderInfo,
403 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
404 mime, index);
405 } else {
406 componentName = GetCodec(
407 kDecoderInfo,
408 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
409 mime, index);
410 }
411
412 if (!componentName) {
413 break;
414 }
415
416 // If a specific codec is requested, skip the non-matching ones.
417 if (matchComponentName && strcmp(componentName, matchComponentName)) {
418 continue;
419 }
420
James Dong170a9292010-10-22 17:28:15 -0700421 // When requesting software-only codecs, only push software codecs
422 // When requesting hardware-only codecs, only push hardware codecs
423 // When there is request neither for software-only nor for
424 // hardware-only codecs, push all codecs
425 if (((flags & kSoftwareCodecsOnly) && IsSoftwareCodec(componentName)) ||
426 ((flags & kHardwareCodecsOnly) && !IsSoftwareCodec(componentName)) ||
427 (!(flags & (kSoftwareCodecsOnly | kHardwareCodecsOnly)))) {
428
429 matchingCodecs->push(String8(componentName));
430 }
Andreas Hubere13526a2009-10-22 10:43:34 -0700431 }
432
433 if (flags & kPreferSoftwareCodecs) {
434 matchingCodecs->sort(CompareSoftwareCodecsFirst);
435 }
436}
437
438// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800439sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700440 const sp<IOMX> &omx,
441 const sp<MetaData> &meta, bool createEncoder,
442 const sp<MediaSource> &source,
443 const char *matchComponentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700444 uint32_t flags,
445 const sp<ANativeWindow> &nativeWindow) {
Andreas Hubere13526a2009-10-22 10:43:34 -0700446 const char *mime;
447 bool success = meta->findCString(kKeyMIMEType, &mime);
448 CHECK(success);
449
450 Vector<String8> matchingCodecs;
451 findMatchingCodecs(
452 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
453
454 if (matchingCodecs.isEmpty()) {
455 return NULL;
456 }
457
458 sp<OMXCodecObserver> observer = new OMXCodecObserver;
459 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700460
461 const char *componentName;
462 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
463 componentName = matchingCodecs[i].string();
464
James Dong17299ab2010-05-14 15:45:22 -0700465 sp<MediaSource> softwareCodec = createEncoder?
466 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800467 InstantiateSoftwareCodec(componentName, source);
468
469 if (softwareCodec != NULL) {
470 LOGV("Successfully allocated software codec '%s'", componentName);
471
472 return softwareCodec;
473 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800474
Andreas Hubere13526a2009-10-22 10:43:34 -0700475 LOGV("Attempting to allocate OMX node '%s'", componentName);
476
Andreas Huber5a40e392010-10-18 09:57:42 -0700477 uint32_t quirks = getComponentQuirks(componentName, createEncoder);
478
479 if (!createEncoder
480 && (quirks & kOutputBuffersAreUnreadable)
481 && (flags & kClientNeedsFramebuffer)) {
482 if (strncmp(componentName, "OMX.SEC.", 8)) {
483 // For OMX.SEC.* decoders we can enable a special mode that
484 // gives the client access to the framebuffer contents.
485
486 LOGW("Component '%s' does not give the client access to "
487 "the framebuffer contents. Skipping.",
488 componentName);
489
490 continue;
491 }
492 }
493
Andreas Hubere13526a2009-10-22 10:43:34 -0700494 status_t err = omx->allocateNode(componentName, observer, &node);
495 if (err == OK) {
496 LOGV("Successfully allocated OMX node '%s'", componentName);
497
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700498 sp<OMXCodec> codec = new OMXCodec(
Andreas Huber5a40e392010-10-18 09:57:42 -0700499 omx, node, quirks,
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700500 createEncoder, mime, componentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700501 source, nativeWindow);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700502
503 observer->setCodec(codec);
504
Andreas Huber4c19bf92010-09-08 14:32:20 -0700505 err = codec->configureCodec(meta, flags);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700506
507 if (err == OK) {
508 return codec;
509 }
510
511 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700512 }
513 }
514
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700515 return NULL;
516}
Andreas Hubere13526a2009-10-22 10:43:34 -0700517
Andreas Huber4c19bf92010-09-08 14:32:20 -0700518status_t OMXCodec::configureCodec(const sp<MetaData> &meta, uint32_t flags) {
James Dong05c2fd52010-11-02 13:20:11 -0700519 mIsMetaDataStoredInVideoBuffers = false;
520 if (flags & kStoreMetaDataInVideoBuffers) {
521 mIsMetaDataStoredInVideoBuffers = true;
522 }
Andreas Huber4c19bf92010-09-08 14:32:20 -0700523 if (!(flags & kIgnoreCodecSpecificData)) {
524 uint32_t type;
525 const void *data;
526 size_t size;
527 if (meta->findData(kKeyESDS, &type, &data, &size)) {
528 ESDS esds((const char *)data, size);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800529 CHECK_EQ(esds.InitCheck(), (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700530
Andreas Huber4c19bf92010-09-08 14:32:20 -0700531 const void *codec_specific_data;
532 size_t codec_specific_data_size;
533 esds.getCodecSpecificInfo(
534 &codec_specific_data, &codec_specific_data_size);
Andreas Huberbe06d262009-08-14 14:37:10 -0700535
Andreas Huber4c19bf92010-09-08 14:32:20 -0700536 addCodecSpecificData(
537 codec_specific_data, codec_specific_data_size);
538 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
539 // Parse the AVCDecoderConfigurationRecord
Andreas Huberebf66ea2009-08-19 13:32:58 -0700540
Andreas Huber4c19bf92010-09-08 14:32:20 -0700541 const uint8_t *ptr = (const uint8_t *)data;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700542
Andreas Huber4c19bf92010-09-08 14:32:20 -0700543 CHECK(size >= 7);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800544 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1
Andreas Huber4c19bf92010-09-08 14:32:20 -0700545 uint8_t profile = ptr[1];
546 uint8_t level = ptr[3];
Andreas Huberebf66ea2009-08-19 13:32:58 -0700547
Andreas Huber4c19bf92010-09-08 14:32:20 -0700548 // There is decodable content out there that fails the following
549 // assertion, let's be lenient for now...
550 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700551
Andreas Huber4c19bf92010-09-08 14:32:20 -0700552 size_t lengthSize = 1 + (ptr[4] & 3);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700553
Andreas Huber4c19bf92010-09-08 14:32:20 -0700554 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
555 // violates it...
556 // CHECK((ptr[5] >> 5) == 7); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700557
Andreas Huber4c19bf92010-09-08 14:32:20 -0700558 size_t numSeqParameterSets = ptr[5] & 31;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700559
Andreas Huber4c19bf92010-09-08 14:32:20 -0700560 ptr += 6;
561 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700562
Andreas Huber4c19bf92010-09-08 14:32:20 -0700563 for (size_t i = 0; i < numSeqParameterSets; ++i) {
564 CHECK(size >= 2);
565 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700566
Andreas Huber4c19bf92010-09-08 14:32:20 -0700567 ptr += 2;
568 size -= 2;
Andreas Huberbe06d262009-08-14 14:37:10 -0700569
Andreas Huber4c19bf92010-09-08 14:32:20 -0700570 CHECK(size >= length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700571
Andreas Huber4c19bf92010-09-08 14:32:20 -0700572 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700573
Andreas Huber4c19bf92010-09-08 14:32:20 -0700574 ptr += length;
575 size -= length;
576 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700577
Andreas Huber4c19bf92010-09-08 14:32:20 -0700578 CHECK(size >= 1);
579 size_t numPictureParameterSets = *ptr;
580 ++ptr;
581 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700582
Andreas Huber4c19bf92010-09-08 14:32:20 -0700583 for (size_t i = 0; i < numPictureParameterSets; ++i) {
584 CHECK(size >= 2);
585 size_t length = U16_AT(ptr);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700586
Andreas Huber4c19bf92010-09-08 14:32:20 -0700587 ptr += 2;
588 size -= 2;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700589
Andreas Huber4c19bf92010-09-08 14:32:20 -0700590 CHECK(size >= length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700591
Andreas Huber4c19bf92010-09-08 14:32:20 -0700592 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700593
Andreas Huber4c19bf92010-09-08 14:32:20 -0700594 ptr += length;
595 size -= length;
596 }
Andreas Huberebf66ea2009-08-19 13:32:58 -0700597
Andreas Huber6ac2cb22010-11-18 14:06:07 -0800598 CODEC_LOGI(
Andreas Huber4c19bf92010-09-08 14:32:20 -0700599 "AVC profile = %d (%s), level = %d",
600 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700601
Andreas Huber4c19bf92010-09-08 14:32:20 -0700602 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
603 && (profile != kAVCProfileBaseline || level > 30)) {
604 // This stream exceeds the decoder's capabilities. The decoder
605 // does not handle this gracefully and would clobber the heap
606 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700607
Andreas Huber4c19bf92010-09-08 14:32:20 -0700608 LOGE("Profile and/or level exceed the decoder's capabilities.");
609 return ERROR_UNSUPPORTED;
610 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700611 }
612 }
613
James Dong17299ab2010-05-14 15:45:22 -0700614 int32_t bitRate = 0;
615 if (mIsEncoder) {
616 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
617 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700618 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700619 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700620 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700621 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700622 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700623 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700624 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700625 int32_t numChannels, sampleRate;
626 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
627 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
628
James Dong17299ab2010-05-14 15:45:22 -0700629 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700630 }
James Dongabed93a2010-04-22 17:27:04 -0700631
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700632 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700633
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700634 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700635 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700636 } else {
James Dong1244eab2010-06-08 11:58:53 -0700637 int32_t width, height;
638 bool success = meta->findInt32(kKeyWidth, &width);
639 success = success && meta->findInt32(kKeyHeight, &height);
640 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700641 status_t err = setVideoOutputFormat(
642 mMIME, width, height);
643
644 if (err != OK) {
645 return err;
646 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700647 }
648 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700649
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700650 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
651 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700652 OMX_COLOR_FORMATTYPE format =
653 OMX_COLOR_Format32bitARGB8888;
654 // OMX_COLOR_FormatYUV420PackedPlanar;
655 // OMX_COLOR_FormatCbYCrY;
656 // OMX_COLOR_FormatYUV411Planar;
657
658 int32_t width, height;
659 bool success = meta->findInt32(kKeyWidth, &width);
660 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700661
662 int32_t compressedSize;
663 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700664 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700665
666 CHECK(success);
667 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700668
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700669 setImageOutputFormat(format, width, height);
670 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700671 }
672
Andreas Huberda050cf22009-09-02 14:01:43 -0700673 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800674 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700675 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700676 }
677
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700678 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700679 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
680 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700681 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700682 }
683
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700684 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700685
Andreas Huber5a40e392010-10-18 09:57:42 -0700686 if ((flags & kClientNeedsFramebuffer)
687 && !strncmp(mComponentName, "OMX.SEC.", 8)) {
688 OMX_INDEXTYPE index;
689
690 status_t err =
691 mOMX->getExtensionIndex(
692 mNode,
693 "OMX.SEC.index.ThumbnailMode",
694 &index);
695
696 if (err != OK) {
697 return err;
698 }
699
700 OMX_BOOL enable = OMX_TRUE;
701 err = mOMX->setConfig(mNode, index, &enable, sizeof(enable));
702
703 if (err != OK) {
704 CODEC_LOGE("setConfig('OMX.SEC.index.ThumbnailMode') "
705 "returned error 0x%08x", err);
706
707 return err;
708 }
709
710 mQuirks &= ~kOutputBuffersAreUnreadable;
711 }
712
Jamie Gennisdbfb32e2010-10-20 15:53:59 -0700713 if (mNativeWindow != NULL
714 && !mIsEncoder
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700715 && !strncasecmp(mMIME, "video/", 6)
716 && !strncmp(mComponentName, "OMX.", 4)) {
717 status_t err = initNativeWindow();
718 if (err != OK) {
719 return err;
720 }
721 }
722
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700723 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700724}
725
Andreas Huberda050cf22009-09-02 14:01:43 -0700726void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
727 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700728 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700729 def.nPortIndex = portIndex;
730
Andreas Huber784202e2009-10-15 13:46:54 -0700731 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700732 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800733 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -0700734
Andreas Huberb8de9572010-02-22 14:58:45 -0800735 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
736 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700737 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700738 }
739
Andreas Huber784202e2009-10-15 13:46:54 -0700740 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700741 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800742 CHECK_EQ(err, (status_t)OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800743
744 err = mOMX->getParameter(
745 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800746 CHECK_EQ(err, (status_t)OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800747
748 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800749 if (portIndex == kPortIndexInput
750 && (mQuirks & kInputBufferSizesAreBogus)) {
751 CHECK_EQ(def.nBufferSize, size);
752 } else {
753 CHECK(def.nBufferSize >= size);
754 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700755}
756
Andreas Huberbe06d262009-08-14 14:37:10 -0700757status_t OMXCodec::setVideoPortFormatType(
758 OMX_U32 portIndex,
759 OMX_VIDEO_CODINGTYPE compressionFormat,
760 OMX_COLOR_FORMATTYPE colorFormat) {
761 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700762 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700763 format.nPortIndex = portIndex;
764 format.nIndex = 0;
765 bool found = false;
766
767 OMX_U32 index = 0;
768 for (;;) {
769 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700770 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700771 mNode, OMX_IndexParamVideoPortFormat,
772 &format, sizeof(format));
773
774 if (err != OK) {
775 return err;
776 }
777
778 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700779 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700780
781#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700782 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700783 portIndex,
784 index, format.eCompressionFormat, format.eColorFormat);
785#endif
786
787 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
788 if (portIndex == kPortIndexInput
789 && colorFormat == format.eColorFormat) {
790 // eCompressionFormat does not seem right.
791 found = true;
792 break;
793 }
794 if (portIndex == kPortIndexOutput
795 && compressionFormat == format.eCompressionFormat) {
796 // eColorFormat does not seem right.
797 found = true;
798 break;
799 }
800 }
801
802 if (format.eCompressionFormat == compressionFormat
803 && format.eColorFormat == colorFormat) {
804 found = true;
805 break;
806 }
807
808 ++index;
809 }
810
811 if (!found) {
812 return UNKNOWN_ERROR;
813 }
814
Andreas Huber53a76bd2009-10-06 16:20:44 -0700815 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700816 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700817 mNode, OMX_IndexParamVideoPortFormat,
818 &format, sizeof(format));
819
820 return err;
821}
822
Andreas Huberb482ce82009-10-29 12:02:48 -0700823static size_t getFrameSize(
824 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
825 switch (colorFormat) {
826 case OMX_COLOR_FormatYCbYCr:
827 case OMX_COLOR_FormatCbYCrY:
828 return width * height * 2;
829
Andreas Huber71c27d92010-03-19 11:43:15 -0700830 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700831 case OMX_COLOR_FormatYUV420SemiPlanar:
832 return (width * height * 3) / 2;
833
834 default:
835 CHECK(!"Should not be here. Unsupported color format.");
836 break;
837 }
838}
839
James Dongafd97e82010-08-03 17:19:23 -0700840status_t OMXCodec::findTargetColorFormat(
841 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) {
842 LOGV("findTargetColorFormat");
843 CHECK(mIsEncoder);
844
845 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
846 int32_t targetColorFormat;
847 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) {
848 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat;
849 } else {
850 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
851 *colorFormat = OMX_COLOR_FormatYCbYCr;
852 }
853 }
854
855 // Check whether the target color format is supported.
856 return isColorFormatSupported(*colorFormat, kPortIndexInput);
857}
858
859status_t OMXCodec::isColorFormatSupported(
860 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) {
861 LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat));
862
863 // Enumerate all the color formats supported by
864 // the omx component to see whether the given
865 // color format is supported.
866 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
867 InitOMXParams(&portFormat);
868 portFormat.nPortIndex = portIndex;
869 OMX_U32 index = 0;
870 portFormat.nIndex = index;
871 while (true) {
872 if (OMX_ErrorNone != mOMX->getParameter(
873 mNode, OMX_IndexParamVideoPortFormat,
874 &portFormat, sizeof(portFormat))) {
James Dongb5024da2010-09-13 16:30:51 -0700875 break;
James Dongafd97e82010-08-03 17:19:23 -0700876 }
877 // Make sure that omx component does not overwrite
878 // the incremented index (bug 2897413).
879 CHECK_EQ(index, portFormat.nIndex);
880 if ((portFormat.eColorFormat == colorFormat)) {
881 LOGV("Found supported color format: %d", portFormat.eColorFormat);
882 return OK; // colorFormat is supported!
883 }
884 ++index;
885 portFormat.nIndex = index;
886
887 // OMX Spec defines less than 50 color formats
888 // 1000 is more than enough for us to tell whether the omx
889 // component in question is buggy or not.
890 if (index >= 1000) {
891 LOGE("More than %ld color formats are supported???", index);
892 break;
893 }
894 }
James Dongb5024da2010-09-13 16:30:51 -0700895
896 LOGE("color format %d is not supported", colorFormat);
James Dongafd97e82010-08-03 17:19:23 -0700897 return UNKNOWN_ERROR;
898}
899
Andreas Huberbe06d262009-08-14 14:37:10 -0700900void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700901 const char *mime, const sp<MetaData>& meta) {
902
903 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
904 bool success = meta->findInt32(kKeyWidth, &width);
905 success = success && meta->findInt32(kKeyHeight, &height);
James Dongaac193c2010-11-10 20:43:53 -0800906 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -0700907 success = success && meta->findInt32(kKeyBitRate, &bitRate);
908 success = success && meta->findInt32(kKeyStride, &stride);
909 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
910 CHECK(success);
911 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700912
913 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700914 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700915 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700916 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700917 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700918 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700919 compressionFormat = OMX_VIDEO_CodingH263;
920 } else {
921 LOGE("Not a supported video mime type: %s", mime);
922 CHECK(!"Should not be here. Not a supported video mime type.");
923 }
924
James Dongafd97e82010-08-03 17:19:23 -0700925 OMX_COLOR_FORMATTYPE colorFormat;
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800926 CHECK_EQ((status_t)OK, findTargetColorFormat(meta, &colorFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -0700927
James Dongb00e2462010-04-26 17:48:26 -0700928 status_t err;
929 OMX_PARAM_PORTDEFINITIONTYPE def;
930 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
931
932 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700933 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700934 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800935 colorFormat), (status_t)OK);
James Dong4f501f02010-06-07 14:41:41 -0700936
James Dongb00e2462010-04-26 17:48:26 -0700937 InitOMXParams(&def);
938 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700939
James Dongb00e2462010-04-26 17:48:26 -0700940 err = mOMX->getParameter(
941 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800942 CHECK_EQ(err, (status_t)OK);
James Dongb00e2462010-04-26 17:48:26 -0700943
James Dong1244eab2010-06-08 11:58:53 -0700944 def.nBufferSize = getFrameSize(colorFormat,
945 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700946
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800947 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
James Dongb00e2462010-04-26 17:48:26 -0700948
949 video_def->nFrameWidth = width;
950 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700951 video_def->nStride = stride;
952 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700953 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700954 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
955 video_def->eColorFormat = colorFormat;
956
James Dongb00e2462010-04-26 17:48:26 -0700957 err = mOMX->setParameter(
958 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800959 CHECK_EQ(err, (status_t)OK);
James Dongb00e2462010-04-26 17:48:26 -0700960
961 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700962 CHECK_EQ(setVideoPortFormatType(
963 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800964 (status_t)OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700965 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700966 def.nPortIndex = kPortIndexOutput;
967
James Dongb00e2462010-04-26 17:48:26 -0700968 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700969 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
970
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800971 CHECK_EQ(err, (status_t)OK);
972 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -0700973
974 video_def->nFrameWidth = width;
975 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700976 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700977 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700978 video_def->eCompressionFormat = compressionFormat;
979 video_def->eColorFormat = OMX_COLOR_FormatUnused;
James Dong90862e22010-08-26 19:12:59 -0700980 if (mQuirks & kRequiresLargerEncoderOutputBuffer) {
981 // Increases the output buffer size
982 def.nBufferSize = ((def.nBufferSize * 3) >> 1);
983 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700984
Andreas Huber784202e2009-10-15 13:46:54 -0700985 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700986 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800987 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700988
James Dongb00e2462010-04-26 17:48:26 -0700989 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700990 switch (compressionFormat) {
991 case OMX_VIDEO_CodingMPEG4:
992 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800993 CHECK_EQ(setupMPEG4EncoderParameters(meta), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700994 break;
995 }
996
997 case OMX_VIDEO_CodingH263:
Andreas Huber1bb0ffd2010-11-22 13:06:35 -0800998 CHECK_EQ(setupH263EncoderParameters(meta), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700999 break;
1000
Andreas Huberea6a38c2009-11-16 15:43:38 -08001001 case OMX_VIDEO_CodingAVC:
1002 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001003 CHECK_EQ(setupAVCEncoderParameters(meta), (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001004 break;
1005 }
1006
Andreas Huberb482ce82009-10-29 12:02:48 -07001007 default:
1008 CHECK(!"Support for this compressionFormat to be implemented.");
1009 break;
1010 }
1011}
1012
James Dong1244eab2010-06-08 11:58:53 -07001013static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
1014 if (iFramesInterval < 0) {
1015 return 0xFFFFFFFF;
1016 } else if (iFramesInterval == 0) {
1017 return 0;
1018 }
1019 OMX_U32 ret = frameRate * iFramesInterval;
1020 CHECK(ret > 1);
1021 return ret;
1022}
1023
James Dongc0ab2a62010-06-29 16:29:19 -07001024status_t OMXCodec::setupErrorCorrectionParameters() {
1025 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
1026 InitOMXParams(&errorCorrectionType);
1027 errorCorrectionType.nPortIndex = kPortIndexOutput;
1028
1029 status_t err = mOMX->getParameter(
1030 mNode, OMX_IndexParamVideoErrorCorrection,
1031 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001032 if (err != OK) {
1033 LOGW("Error correction param query is not supported");
1034 return OK; // Optional feature. Ignore this failure
1035 }
James Dongc0ab2a62010-06-29 16:29:19 -07001036
1037 errorCorrectionType.bEnableHEC = OMX_FALSE;
1038 errorCorrectionType.bEnableResync = OMX_TRUE;
1039 errorCorrectionType.nResynchMarkerSpacing = 256;
1040 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
1041 errorCorrectionType.bEnableRVLC = OMX_FALSE;
1042
1043 err = mOMX->setParameter(
1044 mNode, OMX_IndexParamVideoErrorCorrection,
1045 &errorCorrectionType, sizeof(errorCorrectionType));
James Dong903fc222010-09-22 17:37:42 -07001046 if (err != OK) {
1047 LOGW("Error correction param configuration is not supported");
1048 }
1049
1050 // Optional feature. Ignore the failure.
James Dongc0ab2a62010-06-29 16:29:19 -07001051 return OK;
1052}
1053
1054status_t OMXCodec::setupBitRate(int32_t bitRate) {
1055 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
1056 InitOMXParams(&bitrateType);
1057 bitrateType.nPortIndex = kPortIndexOutput;
1058
1059 status_t err = mOMX->getParameter(
1060 mNode, OMX_IndexParamVideoBitrate,
1061 &bitrateType, sizeof(bitrateType));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001062 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001063
1064 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
1065 bitrateType.nTargetBitrate = bitRate;
1066
1067 err = mOMX->setParameter(
1068 mNode, OMX_IndexParamVideoBitrate,
1069 &bitrateType, sizeof(bitrateType));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001070 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001071 return OK;
1072}
1073
James Dong81c929a2010-07-01 15:02:14 -07001074status_t OMXCodec::getVideoProfileLevel(
1075 const sp<MetaData>& meta,
1076 const CodecProfileLevel& defaultProfileLevel,
1077 CodecProfileLevel &profileLevel) {
1078 CODEC_LOGV("Default profile: %ld, level %ld",
1079 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
1080
1081 // Are the default profile and level overwriten?
1082 int32_t profile, level;
1083 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
1084 profile = defaultProfileLevel.mProfile;
1085 }
1086 if (!meta->findInt32(kKeyVideoLevel, &level)) {
1087 level = defaultProfileLevel.mLevel;
1088 }
1089 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
1090
1091 // Are the target profile and level supported by the encoder?
1092 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
1093 InitOMXParams(&param);
1094 param.nPortIndex = kPortIndexOutput;
1095 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
1096 status_t err = mOMX->getParameter(
1097 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
1098 &param, sizeof(param));
1099
James Dongdfb89912010-09-15 21:07:52 -07001100 if (err != OK) break;
James Dong81c929a2010-07-01 15:02:14 -07001101
1102 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
1103 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -07001104 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -07001105 supportedProfile, supportedLevel);
1106
1107 if (profile == supportedProfile &&
James Dongdfb89912010-09-15 21:07:52 -07001108 level <= supportedLevel) {
1109 // We can further check whether the level is a valid
1110 // value; but we will leave that to the omx encoder component
1111 // via OMX_SetParameter call.
James Dong81c929a2010-07-01 15:02:14 -07001112 profileLevel.mProfile = profile;
1113 profileLevel.mLevel = level;
1114 return OK;
1115 }
1116 }
1117
1118 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
1119 profile, level);
1120 return BAD_VALUE;
1121}
1122
James Dongc0ab2a62010-06-29 16:29:19 -07001123status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
1124 int32_t iFramesInterval, frameRate, bitRate;
1125 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001126 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dongc0ab2a62010-06-29 16:29:19 -07001127 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1128 CHECK(success);
1129 OMX_VIDEO_PARAM_H263TYPE h263type;
1130 InitOMXParams(&h263type);
1131 h263type.nPortIndex = kPortIndexOutput;
1132
1133 status_t err = mOMX->getParameter(
1134 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001135 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001136
1137 h263type.nAllowedPictureTypes =
1138 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1139
1140 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1141 if (h263type.nPFrames == 0) {
1142 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1143 }
1144 h263type.nBFrames = 0;
1145
James Dong81c929a2010-07-01 15:02:14 -07001146 // Check profile and level parameters
1147 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001148 defaultProfileLevel.mProfile = h263type.eProfile;
1149 defaultProfileLevel.mLevel = h263type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001150 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1151 if (err != OK) return err;
1152 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1153 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001154
1155 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1156 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1157 h263type.nPictureHeaderRepetition = 0;
1158 h263type.nGOBHeaderInterval = 0;
1159
1160 err = mOMX->setParameter(
1161 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001162 CHECK_EQ(err, (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001163
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001164 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
1165 CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK);
James Dongc0ab2a62010-06-29 16:29:19 -07001166
1167 return OK;
1168}
1169
James Dong1244eab2010-06-08 11:58:53 -07001170status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1171 int32_t iFramesInterval, frameRate, bitRate;
1172 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001173 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -07001174 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1175 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001176 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1177 InitOMXParams(&mpeg4type);
1178 mpeg4type.nPortIndex = kPortIndexOutput;
1179
1180 status_t err = mOMX->getParameter(
1181 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001182 CHECK_EQ(err, (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001183
1184 mpeg4type.nSliceHeaderSpacing = 0;
1185 mpeg4type.bSVH = OMX_FALSE;
1186 mpeg4type.bGov = OMX_FALSE;
1187
1188 mpeg4type.nAllowedPictureTypes =
1189 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1190
James Dong1244eab2010-06-08 11:58:53 -07001191 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1192 if (mpeg4type.nPFrames == 0) {
1193 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1194 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001195 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001196 mpeg4type.nIDCVLCThreshold = 0;
1197 mpeg4type.bACPred = OMX_TRUE;
1198 mpeg4type.nMaxPacketSize = 256;
1199 mpeg4type.nTimeIncRes = 1000;
1200 mpeg4type.nHeaderExtension = 0;
1201 mpeg4type.bReversibleVLC = OMX_FALSE;
1202
James Dong81c929a2010-07-01 15:02:14 -07001203 // Check profile and level parameters
1204 CodecProfileLevel defaultProfileLevel, profileLevel;
James Dong1e0e1662010-09-22 17:42:09 -07001205 defaultProfileLevel.mProfile = mpeg4type.eProfile;
1206 defaultProfileLevel.mLevel = mpeg4type.eLevel;
James Dong81c929a2010-07-01 15:02:14 -07001207 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1208 if (err != OK) return err;
1209 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1210 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001211
1212 err = mOMX->setParameter(
1213 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001214 CHECK_EQ(err, (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001215
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001216 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
1217 CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001218
1219 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001220}
1221
James Dong1244eab2010-06-08 11:58:53 -07001222status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1223 int32_t iFramesInterval, frameRate, bitRate;
1224 bool success = meta->findInt32(kKeyBitRate, &bitRate);
James Dongaac193c2010-11-10 20:43:53 -08001225 success = success && meta->findInt32(kKeyFrameRate, &frameRate);
James Dong1244eab2010-06-08 11:58:53 -07001226 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1227 CHECK(success);
1228
Andreas Huberea6a38c2009-11-16 15:43:38 -08001229 OMX_VIDEO_PARAM_AVCTYPE h264type;
1230 InitOMXParams(&h264type);
1231 h264type.nPortIndex = kPortIndexOutput;
1232
1233 status_t err = mOMX->getParameter(
1234 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001235 CHECK_EQ(err, (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001236
1237 h264type.nAllowedPictureTypes =
1238 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1239
1240 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001241 h264type.nBFrames = 0; // No B frames support yet
1242 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1243 if (h264type.nPFrames == 0) {
1244 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1245 }
James Dong81c929a2010-07-01 15:02:14 -07001246
1247 // Check profile and level parameters
1248 CodecProfileLevel defaultProfileLevel, profileLevel;
1249 defaultProfileLevel.mProfile = h264type.eProfile;
1250 defaultProfileLevel.mLevel = h264type.eLevel;
1251 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1252 if (err != OK) return err;
1253 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1254 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1255
1256 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1257 h264type.bUseHadamard = OMX_TRUE;
1258 h264type.nRefFrames = 1;
1259 h264type.nRefIdx10ActiveMinus1 = 0;
1260 h264type.nRefIdx11ActiveMinus1 = 0;
1261 h264type.bEntropyCodingCABAC = OMX_FALSE;
1262 h264type.bWeightedPPrediction = OMX_FALSE;
1263 h264type.bconstIpred = OMX_FALSE;
1264 h264type.bDirect8x8Inference = OMX_FALSE;
1265 h264type.bDirectSpatialTemporal = OMX_FALSE;
1266 h264type.nCabacInitIdc = 0;
1267 }
1268
1269 if (h264type.nBFrames != 0) {
1270 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1271 }
1272
Andreas Huberea6a38c2009-11-16 15:43:38 -08001273 h264type.bEnableUEP = OMX_FALSE;
1274 h264type.bEnableFMO = OMX_FALSE;
1275 h264type.bEnableASO = OMX_FALSE;
1276 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001277 h264type.bFrameMBsOnly = OMX_TRUE;
1278 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001279 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1280
pgudadhe9c305322010-07-26 13:59:29 -07001281 if (!strcasecmp("OMX.Nvidia.h264.encoder", mComponentName)) {
1282 h264type.eLevel = OMX_VIDEO_AVCLevelMax;
1283 }
1284
Andreas Huberea6a38c2009-11-16 15:43:38 -08001285 err = mOMX->setParameter(
1286 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001287 CHECK_EQ(err, (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001288
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001289 CHECK_EQ(setupBitRate(bitRate), (status_t)OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001290
1291 return OK;
1292}
1293
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001294status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001295 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001296 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001297
Andreas Huberbe06d262009-08-14 14:37:10 -07001298 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001299 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001300 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001301 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001302 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001303 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001304 compressionFormat = OMX_VIDEO_CodingH263;
1305 } else {
1306 LOGE("Not a supported video mime type: %s", mime);
1307 CHECK(!"Should not be here. Not a supported video mime type.");
1308 }
1309
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001310 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001311 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1312
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001313 if (err != OK) {
1314 return err;
1315 }
1316
Andreas Huberbe06d262009-08-14 14:37:10 -07001317#if 1
1318 {
1319 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001320 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001321 format.nPortIndex = kPortIndexOutput;
1322 format.nIndex = 0;
1323
Andreas Huber784202e2009-10-15 13:46:54 -07001324 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001325 mNode, OMX_IndexParamVideoPortFormat,
1326 &format, sizeof(format));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001327 CHECK_EQ(err, (status_t)OK);
1328 CHECK_EQ((int)format.eCompressionFormat, (int)OMX_VIDEO_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07001329
1330 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1331
1332 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1333 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1334 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1335 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1336
Andreas Huber784202e2009-10-15 13:46:54 -07001337 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001338 mNode, OMX_IndexParamVideoPortFormat,
1339 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001340
1341 if (err != OK) {
1342 return err;
1343 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001344 }
1345#endif
1346
1347 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001348 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001349 def.nPortIndex = kPortIndexInput;
1350
Andreas Huber4c483422009-09-02 16:05:36 -07001351 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1352
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001353 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001354 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1355
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001356 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001357
1358#if 1
1359 // XXX Need a (much) better heuristic to compute input buffer sizes.
1360 const size_t X = 64 * 1024;
1361 if (def.nBufferSize < X) {
1362 def.nBufferSize = X;
1363 }
1364#endif
1365
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001366 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -07001367
1368 video_def->nFrameWidth = width;
1369 video_def->nFrameHeight = height;
1370
Andreas Huberb482ce82009-10-29 12:02:48 -07001371 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001372 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1373
Andreas Huber784202e2009-10-15 13:46:54 -07001374 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001375 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001376
1377 if (err != OK) {
1378 return err;
1379 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001380
1381 ////////////////////////////////////////////////////////////////////////////
1382
Andreas Huber4c483422009-09-02 16:05:36 -07001383 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001384 def.nPortIndex = kPortIndexOutput;
1385
Andreas Huber784202e2009-10-15 13:46:54 -07001386 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001387 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001388 CHECK_EQ(err, (status_t)OK);
1389 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
Andreas Huberbe06d262009-08-14 14:37:10 -07001390
1391#if 0
1392 def.nBufferSize =
1393 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1394#endif
1395
1396 video_def->nFrameWidth = width;
1397 video_def->nFrameHeight = height;
1398
Andreas Huber784202e2009-10-15 13:46:54 -07001399 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001400 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001401
1402 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001403}
1404
Andreas Huberbe06d262009-08-14 14:37:10 -07001405OMXCodec::OMXCodec(
1406 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001407 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001408 const char *mime,
1409 const char *componentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001410 const sp<MediaSource> &source,
1411 const sp<ANativeWindow> &nativeWindow)
Andreas Huberbe06d262009-08-14 14:37:10 -07001412 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001413 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001414 mNode(node),
1415 mQuirks(quirks),
1416 mIsEncoder(isEncoder),
1417 mMIME(strdup(mime)),
1418 mComponentName(strdup(componentName)),
1419 mSource(source),
1420 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001421 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001422 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001423 mSignalledEOS(false),
1424 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001425 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001426 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001427 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1428 mTargetTimeUs(-1),
James Dong53d4e0d2010-07-21 14:51:35 -07001429 mSkipTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001430 mLeftOverBuffer(NULL),
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001431 mPaused(false),
1432 mNativeWindow(nativeWindow) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001433 mPortStatus[kPortIndexInput] = ENABLED;
1434 mPortStatus[kPortIndexOutput] = ENABLED;
1435
Andreas Huber4c483422009-09-02 16:05:36 -07001436 setComponentRole();
1437}
1438
Andreas Hubere6c40962009-09-10 14:13:30 -07001439// static
1440void OMXCodec::setComponentRole(
1441 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1442 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001443 struct MimeToRole {
1444 const char *mime;
1445 const char *decoderRole;
1446 const char *encoderRole;
1447 };
1448
1449 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001450 { MEDIA_MIMETYPE_AUDIO_MPEG,
1451 "audio_decoder.mp3", "audio_encoder.mp3" },
1452 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1453 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1454 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1455 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1456 { MEDIA_MIMETYPE_AUDIO_AAC,
1457 "audio_decoder.aac", "audio_encoder.aac" },
1458 { MEDIA_MIMETYPE_VIDEO_AVC,
1459 "video_decoder.avc", "video_encoder.avc" },
1460 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1461 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1462 { MEDIA_MIMETYPE_VIDEO_H263,
1463 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001464 };
1465
1466 static const size_t kNumMimeToRole =
1467 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1468
1469 size_t i;
1470 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001471 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001472 break;
1473 }
1474 }
1475
1476 if (i == kNumMimeToRole) {
1477 return;
1478 }
1479
1480 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001481 isEncoder ? kMimeToRole[i].encoderRole
1482 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001483
1484 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001485 OMX_PARAM_COMPONENTROLETYPE roleParams;
1486 InitOMXParams(&roleParams);
1487
1488 strncpy((char *)roleParams.cRole,
1489 role, OMX_MAX_STRINGNAME_SIZE - 1);
1490
1491 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1492
Andreas Huber784202e2009-10-15 13:46:54 -07001493 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001494 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001495 &roleParams, sizeof(roleParams));
1496
1497 if (err != OK) {
1498 LOGW("Failed to set standard component role '%s'.", role);
1499 }
1500 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001501}
1502
Andreas Hubere6c40962009-09-10 14:13:30 -07001503void OMXCodec::setComponentRole() {
1504 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1505}
1506
Andreas Huberbe06d262009-08-14 14:37:10 -07001507OMXCodec::~OMXCodec() {
Andreas Huberf98197a2010-09-17 11:49:39 -07001508 mSource.clear();
1509
Andreas Huber4f5e6022009-08-19 09:29:34 -07001510 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001511
Andreas Huber784202e2009-10-15 13:46:54 -07001512 status_t err = mOMX->freeNode(mNode);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001513 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001514
1515 mNode = NULL;
1516 setState(DEAD);
1517
1518 clearCodecSpecificData();
1519
1520 free(mComponentName);
1521 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001522
Andreas Huberbe06d262009-08-14 14:37:10 -07001523 free(mMIME);
1524 mMIME = NULL;
1525}
1526
1527status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001528 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001529
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001530 CHECK_EQ((int)mState, (int)LOADED);
Andreas Huberbe06d262009-08-14 14:37:10 -07001531
1532 status_t err;
1533 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001534 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001535 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001536 setState(LOADED_TO_IDLE);
1537 }
1538
1539 err = allocateBuffers();
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001540 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001541
1542 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001543 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001544 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001545
1546 setState(LOADED_TO_IDLE);
1547 }
1548
1549 while (mState != EXECUTING && mState != ERROR) {
1550 mAsyncCompletion.wait(mLock);
1551 }
1552
1553 return mState == ERROR ? UNKNOWN_ERROR : OK;
1554}
1555
1556// static
1557bool OMXCodec::isIntermediateState(State state) {
1558 return state == LOADED_TO_IDLE
1559 || state == IDLE_TO_EXECUTING
1560 || state == EXECUTING_TO_IDLE
1561 || state == IDLE_TO_LOADED
1562 || state == RECONFIGURING;
1563}
1564
1565status_t OMXCodec::allocateBuffers() {
1566 status_t err = allocateBuffersOnPort(kPortIndexInput);
1567
1568 if (err != OK) {
1569 return err;
1570 }
1571
1572 return allocateBuffersOnPort(kPortIndexOutput);
1573}
1574
1575status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
Jamie Gennisdbfb32e2010-10-20 15:53:59 -07001576 if (mNativeWindow != NULL && portIndex == kPortIndexOutput) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001577 return allocateOutputBuffersFromNativeWindow();
1578 }
1579
Andreas Huberbe06d262009-08-14 14:37:10 -07001580 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001581 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001582 def.nPortIndex = portIndex;
1583
Andreas Huber784202e2009-10-15 13:46:54 -07001584 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001585 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1586
1587 if (err != OK) {
1588 return err;
1589 }
1590
James Dong05c2fd52010-11-02 13:20:11 -07001591 if (mIsMetaDataStoredInVideoBuffers && portIndex == kPortIndexInput) {
1592 err = mOMX->storeMetaDataInBuffers(mNode, kPortIndexInput, OMX_TRUE);
1593 if (err != OK) {
1594 LOGE("Storing meta data in video buffers is not supported");
1595 return err;
1596 }
1597 }
1598
Andreas Huber57648e42010-08-04 10:14:30 -07001599 CODEC_LOGI("allocating %lu buffers of size %lu on %s port",
1600 def.nBufferCountActual, def.nBufferSize,
1601 portIndex == kPortIndexInput ? "input" : "output");
1602
Andreas Huber5c0a9132009-08-20 11:16:40 -07001603 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001604 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001605
Andreas Huberbe06d262009-08-14 14:37:10 -07001606 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001607 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001608 CHECK(mem.get() != NULL);
1609
Andreas Huberc712b9f2010-01-20 15:05:46 -08001610 BufferInfo info;
1611 info.mData = NULL;
1612 info.mSize = def.nBufferSize;
1613
Andreas Huberbe06d262009-08-14 14:37:10 -07001614 IOMX::buffer_id buffer;
1615 if (portIndex == kPortIndexInput
1616 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001617 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001618 mem.clear();
1619
Andreas Huberf1fe0642010-01-15 15:28:19 -08001620 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001621 mNode, portIndex, def.nBufferSize, &buffer,
1622 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001623 } else {
1624 err = mOMX->allocateBufferWithBackup(
1625 mNode, portIndex, mem, &buffer);
1626 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001627 } else if (portIndex == kPortIndexOutput
1628 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001629 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001630 mem.clear();
1631
Andreas Huberf1fe0642010-01-15 15:28:19 -08001632 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001633 mNode, portIndex, def.nBufferSize, &buffer,
1634 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001635 } else {
1636 err = mOMX->allocateBufferWithBackup(
1637 mNode, portIndex, mem, &buffer);
1638 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001639 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001640 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001641 }
1642
1643 if (err != OK) {
1644 LOGE("allocate_buffer_with_backup failed");
1645 return err;
1646 }
1647
Andreas Huberc712b9f2010-01-20 15:05:46 -08001648 if (mem != NULL) {
1649 info.mData = mem->pointer();
1650 }
1651
Andreas Huberbe06d262009-08-14 14:37:10 -07001652 info.mBuffer = buffer;
Andreas Huberbbbcf652010-12-07 14:25:54 -08001653 info.mStatus = OWNED_BY_US;
Andreas Huberbe06d262009-08-14 14:37:10 -07001654 info.mMem = mem;
1655 info.mMediaBuffer = NULL;
1656
1657 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001658 if (!(mOMXLivesLocally
1659 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1660 && (mQuirks & kDefersOutputBufferAllocation))) {
1661 // If the node does not fill in the buffer ptr at this time,
1662 // we will defer creating the MediaBuffer until receiving
1663 // the first FILL_BUFFER_DONE notification instead.
1664 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1665 info.mMediaBuffer->setObserver(this);
1666 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001667 }
1668
1669 mPortBuffers[portIndex].push(info);
1670
Andreas Huber4c483422009-09-02 16:05:36 -07001671 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001672 portIndex == kPortIndexInput ? "input" : "output");
1673 }
1674
Andreas Huber2ea14e22009-12-16 09:30:55 -08001675 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001676
1677 return OK;
1678}
1679
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001680status_t OMXCodec::allocateOutputBuffersFromNativeWindow() {
1681 // Get the number of buffers needed.
1682 OMX_PARAM_PORTDEFINITIONTYPE def;
1683 InitOMXParams(&def);
1684 def.nPortIndex = kPortIndexOutput;
1685
1686 status_t err = mOMX->getParameter(
1687 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1688 if (err != OK) {
1689 return err;
1690 }
1691
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001692 err = native_window_set_buffers_geometry(
1693 mNativeWindow.get(),
1694 def.format.video.nFrameWidth,
1695 def.format.video.nFrameHeight,
Jamie Gennis044ace62010-10-29 15:19:29 -07001696 def.format.video.eColorFormat);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001697
1698 if (err != 0) {
1699 LOGE("native_window_set_buffers_geometry failed: %s (%d)",
1700 strerror(-err), -err);
1701 return err;
1702 }
1703
1704 // Increase the buffer count by one to allow for the ANativeWindow to hold
1705 // on to one of the buffers.
1706 def.nBufferCountActual++;
1707 err = mOMX->setParameter(
1708 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1709 if (err != OK) {
1710 return err;
1711 }
1712
1713 // Set up the native window.
1714 // XXX TODO: Get the gralloc usage flags from the OMX plugin!
1715 err = native_window_set_usage(
Jamie Gennisb04c15032010-11-17 18:57:13 -08001716 mNativeWindow.get(), GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001717 if (err != 0) {
1718 LOGE("native_window_set_usage failed: %s (%d)", strerror(-err), -err);
1719 return err;
1720 }
1721
1722 err = native_window_set_buffer_count(
1723 mNativeWindow.get(), def.nBufferCountActual);
1724 if (err != 0) {
1725 LOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
1726 -err);
1727 return err;
1728 }
1729
1730 // XXX TODO: Do something so the ANativeWindow knows that we'll need to get
1731 // the same set of buffers.
1732
1733 CODEC_LOGI("allocating %lu buffers from a native window of size %lu on "
1734 "output port", def.nBufferCountActual, def.nBufferSize);
1735
1736 // Dequeue buffers and send them to OMX
1737 OMX_U32 i;
1738 for (i = 0; i < def.nBufferCountActual; i++) {
1739 android_native_buffer_t* buf;
1740 err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf);
1741 if (err != 0) {
1742 LOGE("dequeueBuffer failed: %s (%d)", strerror(-err), -err);
1743 break;
1744 }
1745
1746 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false));
1747 IOMX::buffer_id bufferId;
1748 err = mOMX->useGraphicBuffer(mNode, kPortIndexOutput, graphicBuffer,
1749 &bufferId);
1750 if (err != 0) {
1751 break;
1752 }
1753
1754 CODEC_LOGV("registered graphic buffer with ID %p (pointer = %p)",
1755 bufferId, graphicBuffer.get());
1756
1757 BufferInfo info;
1758 info.mData = NULL;
1759 info.mSize = def.nBufferSize;
1760 info.mBuffer = bufferId;
Andreas Huberbbbcf652010-12-07 14:25:54 -08001761 info.mStatus = OWNED_BY_US;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001762 info.mMem = NULL;
1763 info.mMediaBuffer = new MediaBuffer(graphicBuffer);
1764 info.mMediaBuffer->setObserver(this);
1765
1766 mPortBuffers[kPortIndexOutput].push(info);
1767 }
1768
1769 OMX_U32 cancelStart;
1770 OMX_U32 cancelEnd;
1771
1772 if (err != 0) {
1773 // If an error occurred while dequeuing we need to cancel any buffers
1774 // that were dequeued.
1775 cancelStart = 0;
1776 cancelEnd = i;
1777 } else {
1778 // Return the last two buffers to the native window.
1779 // XXX TODO: The number of buffers the native window owns should probably be
1780 // queried from it when we put the native window in fixed buffer pool mode
1781 // (which needs to be implemented). Currently it's hard-coded to 2.
1782 cancelStart = def.nBufferCountActual - 2;
1783 cancelEnd = def.nBufferCountActual;
1784 }
1785
1786 for (OMX_U32 i = cancelStart; i < cancelEnd; i++) {
1787 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(i);
1788 cancelBufferToNativeWindow(info);
1789 }
1790
1791 return err;
1792}
1793
1794status_t OMXCodec::cancelBufferToNativeWindow(BufferInfo *info) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08001795 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001796 CODEC_LOGV("Calling cancelBuffer on buffer %p", info->mBuffer);
1797 int err = mNativeWindow->cancelBuffer(
1798 mNativeWindow.get(), info->mMediaBuffer->graphicBuffer().get());
1799 if (err != 0) {
1800 CODEC_LOGE("cancelBuffer failed w/ error 0x%08x", err);
1801
1802 setState(ERROR);
1803 return err;
1804 }
Andreas Huberbbbcf652010-12-07 14:25:54 -08001805 info->mStatus = OWNED_BY_NATIVE_WINDOW;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001806 return OK;
1807}
1808
1809OMXCodec::BufferInfo* OMXCodec::dequeueBufferFromNativeWindow() {
1810 // Dequeue the next buffer from the native window.
1811 android_native_buffer_t* buf;
1812 int err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf);
1813 if (err != 0) {
1814 CODEC_LOGE("dequeueBuffer failed w/ error 0x%08x", err);
1815
1816 setState(ERROR);
1817 return 0;
1818 }
1819
1820 // Determine which buffer we just dequeued.
1821 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1822 BufferInfo *bufInfo = 0;
1823 for (size_t i = 0; i < buffers->size(); i++) {
1824 sp<GraphicBuffer> graphicBuffer = buffers->itemAt(i).
1825 mMediaBuffer->graphicBuffer();
1826 if (graphicBuffer->handle == buf->handle) {
1827 bufInfo = &buffers->editItemAt(i);
1828 break;
1829 }
1830 }
1831
1832 if (bufInfo == 0) {
1833 CODEC_LOGE("dequeued unrecognized buffer: %p", buf);
1834
1835 setState(ERROR);
1836 return 0;
1837 }
1838
1839 // The native window no longer owns the buffer.
Andreas Huberbbbcf652010-12-07 14:25:54 -08001840 CHECK_EQ((int)bufInfo->mStatus, (int)OWNED_BY_NATIVE_WINDOW);
1841 bufInfo->mStatus = OWNED_BY_US;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001842
1843 return bufInfo;
1844}
1845
Andreas Huberbe06d262009-08-14 14:37:10 -07001846void OMXCodec::on_message(const omx_message &msg) {
1847 Mutex::Autolock autoLock(mLock);
1848
1849 switch (msg.type) {
1850 case omx_message::EVENT:
1851 {
1852 onEvent(
1853 msg.u.event_data.event, msg.u.event_data.data1,
1854 msg.u.event_data.data2);
1855
1856 break;
1857 }
1858
1859 case omx_message::EMPTY_BUFFER_DONE:
1860 {
1861 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1862
Andreas Huber4c483422009-09-02 16:05:36 -07001863 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001864
1865 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1866 size_t i = 0;
1867 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1868 ++i;
1869 }
1870
1871 CHECK(i < buffers->size());
Andreas Huberbbbcf652010-12-07 14:25:54 -08001872 if ((*buffers)[i].mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001873 LOGW("We already own input buffer %p, yet received "
1874 "an EMPTY_BUFFER_DONE.", buffer);
1875 }
1876
James Dong05c2fd52010-11-02 13:20:11 -07001877 BufferInfo* info = &buffers->editItemAt(i);
Andreas Huberbbbcf652010-12-07 14:25:54 -08001878 info->mStatus = OWNED_BY_US;
James Dong05c2fd52010-11-02 13:20:11 -07001879
1880 // Buffer could not be released until empty buffer done is called.
1881 if (info->mMediaBuffer != NULL) {
James Dong31b9375f2010-11-10 21:11:41 -08001882 if (mIsEncoder &&
1883 (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
1884 // If zero-copy mode is enabled this will send the
1885 // input buffer back to the upstream source.
1886 restorePatchedDataPointer(info);
1887 }
1888
James Dong05c2fd52010-11-02 13:20:11 -07001889 info->mMediaBuffer->release();
1890 info->mMediaBuffer = NULL;
1891 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001892
1893 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001894 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001895
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07001896 status_t err = freeBuffer(kPortIndexInput, i);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001897 CHECK_EQ(err, (status_t)OK);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001898 } else if (mState != ERROR
1899 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001900 CHECK_EQ((int)mPortStatus[kPortIndexInput], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07001901 drainInputBuffer(&buffers->editItemAt(i));
1902 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001903 break;
1904 }
1905
1906 case omx_message::FILL_BUFFER_DONE:
1907 {
1908 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1909 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1910
Andreas Huber2ea14e22009-12-16 09:30:55 -08001911 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001912 buffer,
1913 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001914 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001915 msg.u.extended_buffer_data.timestamp,
1916 msg.u.extended_buffer_data.timestamp / 1E6);
1917
1918 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1919 size_t i = 0;
1920 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1921 ++i;
1922 }
1923
1924 CHECK(i < buffers->size());
1925 BufferInfo *info = &buffers->editItemAt(i);
1926
Andreas Huberbbbcf652010-12-07 14:25:54 -08001927 if (info->mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001928 LOGW("We already own output buffer %p, yet received "
1929 "a FILL_BUFFER_DONE.", buffer);
1930 }
1931
Andreas Huberbbbcf652010-12-07 14:25:54 -08001932 info->mStatus = OWNED_BY_US;
Andreas Huberbe06d262009-08-14 14:37:10 -07001933
1934 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001935 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001936
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07001937 status_t err = freeBuffer(kPortIndexOutput, i);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001938 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001939
Andreas Huber2ea14e22009-12-16 09:30:55 -08001940#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001941 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1942 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001943 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001944 mNoMoreOutputData = true;
1945 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001946#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001947 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08001948 CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001949
Andreas Huber52733b82010-01-25 10:41:35 -08001950 if (info->mMediaBuffer == NULL) {
1951 CHECK(mOMXLivesLocally);
1952 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1953 CHECK(mQuirks & kDefersOutputBufferAllocation);
1954
1955 // The qcom video decoders on Nexus don't actually allocate
1956 // output buffer memory on a call to OMX_AllocateBuffer
1957 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1958 // structure is only filled in later.
1959
1960 info->mMediaBuffer = new MediaBuffer(
1961 msg.u.extended_buffer_data.data_ptr,
1962 info->mSize);
1963 info->mMediaBuffer->setObserver(this);
1964 }
1965
Andreas Huberbe06d262009-08-14 14:37:10 -07001966 MediaBuffer *buffer = info->mMediaBuffer;
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001967 bool isGraphicBuffer = buffer->graphicBuffer() != NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -07001968
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001969 if (!isGraphicBuffer
1970 && msg.u.extended_buffer_data.range_offset
Andreas Huberf88f8442010-08-10 11:18:36 -07001971 + msg.u.extended_buffer_data.range_length
1972 > buffer->size()) {
1973 CODEC_LOGE(
1974 "Codec lied about its buffer size requirements, "
1975 "sending a buffer larger than the originally "
1976 "advertised size in FILL_BUFFER_DONE!");
1977 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001978 buffer->set_range(
1979 msg.u.extended_buffer_data.range_offset,
1980 msg.u.extended_buffer_data.range_length);
1981
1982 buffer->meta_data()->clear();
1983
Andreas Huberfa8de752009-10-08 10:07:49 -07001984 buffer->meta_data()->setInt64(
1985 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001986
1987 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1988 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1989 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001990 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1991 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1992 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001993
Jamie Gennis58a36ad2010-10-07 14:08:38 -07001994 if (isGraphicBuffer || mQuirks & kOutputBuffersAreUnreadable) {
Andreas Huber1e194162010-10-06 16:43:57 -07001995 buffer->meta_data()->setInt32(kKeyIsUnreadable, true);
1996 }
1997
Andreas Huberbe06d262009-08-14 14:37:10 -07001998 buffer->meta_data()->setPointer(
1999 kKeyPlatformPrivate,
2000 msg.u.extended_buffer_data.platform_private);
2001
2002 buffer->meta_data()->setPointer(
2003 kKeyBufferID,
2004 msg.u.extended_buffer_data.buffer);
2005
Andreas Huber2ea14e22009-12-16 09:30:55 -08002006 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
2007 CODEC_LOGV("No more output data.");
2008 mNoMoreOutputData = true;
2009 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002010
2011 if (mTargetTimeUs >= 0) {
2012 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
2013
2014 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
2015 CODEC_LOGV(
2016 "skipping output buffer at timestamp %lld us",
2017 msg.u.extended_buffer_data.timestamp);
2018
2019 fillOutputBuffer(info);
2020 break;
2021 }
2022
2023 CODEC_LOGV(
2024 "returning output buffer at target timestamp "
2025 "%lld us",
2026 msg.u.extended_buffer_data.timestamp);
2027
2028 mTargetTimeUs = -1;
2029 }
2030
2031 mFilledBuffers.push_back(i);
2032 mBufferFilled.signal();
Andreas Huberbe06d262009-08-14 14:37:10 -07002033 }
2034
2035 break;
2036 }
2037
2038 default:
2039 {
2040 CHECK(!"should not be here.");
2041 break;
2042 }
2043 }
2044}
2045
Andreas Huberb1678602009-10-19 13:06:40 -07002046// Has the format changed in any way that the client would have to be aware of?
2047static bool formatHasNotablyChanged(
2048 const sp<MetaData> &from, const sp<MetaData> &to) {
2049 if (from.get() == NULL && to.get() == NULL) {
2050 return false;
2051 }
2052
Andreas Huberf68c1682009-10-21 14:01:30 -07002053 if ((from.get() == NULL && to.get() != NULL)
2054 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07002055 return true;
2056 }
2057
2058 const char *mime_from, *mime_to;
2059 CHECK(from->findCString(kKeyMIMEType, &mime_from));
2060 CHECK(to->findCString(kKeyMIMEType, &mime_to));
2061
2062 if (strcasecmp(mime_from, mime_to)) {
2063 return true;
2064 }
2065
2066 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
2067 int32_t colorFormat_from, colorFormat_to;
2068 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
2069 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
2070
2071 if (colorFormat_from != colorFormat_to) {
2072 return true;
2073 }
2074
2075 int32_t width_from, width_to;
2076 CHECK(from->findInt32(kKeyWidth, &width_from));
2077 CHECK(to->findInt32(kKeyWidth, &width_to));
2078
2079 if (width_from != width_to) {
2080 return true;
2081 }
2082
2083 int32_t height_from, height_to;
2084 CHECK(from->findInt32(kKeyHeight, &height_from));
2085 CHECK(to->findInt32(kKeyHeight, &height_to));
2086
2087 if (height_from != height_to) {
2088 return true;
2089 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002090
2091 int32_t left_from, top_from, right_from, bottom_from;
2092 CHECK(from->findRect(
2093 kKeyCropRect,
2094 &left_from, &top_from, &right_from, &bottom_from));
2095
2096 int32_t left_to, top_to, right_to, bottom_to;
2097 CHECK(to->findRect(
2098 kKeyCropRect,
2099 &left_to, &top_to, &right_to, &bottom_to));
2100
2101 if (left_to != left_from || top_to != top_from
2102 || right_to != right_from || bottom_to != bottom_from) {
2103 return true;
2104 }
Andreas Huberb1678602009-10-19 13:06:40 -07002105 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
2106 int32_t numChannels_from, numChannels_to;
2107 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
2108 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
2109
2110 if (numChannels_from != numChannels_to) {
2111 return true;
2112 }
2113
2114 int32_t sampleRate_from, sampleRate_to;
2115 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
2116 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
2117
2118 if (sampleRate_from != sampleRate_to) {
2119 return true;
2120 }
2121 }
2122
2123 return false;
2124}
2125
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002126void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
2127 switch (event) {
2128 case OMX_EventCmdComplete:
2129 {
2130 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
2131 break;
2132 }
2133
2134 case OMX_EventError:
2135 {
2136 CODEC_LOGE("ERROR(0x%08lx, %ld)", data1, data2);
2137
2138 setState(ERROR);
2139 break;
2140 }
2141
2142 case OMX_EventPortSettingsChanged:
2143 {
2144 CODEC_LOGV("OMX_EventPortSettingsChanged(port=%ld, data2=0x%08lx)",
2145 data1, data2);
2146
2147 if (data2 == 0 || data2 == OMX_IndexParamPortDefinition) {
2148 onPortSettingsChanged(data1);
2149 } else if (data1 == kPortIndexOutput
2150 && data2 == OMX_IndexConfigCommonOutputCrop) {
2151
2152 sp<MetaData> oldOutputFormat = mOutputFormat;
2153 initOutputFormat(mSource->getFormat());
2154
2155 if (formatHasNotablyChanged(oldOutputFormat, mOutputFormat)) {
2156 mOutputPortSettingsHaveChanged = true;
2157
2158 if (mNativeWindow != NULL) {
2159 int32_t left, top, right, bottom;
2160 CHECK(mOutputFormat->findRect(
2161 kKeyCropRect,
2162 &left, &top, &right, &bottom));
2163
2164 android_native_rect_t crop;
2165 crop.left = left;
2166 crop.top = top;
2167 crop.right = right;
2168 crop.bottom = bottom;
2169
2170 CHECK_EQ(0, native_window_set_crop(
2171 mNativeWindow.get(), &crop));
2172 }
2173 }
2174 }
2175 break;
2176 }
2177
2178#if 0
2179 case OMX_EventBufferFlag:
2180 {
2181 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
2182
2183 if (data1 == kPortIndexOutput) {
2184 mNoMoreOutputData = true;
2185 }
2186 break;
2187 }
2188#endif
2189
2190 default:
2191 {
2192 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
2193 break;
2194 }
2195 }
2196}
2197
Andreas Huberbe06d262009-08-14 14:37:10 -07002198void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
2199 switch (cmd) {
2200 case OMX_CommandStateSet:
2201 {
2202 onStateChange((OMX_STATETYPE)data);
2203 break;
2204 }
2205
2206 case OMX_CommandPortDisable:
2207 {
2208 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07002209 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002210
2211 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002212 CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLING);
2213 CHECK_EQ(mPortBuffers[portIndex].size(), 0u);
Andreas Huberbe06d262009-08-14 14:37:10 -07002214
2215 mPortStatus[portIndex] = DISABLED;
2216
2217 if (mState == RECONFIGURING) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002218 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002219
Andreas Huberb1678602009-10-19 13:06:40 -07002220 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07002221 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07002222
2223 // Don't notify clients if the output port settings change
2224 // wasn't of importance to them, i.e. it may be that just the
2225 // number of buffers has changed and nothing else.
2226 mOutputPortSettingsHaveChanged =
2227 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07002228
Andreas Huberbe06d262009-08-14 14:37:10 -07002229 enablePortAsync(portIndex);
2230
2231 status_t err = allocateBuffersOnPort(portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002232 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002233 }
2234 break;
2235 }
2236
2237 case OMX_CommandPortEnable:
2238 {
2239 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07002240 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002241
2242 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002243 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002244
2245 mPortStatus[portIndex] = ENABLED;
2246
2247 if (mState == RECONFIGURING) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002248 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002249
2250 setState(EXECUTING);
2251
2252 fillOutputBuffers();
2253 }
2254 break;
2255 }
2256
2257 case OMX_CommandFlush:
2258 {
2259 OMX_U32 portIndex = data;
2260
Andreas Huber4c483422009-09-02 16:05:36 -07002261 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002262
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002263 CHECK_EQ((int)mPortStatus[portIndex], (int)SHUTTING_DOWN);
Andreas Huberbe06d262009-08-14 14:37:10 -07002264 mPortStatus[portIndex] = ENABLED;
2265
2266 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
2267 mPortBuffers[portIndex].size());
2268
2269 if (mState == RECONFIGURING) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002270 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002271
2272 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002273 } else if (mState == EXECUTING_TO_IDLE) {
2274 if (mPortStatus[kPortIndexInput] == ENABLED
2275 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002276 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002277 "transition from EXECUTING to IDLE.");
2278
2279 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2280 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2281
2282 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002283 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002284 CHECK_EQ(err, (status_t)OK);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002285 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002286 } else {
2287 // We're flushing both ports in preparation for seeking.
2288
2289 if (mPortStatus[kPortIndexInput] == ENABLED
2290 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07002291 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07002292 " seek-time.");
2293
Andreas Huber1f24b302010-06-10 11:12:39 -07002294 // We implicitly resume pulling on our upstream source.
2295 mPaused = false;
2296
Andreas Huberbe06d262009-08-14 14:37:10 -07002297 drainInputBuffers();
2298 fillOutputBuffers();
2299 }
2300 }
2301
2302 break;
2303 }
2304
2305 default:
2306 {
Andreas Huber4c483422009-09-02 16:05:36 -07002307 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07002308 break;
2309 }
2310 }
2311}
2312
2313void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002314 CODEC_LOGV("onStateChange %d", newState);
2315
Andreas Huberbe06d262009-08-14 14:37:10 -07002316 switch (newState) {
2317 case OMX_StateIdle:
2318 {
Andreas Huber4c483422009-09-02 16:05:36 -07002319 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002320 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07002321 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002322 mNode, OMX_CommandStateSet, OMX_StateExecuting);
2323
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002324 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002325
2326 setState(IDLE_TO_EXECUTING);
2327 } else {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002328 CHECK_EQ((int)mState, (int)EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002329
2330 CHECK_EQ(
2331 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
2332 mPortBuffers[kPortIndexInput].size());
2333
2334 CHECK_EQ(
2335 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
2336 mPortBuffers[kPortIndexOutput].size());
2337
Andreas Huber784202e2009-10-15 13:46:54 -07002338 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002339 mNode, OMX_CommandStateSet, OMX_StateLoaded);
2340
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002341 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002342
2343 err = freeBuffersOnPort(kPortIndexInput);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002344 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002345
2346 err = freeBuffersOnPort(kPortIndexOutput);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002347 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002348
2349 mPortStatus[kPortIndexInput] = ENABLED;
2350 mPortStatus[kPortIndexOutput] = ENABLED;
2351
2352 setState(IDLE_TO_LOADED);
2353 }
2354 break;
2355 }
2356
2357 case OMX_StateExecuting:
2358 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002359 CHECK_EQ((int)mState, (int)IDLE_TO_EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002360
Andreas Huber4c483422009-09-02 16:05:36 -07002361 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002362
2363 setState(EXECUTING);
2364
Andreas Huber42978e52009-08-27 10:08:39 -07002365 // Buffers will be submitted to the component in the first
2366 // call to OMXCodec::read as mInitialBufferSubmit is true at
2367 // this point. This ensures that this on_message call returns,
2368 // releases the lock and ::init can notice the state change and
2369 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07002370 break;
2371 }
2372
2373 case OMX_StateLoaded:
2374 {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002375 CHECK_EQ((int)mState, (int)IDLE_TO_LOADED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002376
Andreas Huber4c483422009-09-02 16:05:36 -07002377 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002378
2379 setState(LOADED);
2380 break;
2381 }
2382
Andreas Huberc712b9f2010-01-20 15:05:46 -08002383 case OMX_StateInvalid:
2384 {
2385 setState(ERROR);
2386 break;
2387 }
2388
Andreas Huberbe06d262009-08-14 14:37:10 -07002389 default:
2390 {
2391 CHECK(!"should not be here.");
2392 break;
2393 }
2394 }
2395}
2396
2397// static
2398size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
2399 size_t n = 0;
2400 for (size_t i = 0; i < buffers.size(); ++i) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002401 if (buffers[i].mStatus != OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002402 ++n;
2403 }
2404 }
2405
2406 return n;
2407}
2408
2409status_t OMXCodec::freeBuffersOnPort(
2410 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2411 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2412
2413 status_t stickyErr = OK;
2414
2415 for (size_t i = buffers->size(); i-- > 0;) {
2416 BufferInfo *info = &buffers->editItemAt(i);
2417
Andreas Huberbbbcf652010-12-07 14:25:54 -08002418 if (onlyThoseWeOwn && info->mStatus == OWNED_BY_COMPONENT) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002419 continue;
2420 }
2421
Andreas Huberbbbcf652010-12-07 14:25:54 -08002422 CHECK(info->mStatus == OWNED_BY_US
2423 || info->mStatus == OWNED_BY_NATIVE_WINDOW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002424
Andreas Huber92022852009-09-14 15:24:14 -07002425 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2426
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002427 status_t err = freeBuffer(portIndex, i);
Andreas Huberbe06d262009-08-14 14:37:10 -07002428
2429 if (err != OK) {
2430 stickyErr = err;
2431 }
2432
Andreas Huberbe06d262009-08-14 14:37:10 -07002433 }
2434
2435 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2436
2437 return stickyErr;
2438}
2439
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002440status_t OMXCodec::freeBuffer(OMX_U32 portIndex, size_t bufIndex) {
2441 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2442
2443 BufferInfo *info = &buffers->editItemAt(bufIndex);
2444
2445 status_t err = mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
2446
2447 if (err == OK && info->mMediaBuffer != NULL) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002448 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002449 info->mMediaBuffer->setObserver(NULL);
2450
2451 // Make sure nobody but us owns this buffer at this point.
2452 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2453
2454 // Cancel the buffer if it belongs to an ANativeWindow.
2455 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer();
Andreas Huberbbbcf652010-12-07 14:25:54 -08002456 if (info->mStatus == OWNED_BY_US && graphicBuffer != 0) {
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002457 err = cancelBufferToNativeWindow(info);
2458 }
2459
2460 info->mMediaBuffer->release();
James Dong31b9375f2010-11-10 21:11:41 -08002461 info->mMediaBuffer = NULL;
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -07002462 }
2463
2464 if (err == OK) {
2465 buffers->removeAt(bufIndex);
2466 }
2467
2468 return err;
2469}
2470
Andreas Huberbe06d262009-08-14 14:37:10 -07002471void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002472 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002473
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002474 CHECK_EQ((int)mState, (int)EXECUTING);
2475 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput);
Andreas Huberbe06d262009-08-14 14:37:10 -07002476 setState(RECONFIGURING);
2477
2478 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002479 if (!flushPortAsync(portIndex)) {
2480 onCmdComplete(OMX_CommandFlush, portIndex);
2481 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002482 } else {
2483 disablePortAsync(portIndex);
2484 }
2485}
2486
Andreas Huber404cc412009-08-25 14:26:05 -07002487bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002488 CHECK(mState == EXECUTING || mState == RECONFIGURING
2489 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002490
Andreas Huber4c483422009-09-02 16:05:36 -07002491 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002492 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2493 mPortBuffers[portIndex].size());
2494
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002495 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002496 mPortStatus[portIndex] = SHUTTING_DOWN;
2497
Andreas Huber404cc412009-08-25 14:26:05 -07002498 if ((mQuirks & kRequiresFlushCompleteEmulation)
2499 && countBuffersWeOwn(mPortBuffers[portIndex])
2500 == mPortBuffers[portIndex].size()) {
2501 // No flush is necessary and this component fails to send a
2502 // flush-complete event in this case.
2503
2504 return false;
2505 }
2506
Andreas Huberbe06d262009-08-14 14:37:10 -07002507 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002508 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002509 CHECK_EQ(err, (status_t)OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002510
2511 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002512}
2513
2514void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2515 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2516
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002517 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002518 mPortStatus[portIndex] = DISABLING;
2519
Andreas Huberd222c842010-08-26 14:29:34 -07002520 CODEC_LOGV("sending OMX_CommandPortDisable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002521 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002522 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002523 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002524
2525 freeBuffersOnPort(portIndex, true);
2526}
2527
2528void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2529 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2530
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002531 CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLED);
Andreas Huberbe06d262009-08-14 14:37:10 -07002532 mPortStatus[portIndex] = ENABLING;
2533
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002534 CODEC_LOGV("sending OMX_CommandPortEnable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002535 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002536 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002537 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002538}
2539
2540void OMXCodec::fillOutputBuffers() {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002541 CHECK_EQ((int)mState, (int)EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002542
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002543 // This is a workaround for some decoders not properly reporting
2544 // end-of-output-stream. If we own all input buffers and also own
2545 // all output buffers and we already signalled end-of-input-stream,
2546 // the end-of-output-stream is implied.
2547 if (mSignalledEOS
2548 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2549 == mPortBuffers[kPortIndexInput].size()
2550 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2551 == mPortBuffers[kPortIndexOutput].size()) {
2552 mNoMoreOutputData = true;
2553 mBufferFilled.signal();
2554
2555 return;
2556 }
2557
Andreas Huberbe06d262009-08-14 14:37:10 -07002558 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2559 for (size_t i = 0; i < buffers->size(); ++i) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002560 BufferInfo *info = &buffers->editItemAt(i);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002561 if (info->mStatus == OWNED_BY_US) {
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002562 fillOutputBuffer(&buffers->editItemAt(i));
2563 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002564 }
2565}
2566
2567void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002568 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002569
2570 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2571 for (size_t i = 0; i < buffers->size(); ++i) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002572 if (!drainInputBuffer(&buffers->editItemAt(i))) {
2573 break;
2574 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002575 }
2576}
2577
Andreas Huberbbbcf652010-12-07 14:25:54 -08002578bool OMXCodec::drainInputBuffer(BufferInfo *info) {
2579 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Andreas Huberbe06d262009-08-14 14:37:10 -07002580
2581 if (mSignalledEOS) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002582 return false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002583 }
2584
2585 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2586 const CodecSpecificData *specific =
2587 mCodecSpecificData[mCodecSpecificDataIndex];
2588
2589 size_t size = specific->mSize;
2590
Andreas Hubere6c40962009-09-10 14:13:30 -07002591 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002592 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002593 static const uint8_t kNALStartCode[4] =
2594 { 0x00, 0x00, 0x00, 0x01 };
2595
Andreas Huberc712b9f2010-01-20 15:05:46 -08002596 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002597
2598 size += 4;
2599
Andreas Huberc712b9f2010-01-20 15:05:46 -08002600 memcpy(info->mData, kNALStartCode, 4);
2601 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002602 specific->mData, specific->mSize);
2603 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002604 CHECK(info->mSize >= specific->mSize);
2605 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002606 }
2607
Andreas Huber2ea14e22009-12-16 09:30:55 -08002608 mNoMoreOutputData = false;
2609
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002610 CODEC_LOGV("calling emptyBuffer with codec specific data");
2611
Andreas Huber784202e2009-10-15 13:46:54 -07002612 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002613 mNode, info->mBuffer, 0, size,
2614 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2615 0);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002616 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002617
Andreas Huberbbbcf652010-12-07 14:25:54 -08002618 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberbe06d262009-08-14 14:37:10 -07002619
2620 ++mCodecSpecificDataIndex;
Andreas Huberbbbcf652010-12-07 14:25:54 -08002621 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002622 }
2623
Andreas Huber1f24b302010-06-10 11:12:39 -07002624 if (mPaused) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002625 return false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002626 }
2627
Andreas Huberbe06d262009-08-14 14:37:10 -07002628 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002629
Andreas Hubera4357ad2010-04-02 12:49:54 -07002630 bool signalEOS = false;
2631 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002632
Andreas Hubera4357ad2010-04-02 12:49:54 -07002633 size_t offset = 0;
2634 int32_t n = 0;
Andreas Huberbbbcf652010-12-07 14:25:54 -08002635
Andreas Hubera4357ad2010-04-02 12:49:54 -07002636 for (;;) {
2637 MediaBuffer *srcBuffer;
James Dong53d4e0d2010-07-21 14:51:35 -07002638 MediaSource::ReadOptions options;
2639 if (mSkipTimeUs >= 0) {
2640 options.setSkipFrame(mSkipTimeUs);
2641 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002642 if (mSeekTimeUs >= 0) {
2643 if (mLeftOverBuffer) {
2644 mLeftOverBuffer->release();
2645 mLeftOverBuffer = NULL;
2646 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002647 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002648
2649 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002650 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002651 mBufferFilled.signal();
2652
2653 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002654
2655 if (err == OK) {
2656 int64_t targetTimeUs;
2657 if (srcBuffer->meta_data()->findInt64(
2658 kKeyTargetTime, &targetTimeUs)
2659 && targetTimeUs >= 0) {
2660 mTargetTimeUs = targetTimeUs;
2661 } else {
2662 mTargetTimeUs = -1;
2663 }
2664 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002665 } else if (mLeftOverBuffer) {
2666 srcBuffer = mLeftOverBuffer;
2667 mLeftOverBuffer = NULL;
2668
2669 err = OK;
2670 } else {
James Dong53d4e0d2010-07-21 14:51:35 -07002671 err = mSource->read(&srcBuffer, &options);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002672 }
2673
2674 if (err != OK) {
2675 signalEOS = true;
2676 mFinalStatus = err;
2677 mSignalledEOS = true;
2678 break;
2679 }
2680
2681 size_t remainingBytes = info->mSize - offset;
2682
2683 if (srcBuffer->range_length() > remainingBytes) {
2684 if (offset == 0) {
2685 CODEC_LOGE(
2686 "Codec's input buffers are too small to accomodate "
2687 "buffer read from source (info->mSize = %d, srcLength = %d)",
2688 info->mSize, srcBuffer->range_length());
2689
2690 srcBuffer->release();
2691 srcBuffer = NULL;
2692
2693 setState(ERROR);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002694 return false;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002695 }
2696
2697 mLeftOverBuffer = srcBuffer;
2698 break;
2699 }
2700
James Dong05c2fd52010-11-02 13:20:11 -07002701 bool releaseBuffer = true;
James Dong4f501f02010-06-07 14:41:41 -07002702 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2703 CHECK(mOMXLivesLocally && offset == 0);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002704
2705 OMX_BUFFERHEADERTYPE *header =
2706 (OMX_BUFFERHEADERTYPE *)info->mBuffer;
2707
James Dong31b9375f2010-11-10 21:11:41 -08002708 CHECK(header->pBuffer == info->mData);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002709
2710 header->pBuffer =
2711 (OMX_U8 *)srcBuffer->data() + srcBuffer->range_offset();
2712
James Dong05c2fd52010-11-02 13:20:11 -07002713 releaseBuffer = false;
2714 info->mMediaBuffer = srcBuffer;
James Dong4f501f02010-06-07 14:41:41 -07002715 } else {
James Dong05c2fd52010-11-02 13:20:11 -07002716 if (mIsMetaDataStoredInVideoBuffers) {
2717 releaseBuffer = false;
2718 info->mMediaBuffer = srcBuffer;
2719 }
James Dong4f501f02010-06-07 14:41:41 -07002720 memcpy((uint8_t *)info->mData + offset,
Andreas Huberbbbcf652010-12-07 14:25:54 -08002721 (const uint8_t *)srcBuffer->data()
2722 + srcBuffer->range_offset(),
James Dong4f501f02010-06-07 14:41:41 -07002723 srcBuffer->range_length());
2724 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002725
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002726 int64_t lastBufferTimeUs;
2727 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002728 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002729
Andreas Hubera4357ad2010-04-02 12:49:54 -07002730 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002731 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002732 }
2733
2734 offset += srcBuffer->range_length();
2735
James Dong05c2fd52010-11-02 13:20:11 -07002736 if (releaseBuffer) {
2737 srcBuffer->release();
2738 srcBuffer = NULL;
2739 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002740
2741 ++n;
2742
2743 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2744 break;
2745 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002746
2747 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2748
2749 if (coalescedDurationUs > 250000ll) {
2750 // Don't coalesce more than 250ms worth of encoded data at once.
2751 break;
2752 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002753 }
2754
2755 if (n > 1) {
2756 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002757 }
2758
2759 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002760
Andreas Hubera4357ad2010-04-02 12:49:54 -07002761 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002762 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002763 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002764 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002765 }
2766
Andreas Hubera4357ad2010-04-02 12:49:54 -07002767 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2768 "timestamp %lld us (%.2f secs)",
2769 info->mBuffer, offset,
2770 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002771
Andreas Huber784202e2009-10-15 13:46:54 -07002772 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002773 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002774 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002775
2776 if (err != OK) {
2777 setState(ERROR);
Andreas Huberbbbcf652010-12-07 14:25:54 -08002778 return false;
Andreas Huber3f427072009-10-08 11:02:27 -07002779 }
2780
Andreas Huberbbbcf652010-12-07 14:25:54 -08002781 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002782
2783 // This component does not ever signal the EOS flag on output buffers,
2784 // Thanks for nothing.
2785 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2786 mNoMoreOutputData = true;
2787 mBufferFilled.signal();
2788 }
Andreas Huberbbbcf652010-12-07 14:25:54 -08002789
2790 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002791}
2792
2793void OMXCodec::fillOutputBuffer(BufferInfo *info) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002794 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
Andreas Huberbe06d262009-08-14 14:37:10 -07002795
Andreas Huber404cc412009-08-25 14:26:05 -07002796 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002797 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002798 "calling fillOutputBuffer");
2799 return;
2800 }
2801
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002802 if (info->mMediaBuffer != NULL) {
2803 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer();
2804 if (graphicBuffer != 0) {
2805 // When using a native buffer we need to lock the buffer before
2806 // giving it to OMX.
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002807 CODEC_LOGV("Calling lockBuffer on %p", info->mBuffer);
2808 int err = mNativeWindow->lockBuffer(mNativeWindow.get(),
2809 graphicBuffer.get());
2810 if (err != 0) {
2811 CODEC_LOGE("lockBuffer failed w/ error 0x%08x", err);
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002812
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002813 setState(ERROR);
2814 return;
2815 }
Jamie Gennis58a36ad2010-10-07 14:08:38 -07002816 }
2817 }
2818
2819 CODEC_LOGV("Calling fillBuffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002820 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002821
2822 if (err != OK) {
2823 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2824
2825 setState(ERROR);
2826 return;
2827 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002828
Andreas Huberbbbcf652010-12-07 14:25:54 -08002829 info->mStatus = OWNED_BY_COMPONENT;
Andreas Huberbe06d262009-08-14 14:37:10 -07002830}
2831
Andreas Huberbbbcf652010-12-07 14:25:54 -08002832bool OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002833 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2834 for (size_t i = 0; i < buffers->size(); ++i) {
2835 if ((*buffers)[i].mBuffer == buffer) {
Andreas Huberbbbcf652010-12-07 14:25:54 -08002836 return drainInputBuffer(&buffers->editItemAt(i));
Andreas Huberbe06d262009-08-14 14:37:10 -07002837 }
2838 }
2839
2840 CHECK(!"should not be here.");
Andreas Huberbbbcf652010-12-07 14:25:54 -08002841
2842 return false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002843}
2844
2845void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2846 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2847 for (size_t i = 0; i < buffers->size(); ++i) {
2848 if ((*buffers)[i].mBuffer == buffer) {
2849 fillOutputBuffer(&buffers->editItemAt(i));
2850 return;
2851 }
2852 }
2853
2854 CHECK(!"should not be here.");
2855}
2856
2857void OMXCodec::setState(State newState) {
2858 mState = newState;
2859 mAsyncCompletion.signal();
2860
2861 // This may cause some spurious wakeups but is necessary to
2862 // unblock the reader if we enter ERROR state.
2863 mBufferFilled.signal();
2864}
2865
Andreas Huberda050cf22009-09-02 14:01:43 -07002866void OMXCodec::setRawAudioFormat(
2867 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002868
2869 // port definition
2870 OMX_PARAM_PORTDEFINITIONTYPE def;
2871 InitOMXParams(&def);
2872 def.nPortIndex = portIndex;
2873 status_t err = mOMX->getParameter(
2874 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002875 CHECK_EQ(err, (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07002876 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2877 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002878 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07002879
2880 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002881 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002882 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002883 pcmParams.nPortIndex = portIndex;
2884
James Dongabed93a2010-04-22 17:27:04 -07002885 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002886 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2887
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002888 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07002889
2890 pcmParams.nChannels = numChannels;
2891 pcmParams.eNumData = OMX_NumericalDataSigned;
2892 pcmParams.bInterleaved = OMX_TRUE;
2893 pcmParams.nBitPerSample = 16;
2894 pcmParams.nSamplingRate = sampleRate;
2895 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2896
2897 if (numChannels == 1) {
2898 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2899 } else {
2900 CHECK_EQ(numChannels, 2);
2901
2902 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2903 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2904 }
2905
Andreas Huber784202e2009-10-15 13:46:54 -07002906 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002907 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2908
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002909 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07002910}
2911
James Dong17299ab2010-05-14 15:45:22 -07002912static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2913 if (isAMRWB) {
2914 if (bps <= 6600) {
2915 return OMX_AUDIO_AMRBandModeWB0;
2916 } else if (bps <= 8850) {
2917 return OMX_AUDIO_AMRBandModeWB1;
2918 } else if (bps <= 12650) {
2919 return OMX_AUDIO_AMRBandModeWB2;
2920 } else if (bps <= 14250) {
2921 return OMX_AUDIO_AMRBandModeWB3;
2922 } else if (bps <= 15850) {
2923 return OMX_AUDIO_AMRBandModeWB4;
2924 } else if (bps <= 18250) {
2925 return OMX_AUDIO_AMRBandModeWB5;
2926 } else if (bps <= 19850) {
2927 return OMX_AUDIO_AMRBandModeWB6;
2928 } else if (bps <= 23050) {
2929 return OMX_AUDIO_AMRBandModeWB7;
2930 }
2931
2932 // 23850 bps
2933 return OMX_AUDIO_AMRBandModeWB8;
2934 } else { // AMRNB
2935 if (bps <= 4750) {
2936 return OMX_AUDIO_AMRBandModeNB0;
2937 } else if (bps <= 5150) {
2938 return OMX_AUDIO_AMRBandModeNB1;
2939 } else if (bps <= 5900) {
2940 return OMX_AUDIO_AMRBandModeNB2;
2941 } else if (bps <= 6700) {
2942 return OMX_AUDIO_AMRBandModeNB3;
2943 } else if (bps <= 7400) {
2944 return OMX_AUDIO_AMRBandModeNB4;
2945 } else if (bps <= 7950) {
2946 return OMX_AUDIO_AMRBandModeNB5;
2947 } else if (bps <= 10200) {
2948 return OMX_AUDIO_AMRBandModeNB6;
2949 }
2950
2951 // 12200 bps
2952 return OMX_AUDIO_AMRBandModeNB7;
2953 }
2954}
2955
2956void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002957 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002958
Andreas Huber8768f2c2009-12-01 15:26:54 -08002959 OMX_AUDIO_PARAM_AMRTYPE def;
2960 InitOMXParams(&def);
2961 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002962
Andreas Huber8768f2c2009-12-01 15:26:54 -08002963 status_t err =
2964 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002965
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002966 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002967
Andreas Huber8768f2c2009-12-01 15:26:54 -08002968 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002969
James Dong17299ab2010-05-14 15:45:22 -07002970 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002971 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08002972 CHECK_EQ(err, (status_t)OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002973
2974 ////////////////////////
2975
2976 if (mIsEncoder) {
2977 sp<MetaData> format = mSource->getFormat();
2978 int32_t sampleRate;
2979 int32_t numChannels;
2980 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2981 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2982
2983 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2984 }
2985}
2986
James Dong17299ab2010-05-14 15:45:22 -07002987void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002988 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002989 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002990 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002991 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002992
2993 //////////////// output port ////////////////////
2994 // format
2995 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2996 format.nPortIndex = kPortIndexOutput;
2997 format.nIndex = 0;
2998 status_t err = OMX_ErrorNone;
2999 while (OMX_ErrorNone == err) {
3000 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003001 &format, sizeof(format)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003002 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
3003 break;
3004 }
3005 format.nIndex++;
3006 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003007 CHECK_EQ((status_t)OK, err);
James Dongabed93a2010-04-22 17:27:04 -07003008 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003009 &format, sizeof(format)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003010
3011 // port definition
3012 OMX_PARAM_PORTDEFINITIONTYPE def;
3013 InitOMXParams(&def);
3014 def.nPortIndex = kPortIndexOutput;
3015 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003016 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003017 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
3018 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
3019 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003020 &def, sizeof(def)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003021
3022 // profile
3023 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
3024 InitOMXParams(&profile);
3025 profile.nPortIndex = kPortIndexOutput;
3026 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003027 &profile, sizeof(profile)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003028 profile.nChannels = numChannels;
3029 profile.eChannelMode = (numChannels == 1?
3030 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
3031 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07003032 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003033 profile.nAudioBandWidth = 0;
3034 profile.nFrameLength = 0;
3035 profile.nAACtools = OMX_AUDIO_AACToolAll;
3036 profile.nAACERtools = OMX_AUDIO_AACERNone;
3037 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
3038 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
3039 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003040 &profile, sizeof(profile)), (status_t)OK);
James Dongabed93a2010-04-22 17:27:04 -07003041
Andreas Huberda050cf22009-09-02 14:01:43 -07003042 } else {
3043 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07003044 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07003045 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003046
Andreas Huber784202e2009-10-15 13:46:54 -07003047 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003048 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003049 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003050
Andreas Huberda050cf22009-09-02 14:01:43 -07003051 profile.nChannels = numChannels;
3052 profile.nSampleRate = sampleRate;
3053 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07003054
Andreas Huber784202e2009-10-15 13:46:54 -07003055 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003056 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003057 CHECK_EQ(err, (status_t)OK);
Andreas Huberda050cf22009-09-02 14:01:43 -07003058 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003059}
3060
3061void OMXCodec::setImageOutputFormat(
3062 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07003063 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07003064
3065#if 0
3066 OMX_INDEXTYPE index;
3067 status_t err = mOMX->get_extension_index(
3068 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003069 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003070
3071 err = mOMX->set_config(mNode, index, &format, sizeof(format));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003072 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003073#endif
3074
3075 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003076 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003077 def.nPortIndex = kPortIndexOutput;
3078
Andreas Huber784202e2009-10-15 13:46:54 -07003079 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003080 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003081 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003082
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003083 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage);
Andreas Huberbe06d262009-08-14 14:37:10 -07003084
3085 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07003086
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003087 CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07003088 imageDef->eColorFormat = format;
3089 imageDef->nFrameWidth = width;
3090 imageDef->nFrameHeight = height;
3091
3092 switch (format) {
3093 case OMX_COLOR_FormatYUV420PackedPlanar:
3094 case OMX_COLOR_FormatYUV411Planar:
3095 {
3096 def.nBufferSize = (width * height * 3) / 2;
3097 break;
3098 }
3099
3100 case OMX_COLOR_FormatCbYCrY:
3101 {
3102 def.nBufferSize = width * height * 2;
3103 break;
3104 }
3105
3106 case OMX_COLOR_Format32bitARGB8888:
3107 {
3108 def.nBufferSize = width * height * 4;
3109 break;
3110 }
3111
Andreas Huber201511c2009-09-08 14:01:44 -07003112 case OMX_COLOR_Format16bitARGB4444:
3113 case OMX_COLOR_Format16bitARGB1555:
3114 case OMX_COLOR_Format16bitRGB565:
3115 case OMX_COLOR_Format16bitBGR565:
3116 {
3117 def.nBufferSize = width * height * 2;
3118 break;
3119 }
3120
Andreas Huberbe06d262009-08-14 14:37:10 -07003121 default:
3122 CHECK(!"Should not be here. Unknown color format.");
3123 break;
3124 }
3125
Andreas Huber5c0a9132009-08-20 11:16:40 -07003126 def.nBufferCountActual = def.nBufferCountMin;
3127
Andreas Huber784202e2009-10-15 13:46:54 -07003128 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003129 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003130 CHECK_EQ(err, (status_t)OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07003131}
Andreas Huberbe06d262009-08-14 14:37:10 -07003132
Andreas Huber5c0a9132009-08-20 11:16:40 -07003133void OMXCodec::setJPEGInputFormat(
3134 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
3135 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003136 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003137 def.nPortIndex = kPortIndexInput;
3138
Andreas Huber784202e2009-10-15 13:46:54 -07003139 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003140 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003141 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003142
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003143 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage);
Andreas Huber5c0a9132009-08-20 11:16:40 -07003144 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3145
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003146 CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingJPEG);
Andreas Huberbe06d262009-08-14 14:37:10 -07003147 imageDef->nFrameWidth = width;
3148 imageDef->nFrameHeight = height;
3149
Andreas Huber5c0a9132009-08-20 11:16:40 -07003150 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07003151 def.nBufferCountActual = def.nBufferCountMin;
3152
Andreas Huber784202e2009-10-15 13:46:54 -07003153 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003154 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003155 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003156}
3157
3158void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
3159 CodecSpecificData *specific =
3160 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
3161
3162 specific->mSize = size;
3163 memcpy(specific->mData, data, size);
3164
3165 mCodecSpecificData.push(specific);
3166}
3167
3168void OMXCodec::clearCodecSpecificData() {
3169 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
3170 free(mCodecSpecificData.editItemAt(i));
3171 }
3172 mCodecSpecificData.clear();
3173 mCodecSpecificDataIndex = 0;
3174}
3175
James Dong36e573b2010-06-19 09:04:18 -07003176status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07003177 Mutex::Autolock autoLock(mLock);
3178
Andreas Huberbe06d262009-08-14 14:37:10 -07003179 if (mState != LOADED) {
3180 return UNKNOWN_ERROR;
3181 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07003182
Andreas Huberbe06d262009-08-14 14:37:10 -07003183 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07003184 if (mQuirks & kWantsNALFragments) {
3185 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07003186 }
James Dong36e573b2010-06-19 09:04:18 -07003187 if (meta) {
3188 int64_t startTimeUs = 0;
3189 int64_t timeUs;
3190 if (meta->findInt64(kKeyTime, &timeUs)) {
3191 startTimeUs = timeUs;
3192 }
3193 params->setInt64(kKeyTime, startTimeUs);
3194 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003195 status_t err = mSource->start(params.get());
3196
3197 if (err != OK) {
3198 return err;
3199 }
3200
3201 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07003202 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07003203 mSignalledEOS = false;
3204 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07003205 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003206 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003207 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
3208 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07003209 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07003210 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003211
3212 return init();
3213}
3214
3215status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08003216 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07003217
3218 Mutex::Autolock autoLock(mLock);
3219
3220 while (isIntermediateState(mState)) {
3221 mAsyncCompletion.wait(mLock);
3222 }
3223
3224 switch (mState) {
3225 case LOADED:
3226 case ERROR:
3227 break;
3228
3229 case EXECUTING:
3230 {
3231 setState(EXECUTING_TO_IDLE);
3232
Andreas Huber127fcdc2009-08-26 16:27:02 -07003233 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07003234 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07003235 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07003236
Andreas Huber127fcdc2009-08-26 16:27:02 -07003237 bool emulateInputFlushCompletion =
3238 !flushPortAsync(kPortIndexInput);
3239
3240 bool emulateOutputFlushCompletion =
3241 !flushPortAsync(kPortIndexOutput);
3242
3243 if (emulateInputFlushCompletion) {
3244 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3245 }
3246
3247 if (emulateOutputFlushCompletion) {
3248 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3249 }
3250 } else {
3251 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
3252 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
3253
3254 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07003255 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003256 CHECK_EQ(err, (status_t)OK);
Andreas Huber127fcdc2009-08-26 16:27:02 -07003257 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003258
3259 while (mState != LOADED && mState != ERROR) {
3260 mAsyncCompletion.wait(mLock);
3261 }
3262
3263 break;
3264 }
3265
3266 default:
3267 {
3268 CHECK(!"should not be here.");
3269 break;
3270 }
3271 }
3272
Andreas Hubera4357ad2010-04-02 12:49:54 -07003273 if (mLeftOverBuffer) {
3274 mLeftOverBuffer->release();
3275 mLeftOverBuffer = NULL;
3276 }
3277
Andreas Huberbe06d262009-08-14 14:37:10 -07003278 mSource->stop();
3279
Andreas Huber4a9375e2010-02-09 11:54:33 -08003280 CODEC_LOGV("stopped");
3281
Andreas Huberbe06d262009-08-14 14:37:10 -07003282 return OK;
3283}
3284
3285sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07003286 Mutex::Autolock autoLock(mLock);
3287
Andreas Huberbe06d262009-08-14 14:37:10 -07003288 return mOutputFormat;
3289}
3290
3291status_t OMXCodec::read(
3292 MediaBuffer **buffer, const ReadOptions *options) {
3293 *buffer = NULL;
3294
3295 Mutex::Autolock autoLock(mLock);
3296
Andreas Huberd06e5b82009-08-28 13:18:14 -07003297 if (mState != EXECUTING && mState != RECONFIGURING) {
3298 return UNKNOWN_ERROR;
3299 }
3300
Andreas Hubere981c332009-10-22 13:49:30 -07003301 bool seeking = false;
3302 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003303 ReadOptions::SeekMode seekMode;
3304 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07003305 seeking = true;
3306 }
James Dong53d4e0d2010-07-21 14:51:35 -07003307 int64_t skipTimeUs;
3308 if (options && options->getSkipFrame(&skipTimeUs)) {
3309 mSkipTimeUs = skipTimeUs;
3310 } else {
3311 mSkipTimeUs = -1;
3312 }
Andreas Hubere981c332009-10-22 13:49:30 -07003313
Andreas Huber42978e52009-08-27 10:08:39 -07003314 if (mInitialBufferSubmit) {
3315 mInitialBufferSubmit = false;
3316
Andreas Hubere981c332009-10-22 13:49:30 -07003317 if (seeking) {
3318 CHECK(seekTimeUs >= 0);
3319 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003320 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07003321
3322 // There's no reason to trigger the code below, there's
3323 // nothing to flush yet.
3324 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07003325 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07003326 }
3327
Andreas Huber42978e52009-08-27 10:08:39 -07003328 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07003329
Andreas Huberd06e5b82009-08-28 13:18:14 -07003330 if (mState == EXECUTING) {
3331 // Otherwise mState == RECONFIGURING and this code will trigger
3332 // after the output port is reenabled.
3333 fillOutputBuffers();
3334 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003335 }
3336
Andreas Hubere981c332009-10-22 13:49:30 -07003337 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07003338 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07003339
3340 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07003341
3342 CHECK(seekTimeUs >= 0);
3343 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07003344 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07003345
3346 mFilledBuffers.clear();
3347
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003348 CHECK_EQ((int)mState, (int)EXECUTING);
Andreas Huberbe06d262009-08-14 14:37:10 -07003349
Andreas Huber404cc412009-08-25 14:26:05 -07003350 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
3351 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
3352
3353 if (emulateInputFlushCompletion) {
3354 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
3355 }
3356
3357 if (emulateOutputFlushCompletion) {
3358 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
3359 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08003360
3361 while (mSeekTimeUs >= 0) {
3362 mBufferFilled.wait(mLock);
3363 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003364 }
3365
3366 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
3367 mBufferFilled.wait(mLock);
3368 }
3369
3370 if (mState == ERROR) {
3371 return UNKNOWN_ERROR;
3372 }
3373
3374 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08003375 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07003376 }
3377
Andreas Hubercfd55572009-10-09 14:11:28 -07003378 if (mOutputPortSettingsHaveChanged) {
3379 mOutputPortSettingsHaveChanged = false;
3380
3381 return INFO_FORMAT_CHANGED;
3382 }
3383
Andreas Huberbe06d262009-08-14 14:37:10 -07003384 size_t index = *mFilledBuffers.begin();
3385 mFilledBuffers.erase(mFilledBuffers.begin());
3386
3387 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
Andreas Huberbbbcf652010-12-07 14:25:54 -08003388 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US);
3389 info->mStatus = OWNED_BY_CLIENT;
3390
Andreas Huberbe06d262009-08-14 14:37:10 -07003391 info->mMediaBuffer->add_ref();
3392 *buffer = info->mMediaBuffer;
3393
3394 return OK;
3395}
3396
3397void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
3398 Mutex::Autolock autoLock(mLock);
3399
3400 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
3401 for (size_t i = 0; i < buffers->size(); ++i) {
3402 BufferInfo *info = &buffers->editItemAt(i);
3403
3404 if (info->mMediaBuffer == buffer) {
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003405 CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED);
Andreas Huberbbbcf652010-12-07 14:25:54 -08003406 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_CLIENT);
3407
3408 info->mStatus = OWNED_BY_US;
3409
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003410 if (buffer->graphicBuffer() == 0) {
3411 fillOutputBuffer(info);
3412 } else {
3413 sp<MetaData> metaData = info->mMediaBuffer->meta_data();
3414 int32_t rendered = 0;
3415 if (!metaData->findInt32(kKeyRendered, &rendered)) {
3416 rendered = 0;
3417 }
3418 if (!rendered) {
3419 status_t err = cancelBufferToNativeWindow(info);
3420 if (err < 0) {
3421 return;
3422 }
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003423 }
3424
Andreas Huberbbbcf652010-12-07 14:25:54 -08003425 info->mStatus = OWNED_BY_NATIVE_WINDOW;
3426
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003427 // Dequeue the next buffer from the native window.
3428 BufferInfo *nextBufInfo = dequeueBufferFromNativeWindow();
3429 if (nextBufInfo == 0) {
3430 return;
3431 }
3432
3433 // Give the buffer to the OMX node to fill.
3434 fillOutputBuffer(nextBufInfo);
3435 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003436 return;
3437 }
3438 }
3439
3440 CHECK(!"should not be here.");
3441}
3442
3443static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
3444 static const char *kNames[] = {
3445 "OMX_IMAGE_CodingUnused",
3446 "OMX_IMAGE_CodingAutoDetect",
3447 "OMX_IMAGE_CodingJPEG",
3448 "OMX_IMAGE_CodingJPEG2K",
3449 "OMX_IMAGE_CodingEXIF",
3450 "OMX_IMAGE_CodingTIFF",
3451 "OMX_IMAGE_CodingGIF",
3452 "OMX_IMAGE_CodingPNG",
3453 "OMX_IMAGE_CodingLZW",
3454 "OMX_IMAGE_CodingBMP",
3455 };
3456
3457 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3458
3459 if (type < 0 || (size_t)type >= numNames) {
3460 return "UNKNOWN";
3461 } else {
3462 return kNames[type];
3463 }
3464}
3465
3466static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
3467 static const char *kNames[] = {
3468 "OMX_COLOR_FormatUnused",
3469 "OMX_COLOR_FormatMonochrome",
3470 "OMX_COLOR_Format8bitRGB332",
3471 "OMX_COLOR_Format12bitRGB444",
3472 "OMX_COLOR_Format16bitARGB4444",
3473 "OMX_COLOR_Format16bitARGB1555",
3474 "OMX_COLOR_Format16bitRGB565",
3475 "OMX_COLOR_Format16bitBGR565",
3476 "OMX_COLOR_Format18bitRGB666",
3477 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003478 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07003479 "OMX_COLOR_Format24bitRGB888",
3480 "OMX_COLOR_Format24bitBGR888",
3481 "OMX_COLOR_Format24bitARGB1887",
3482 "OMX_COLOR_Format25bitARGB1888",
3483 "OMX_COLOR_Format32bitBGRA8888",
3484 "OMX_COLOR_Format32bitARGB8888",
3485 "OMX_COLOR_FormatYUV411Planar",
3486 "OMX_COLOR_FormatYUV411PackedPlanar",
3487 "OMX_COLOR_FormatYUV420Planar",
3488 "OMX_COLOR_FormatYUV420PackedPlanar",
3489 "OMX_COLOR_FormatYUV420SemiPlanar",
3490 "OMX_COLOR_FormatYUV422Planar",
3491 "OMX_COLOR_FormatYUV422PackedPlanar",
3492 "OMX_COLOR_FormatYUV422SemiPlanar",
3493 "OMX_COLOR_FormatYCbYCr",
3494 "OMX_COLOR_FormatYCrYCb",
3495 "OMX_COLOR_FormatCbYCrY",
3496 "OMX_COLOR_FormatCrYCbY",
3497 "OMX_COLOR_FormatYUV444Interleaved",
3498 "OMX_COLOR_FormatRawBayer8bit",
3499 "OMX_COLOR_FormatRawBayer10bit",
3500 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003501 "OMX_COLOR_FormatL2",
3502 "OMX_COLOR_FormatL4",
3503 "OMX_COLOR_FormatL8",
3504 "OMX_COLOR_FormatL16",
3505 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07003506 "OMX_COLOR_FormatL32",
3507 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
3508 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
3509 "OMX_COLOR_Format18BitBGR666",
3510 "OMX_COLOR_Format24BitARGB6666",
3511 "OMX_COLOR_Format24BitABGR6666",
3512 };
3513
3514 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3515
Andreas Huberbe06d262009-08-14 14:37:10 -07003516 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3517 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3518 } else if (type < 0 || (size_t)type >= numNames) {
3519 return "UNKNOWN";
3520 } else {
3521 return kNames[type];
3522 }
3523}
3524
3525static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3526 static const char *kNames[] = {
3527 "OMX_VIDEO_CodingUnused",
3528 "OMX_VIDEO_CodingAutoDetect",
3529 "OMX_VIDEO_CodingMPEG2",
3530 "OMX_VIDEO_CodingH263",
3531 "OMX_VIDEO_CodingMPEG4",
3532 "OMX_VIDEO_CodingWMV",
3533 "OMX_VIDEO_CodingRV",
3534 "OMX_VIDEO_CodingAVC",
3535 "OMX_VIDEO_CodingMJPEG",
3536 };
3537
3538 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3539
3540 if (type < 0 || (size_t)type >= numNames) {
3541 return "UNKNOWN";
3542 } else {
3543 return kNames[type];
3544 }
3545}
3546
3547static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3548 static const char *kNames[] = {
3549 "OMX_AUDIO_CodingUnused",
3550 "OMX_AUDIO_CodingAutoDetect",
3551 "OMX_AUDIO_CodingPCM",
3552 "OMX_AUDIO_CodingADPCM",
3553 "OMX_AUDIO_CodingAMR",
3554 "OMX_AUDIO_CodingGSMFR",
3555 "OMX_AUDIO_CodingGSMEFR",
3556 "OMX_AUDIO_CodingGSMHR",
3557 "OMX_AUDIO_CodingPDCFR",
3558 "OMX_AUDIO_CodingPDCEFR",
3559 "OMX_AUDIO_CodingPDCHR",
3560 "OMX_AUDIO_CodingTDMAFR",
3561 "OMX_AUDIO_CodingTDMAEFR",
3562 "OMX_AUDIO_CodingQCELP8",
3563 "OMX_AUDIO_CodingQCELP13",
3564 "OMX_AUDIO_CodingEVRC",
3565 "OMX_AUDIO_CodingSMV",
3566 "OMX_AUDIO_CodingG711",
3567 "OMX_AUDIO_CodingG723",
3568 "OMX_AUDIO_CodingG726",
3569 "OMX_AUDIO_CodingG729",
3570 "OMX_AUDIO_CodingAAC",
3571 "OMX_AUDIO_CodingMP3",
3572 "OMX_AUDIO_CodingSBC",
3573 "OMX_AUDIO_CodingVORBIS",
3574 "OMX_AUDIO_CodingWMA",
3575 "OMX_AUDIO_CodingRA",
3576 "OMX_AUDIO_CodingMIDI",
3577 };
3578
3579 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3580
3581 if (type < 0 || (size_t)type >= numNames) {
3582 return "UNKNOWN";
3583 } else {
3584 return kNames[type];
3585 }
3586}
3587
3588static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3589 static const char *kNames[] = {
3590 "OMX_AUDIO_PCMModeLinear",
3591 "OMX_AUDIO_PCMModeALaw",
3592 "OMX_AUDIO_PCMModeMULaw",
3593 };
3594
3595 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3596
3597 if (type < 0 || (size_t)type >= numNames) {
3598 return "UNKNOWN";
3599 } else {
3600 return kNames[type];
3601 }
3602}
3603
Andreas Huber7ae02c82009-09-09 16:29:47 -07003604static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3605 static const char *kNames[] = {
3606 "OMX_AUDIO_AMRBandModeUnused",
3607 "OMX_AUDIO_AMRBandModeNB0",
3608 "OMX_AUDIO_AMRBandModeNB1",
3609 "OMX_AUDIO_AMRBandModeNB2",
3610 "OMX_AUDIO_AMRBandModeNB3",
3611 "OMX_AUDIO_AMRBandModeNB4",
3612 "OMX_AUDIO_AMRBandModeNB5",
3613 "OMX_AUDIO_AMRBandModeNB6",
3614 "OMX_AUDIO_AMRBandModeNB7",
3615 "OMX_AUDIO_AMRBandModeWB0",
3616 "OMX_AUDIO_AMRBandModeWB1",
3617 "OMX_AUDIO_AMRBandModeWB2",
3618 "OMX_AUDIO_AMRBandModeWB3",
3619 "OMX_AUDIO_AMRBandModeWB4",
3620 "OMX_AUDIO_AMRBandModeWB5",
3621 "OMX_AUDIO_AMRBandModeWB6",
3622 "OMX_AUDIO_AMRBandModeWB7",
3623 "OMX_AUDIO_AMRBandModeWB8",
3624 };
3625
3626 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3627
3628 if (type < 0 || (size_t)type >= numNames) {
3629 return "UNKNOWN";
3630 } else {
3631 return kNames[type];
3632 }
3633}
3634
3635static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3636 static const char *kNames[] = {
3637 "OMX_AUDIO_AMRFrameFormatConformance",
3638 "OMX_AUDIO_AMRFrameFormatIF1",
3639 "OMX_AUDIO_AMRFrameFormatIF2",
3640 "OMX_AUDIO_AMRFrameFormatFSF",
3641 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3642 "OMX_AUDIO_AMRFrameFormatITU",
3643 };
3644
3645 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3646
3647 if (type < 0 || (size_t)type >= numNames) {
3648 return "UNKNOWN";
3649 } else {
3650 return kNames[type];
3651 }
3652}
Andreas Huberbe06d262009-08-14 14:37:10 -07003653
3654void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3655 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003656 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003657 def.nPortIndex = portIndex;
3658
Andreas Huber784202e2009-10-15 13:46:54 -07003659 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003660 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003661 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003662
3663 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3664
3665 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3666 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3667
3668 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3669 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3670 printf(" nBufferSize = %ld\n", def.nBufferSize);
3671
3672 switch (def.eDomain) {
3673 case OMX_PortDomainImage:
3674 {
3675 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3676
3677 printf("\n");
3678 printf(" // Image\n");
3679 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3680 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3681 printf(" nStride = %ld\n", imageDef->nStride);
3682
3683 printf(" eCompressionFormat = %s\n",
3684 imageCompressionFormatString(imageDef->eCompressionFormat));
3685
3686 printf(" eColorFormat = %s\n",
3687 colorFormatString(imageDef->eColorFormat));
3688
3689 break;
3690 }
3691
3692 case OMX_PortDomainVideo:
3693 {
3694 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3695
3696 printf("\n");
3697 printf(" // Video\n");
3698 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3699 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3700 printf(" nStride = %ld\n", videoDef->nStride);
3701
3702 printf(" eCompressionFormat = %s\n",
3703 videoCompressionFormatString(videoDef->eCompressionFormat));
3704
3705 printf(" eColorFormat = %s\n",
3706 colorFormatString(videoDef->eColorFormat));
3707
3708 break;
3709 }
3710
3711 case OMX_PortDomainAudio:
3712 {
3713 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3714
3715 printf("\n");
3716 printf(" // Audio\n");
3717 printf(" eEncoding = %s\n",
3718 audioCodingTypeString(audioDef->eEncoding));
3719
3720 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3721 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003722 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003723 params.nPortIndex = portIndex;
3724
Andreas Huber784202e2009-10-15 13:46:54 -07003725 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003726 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003727 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003728
3729 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3730 printf(" nChannels = %ld\n", params.nChannels);
3731 printf(" bInterleaved = %d\n", params.bInterleaved);
3732 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3733
3734 printf(" eNumData = %s\n",
3735 params.eNumData == OMX_NumericalDataSigned
3736 ? "signed" : "unsigned");
3737
3738 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003739 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3740 OMX_AUDIO_PARAM_AMRTYPE amr;
3741 InitOMXParams(&amr);
3742 amr.nPortIndex = portIndex;
3743
Andreas Huber784202e2009-10-15 13:46:54 -07003744 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003745 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003746 CHECK_EQ(err, (status_t)OK);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003747
3748 printf(" nChannels = %ld\n", amr.nChannels);
3749 printf(" eAMRBandMode = %s\n",
3750 amrBandModeString(amr.eAMRBandMode));
3751 printf(" eAMRFrameFormat = %s\n",
3752 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003753 }
3754
3755 break;
3756 }
3757
3758 default:
3759 {
3760 printf(" // Unknown\n");
3761 break;
3762 }
3763 }
3764
3765 printf("}\n");
3766}
3767
Jamie Gennis58a36ad2010-10-07 14:08:38 -07003768status_t OMXCodec::initNativeWindow() {
3769 // Enable use of a GraphicBuffer as the output for this node. This must
3770 // happen before getting the IndexParamPortDefinition parameter because it
3771 // will affect the pixel format that the node reports.
3772 status_t err = mOMX->enableGraphicBuffers(mNode, kPortIndexOutput, OMX_TRUE);
3773 if (err != 0) {
3774 return err;
3775 }
3776
3777 return OK;
3778}
3779
Andreas Huberbe06d262009-08-14 14:37:10 -07003780void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3781 mOutputFormat = new MetaData;
3782 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003783 if (mIsEncoder) {
3784 int32_t timeScale;
3785 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3786 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3787 }
3788 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003789
3790 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003791 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003792 def.nPortIndex = kPortIndexOutput;
3793
Andreas Huber784202e2009-10-15 13:46:54 -07003794 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003795 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003796 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003797
3798 switch (def.eDomain) {
3799 case OMX_PortDomainImage:
3800 {
3801 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003802 CHECK_EQ((int)imageDef->eCompressionFormat,
3803 (int)OMX_IMAGE_CodingUnused);
Andreas Huberbe06d262009-08-14 14:37:10 -07003804
Andreas Hubere6c40962009-09-10 14:13:30 -07003805 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003806 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3807 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3808 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3809 break;
3810 }
3811
3812 case OMX_PortDomainAudio:
3813 {
3814 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3815
Andreas Huberda050cf22009-09-02 14:01:43 -07003816 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3817 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003818 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003819 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003820
Andreas Huber784202e2009-10-15 13:46:54 -07003821 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003822 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003823 CHECK_EQ(err, (status_t)OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003824
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003825 CHECK_EQ((int)params.eNumData, (int)OMX_NumericalDataSigned);
3826 CHECK_EQ(params.nBitPerSample, 16u);
3827 CHECK_EQ((int)params.ePCMMode, (int)OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003828
Andreas Huberda050cf22009-09-02 14:01:43 -07003829 int32_t numChannels, sampleRate;
3830 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3831 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003832
Andreas Huberda050cf22009-09-02 14:01:43 -07003833 if ((OMX_U32)numChannels != params.nChannels) {
3834 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003835 "the input stream contains (contains %d channels, "
3836 "codec outputs %ld channels).",
3837 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003838 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003839
Andreas Hubere6c40962009-09-10 14:13:30 -07003840 mOutputFormat->setCString(
3841 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003842
3843 // Use the codec-advertised number of channels, as some
3844 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003845 // mono. If we know the codec lies about this information,
3846 // use the actual number of channels instead.
3847 mOutputFormat->setInt32(
3848 kKeyChannelCount,
3849 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3850 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003851
3852 // The codec-reported sampleRate is not reliable...
3853 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3854 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003855 OMX_AUDIO_PARAM_AMRTYPE amr;
3856 InitOMXParams(&amr);
3857 amr.nPortIndex = kPortIndexOutput;
3858
Andreas Huber784202e2009-10-15 13:46:54 -07003859 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003860 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003861 CHECK_EQ(err, (status_t)OK);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003862
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003863 CHECK_EQ(amr.nChannels, 1u);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003864 mOutputFormat->setInt32(kKeyChannelCount, 1);
3865
3866 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3867 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003868 mOutputFormat->setCString(
3869 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003870 mOutputFormat->setInt32(kKeySampleRate, 8000);
3871 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3872 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003873 mOutputFormat->setCString(
3874 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003875 mOutputFormat->setInt32(kKeySampleRate, 16000);
3876 } else {
3877 CHECK(!"Unknown AMR band mode.");
3878 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003879 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003880 mOutputFormat->setCString(
3881 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003882 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003883 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3884 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003885 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003886 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3887 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003888 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003889 } else {
3890 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003891 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003892 break;
3893 }
3894
3895 case OMX_PortDomainVideo:
3896 {
3897 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3898
3899 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003900 mOutputFormat->setCString(
3901 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003902 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003903 mOutputFormat->setCString(
3904 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003905 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003906 mOutputFormat->setCString(
3907 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003908 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003909 mOutputFormat->setCString(
3910 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003911 } else {
3912 CHECK(!"Unknown compression format.");
3913 }
3914
James Dong5592bcc2010-10-22 17:10:43 -07003915 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3916 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
Andreas Huberbe06d262009-08-14 14:37:10 -07003917 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003918
James Dong0c9153d2010-11-23 11:30:21 -08003919 if (!mIsEncoder) {
3920 OMX_CONFIG_RECTTYPE rect;
3921 status_t err =
3922 mOMX->getConfig(
3923 mNode, OMX_IndexConfigCommonOutputCrop,
3924 &rect, sizeof(rect));
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003925
James Dong0c9153d2010-11-23 11:30:21 -08003926 if (err == OK) {
3927 CHECK_GE(rect.nLeft, 0);
3928 CHECK_GE(rect.nTop, 0);
3929 CHECK_GE(rect.nWidth, 0u);
3930 CHECK_GE(rect.nHeight, 0u);
3931 CHECK_LE(rect.nLeft + rect.nWidth - 1, video_def->nFrameWidth);
3932 CHECK_LE(rect.nTop + rect.nHeight - 1, video_def->nFrameHeight);
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003933
James Dong0c9153d2010-11-23 11:30:21 -08003934 mOutputFormat->setRect(
3935 kKeyCropRect,
3936 rect.nLeft,
3937 rect.nTop,
3938 rect.nLeft + rect.nWidth - 1,
3939 rect.nTop + rect.nHeight - 1);
3940 } else {
3941 mOutputFormat->setRect(
3942 kKeyCropRect,
3943 0, 0,
3944 video_def->nFrameWidth - 1,
3945 video_def->nFrameHeight - 1);
3946 }
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08003947 }
3948
Andreas Huberbe06d262009-08-14 14:37:10 -07003949 break;
3950 }
3951
3952 default:
3953 {
3954 CHECK(!"should not be here, neither audio nor video.");
3955 break;
3956 }
3957 }
3958}
3959
Andreas Huber1f24b302010-06-10 11:12:39 -07003960status_t OMXCodec::pause() {
3961 Mutex::Autolock autoLock(mLock);
3962
3963 mPaused = true;
3964
3965 return OK;
3966}
3967
Andreas Hubere6c40962009-09-10 14:13:30 -07003968////////////////////////////////////////////////////////////////////////////////
3969
3970status_t QueryCodecs(
3971 const sp<IOMX> &omx,
3972 const char *mime, bool queryDecoders,
3973 Vector<CodecCapabilities> *results) {
3974 results->clear();
3975
3976 for (int index = 0;; ++index) {
3977 const char *componentName;
3978
3979 if (!queryDecoders) {
3980 componentName = GetCodec(
3981 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3982 mime, index);
3983 } else {
3984 componentName = GetCodec(
3985 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3986 mime, index);
3987 }
3988
3989 if (!componentName) {
3990 return OK;
3991 }
3992
Andreas Huber1a189a82010-03-24 13:49:20 -07003993 if (strncmp(componentName, "OMX.", 4)) {
3994 // Not an OpenMax component but a software codec.
3995
3996 results->push();
3997 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3998 caps->mComponentName = componentName;
3999
4000 continue;
4001 }
4002
Andreas Huber784202e2009-10-15 13:46:54 -07004003 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07004004 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07004005 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07004006
4007 if (err != OK) {
4008 continue;
4009 }
4010
James Dong722d5912010-04-13 10:56:59 -07004011 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07004012
4013 results->push();
4014 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
4015 caps->mComponentName = componentName;
4016
4017 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
4018 InitOMXParams(&param);
4019
4020 param.nPortIndex = queryDecoders ? 0 : 1;
4021
4022 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07004023 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07004024 node, OMX_IndexParamVideoProfileLevelQuerySupported,
4025 &param, sizeof(param));
4026
4027 if (err != OK) {
4028 break;
4029 }
4030
4031 CodecProfileLevel profileLevel;
4032 profileLevel.mProfile = param.eProfile;
4033 profileLevel.mLevel = param.eLevel;
4034
4035 caps->mProfileLevels.push(profileLevel);
4036 }
4037
James Dongd7810892010-11-11 00:33:05 -08004038 // Color format query
4039 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
4040 InitOMXParams(&portFormat);
4041 portFormat.nPortIndex = queryDecoders ? 1 : 0;
4042 for (portFormat.nIndex = 0;; ++portFormat.nIndex) {
4043 err = omx->getParameter(
4044 node, OMX_IndexParamVideoPortFormat,
4045 &portFormat, sizeof(portFormat));
4046 if (err != OK) {
4047 break;
4048 }
4049 caps->mColorFormats.push(portFormat.eColorFormat);
4050 }
4051
Andreas Huber1bb0ffd2010-11-22 13:06:35 -08004052 CHECK_EQ(omx->freeNode(node), (status_t)OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07004053 }
4054}
4055
James Dong31b9375f2010-11-10 21:11:41 -08004056void OMXCodec::restorePatchedDataPointer(BufferInfo *info) {
4057 CHECK(mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames));
4058 CHECK(mOMXLivesLocally);
4059
4060 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *)info->mBuffer;
4061 header->pBuffer = (OMX_U8 *)info->mData;
4062}
4063
Andreas Huberbe06d262009-08-14 14:37:10 -07004064} // namespace android