blob: b7d6d42802a63697de7fb25e111957914d34eb9c [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"
Andreas Hubera30d4002009-12-08 15:40:06 -080022#include "include/AMRNBDecoder.h"
Andreas Huberd49b526dd2009-12-11 15:07:25 -080023#include "include/AMRNBEncoder.h"
Andreas Hubera30d4002009-12-08 15:40:06 -080024#include "include/AMRWBDecoder.h"
Andreas Huber4a0ec3f2009-12-10 09:44:29 -080025#include "include/AVCDecoder.h"
James Dong02f5b542009-12-15 16:26:55 -080026#include "include/M4vH263Decoder.h"
Andreas Huber250f2432009-12-07 14:22:35 -080027#include "include/MP3Decoder.h"
Andreas Huber8c7ab032009-12-07 11:23:44 -080028
Andreas Huberbd7b43b2009-10-13 10:22:55 -070029#include "include/ESDS.h"
30
Andreas Huberbe06d262009-08-14 14:37:10 -070031#include <binder/IServiceManager.h>
32#include <binder/MemoryDealer.h>
33#include <binder/ProcessState.h>
34#include <media/IMediaPlayerService.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070035#include <media/stagefright/MediaBuffer.h>
36#include <media/stagefright/MediaBufferGroup.h>
37#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070038#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070039#include <media/stagefright/MediaExtractor.h>
40#include <media/stagefright/MetaData.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070041#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070042#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070043#include <utils/Vector.h>
44
45#include <OMX_Audio.h>
46#include <OMX_Component.h>
47
48namespace android {
49
Andreas Huber8b432b12009-10-07 13:36:52 -070050static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
51
Andreas Huberbe06d262009-08-14 14:37:10 -070052struct CodecInfo {
53 const char *mime;
54 const char *codec;
55};
56
Andreas Huberfb1c2f82009-12-15 13:25:11 -080057#define FACTORY_CREATE(name) \
58static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
59 return new name(source); \
60}
61
62#define FACTORY_REF(name) { #name, Make##name },
63
64FACTORY_CREATE(MP3Decoder)
65FACTORY_CREATE(AMRNBDecoder)
66FACTORY_CREATE(AMRWBDecoder)
67FACTORY_CREATE(AACDecoder)
68FACTORY_CREATE(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -080069FACTORY_CREATE(M4vH263Decoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -080070FACTORY_CREATE(AMRNBEncoder)
71
72static sp<MediaSource> InstantiateSoftwareCodec(
73 const char *name, const sp<MediaSource> &source) {
74 struct FactoryInfo {
75 const char *name;
76 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
77 };
78
79 static const FactoryInfo kFactoryInfo[] = {
80 FACTORY_REF(MP3Decoder)
81 FACTORY_REF(AMRNBDecoder)
82 FACTORY_REF(AMRWBDecoder)
83 FACTORY_REF(AACDecoder)
84 FACTORY_REF(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -080085 FACTORY_REF(M4vH263Decoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -080086 FACTORY_REF(AMRNBEncoder)
87 };
88 for (size_t i = 0;
89 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
90 if (!strcmp(name, kFactoryInfo[i].name)) {
91 return (*kFactoryInfo[i].CreateFunc)(source);
92 }
93 }
94
95 return NULL;
96}
97
98#undef FACTORY_REF
99#undef FACTORY_CREATE
100
Andreas Huberbe06d262009-08-14 14:37:10 -0700101static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700102 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
James Dong374aee62010-04-26 10:23:30 -0700103// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800104 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700105// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700106// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800107 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700108// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700109 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800110 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700111// { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700112 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800113 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700114// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700115 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
116 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800117 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700118// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700119 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800120 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700121// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700122 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
123 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800124 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700125// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700126};
127
128static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700129 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800130 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700131 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
132 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
133 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
134 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
135 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
136 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
137 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
138 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
139 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
Andreas Huber71c27d92010-03-19 11:43:15 -0700140 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700141 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
142 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700143};
144
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800145#undef OPTIONAL
146
Andreas Hubere0873732009-09-10 09:57:53 -0700147#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700148#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber42c444a2010-02-09 10:20:00 -0800149#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700150
Andreas Huberbe06d262009-08-14 14:37:10 -0700151struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700152 OMXCodecObserver() {
153 }
154
155 void setCodec(const sp<OMXCodec> &target) {
156 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700157 }
158
159 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700160 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700161 sp<OMXCodec> codec = mTarget.promote();
162
163 if (codec.get() != NULL) {
164 codec->on_message(msg);
165 }
166 }
167
168protected:
169 virtual ~OMXCodecObserver() {}
170
171private:
172 wp<OMXCodec> mTarget;
173
174 OMXCodecObserver(const OMXCodecObserver &);
175 OMXCodecObserver &operator=(const OMXCodecObserver &);
176};
177
178static const char *GetCodec(const CodecInfo *info, size_t numInfos,
179 const char *mime, int index) {
180 CHECK(index >= 0);
181 for(size_t i = 0; i < numInfos; ++i) {
182 if (!strcasecmp(mime, info[i].mime)) {
183 if (index == 0) {
184 return info[i].codec;
185 }
186
187 --index;
188 }
189 }
190
191 return NULL;
192}
193
Andreas Huberebf66ea2009-08-19 13:32:58 -0700194enum {
195 kAVCProfileBaseline = 0x42,
196 kAVCProfileMain = 0x4d,
197 kAVCProfileExtended = 0x58,
198 kAVCProfileHigh = 0x64,
199 kAVCProfileHigh10 = 0x6e,
200 kAVCProfileHigh422 = 0x7a,
201 kAVCProfileHigh444 = 0xf4,
202 kAVCProfileCAVLC444Intra = 0x2c
203};
204
205static const char *AVCProfileToString(uint8_t profile) {
206 switch (profile) {
207 case kAVCProfileBaseline:
208 return "Baseline";
209 case kAVCProfileMain:
210 return "Main";
211 case kAVCProfileExtended:
212 return "Extended";
213 case kAVCProfileHigh:
214 return "High";
215 case kAVCProfileHigh10:
216 return "High 10";
217 case kAVCProfileHigh422:
218 return "High 422";
219 case kAVCProfileHigh444:
220 return "High 444";
221 case kAVCProfileCAVLC444Intra:
222 return "CAVLC 444 Intra";
223 default: return "Unknown";
224 }
225}
226
Andreas Huber4c483422009-09-02 16:05:36 -0700227template<class T>
228static void InitOMXParams(T *params) {
229 params->nSize = sizeof(T);
230 params->nVersion.s.nVersionMajor = 1;
231 params->nVersion.s.nVersionMinor = 0;
232 params->nVersion.s.nRevision = 0;
233 params->nVersion.s.nStep = 0;
234}
235
Andreas Hubere13526a2009-10-22 10:43:34 -0700236static bool IsSoftwareCodec(const char *componentName) {
237 if (!strncmp("OMX.PV.", componentName, 7)) {
238 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700239 }
240
Andreas Hubere13526a2009-10-22 10:43:34 -0700241 return false;
242}
243
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800244// A sort order in which non-OMX components are first,
245// followed by software codecs, i.e. OMX.PV.*, followed
246// by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700247static int CompareSoftwareCodecsFirst(
248 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800249 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
250 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
251
252 if (isNotOMX1) {
253 if (isNotOMX2) { return 0; }
254 return -1;
255 }
256 if (isNotOMX2) {
257 return 1;
258 }
259
Andreas Hubere13526a2009-10-22 10:43:34 -0700260 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
261 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
262
263 if (isSoftwareCodec1) {
264 if (isSoftwareCodec2) { return 0; }
265 return -1;
266 }
267
268 if (isSoftwareCodec2) {
269 return 1;
270 }
271
272 return 0;
273}
274
275// static
276uint32_t OMXCodec::getComponentQuirks(const char *componentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700277 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700278
Andreas Huberbe06d262009-08-14 14:37:10 -0700279 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700280 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700281 }
282 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
283 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800284 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700285 }
286 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
287 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700288 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700289 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700290 }
291 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
292 quirks |= kRequiresLoadedToIdleAfterAllocation;
293 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700294 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huberbe06d262009-08-14 14:37:10 -0700295 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700296 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700297 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800298 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700299 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700300
Andreas Huber2dc64d82009-09-11 12:58:53 -0700301 if (!strncmp(componentName, "OMX.TI.", 7)) {
302 // Apparently I must not use OMX_UseBuffer on either input or
303 // output ports on any of the TI components or quote:
304 // "(I) may have unexpected problem (sic) which can be timing related
305 // and hard to reproduce."
306
307 quirks |= kRequiresAllocateBufferOnInputPorts;
308 quirks |= kRequiresAllocateBufferOnOutputPorts;
309 }
310
Andreas Huberb8de9572010-02-22 14:58:45 -0800311 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
312 quirks |= kInputBufferSizesAreBogus;
313 }
314
Andreas Hubere13526a2009-10-22 10:43:34 -0700315 return quirks;
316}
317
318// static
319void OMXCodec::findMatchingCodecs(
320 const char *mime,
321 bool createEncoder, const char *matchComponentName,
322 uint32_t flags,
323 Vector<String8> *matchingCodecs) {
324 matchingCodecs->clear();
325
326 for (int index = 0;; ++index) {
327 const char *componentName;
328
329 if (createEncoder) {
330 componentName = GetCodec(
331 kEncoderInfo,
332 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
333 mime, index);
334 } else {
335 componentName = GetCodec(
336 kDecoderInfo,
337 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
338 mime, index);
339 }
340
341 if (!componentName) {
342 break;
343 }
344
345 // If a specific codec is requested, skip the non-matching ones.
346 if (matchComponentName && strcmp(componentName, matchComponentName)) {
347 continue;
348 }
349
350 matchingCodecs->push(String8(componentName));
351 }
352
353 if (flags & kPreferSoftwareCodecs) {
354 matchingCodecs->sort(CompareSoftwareCodecsFirst);
355 }
356}
357
358// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800359sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700360 const sp<IOMX> &omx,
361 const sp<MetaData> &meta, bool createEncoder,
362 const sp<MediaSource> &source,
363 const char *matchComponentName,
364 uint32_t flags) {
365 const char *mime;
366 bool success = meta->findCString(kKeyMIMEType, &mime);
367 CHECK(success);
368
369 Vector<String8> matchingCodecs;
370 findMatchingCodecs(
371 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
372
373 if (matchingCodecs.isEmpty()) {
374 return NULL;
375 }
376
377 sp<OMXCodecObserver> observer = new OMXCodecObserver;
378 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700379
380 const char *componentName;
381 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
382 componentName = matchingCodecs[i].string();
383
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800384#if BUILD_WITH_FULL_STAGEFRIGHT
385 sp<MediaSource> softwareCodec =
386 InstantiateSoftwareCodec(componentName, source);
387
388 if (softwareCodec != NULL) {
389 LOGV("Successfully allocated software codec '%s'", componentName);
390
391 return softwareCodec;
392 }
393#endif
394
Andreas Hubere13526a2009-10-22 10:43:34 -0700395 LOGV("Attempting to allocate OMX node '%s'", componentName);
396
397 status_t err = omx->allocateNode(componentName, observer, &node);
398 if (err == OK) {
399 LOGV("Successfully allocated OMX node '%s'", componentName);
400
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700401 sp<OMXCodec> codec = new OMXCodec(
402 omx, node, getComponentQuirks(componentName),
403 createEncoder, mime, componentName,
404 source);
405
406 observer->setCodec(codec);
407
408 err = codec->configureCodec(meta);
409
410 if (err == OK) {
411 return codec;
412 }
413
414 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700415 }
416 }
417
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700418 return NULL;
419}
Andreas Hubere13526a2009-10-22 10:43:34 -0700420
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700421status_t OMXCodec::configureCodec(const sp<MetaData> &meta) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700422 uint32_t type;
423 const void *data;
424 size_t size;
425 if (meta->findData(kKeyESDS, &type, &data, &size)) {
426 ESDS esds((const char *)data, size);
427 CHECK_EQ(esds.InitCheck(), OK);
428
429 const void *codec_specific_data;
430 size_t codec_specific_data_size;
431 esds.getCodecSpecificInfo(
432 &codec_specific_data, &codec_specific_data_size);
433
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700434 addCodecSpecificData(
Andreas Huberbe06d262009-08-14 14:37:10 -0700435 codec_specific_data, codec_specific_data_size);
436 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
Andreas Huberebf66ea2009-08-19 13:32:58 -0700437 // Parse the AVCDecoderConfigurationRecord
438
439 const uint8_t *ptr = (const uint8_t *)data;
440
441 CHECK(size >= 7);
442 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
443 uint8_t profile = ptr[1];
444 uint8_t level = ptr[3];
445
Andreas Huber44e15c42009-11-23 14:39:38 -0800446 // There is decodable content out there that fails the following
447 // assertion, let's be lenient for now...
448 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700449
450 size_t lengthSize = 1 + (ptr[4] & 3);
451
452 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
453 // violates it...
454 // CHECK((ptr[5] >> 5) == 7); // reserved
455
456 size_t numSeqParameterSets = ptr[5] & 31;
457
458 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700459 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700460
461 for (size_t i = 0; i < numSeqParameterSets; ++i) {
462 CHECK(size >= 2);
463 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700464
465 ptr += 2;
466 size -= 2;
467
Andreas Huberbe06d262009-08-14 14:37:10 -0700468 CHECK(size >= length);
469
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700470 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700471
472 ptr += length;
473 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700474 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700475
Andreas Huberebf66ea2009-08-19 13:32:58 -0700476 CHECK(size >= 1);
477 size_t numPictureParameterSets = *ptr;
478 ++ptr;
479 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700480
Andreas Huberebf66ea2009-08-19 13:32:58 -0700481 for (size_t i = 0; i < numPictureParameterSets; ++i) {
482 CHECK(size >= 2);
483 size_t length = U16_AT(ptr);
484
485 ptr += 2;
486 size -= 2;
487
488 CHECK(size >= length);
489
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700490 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700491
492 ptr += length;
493 size -= length;
494 }
495
Andreas Huber53a76bd2009-10-06 16:20:44 -0700496 LOGV("AVC profile = %d (%s), level = %d",
Andreas Huberebf66ea2009-08-19 13:32:58 -0700497 (int)profile, AVCProfileToString(profile), (int)level / 10);
498
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700499 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
Andreas Huberebf66ea2009-08-19 13:32:58 -0700500 && (profile != kAVCProfileBaseline || level > 39)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700501 // This stream exceeds the decoder's capabilities. The decoder
502 // does not handle this gracefully and would clobber the heap
503 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700504
505 LOGE("Profile and/or level exceed the decoder's capabilities.");
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700506 return ERROR_UNSUPPORTED;
Andreas Huberbe06d262009-08-14 14:37:10 -0700507 }
508 }
509
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700510 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
511 setAMRFormat(false /* isWAMR */);
Andreas Huberbe06d262009-08-14 14:37:10 -0700512 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700513 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
514 setAMRFormat(true /* isWAMR */);
Andreas Huberee606e62009-09-08 10:19:21 -0700515 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700516 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700517 int32_t numChannels, sampleRate;
518 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
519 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
520
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700521 setAACFormat(numChannels, sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700522 }
James Dongabed93a2010-04-22 17:27:04 -0700523
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700524 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700525 int32_t width, height;
526 bool success = meta->findInt32(kKeyWidth, &width);
527 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700528 CHECK(success);
Andreas Huberbe06d262009-08-14 14:37:10 -0700529
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700530 if (mIsEncoder) {
531 setVideoInputFormat(mMIME, width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700532 } else {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700533 status_t err = setVideoOutputFormat(
534 mMIME, width, height);
535
536 if (err != OK) {
537 return err;
538 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700539 }
540 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700541
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700542 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
543 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700544 OMX_COLOR_FORMATTYPE format =
545 OMX_COLOR_Format32bitARGB8888;
546 // OMX_COLOR_FormatYUV420PackedPlanar;
547 // OMX_COLOR_FormatCbYCrY;
548 // OMX_COLOR_FormatYUV411Planar;
549
550 int32_t width, height;
551 bool success = meta->findInt32(kKeyWidth, &width);
552 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700553
554 int32_t compressedSize;
555 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700556 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700557
558 CHECK(success);
559 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700560
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700561 setImageOutputFormat(format, width, height);
562 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700563 }
564
Andreas Huberda050cf22009-09-02 14:01:43 -0700565 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800566 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700567 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700568 }
569
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700570 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700571 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
572 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700573 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700574 }
575
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700576 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700577
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700578 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700579}
580
Andreas Huberda050cf22009-09-02 14:01:43 -0700581void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
582 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700583 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700584 def.nPortIndex = portIndex;
585
Andreas Huber784202e2009-10-15 13:46:54 -0700586 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700587 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
588 CHECK_EQ(err, OK);
589
Andreas Huberb8de9572010-02-22 14:58:45 -0800590 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
591 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700592 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700593 }
594
Andreas Huber784202e2009-10-15 13:46:54 -0700595 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700596 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
597 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800598
599 err = mOMX->getParameter(
600 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
601 CHECK_EQ(err, OK);
602
603 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800604 if (portIndex == kPortIndexInput
605 && (mQuirks & kInputBufferSizesAreBogus)) {
606 CHECK_EQ(def.nBufferSize, size);
607 } else {
608 CHECK(def.nBufferSize >= size);
609 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700610}
611
Andreas Huberbe06d262009-08-14 14:37:10 -0700612status_t OMXCodec::setVideoPortFormatType(
613 OMX_U32 portIndex,
614 OMX_VIDEO_CODINGTYPE compressionFormat,
615 OMX_COLOR_FORMATTYPE colorFormat) {
616 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700617 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700618 format.nPortIndex = portIndex;
619 format.nIndex = 0;
620 bool found = false;
621
622 OMX_U32 index = 0;
623 for (;;) {
624 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700625 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700626 mNode, OMX_IndexParamVideoPortFormat,
627 &format, sizeof(format));
628
629 if (err != OK) {
630 return err;
631 }
632
633 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700634 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700635
636#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700637 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700638 portIndex,
639 index, format.eCompressionFormat, format.eColorFormat);
640#endif
641
642 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
643 if (portIndex == kPortIndexInput
644 && colorFormat == format.eColorFormat) {
645 // eCompressionFormat does not seem right.
646 found = true;
647 break;
648 }
649 if (portIndex == kPortIndexOutput
650 && compressionFormat == format.eCompressionFormat) {
651 // eColorFormat does not seem right.
652 found = true;
653 break;
654 }
655 }
656
657 if (format.eCompressionFormat == compressionFormat
658 && format.eColorFormat == colorFormat) {
659 found = true;
660 break;
661 }
662
663 ++index;
664 }
665
666 if (!found) {
667 return UNKNOWN_ERROR;
668 }
669
Andreas Huber53a76bd2009-10-06 16:20:44 -0700670 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700671 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700672 mNode, OMX_IndexParamVideoPortFormat,
673 &format, sizeof(format));
674
675 return err;
676}
677
Andreas Huberb482ce82009-10-29 12:02:48 -0700678static size_t getFrameSize(
679 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
680 switch (colorFormat) {
681 case OMX_COLOR_FormatYCbYCr:
682 case OMX_COLOR_FormatCbYCrY:
683 return width * height * 2;
684
Andreas Huber71c27d92010-03-19 11:43:15 -0700685 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700686 case OMX_COLOR_FormatYUV420SemiPlanar:
687 return (width * height * 3) / 2;
688
689 default:
690 CHECK(!"Should not be here. Unsupported color format.");
691 break;
692 }
693}
694
Andreas Huberbe06d262009-08-14 14:37:10 -0700695void OMXCodec::setVideoInputFormat(
696 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700697 CODEC_LOGV("setVideoInputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700698
699 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700700 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700701 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700702 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700703 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700704 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700705 compressionFormat = OMX_VIDEO_CodingH263;
706 } else {
707 LOGE("Not a supported video mime type: %s", mime);
708 CHECK(!"Should not be here. Not a supported video mime type.");
709 }
710
Andreas Huberea6a38c2009-11-16 15:43:38 -0800711 OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
712 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
James Dongabed93a2010-04-22 17:27:04 -0700713 colorFormat = OMX_COLOR_FormatYCbYCr;
Andreas Huberbe06d262009-08-14 14:37:10 -0700714 }
715
James Dongb00e2462010-04-26 17:48:26 -0700716
717
718 status_t err;
719 OMX_PARAM_PORTDEFINITIONTYPE def;
720 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
721
722 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700723 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700724 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700725 colorFormat), OK);
James Dongb00e2462010-04-26 17:48:26 -0700726 InitOMXParams(&def);
727 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700728
James Dongb00e2462010-04-26 17:48:26 -0700729 err = mOMX->getParameter(
730 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
731 CHECK_EQ(err, OK);
732
733 def.nBufferSize = getFrameSize(colorFormat, width, height);
734
735 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
736
737 video_def->nFrameWidth = width;
738 video_def->nFrameHeight = height;
739 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
740 video_def->eColorFormat = colorFormat;
741
742 video_def->xFramerate = (24 << 16); // Q16 format
743
744 err = mOMX->setParameter(
745 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
746 CHECK_EQ(err, OK);
747
748 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700749 CHECK_EQ(setVideoPortFormatType(
750 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
751 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700752 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700753 def.nPortIndex = kPortIndexOutput;
754
James Dongb00e2462010-04-26 17:48:26 -0700755 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700756 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
757
758 CHECK_EQ(err, OK);
759 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
760
761 video_def->nFrameWidth = width;
762 video_def->nFrameHeight = height;
763
764 video_def->eCompressionFormat = compressionFormat;
765 video_def->eColorFormat = OMX_COLOR_FormatUnused;
766
Andreas Huber784202e2009-10-15 13:46:54 -0700767 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700768 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
769 CHECK_EQ(err, OK);
770
James Dongb00e2462010-04-26 17:48:26 -0700771 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700772 switch (compressionFormat) {
773 case OMX_VIDEO_CodingMPEG4:
774 {
775 CHECK_EQ(setupMPEG4EncoderParameters(), OK);
776 break;
777 }
778
779 case OMX_VIDEO_CodingH263:
780 break;
781
Andreas Huberea6a38c2009-11-16 15:43:38 -0800782 case OMX_VIDEO_CodingAVC:
783 {
784 CHECK_EQ(setupAVCEncoderParameters(), OK);
785 break;
786 }
787
Andreas Huberb482ce82009-10-29 12:02:48 -0700788 default:
789 CHECK(!"Support for this compressionFormat to be implemented.");
790 break;
791 }
792}
793
794status_t OMXCodec::setupMPEG4EncoderParameters() {
795 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
796 InitOMXParams(&mpeg4type);
797 mpeg4type.nPortIndex = kPortIndexOutput;
798
799 status_t err = mOMX->getParameter(
800 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
801 CHECK_EQ(err, OK);
802
803 mpeg4type.nSliceHeaderSpacing = 0;
804 mpeg4type.bSVH = OMX_FALSE;
805 mpeg4type.bGov = OMX_FALSE;
806
807 mpeg4type.nAllowedPictureTypes =
808 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
809
810 mpeg4type.nPFrames = 23;
811 mpeg4type.nBFrames = 0;
812
813 mpeg4type.nIDCVLCThreshold = 0;
814 mpeg4type.bACPred = OMX_TRUE;
815 mpeg4type.nMaxPacketSize = 256;
816 mpeg4type.nTimeIncRes = 1000;
817 mpeg4type.nHeaderExtension = 0;
818 mpeg4type.bReversibleVLC = OMX_FALSE;
819
820 mpeg4type.eProfile = OMX_VIDEO_MPEG4ProfileCore;
821 mpeg4type.eLevel = OMX_VIDEO_MPEG4Level2;
822
823 err = mOMX->setParameter(
824 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
825 CHECK_EQ(err, OK);
826
827 // ----------------
828
829 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
830 InitOMXParams(&bitrateType);
831 bitrateType.nPortIndex = kPortIndexOutput;
832
833 err = mOMX->getParameter(
834 mNode, OMX_IndexParamVideoBitrate,
835 &bitrateType, sizeof(bitrateType));
836 CHECK_EQ(err, OK);
837
838 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
839 bitrateType.nTargetBitrate = 1000000;
840
841 err = mOMX->setParameter(
842 mNode, OMX_IndexParamVideoBitrate,
843 &bitrateType, sizeof(bitrateType));
844 CHECK_EQ(err, OK);
845
846 // ----------------
847
848 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
849 InitOMXParams(&errorCorrectionType);
850 errorCorrectionType.nPortIndex = kPortIndexOutput;
851
852 err = mOMX->getParameter(
853 mNode, OMX_IndexParamVideoErrorCorrection,
854 &errorCorrectionType, sizeof(errorCorrectionType));
855 CHECK_EQ(err, OK);
856
857 errorCorrectionType.bEnableHEC = OMX_FALSE;
858 errorCorrectionType.bEnableResync = OMX_TRUE;
859 errorCorrectionType.nResynchMarkerSpacing = 256;
860 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
861 errorCorrectionType.bEnableRVLC = OMX_FALSE;
862
863 err = mOMX->setParameter(
864 mNode, OMX_IndexParamVideoErrorCorrection,
865 &errorCorrectionType, sizeof(errorCorrectionType));
866 CHECK_EQ(err, OK);
867
868 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700869}
870
Andreas Huberea6a38c2009-11-16 15:43:38 -0800871status_t OMXCodec::setupAVCEncoderParameters() {
872 OMX_VIDEO_PARAM_AVCTYPE h264type;
873 InitOMXParams(&h264type);
874 h264type.nPortIndex = kPortIndexOutput;
875
876 status_t err = mOMX->getParameter(
877 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
878 CHECK_EQ(err, OK);
879
880 h264type.nAllowedPictureTypes =
881 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
882
883 h264type.nSliceHeaderSpacing = 0;
884 h264type.nBFrames = 0;
885 h264type.bUseHadamard = OMX_TRUE;
886 h264type.nRefFrames = 1;
887 h264type.nRefIdx10ActiveMinus1 = 0;
888 h264type.nRefIdx11ActiveMinus1 = 0;
889 h264type.bEnableUEP = OMX_FALSE;
890 h264type.bEnableFMO = OMX_FALSE;
891 h264type.bEnableASO = OMX_FALSE;
892 h264type.bEnableRS = OMX_FALSE;
893 h264type.eProfile = OMX_VIDEO_AVCProfileBaseline;
894 h264type.eLevel = OMX_VIDEO_AVCLevel1b;
895 h264type.bFrameMBsOnly = OMX_TRUE;
896 h264type.bMBAFF = OMX_FALSE;
897 h264type.bEntropyCodingCABAC = OMX_FALSE;
898 h264type.bWeightedPPrediction = OMX_FALSE;
899 h264type.bconstIpred = OMX_FALSE;
900 h264type.bDirect8x8Inference = OMX_FALSE;
901 h264type.bDirectSpatialTemporal = OMX_FALSE;
902 h264type.nCabacInitIdc = 0;
903 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
904
905 err = mOMX->setParameter(
906 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
907 CHECK_EQ(err, OK);
908
909 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
910 InitOMXParams(&bitrateType);
911 bitrateType.nPortIndex = kPortIndexOutput;
912
913 err = mOMX->getParameter(
914 mNode, OMX_IndexParamVideoBitrate,
915 &bitrateType, sizeof(bitrateType));
916 CHECK_EQ(err, OK);
917
918 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
Andreas Huber71c27d92010-03-19 11:43:15 -0700919 bitrateType.nTargetBitrate = 3000000;
Andreas Huberea6a38c2009-11-16 15:43:38 -0800920
921 err = mOMX->setParameter(
922 mNode, OMX_IndexParamVideoBitrate,
923 &bitrateType, sizeof(bitrateType));
924 CHECK_EQ(err, OK);
925
926 return OK;
927}
928
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700929status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -0700930 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700931 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700932
Andreas Huberbe06d262009-08-14 14:37:10 -0700933 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700934 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700935 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700936 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700937 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700938 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700939 compressionFormat = OMX_VIDEO_CodingH263;
940 } else {
941 LOGE("Not a supported video mime type: %s", mime);
942 CHECK(!"Should not be here. Not a supported video mime type.");
943 }
944
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700945 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700946 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
947
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700948 if (err != OK) {
949 return err;
950 }
951
Andreas Huberbe06d262009-08-14 14:37:10 -0700952#if 1
953 {
954 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700955 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700956 format.nPortIndex = kPortIndexOutput;
957 format.nIndex = 0;
958
Andreas Huber784202e2009-10-15 13:46:54 -0700959 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700960 mNode, OMX_IndexParamVideoPortFormat,
961 &format, sizeof(format));
962 CHECK_EQ(err, OK);
963 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
964
965 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
966
967 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
968 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
969 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
970 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
971
Andreas Huber784202e2009-10-15 13:46:54 -0700972 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700973 mNode, OMX_IndexParamVideoPortFormat,
974 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700975
976 if (err != OK) {
977 return err;
978 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700979 }
980#endif
981
982 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700983 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700984 def.nPortIndex = kPortIndexInput;
985
Andreas Huber4c483422009-09-02 16:05:36 -0700986 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
987
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700988 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700989 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
990
991 CHECK_EQ(err, OK);
992
993#if 1
994 // XXX Need a (much) better heuristic to compute input buffer sizes.
995 const size_t X = 64 * 1024;
996 if (def.nBufferSize < X) {
997 def.nBufferSize = X;
998 }
999#endif
1000
1001 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1002
1003 video_def->nFrameWidth = width;
1004 video_def->nFrameHeight = height;
1005
Andreas Huberb482ce82009-10-29 12:02:48 -07001006 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001007 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1008
Andreas Huber784202e2009-10-15 13:46:54 -07001009 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001010 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001011
1012 if (err != OK) {
1013 return err;
1014 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001015
1016 ////////////////////////////////////////////////////////////////////////////
1017
Andreas Huber4c483422009-09-02 16:05:36 -07001018 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001019 def.nPortIndex = kPortIndexOutput;
1020
Andreas Huber784202e2009-10-15 13:46:54 -07001021 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001022 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1023 CHECK_EQ(err, OK);
1024 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1025
1026#if 0
1027 def.nBufferSize =
1028 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1029#endif
1030
1031 video_def->nFrameWidth = width;
1032 video_def->nFrameHeight = height;
1033
Andreas Huber784202e2009-10-15 13:46:54 -07001034 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001035 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001036
1037 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001038}
1039
Andreas Huberbe06d262009-08-14 14:37:10 -07001040OMXCodec::OMXCodec(
1041 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001042 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001043 const char *mime,
1044 const char *componentName,
1045 const sp<MediaSource> &source)
1046 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001047 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001048 mNode(node),
1049 mQuirks(quirks),
1050 mIsEncoder(isEncoder),
1051 mMIME(strdup(mime)),
1052 mComponentName(strdup(componentName)),
1053 mSource(source),
1054 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001055 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001056 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001057 mSignalledEOS(false),
1058 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001059 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001060 mSeekTimeUs(-1),
1061 mLeftOverBuffer(NULL) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001062 mPortStatus[kPortIndexInput] = ENABLED;
1063 mPortStatus[kPortIndexOutput] = ENABLED;
1064
Andreas Huber4c483422009-09-02 16:05:36 -07001065 setComponentRole();
1066}
1067
Andreas Hubere6c40962009-09-10 14:13:30 -07001068// static
1069void OMXCodec::setComponentRole(
1070 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1071 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001072 struct MimeToRole {
1073 const char *mime;
1074 const char *decoderRole;
1075 const char *encoderRole;
1076 };
1077
1078 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001079 { MEDIA_MIMETYPE_AUDIO_MPEG,
1080 "audio_decoder.mp3", "audio_encoder.mp3" },
1081 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1082 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1083 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1084 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1085 { MEDIA_MIMETYPE_AUDIO_AAC,
1086 "audio_decoder.aac", "audio_encoder.aac" },
1087 { MEDIA_MIMETYPE_VIDEO_AVC,
1088 "video_decoder.avc", "video_encoder.avc" },
1089 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1090 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1091 { MEDIA_MIMETYPE_VIDEO_H263,
1092 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001093 };
1094
1095 static const size_t kNumMimeToRole =
1096 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1097
1098 size_t i;
1099 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001100 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001101 break;
1102 }
1103 }
1104
1105 if (i == kNumMimeToRole) {
1106 return;
1107 }
1108
1109 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001110 isEncoder ? kMimeToRole[i].encoderRole
1111 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001112
1113 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001114 OMX_PARAM_COMPONENTROLETYPE roleParams;
1115 InitOMXParams(&roleParams);
1116
1117 strncpy((char *)roleParams.cRole,
1118 role, OMX_MAX_STRINGNAME_SIZE - 1);
1119
1120 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1121
Andreas Huber784202e2009-10-15 13:46:54 -07001122 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001123 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001124 &roleParams, sizeof(roleParams));
1125
1126 if (err != OK) {
1127 LOGW("Failed to set standard component role '%s'.", role);
1128 }
1129 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001130}
1131
Andreas Hubere6c40962009-09-10 14:13:30 -07001132void OMXCodec::setComponentRole() {
1133 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1134}
1135
Andreas Huberbe06d262009-08-14 14:37:10 -07001136OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001137 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001138
Andreas Huber784202e2009-10-15 13:46:54 -07001139 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001140 CHECK_EQ(err, OK);
1141
1142 mNode = NULL;
1143 setState(DEAD);
1144
1145 clearCodecSpecificData();
1146
1147 free(mComponentName);
1148 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001149
Andreas Huberbe06d262009-08-14 14:37:10 -07001150 free(mMIME);
1151 mMIME = NULL;
1152}
1153
1154status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001155 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001156
1157 CHECK_EQ(mState, LOADED);
1158
1159 status_t err;
1160 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001161 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001162 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001163 setState(LOADED_TO_IDLE);
1164 }
1165
1166 err = allocateBuffers();
1167 CHECK_EQ(err, OK);
1168
1169 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001170 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001171 CHECK_EQ(err, OK);
1172
1173 setState(LOADED_TO_IDLE);
1174 }
1175
1176 while (mState != EXECUTING && mState != ERROR) {
1177 mAsyncCompletion.wait(mLock);
1178 }
1179
1180 return mState == ERROR ? UNKNOWN_ERROR : OK;
1181}
1182
1183// static
1184bool OMXCodec::isIntermediateState(State state) {
1185 return state == LOADED_TO_IDLE
1186 || state == IDLE_TO_EXECUTING
1187 || state == EXECUTING_TO_IDLE
1188 || state == IDLE_TO_LOADED
1189 || state == RECONFIGURING;
1190}
1191
1192status_t OMXCodec::allocateBuffers() {
1193 status_t err = allocateBuffersOnPort(kPortIndexInput);
1194
1195 if (err != OK) {
1196 return err;
1197 }
1198
1199 return allocateBuffersOnPort(kPortIndexOutput);
1200}
1201
1202status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1203 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001204 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001205 def.nPortIndex = portIndex;
1206
Andreas Huber784202e2009-10-15 13:46:54 -07001207 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001208 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1209
1210 if (err != OK) {
1211 return err;
1212 }
1213
Andreas Huber5c0a9132009-08-20 11:16:40 -07001214 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001215 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001216
Andreas Huberbe06d262009-08-14 14:37:10 -07001217 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001218 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001219 CHECK(mem.get() != NULL);
1220
Andreas Huberc712b9f2010-01-20 15:05:46 -08001221 BufferInfo info;
1222 info.mData = NULL;
1223 info.mSize = def.nBufferSize;
1224
Andreas Huberbe06d262009-08-14 14:37:10 -07001225 IOMX::buffer_id buffer;
1226 if (portIndex == kPortIndexInput
1227 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001228 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001229 mem.clear();
1230
Andreas Huberf1fe0642010-01-15 15:28:19 -08001231 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001232 mNode, portIndex, def.nBufferSize, &buffer,
1233 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001234 } else {
1235 err = mOMX->allocateBufferWithBackup(
1236 mNode, portIndex, mem, &buffer);
1237 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001238 } else if (portIndex == kPortIndexOutput
1239 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001240 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001241 mem.clear();
1242
Andreas Huberf1fe0642010-01-15 15:28:19 -08001243 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001244 mNode, portIndex, def.nBufferSize, &buffer,
1245 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001246 } else {
1247 err = mOMX->allocateBufferWithBackup(
1248 mNode, portIndex, mem, &buffer);
1249 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001250 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001251 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001252 }
1253
1254 if (err != OK) {
1255 LOGE("allocate_buffer_with_backup failed");
1256 return err;
1257 }
1258
Andreas Huberc712b9f2010-01-20 15:05:46 -08001259 if (mem != NULL) {
1260 info.mData = mem->pointer();
1261 }
1262
Andreas Huberbe06d262009-08-14 14:37:10 -07001263 info.mBuffer = buffer;
1264 info.mOwnedByComponent = false;
1265 info.mMem = mem;
1266 info.mMediaBuffer = NULL;
1267
1268 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001269 if (!(mOMXLivesLocally
1270 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1271 && (mQuirks & kDefersOutputBufferAllocation))) {
1272 // If the node does not fill in the buffer ptr at this time,
1273 // we will defer creating the MediaBuffer until receiving
1274 // the first FILL_BUFFER_DONE notification instead.
1275 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1276 info.mMediaBuffer->setObserver(this);
1277 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001278 }
1279
1280 mPortBuffers[portIndex].push(info);
1281
Andreas Huber4c483422009-09-02 16:05:36 -07001282 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001283 portIndex == kPortIndexInput ? "input" : "output");
1284 }
1285
Andreas Huber2ea14e22009-12-16 09:30:55 -08001286 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001287
1288 return OK;
1289}
1290
1291void OMXCodec::on_message(const omx_message &msg) {
1292 Mutex::Autolock autoLock(mLock);
1293
1294 switch (msg.type) {
1295 case omx_message::EVENT:
1296 {
1297 onEvent(
1298 msg.u.event_data.event, msg.u.event_data.data1,
1299 msg.u.event_data.data2);
1300
1301 break;
1302 }
1303
1304 case omx_message::EMPTY_BUFFER_DONE:
1305 {
1306 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1307
Andreas Huber4c483422009-09-02 16:05:36 -07001308 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001309
1310 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1311 size_t i = 0;
1312 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1313 ++i;
1314 }
1315
1316 CHECK(i < buffers->size());
1317 if (!(*buffers)[i].mOwnedByComponent) {
1318 LOGW("We already own input buffer %p, yet received "
1319 "an EMPTY_BUFFER_DONE.", buffer);
1320 }
1321
1322 buffers->editItemAt(i).mOwnedByComponent = false;
1323
1324 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001325 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001326
1327 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001328 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001329 CHECK_EQ(err, OK);
1330
1331 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001332 } else if (mState != ERROR
1333 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001334 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1335 drainInputBuffer(&buffers->editItemAt(i));
1336 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001337 break;
1338 }
1339
1340 case omx_message::FILL_BUFFER_DONE:
1341 {
1342 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1343 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1344
Andreas Huber2ea14e22009-12-16 09:30:55 -08001345 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001346 buffer,
1347 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001348 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001349 msg.u.extended_buffer_data.timestamp,
1350 msg.u.extended_buffer_data.timestamp / 1E6);
1351
1352 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1353 size_t i = 0;
1354 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1355 ++i;
1356 }
1357
1358 CHECK(i < buffers->size());
1359 BufferInfo *info = &buffers->editItemAt(i);
1360
1361 if (!info->mOwnedByComponent) {
1362 LOGW("We already own output buffer %p, yet received "
1363 "a FILL_BUFFER_DONE.", buffer);
1364 }
1365
1366 info->mOwnedByComponent = false;
1367
1368 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001369 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001370
1371 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001372 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001373 CHECK_EQ(err, OK);
1374
1375 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001376#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001377 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1378 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001379 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001380 mNoMoreOutputData = true;
1381 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001382#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001383 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1384 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001385
Andreas Huber52733b82010-01-25 10:41:35 -08001386 if (info->mMediaBuffer == NULL) {
1387 CHECK(mOMXLivesLocally);
1388 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1389 CHECK(mQuirks & kDefersOutputBufferAllocation);
1390
1391 // The qcom video decoders on Nexus don't actually allocate
1392 // output buffer memory on a call to OMX_AllocateBuffer
1393 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1394 // structure is only filled in later.
1395
1396 info->mMediaBuffer = new MediaBuffer(
1397 msg.u.extended_buffer_data.data_ptr,
1398 info->mSize);
1399 info->mMediaBuffer->setObserver(this);
1400 }
1401
Andreas Huberbe06d262009-08-14 14:37:10 -07001402 MediaBuffer *buffer = info->mMediaBuffer;
1403
1404 buffer->set_range(
1405 msg.u.extended_buffer_data.range_offset,
1406 msg.u.extended_buffer_data.range_length);
1407
1408 buffer->meta_data()->clear();
1409
Andreas Huberfa8de752009-10-08 10:07:49 -07001410 buffer->meta_data()->setInt64(
1411 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001412
1413 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1414 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1415 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001416 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1417 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1418 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001419
1420 buffer->meta_data()->setPointer(
1421 kKeyPlatformPrivate,
1422 msg.u.extended_buffer_data.platform_private);
1423
1424 buffer->meta_data()->setPointer(
1425 kKeyBufferID,
1426 msg.u.extended_buffer_data.buffer);
1427
1428 mFilledBuffers.push_back(i);
1429 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001430
1431 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1432 CODEC_LOGV("No more output data.");
1433 mNoMoreOutputData = true;
1434 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001435 }
1436
1437 break;
1438 }
1439
1440 default:
1441 {
1442 CHECK(!"should not be here.");
1443 break;
1444 }
1445 }
1446}
1447
1448void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1449 switch (event) {
1450 case OMX_EventCmdComplete:
1451 {
1452 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1453 break;
1454 }
1455
1456 case OMX_EventError:
1457 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001458 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001459
1460 setState(ERROR);
1461 break;
1462 }
1463
1464 case OMX_EventPortSettingsChanged:
1465 {
1466 onPortSettingsChanged(data1);
1467 break;
1468 }
1469
Andreas Huber2ea14e22009-12-16 09:30:55 -08001470#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001471 case OMX_EventBufferFlag:
1472 {
Andreas Huber4c483422009-09-02 16:05:36 -07001473 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001474
1475 if (data1 == kPortIndexOutput) {
1476 mNoMoreOutputData = true;
1477 }
1478 break;
1479 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001480#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001481
1482 default:
1483 {
Andreas Huber4c483422009-09-02 16:05:36 -07001484 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001485 break;
1486 }
1487 }
1488}
1489
Andreas Huberb1678602009-10-19 13:06:40 -07001490// Has the format changed in any way that the client would have to be aware of?
1491static bool formatHasNotablyChanged(
1492 const sp<MetaData> &from, const sp<MetaData> &to) {
1493 if (from.get() == NULL && to.get() == NULL) {
1494 return false;
1495 }
1496
Andreas Huberf68c1682009-10-21 14:01:30 -07001497 if ((from.get() == NULL && to.get() != NULL)
1498 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001499 return true;
1500 }
1501
1502 const char *mime_from, *mime_to;
1503 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1504 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1505
1506 if (strcasecmp(mime_from, mime_to)) {
1507 return true;
1508 }
1509
1510 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1511 int32_t colorFormat_from, colorFormat_to;
1512 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1513 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1514
1515 if (colorFormat_from != colorFormat_to) {
1516 return true;
1517 }
1518
1519 int32_t width_from, width_to;
1520 CHECK(from->findInt32(kKeyWidth, &width_from));
1521 CHECK(to->findInt32(kKeyWidth, &width_to));
1522
1523 if (width_from != width_to) {
1524 return true;
1525 }
1526
1527 int32_t height_from, height_to;
1528 CHECK(from->findInt32(kKeyHeight, &height_from));
1529 CHECK(to->findInt32(kKeyHeight, &height_to));
1530
1531 if (height_from != height_to) {
1532 return true;
1533 }
1534 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1535 int32_t numChannels_from, numChannels_to;
1536 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1537 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1538
1539 if (numChannels_from != numChannels_to) {
1540 return true;
1541 }
1542
1543 int32_t sampleRate_from, sampleRate_to;
1544 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1545 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1546
1547 if (sampleRate_from != sampleRate_to) {
1548 return true;
1549 }
1550 }
1551
1552 return false;
1553}
1554
Andreas Huberbe06d262009-08-14 14:37:10 -07001555void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1556 switch (cmd) {
1557 case OMX_CommandStateSet:
1558 {
1559 onStateChange((OMX_STATETYPE)data);
1560 break;
1561 }
1562
1563 case OMX_CommandPortDisable:
1564 {
1565 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001566 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001567
1568 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1569 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1570 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1571
1572 mPortStatus[portIndex] = DISABLED;
1573
1574 if (mState == RECONFIGURING) {
1575 CHECK_EQ(portIndex, kPortIndexOutput);
1576
Andreas Huberb1678602009-10-19 13:06:40 -07001577 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001578 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001579
1580 // Don't notify clients if the output port settings change
1581 // wasn't of importance to them, i.e. it may be that just the
1582 // number of buffers has changed and nothing else.
1583 mOutputPortSettingsHaveChanged =
1584 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001585
Andreas Huberbe06d262009-08-14 14:37:10 -07001586 enablePortAsync(portIndex);
1587
1588 status_t err = allocateBuffersOnPort(portIndex);
1589 CHECK_EQ(err, OK);
1590 }
1591 break;
1592 }
1593
1594 case OMX_CommandPortEnable:
1595 {
1596 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001597 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001598
1599 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1600 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1601
1602 mPortStatus[portIndex] = ENABLED;
1603
1604 if (mState == RECONFIGURING) {
1605 CHECK_EQ(portIndex, kPortIndexOutput);
1606
1607 setState(EXECUTING);
1608
1609 fillOutputBuffers();
1610 }
1611 break;
1612 }
1613
1614 case OMX_CommandFlush:
1615 {
1616 OMX_U32 portIndex = data;
1617
Andreas Huber4c483422009-09-02 16:05:36 -07001618 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001619
1620 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1621 mPortStatus[portIndex] = ENABLED;
1622
1623 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1624 mPortBuffers[portIndex].size());
1625
1626 if (mState == RECONFIGURING) {
1627 CHECK_EQ(portIndex, kPortIndexOutput);
1628
1629 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001630 } else if (mState == EXECUTING_TO_IDLE) {
1631 if (mPortStatus[kPortIndexInput] == ENABLED
1632 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001633 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001634 "transition from EXECUTING to IDLE.");
1635
1636 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1637 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1638
1639 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001640 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001641 CHECK_EQ(err, OK);
1642 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001643 } else {
1644 // We're flushing both ports in preparation for seeking.
1645
1646 if (mPortStatus[kPortIndexInput] == ENABLED
1647 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001648 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001649 " seek-time.");
1650
1651 drainInputBuffers();
1652 fillOutputBuffers();
1653 }
1654 }
1655
1656 break;
1657 }
1658
1659 default:
1660 {
Andreas Huber4c483422009-09-02 16:05:36 -07001661 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001662 break;
1663 }
1664 }
1665}
1666
1667void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001668 CODEC_LOGV("onStateChange %d", newState);
1669
Andreas Huberbe06d262009-08-14 14:37:10 -07001670 switch (newState) {
1671 case OMX_StateIdle:
1672 {
Andreas Huber4c483422009-09-02 16:05:36 -07001673 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001674 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001675 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001676 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1677
1678 CHECK_EQ(err, OK);
1679
1680 setState(IDLE_TO_EXECUTING);
1681 } else {
1682 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1683
1684 CHECK_EQ(
1685 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1686 mPortBuffers[kPortIndexInput].size());
1687
1688 CHECK_EQ(
1689 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1690 mPortBuffers[kPortIndexOutput].size());
1691
Andreas Huber784202e2009-10-15 13:46:54 -07001692 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001693 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1694
1695 CHECK_EQ(err, OK);
1696
1697 err = freeBuffersOnPort(kPortIndexInput);
1698 CHECK_EQ(err, OK);
1699
1700 err = freeBuffersOnPort(kPortIndexOutput);
1701 CHECK_EQ(err, OK);
1702
1703 mPortStatus[kPortIndexInput] = ENABLED;
1704 mPortStatus[kPortIndexOutput] = ENABLED;
1705
1706 setState(IDLE_TO_LOADED);
1707 }
1708 break;
1709 }
1710
1711 case OMX_StateExecuting:
1712 {
1713 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1714
Andreas Huber4c483422009-09-02 16:05:36 -07001715 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001716
1717 setState(EXECUTING);
1718
Andreas Huber42978e52009-08-27 10:08:39 -07001719 // Buffers will be submitted to the component in the first
1720 // call to OMXCodec::read as mInitialBufferSubmit is true at
1721 // this point. This ensures that this on_message call returns,
1722 // releases the lock and ::init can notice the state change and
1723 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001724 break;
1725 }
1726
1727 case OMX_StateLoaded:
1728 {
1729 CHECK_EQ(mState, IDLE_TO_LOADED);
1730
Andreas Huber4c483422009-09-02 16:05:36 -07001731 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001732
1733 setState(LOADED);
1734 break;
1735 }
1736
Andreas Huberc712b9f2010-01-20 15:05:46 -08001737 case OMX_StateInvalid:
1738 {
1739 setState(ERROR);
1740 break;
1741 }
1742
Andreas Huberbe06d262009-08-14 14:37:10 -07001743 default:
1744 {
1745 CHECK(!"should not be here.");
1746 break;
1747 }
1748 }
1749}
1750
1751// static
1752size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1753 size_t n = 0;
1754 for (size_t i = 0; i < buffers.size(); ++i) {
1755 if (!buffers[i].mOwnedByComponent) {
1756 ++n;
1757 }
1758 }
1759
1760 return n;
1761}
1762
1763status_t OMXCodec::freeBuffersOnPort(
1764 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1765 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1766
1767 status_t stickyErr = OK;
1768
1769 for (size_t i = buffers->size(); i-- > 0;) {
1770 BufferInfo *info = &buffers->editItemAt(i);
1771
1772 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1773 continue;
1774 }
1775
1776 CHECK_EQ(info->mOwnedByComponent, false);
1777
Andreas Huber92022852009-09-14 15:24:14 -07001778 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1779
Andreas Huberbe06d262009-08-14 14:37:10 -07001780 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001781 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001782
1783 if (err != OK) {
1784 stickyErr = err;
1785 }
1786
1787 if (info->mMediaBuffer != NULL) {
1788 info->mMediaBuffer->setObserver(NULL);
1789
1790 // Make sure nobody but us owns this buffer at this point.
1791 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1792
1793 info->mMediaBuffer->release();
1794 }
1795
1796 buffers->removeAt(i);
1797 }
1798
1799 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1800
1801 return stickyErr;
1802}
1803
1804void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001805 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001806
1807 CHECK_EQ(mState, EXECUTING);
1808 CHECK_EQ(portIndex, kPortIndexOutput);
1809 setState(RECONFIGURING);
1810
1811 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001812 if (!flushPortAsync(portIndex)) {
1813 onCmdComplete(OMX_CommandFlush, portIndex);
1814 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001815 } else {
1816 disablePortAsync(portIndex);
1817 }
1818}
1819
Andreas Huber404cc412009-08-25 14:26:05 -07001820bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001821 CHECK(mState == EXECUTING || mState == RECONFIGURING
1822 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001823
Andreas Huber4c483422009-09-02 16:05:36 -07001824 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001825 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1826 mPortBuffers[portIndex].size());
1827
Andreas Huberbe06d262009-08-14 14:37:10 -07001828 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1829 mPortStatus[portIndex] = SHUTTING_DOWN;
1830
Andreas Huber404cc412009-08-25 14:26:05 -07001831 if ((mQuirks & kRequiresFlushCompleteEmulation)
1832 && countBuffersWeOwn(mPortBuffers[portIndex])
1833 == mPortBuffers[portIndex].size()) {
1834 // No flush is necessary and this component fails to send a
1835 // flush-complete event in this case.
1836
1837 return false;
1838 }
1839
Andreas Huberbe06d262009-08-14 14:37:10 -07001840 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001841 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001842 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001843
1844 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001845}
1846
1847void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1848 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1849
1850 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1851 mPortStatus[portIndex] = DISABLING;
1852
1853 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001854 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001855 CHECK_EQ(err, OK);
1856
1857 freeBuffersOnPort(portIndex, true);
1858}
1859
1860void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1861 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1862
1863 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1864 mPortStatus[portIndex] = ENABLING;
1865
1866 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001867 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001868 CHECK_EQ(err, OK);
1869}
1870
1871void OMXCodec::fillOutputBuffers() {
1872 CHECK_EQ(mState, EXECUTING);
1873
Andreas Huberdbcb2c62010-01-14 11:32:13 -08001874 // This is a workaround for some decoders not properly reporting
1875 // end-of-output-stream. If we own all input buffers and also own
1876 // all output buffers and we already signalled end-of-input-stream,
1877 // the end-of-output-stream is implied.
1878 if (mSignalledEOS
1879 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
1880 == mPortBuffers[kPortIndexInput].size()
1881 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
1882 == mPortBuffers[kPortIndexOutput].size()) {
1883 mNoMoreOutputData = true;
1884 mBufferFilled.signal();
1885
1886 return;
1887 }
1888
Andreas Huberbe06d262009-08-14 14:37:10 -07001889 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1890 for (size_t i = 0; i < buffers->size(); ++i) {
1891 fillOutputBuffer(&buffers->editItemAt(i));
1892 }
1893}
1894
1895void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001896 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001897
1898 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1899 for (size_t i = 0; i < buffers->size(); ++i) {
1900 drainInputBuffer(&buffers->editItemAt(i));
1901 }
1902}
1903
1904void OMXCodec::drainInputBuffer(BufferInfo *info) {
1905 CHECK_EQ(info->mOwnedByComponent, false);
1906
1907 if (mSignalledEOS) {
1908 return;
1909 }
1910
1911 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1912 const CodecSpecificData *specific =
1913 mCodecSpecificData[mCodecSpecificDataIndex];
1914
1915 size_t size = specific->mSize;
1916
Andreas Hubere6c40962009-09-10 14:13:30 -07001917 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07001918 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001919 static const uint8_t kNALStartCode[4] =
1920 { 0x00, 0x00, 0x00, 0x01 };
1921
Andreas Huberc712b9f2010-01-20 15:05:46 -08001922 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07001923
1924 size += 4;
1925
Andreas Huberc712b9f2010-01-20 15:05:46 -08001926 memcpy(info->mData, kNALStartCode, 4);
1927 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07001928 specific->mData, specific->mSize);
1929 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001930 CHECK(info->mSize >= specific->mSize);
1931 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001932 }
1933
Andreas Huber2ea14e22009-12-16 09:30:55 -08001934 mNoMoreOutputData = false;
1935
Andreas Huberdbcb2c62010-01-14 11:32:13 -08001936 CODEC_LOGV("calling emptyBuffer with codec specific data");
1937
Andreas Huber784202e2009-10-15 13:46:54 -07001938 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07001939 mNode, info->mBuffer, 0, size,
1940 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
1941 0);
Andreas Huber3f427072009-10-08 11:02:27 -07001942 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001943
1944 info->mOwnedByComponent = true;
1945
1946 ++mCodecSpecificDataIndex;
1947 return;
1948 }
1949
Andreas Huberbe06d262009-08-14 14:37:10 -07001950 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08001951
Andreas Hubera4357ad2010-04-02 12:49:54 -07001952 bool signalEOS = false;
1953 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07001954
Andreas Hubera4357ad2010-04-02 12:49:54 -07001955 size_t offset = 0;
1956 int32_t n = 0;
1957 for (;;) {
1958 MediaBuffer *srcBuffer;
1959 if (mSeekTimeUs >= 0) {
1960 if (mLeftOverBuffer) {
1961 mLeftOverBuffer->release();
1962 mLeftOverBuffer = NULL;
1963 }
1964
1965 MediaSource::ReadOptions options;
1966 options.setSeekTo(mSeekTimeUs);
1967
1968 mSeekTimeUs = -1;
1969 mBufferFilled.signal();
1970
1971 err = mSource->read(&srcBuffer, &options);
1972 } else if (mLeftOverBuffer) {
1973 srcBuffer = mLeftOverBuffer;
1974 mLeftOverBuffer = NULL;
1975
1976 err = OK;
1977 } else {
1978 err = mSource->read(&srcBuffer);
1979 }
1980
1981 if (err != OK) {
1982 signalEOS = true;
1983 mFinalStatus = err;
1984 mSignalledEOS = true;
1985 break;
1986 }
1987
1988 size_t remainingBytes = info->mSize - offset;
1989
1990 if (srcBuffer->range_length() > remainingBytes) {
1991 if (offset == 0) {
1992 CODEC_LOGE(
1993 "Codec's input buffers are too small to accomodate "
1994 "buffer read from source (info->mSize = %d, srcLength = %d)",
1995 info->mSize, srcBuffer->range_length());
1996
1997 srcBuffer->release();
1998 srcBuffer = NULL;
1999
2000 setState(ERROR);
2001 return;
2002 }
2003
2004 mLeftOverBuffer = srcBuffer;
2005 break;
2006 }
2007
2008 memcpy((uint8_t *)info->mData + offset,
2009 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2010 srcBuffer->range_length());
2011
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002012 int64_t lastBufferTimeUs;
2013 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
2014 CHECK(timestampUs >= 0);
2015
Andreas Hubera4357ad2010-04-02 12:49:54 -07002016 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002017 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002018 }
2019
2020 offset += srcBuffer->range_length();
2021
2022 srcBuffer->release();
2023 srcBuffer = NULL;
2024
2025 ++n;
2026
2027 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2028 break;
2029 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002030
2031 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2032
2033 if (coalescedDurationUs > 250000ll) {
2034 // Don't coalesce more than 250ms worth of encoded data at once.
2035 break;
2036 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002037 }
2038
2039 if (n > 1) {
2040 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002041 }
2042
2043 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002044
Andreas Hubera4357ad2010-04-02 12:49:54 -07002045 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002046 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002047 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002048 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002049 }
2050
Andreas Hubera4357ad2010-04-02 12:49:54 -07002051 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2052 "timestamp %lld us (%.2f secs)",
2053 info->mBuffer, offset,
2054 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002055
Andreas Huber784202e2009-10-15 13:46:54 -07002056 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002057 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002058 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002059
2060 if (err != OK) {
2061 setState(ERROR);
2062 return;
2063 }
2064
2065 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002066
2067 // This component does not ever signal the EOS flag on output buffers,
2068 // Thanks for nothing.
2069 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2070 mNoMoreOutputData = true;
2071 mBufferFilled.signal();
2072 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002073}
2074
2075void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2076 CHECK_EQ(info->mOwnedByComponent, false);
2077
Andreas Huber404cc412009-08-25 14:26:05 -07002078 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002079 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002080 "calling fillOutputBuffer");
2081 return;
2082 }
2083
Andreas Huber4c483422009-09-02 16:05:36 -07002084 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002085 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002086
2087 if (err != OK) {
2088 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2089
2090 setState(ERROR);
2091 return;
2092 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002093
2094 info->mOwnedByComponent = true;
2095}
2096
2097void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2098 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2099 for (size_t i = 0; i < buffers->size(); ++i) {
2100 if ((*buffers)[i].mBuffer == buffer) {
2101 drainInputBuffer(&buffers->editItemAt(i));
2102 return;
2103 }
2104 }
2105
2106 CHECK(!"should not be here.");
2107}
2108
2109void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2110 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2111 for (size_t i = 0; i < buffers->size(); ++i) {
2112 if ((*buffers)[i].mBuffer == buffer) {
2113 fillOutputBuffer(&buffers->editItemAt(i));
2114 return;
2115 }
2116 }
2117
2118 CHECK(!"should not be here.");
2119}
2120
2121void OMXCodec::setState(State newState) {
2122 mState = newState;
2123 mAsyncCompletion.signal();
2124
2125 // This may cause some spurious wakeups but is necessary to
2126 // unblock the reader if we enter ERROR state.
2127 mBufferFilled.signal();
2128}
2129
Andreas Huberda050cf22009-09-02 14:01:43 -07002130void OMXCodec::setRawAudioFormat(
2131 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002132
2133 // port definition
2134 OMX_PARAM_PORTDEFINITIONTYPE def;
2135 InitOMXParams(&def);
2136 def.nPortIndex = portIndex;
2137 status_t err = mOMX->getParameter(
2138 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2139 CHECK_EQ(err, OK);
2140 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2141 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2142 &def, sizeof(def)), OK);
2143
2144 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002145 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002146 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002147 pcmParams.nPortIndex = portIndex;
2148
James Dongabed93a2010-04-22 17:27:04 -07002149 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002150 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2151
2152 CHECK_EQ(err, OK);
2153
2154 pcmParams.nChannels = numChannels;
2155 pcmParams.eNumData = OMX_NumericalDataSigned;
2156 pcmParams.bInterleaved = OMX_TRUE;
2157 pcmParams.nBitPerSample = 16;
2158 pcmParams.nSamplingRate = sampleRate;
2159 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2160
2161 if (numChannels == 1) {
2162 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2163 } else {
2164 CHECK_EQ(numChannels, 2);
2165
2166 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2167 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2168 }
2169
Andreas Huber784202e2009-10-15 13:46:54 -07002170 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002171 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2172
2173 CHECK_EQ(err, OK);
2174}
2175
Andreas Huber8768f2c2009-12-01 15:26:54 -08002176void OMXCodec::setAMRFormat(bool isWAMR) {
2177 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002178
Andreas Huber8768f2c2009-12-01 15:26:54 -08002179 OMX_AUDIO_PARAM_AMRTYPE def;
2180 InitOMXParams(&def);
2181 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002182
Andreas Huber8768f2c2009-12-01 15:26:54 -08002183 status_t err =
2184 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002185
Andreas Huber8768f2c2009-12-01 15:26:54 -08002186 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002187
Andreas Huber8768f2c2009-12-01 15:26:54 -08002188 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002189
2190 // XXX: Select bandmode based on bit rate
Andreas Huber8768f2c2009-12-01 15:26:54 -08002191 def.eAMRBandMode =
2192 isWAMR ? OMX_AUDIO_AMRBandModeWB0 : OMX_AUDIO_AMRBandModeNB0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002193
Andreas Huber8768f2c2009-12-01 15:26:54 -08002194 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2195 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002196
2197 ////////////////////////
2198
2199 if (mIsEncoder) {
2200 sp<MetaData> format = mSource->getFormat();
2201 int32_t sampleRate;
2202 int32_t numChannels;
2203 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2204 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2205
2206 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2207 }
2208}
2209
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07002210void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate) {
James Dongabed93a2010-04-22 17:27:04 -07002211 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002212 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002213 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002214 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002215
2216 //////////////// output port ////////////////////
2217 // format
2218 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2219 format.nPortIndex = kPortIndexOutput;
2220 format.nIndex = 0;
2221 status_t err = OMX_ErrorNone;
2222 while (OMX_ErrorNone == err) {
2223 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2224 &format, sizeof(format)), OK);
2225 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2226 break;
2227 }
2228 format.nIndex++;
2229 }
2230 CHECK_EQ(OK, err);
2231 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2232 &format, sizeof(format)), OK);
2233
2234 // port definition
2235 OMX_PARAM_PORTDEFINITIONTYPE def;
2236 InitOMXParams(&def);
2237 def.nPortIndex = kPortIndexOutput;
2238 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2239 &def, sizeof(def)), OK);
2240 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2241 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2242 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2243 &def, sizeof(def)), OK);
2244
2245 // profile
2246 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2247 InitOMXParams(&profile);
2248 profile.nPortIndex = kPortIndexOutput;
2249 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2250 &profile, sizeof(profile)), OK);
2251 profile.nChannels = numChannels;
2252 profile.eChannelMode = (numChannels == 1?
2253 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2254 profile.nSampleRate = sampleRate;
2255 profile.nBitRate = 96000; // XXX
2256 profile.nAudioBandWidth = 0;
2257 profile.nFrameLength = 0;
2258 profile.nAACtools = OMX_AUDIO_AACToolAll;
2259 profile.nAACERtools = OMX_AUDIO_AACERNone;
2260 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2261 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2262 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2263 &profile, sizeof(profile)), OK);
2264
Andreas Huberda050cf22009-09-02 14:01:43 -07002265 } else {
2266 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002267 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002268 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002269
Andreas Huber784202e2009-10-15 13:46:54 -07002270 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002271 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2272 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002273
Andreas Huberda050cf22009-09-02 14:01:43 -07002274 profile.nChannels = numChannels;
2275 profile.nSampleRate = sampleRate;
2276 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002277
Andreas Huber784202e2009-10-15 13:46:54 -07002278 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002279 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2280 CHECK_EQ(err, OK);
2281 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002282}
2283
2284void OMXCodec::setImageOutputFormat(
2285 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002286 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002287
2288#if 0
2289 OMX_INDEXTYPE index;
2290 status_t err = mOMX->get_extension_index(
2291 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2292 CHECK_EQ(err, OK);
2293
2294 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2295 CHECK_EQ(err, OK);
2296#endif
2297
2298 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002299 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002300 def.nPortIndex = kPortIndexOutput;
2301
Andreas Huber784202e2009-10-15 13:46:54 -07002302 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002303 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2304 CHECK_EQ(err, OK);
2305
2306 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2307
2308 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002309
Andreas Huberbe06d262009-08-14 14:37:10 -07002310 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2311 imageDef->eColorFormat = format;
2312 imageDef->nFrameWidth = width;
2313 imageDef->nFrameHeight = height;
2314
2315 switch (format) {
2316 case OMX_COLOR_FormatYUV420PackedPlanar:
2317 case OMX_COLOR_FormatYUV411Planar:
2318 {
2319 def.nBufferSize = (width * height * 3) / 2;
2320 break;
2321 }
2322
2323 case OMX_COLOR_FormatCbYCrY:
2324 {
2325 def.nBufferSize = width * height * 2;
2326 break;
2327 }
2328
2329 case OMX_COLOR_Format32bitARGB8888:
2330 {
2331 def.nBufferSize = width * height * 4;
2332 break;
2333 }
2334
Andreas Huber201511c2009-09-08 14:01:44 -07002335 case OMX_COLOR_Format16bitARGB4444:
2336 case OMX_COLOR_Format16bitARGB1555:
2337 case OMX_COLOR_Format16bitRGB565:
2338 case OMX_COLOR_Format16bitBGR565:
2339 {
2340 def.nBufferSize = width * height * 2;
2341 break;
2342 }
2343
Andreas Huberbe06d262009-08-14 14:37:10 -07002344 default:
2345 CHECK(!"Should not be here. Unknown color format.");
2346 break;
2347 }
2348
Andreas Huber5c0a9132009-08-20 11:16:40 -07002349 def.nBufferCountActual = def.nBufferCountMin;
2350
Andreas Huber784202e2009-10-15 13:46:54 -07002351 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002352 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2353 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002354}
Andreas Huberbe06d262009-08-14 14:37:10 -07002355
Andreas Huber5c0a9132009-08-20 11:16:40 -07002356void OMXCodec::setJPEGInputFormat(
2357 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2358 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002359 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002360 def.nPortIndex = kPortIndexInput;
2361
Andreas Huber784202e2009-10-15 13:46:54 -07002362 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002363 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2364 CHECK_EQ(err, OK);
2365
Andreas Huber5c0a9132009-08-20 11:16:40 -07002366 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2367 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2368
Andreas Huberbe06d262009-08-14 14:37:10 -07002369 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2370 imageDef->nFrameWidth = width;
2371 imageDef->nFrameHeight = height;
2372
Andreas Huber5c0a9132009-08-20 11:16:40 -07002373 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002374 def.nBufferCountActual = def.nBufferCountMin;
2375
Andreas Huber784202e2009-10-15 13:46:54 -07002376 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002377 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2378 CHECK_EQ(err, OK);
2379}
2380
2381void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2382 CodecSpecificData *specific =
2383 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2384
2385 specific->mSize = size;
2386 memcpy(specific->mData, data, size);
2387
2388 mCodecSpecificData.push(specific);
2389}
2390
2391void OMXCodec::clearCodecSpecificData() {
2392 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2393 free(mCodecSpecificData.editItemAt(i));
2394 }
2395 mCodecSpecificData.clear();
2396 mCodecSpecificDataIndex = 0;
2397}
2398
2399status_t OMXCodec::start(MetaData *) {
Andreas Huber42978e52009-08-27 10:08:39 -07002400 Mutex::Autolock autoLock(mLock);
2401
Andreas Huberbe06d262009-08-14 14:37:10 -07002402 if (mState != LOADED) {
2403 return UNKNOWN_ERROR;
2404 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002405
Andreas Huberbe06d262009-08-14 14:37:10 -07002406 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002407 if (mQuirks & kWantsNALFragments) {
2408 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002409 }
2410 status_t err = mSource->start(params.get());
2411
2412 if (err != OK) {
2413 return err;
2414 }
2415
2416 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002417 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002418 mSignalledEOS = false;
2419 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002420 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002421 mSeekTimeUs = -1;
2422 mFilledBuffers.clear();
2423
2424 return init();
2425}
2426
2427status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002428 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002429
2430 Mutex::Autolock autoLock(mLock);
2431
2432 while (isIntermediateState(mState)) {
2433 mAsyncCompletion.wait(mLock);
2434 }
2435
2436 switch (mState) {
2437 case LOADED:
2438 case ERROR:
2439 break;
2440
2441 case EXECUTING:
2442 {
2443 setState(EXECUTING_TO_IDLE);
2444
Andreas Huber127fcdc2009-08-26 16:27:02 -07002445 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002446 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002447 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002448
Andreas Huber127fcdc2009-08-26 16:27:02 -07002449 bool emulateInputFlushCompletion =
2450 !flushPortAsync(kPortIndexInput);
2451
2452 bool emulateOutputFlushCompletion =
2453 !flushPortAsync(kPortIndexOutput);
2454
2455 if (emulateInputFlushCompletion) {
2456 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2457 }
2458
2459 if (emulateOutputFlushCompletion) {
2460 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2461 }
2462 } else {
2463 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2464 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2465
2466 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002467 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002468 CHECK_EQ(err, OK);
2469 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002470
2471 while (mState != LOADED && mState != ERROR) {
2472 mAsyncCompletion.wait(mLock);
2473 }
2474
2475 break;
2476 }
2477
2478 default:
2479 {
2480 CHECK(!"should not be here.");
2481 break;
2482 }
2483 }
2484
Andreas Hubera4357ad2010-04-02 12:49:54 -07002485 if (mLeftOverBuffer) {
2486 mLeftOverBuffer->release();
2487 mLeftOverBuffer = NULL;
2488 }
2489
Andreas Huberbe06d262009-08-14 14:37:10 -07002490 mSource->stop();
2491
Andreas Huber4a9375e2010-02-09 11:54:33 -08002492 CODEC_LOGV("stopped");
2493
Andreas Huberbe06d262009-08-14 14:37:10 -07002494 return OK;
2495}
2496
2497sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002498 Mutex::Autolock autoLock(mLock);
2499
Andreas Huberbe06d262009-08-14 14:37:10 -07002500 return mOutputFormat;
2501}
2502
2503status_t OMXCodec::read(
2504 MediaBuffer **buffer, const ReadOptions *options) {
2505 *buffer = NULL;
2506
2507 Mutex::Autolock autoLock(mLock);
2508
Andreas Huberd06e5b82009-08-28 13:18:14 -07002509 if (mState != EXECUTING && mState != RECONFIGURING) {
2510 return UNKNOWN_ERROR;
2511 }
2512
Andreas Hubere981c332009-10-22 13:49:30 -07002513 bool seeking = false;
2514 int64_t seekTimeUs;
2515 if (options && options->getSeekTo(&seekTimeUs)) {
2516 seeking = true;
2517 }
2518
Andreas Huber42978e52009-08-27 10:08:39 -07002519 if (mInitialBufferSubmit) {
2520 mInitialBufferSubmit = false;
2521
Andreas Hubere981c332009-10-22 13:49:30 -07002522 if (seeking) {
2523 CHECK(seekTimeUs >= 0);
2524 mSeekTimeUs = seekTimeUs;
2525
2526 // There's no reason to trigger the code below, there's
2527 // nothing to flush yet.
2528 seeking = false;
2529 }
2530
Andreas Huber42978e52009-08-27 10:08:39 -07002531 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002532
Andreas Huberd06e5b82009-08-28 13:18:14 -07002533 if (mState == EXECUTING) {
2534 // Otherwise mState == RECONFIGURING and this code will trigger
2535 // after the output port is reenabled.
2536 fillOutputBuffers();
2537 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002538 }
2539
Andreas Hubere981c332009-10-22 13:49:30 -07002540 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002541 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002542
2543 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002544
2545 CHECK(seekTimeUs >= 0);
2546 mSeekTimeUs = seekTimeUs;
2547
2548 mFilledBuffers.clear();
2549
2550 CHECK_EQ(mState, EXECUTING);
2551
Andreas Huber404cc412009-08-25 14:26:05 -07002552 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2553 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2554
2555 if (emulateInputFlushCompletion) {
2556 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2557 }
2558
2559 if (emulateOutputFlushCompletion) {
2560 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2561 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002562
2563 while (mSeekTimeUs >= 0) {
2564 mBufferFilled.wait(mLock);
2565 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002566 }
2567
2568 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2569 mBufferFilled.wait(mLock);
2570 }
2571
2572 if (mState == ERROR) {
2573 return UNKNOWN_ERROR;
2574 }
2575
2576 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08002577 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07002578 }
2579
Andreas Hubercfd55572009-10-09 14:11:28 -07002580 if (mOutputPortSettingsHaveChanged) {
2581 mOutputPortSettingsHaveChanged = false;
2582
2583 return INFO_FORMAT_CHANGED;
2584 }
2585
Andreas Huberbe06d262009-08-14 14:37:10 -07002586 size_t index = *mFilledBuffers.begin();
2587 mFilledBuffers.erase(mFilledBuffers.begin());
2588
2589 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2590 info->mMediaBuffer->add_ref();
2591 *buffer = info->mMediaBuffer;
2592
2593 return OK;
2594}
2595
2596void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2597 Mutex::Autolock autoLock(mLock);
2598
2599 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2600 for (size_t i = 0; i < buffers->size(); ++i) {
2601 BufferInfo *info = &buffers->editItemAt(i);
2602
2603 if (info->mMediaBuffer == buffer) {
2604 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2605 fillOutputBuffer(info);
2606 return;
2607 }
2608 }
2609
2610 CHECK(!"should not be here.");
2611}
2612
2613static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2614 static const char *kNames[] = {
2615 "OMX_IMAGE_CodingUnused",
2616 "OMX_IMAGE_CodingAutoDetect",
2617 "OMX_IMAGE_CodingJPEG",
2618 "OMX_IMAGE_CodingJPEG2K",
2619 "OMX_IMAGE_CodingEXIF",
2620 "OMX_IMAGE_CodingTIFF",
2621 "OMX_IMAGE_CodingGIF",
2622 "OMX_IMAGE_CodingPNG",
2623 "OMX_IMAGE_CodingLZW",
2624 "OMX_IMAGE_CodingBMP",
2625 };
2626
2627 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2628
2629 if (type < 0 || (size_t)type >= numNames) {
2630 return "UNKNOWN";
2631 } else {
2632 return kNames[type];
2633 }
2634}
2635
2636static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2637 static const char *kNames[] = {
2638 "OMX_COLOR_FormatUnused",
2639 "OMX_COLOR_FormatMonochrome",
2640 "OMX_COLOR_Format8bitRGB332",
2641 "OMX_COLOR_Format12bitRGB444",
2642 "OMX_COLOR_Format16bitARGB4444",
2643 "OMX_COLOR_Format16bitARGB1555",
2644 "OMX_COLOR_Format16bitRGB565",
2645 "OMX_COLOR_Format16bitBGR565",
2646 "OMX_COLOR_Format18bitRGB666",
2647 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002648 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002649 "OMX_COLOR_Format24bitRGB888",
2650 "OMX_COLOR_Format24bitBGR888",
2651 "OMX_COLOR_Format24bitARGB1887",
2652 "OMX_COLOR_Format25bitARGB1888",
2653 "OMX_COLOR_Format32bitBGRA8888",
2654 "OMX_COLOR_Format32bitARGB8888",
2655 "OMX_COLOR_FormatYUV411Planar",
2656 "OMX_COLOR_FormatYUV411PackedPlanar",
2657 "OMX_COLOR_FormatYUV420Planar",
2658 "OMX_COLOR_FormatYUV420PackedPlanar",
2659 "OMX_COLOR_FormatYUV420SemiPlanar",
2660 "OMX_COLOR_FormatYUV422Planar",
2661 "OMX_COLOR_FormatYUV422PackedPlanar",
2662 "OMX_COLOR_FormatYUV422SemiPlanar",
2663 "OMX_COLOR_FormatYCbYCr",
2664 "OMX_COLOR_FormatYCrYCb",
2665 "OMX_COLOR_FormatCbYCrY",
2666 "OMX_COLOR_FormatCrYCbY",
2667 "OMX_COLOR_FormatYUV444Interleaved",
2668 "OMX_COLOR_FormatRawBayer8bit",
2669 "OMX_COLOR_FormatRawBayer10bit",
2670 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002671 "OMX_COLOR_FormatL2",
2672 "OMX_COLOR_FormatL4",
2673 "OMX_COLOR_FormatL8",
2674 "OMX_COLOR_FormatL16",
2675 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002676 "OMX_COLOR_FormatL32",
2677 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2678 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2679 "OMX_COLOR_Format18BitBGR666",
2680 "OMX_COLOR_Format24BitARGB6666",
2681 "OMX_COLOR_Format24BitABGR6666",
2682 };
2683
2684 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2685
Andreas Huberbe06d262009-08-14 14:37:10 -07002686 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2687 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2688 } else if (type < 0 || (size_t)type >= numNames) {
2689 return "UNKNOWN";
2690 } else {
2691 return kNames[type];
2692 }
2693}
2694
2695static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2696 static const char *kNames[] = {
2697 "OMX_VIDEO_CodingUnused",
2698 "OMX_VIDEO_CodingAutoDetect",
2699 "OMX_VIDEO_CodingMPEG2",
2700 "OMX_VIDEO_CodingH263",
2701 "OMX_VIDEO_CodingMPEG4",
2702 "OMX_VIDEO_CodingWMV",
2703 "OMX_VIDEO_CodingRV",
2704 "OMX_VIDEO_CodingAVC",
2705 "OMX_VIDEO_CodingMJPEG",
2706 };
2707
2708 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2709
2710 if (type < 0 || (size_t)type >= numNames) {
2711 return "UNKNOWN";
2712 } else {
2713 return kNames[type];
2714 }
2715}
2716
2717static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2718 static const char *kNames[] = {
2719 "OMX_AUDIO_CodingUnused",
2720 "OMX_AUDIO_CodingAutoDetect",
2721 "OMX_AUDIO_CodingPCM",
2722 "OMX_AUDIO_CodingADPCM",
2723 "OMX_AUDIO_CodingAMR",
2724 "OMX_AUDIO_CodingGSMFR",
2725 "OMX_AUDIO_CodingGSMEFR",
2726 "OMX_AUDIO_CodingGSMHR",
2727 "OMX_AUDIO_CodingPDCFR",
2728 "OMX_AUDIO_CodingPDCEFR",
2729 "OMX_AUDIO_CodingPDCHR",
2730 "OMX_AUDIO_CodingTDMAFR",
2731 "OMX_AUDIO_CodingTDMAEFR",
2732 "OMX_AUDIO_CodingQCELP8",
2733 "OMX_AUDIO_CodingQCELP13",
2734 "OMX_AUDIO_CodingEVRC",
2735 "OMX_AUDIO_CodingSMV",
2736 "OMX_AUDIO_CodingG711",
2737 "OMX_AUDIO_CodingG723",
2738 "OMX_AUDIO_CodingG726",
2739 "OMX_AUDIO_CodingG729",
2740 "OMX_AUDIO_CodingAAC",
2741 "OMX_AUDIO_CodingMP3",
2742 "OMX_AUDIO_CodingSBC",
2743 "OMX_AUDIO_CodingVORBIS",
2744 "OMX_AUDIO_CodingWMA",
2745 "OMX_AUDIO_CodingRA",
2746 "OMX_AUDIO_CodingMIDI",
2747 };
2748
2749 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2750
2751 if (type < 0 || (size_t)type >= numNames) {
2752 return "UNKNOWN";
2753 } else {
2754 return kNames[type];
2755 }
2756}
2757
2758static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2759 static const char *kNames[] = {
2760 "OMX_AUDIO_PCMModeLinear",
2761 "OMX_AUDIO_PCMModeALaw",
2762 "OMX_AUDIO_PCMModeMULaw",
2763 };
2764
2765 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2766
2767 if (type < 0 || (size_t)type >= numNames) {
2768 return "UNKNOWN";
2769 } else {
2770 return kNames[type];
2771 }
2772}
2773
Andreas Huber7ae02c82009-09-09 16:29:47 -07002774static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2775 static const char *kNames[] = {
2776 "OMX_AUDIO_AMRBandModeUnused",
2777 "OMX_AUDIO_AMRBandModeNB0",
2778 "OMX_AUDIO_AMRBandModeNB1",
2779 "OMX_AUDIO_AMRBandModeNB2",
2780 "OMX_AUDIO_AMRBandModeNB3",
2781 "OMX_AUDIO_AMRBandModeNB4",
2782 "OMX_AUDIO_AMRBandModeNB5",
2783 "OMX_AUDIO_AMRBandModeNB6",
2784 "OMX_AUDIO_AMRBandModeNB7",
2785 "OMX_AUDIO_AMRBandModeWB0",
2786 "OMX_AUDIO_AMRBandModeWB1",
2787 "OMX_AUDIO_AMRBandModeWB2",
2788 "OMX_AUDIO_AMRBandModeWB3",
2789 "OMX_AUDIO_AMRBandModeWB4",
2790 "OMX_AUDIO_AMRBandModeWB5",
2791 "OMX_AUDIO_AMRBandModeWB6",
2792 "OMX_AUDIO_AMRBandModeWB7",
2793 "OMX_AUDIO_AMRBandModeWB8",
2794 };
2795
2796 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2797
2798 if (type < 0 || (size_t)type >= numNames) {
2799 return "UNKNOWN";
2800 } else {
2801 return kNames[type];
2802 }
2803}
2804
2805static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2806 static const char *kNames[] = {
2807 "OMX_AUDIO_AMRFrameFormatConformance",
2808 "OMX_AUDIO_AMRFrameFormatIF1",
2809 "OMX_AUDIO_AMRFrameFormatIF2",
2810 "OMX_AUDIO_AMRFrameFormatFSF",
2811 "OMX_AUDIO_AMRFrameFormatRTPPayload",
2812 "OMX_AUDIO_AMRFrameFormatITU",
2813 };
2814
2815 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2816
2817 if (type < 0 || (size_t)type >= numNames) {
2818 return "UNKNOWN";
2819 } else {
2820 return kNames[type];
2821 }
2822}
Andreas Huberbe06d262009-08-14 14:37:10 -07002823
2824void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2825 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002826 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002827 def.nPortIndex = portIndex;
2828
Andreas Huber784202e2009-10-15 13:46:54 -07002829 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002830 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2831 CHECK_EQ(err, OK);
2832
2833 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2834
2835 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2836 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2837
2838 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2839 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2840 printf(" nBufferSize = %ld\n", def.nBufferSize);
2841
2842 switch (def.eDomain) {
2843 case OMX_PortDomainImage:
2844 {
2845 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2846
2847 printf("\n");
2848 printf(" // Image\n");
2849 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
2850 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
2851 printf(" nStride = %ld\n", imageDef->nStride);
2852
2853 printf(" eCompressionFormat = %s\n",
2854 imageCompressionFormatString(imageDef->eCompressionFormat));
2855
2856 printf(" eColorFormat = %s\n",
2857 colorFormatString(imageDef->eColorFormat));
2858
2859 break;
2860 }
2861
2862 case OMX_PortDomainVideo:
2863 {
2864 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2865
2866 printf("\n");
2867 printf(" // Video\n");
2868 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
2869 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
2870 printf(" nStride = %ld\n", videoDef->nStride);
2871
2872 printf(" eCompressionFormat = %s\n",
2873 videoCompressionFormatString(videoDef->eCompressionFormat));
2874
2875 printf(" eColorFormat = %s\n",
2876 colorFormatString(videoDef->eColorFormat));
2877
2878 break;
2879 }
2880
2881 case OMX_PortDomainAudio:
2882 {
2883 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
2884
2885 printf("\n");
2886 printf(" // Audio\n");
2887 printf(" eEncoding = %s\n",
2888 audioCodingTypeString(audioDef->eEncoding));
2889
2890 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
2891 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002892 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07002893 params.nPortIndex = portIndex;
2894
Andreas Huber784202e2009-10-15 13:46:54 -07002895 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002896 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2897 CHECK_EQ(err, OK);
2898
2899 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
2900 printf(" nChannels = %ld\n", params.nChannels);
2901 printf(" bInterleaved = %d\n", params.bInterleaved);
2902 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
2903
2904 printf(" eNumData = %s\n",
2905 params.eNumData == OMX_NumericalDataSigned
2906 ? "signed" : "unsigned");
2907
2908 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07002909 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
2910 OMX_AUDIO_PARAM_AMRTYPE amr;
2911 InitOMXParams(&amr);
2912 amr.nPortIndex = portIndex;
2913
Andreas Huber784202e2009-10-15 13:46:54 -07002914 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07002915 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2916 CHECK_EQ(err, OK);
2917
2918 printf(" nChannels = %ld\n", amr.nChannels);
2919 printf(" eAMRBandMode = %s\n",
2920 amrBandModeString(amr.eAMRBandMode));
2921 printf(" eAMRFrameFormat = %s\n",
2922 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07002923 }
2924
2925 break;
2926 }
2927
2928 default:
2929 {
2930 printf(" // Unknown\n");
2931 break;
2932 }
2933 }
2934
2935 printf("}\n");
2936}
2937
2938void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
2939 mOutputFormat = new MetaData;
2940 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
2941
2942 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002943 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002944 def.nPortIndex = kPortIndexOutput;
2945
Andreas Huber784202e2009-10-15 13:46:54 -07002946 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002947 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2948 CHECK_EQ(err, OK);
2949
2950 switch (def.eDomain) {
2951 case OMX_PortDomainImage:
2952 {
2953 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2954 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2955
Andreas Hubere6c40962009-09-10 14:13:30 -07002956 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002957 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
2958 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
2959 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
2960 break;
2961 }
2962
2963 case OMX_PortDomainAudio:
2964 {
2965 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
2966
Andreas Huberda050cf22009-09-02 14:01:43 -07002967 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
2968 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002969 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07002970 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002971
Andreas Huber784202e2009-10-15 13:46:54 -07002972 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002973 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2974 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002975
Andreas Huberda050cf22009-09-02 14:01:43 -07002976 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
2977 CHECK_EQ(params.nBitPerSample, 16);
2978 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07002979
Andreas Huberda050cf22009-09-02 14:01:43 -07002980 int32_t numChannels, sampleRate;
2981 inputFormat->findInt32(kKeyChannelCount, &numChannels);
2982 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07002983
Andreas Huberda050cf22009-09-02 14:01:43 -07002984 if ((OMX_U32)numChannels != params.nChannels) {
2985 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08002986 "the input stream contains (contains %d channels, "
2987 "codec outputs %ld channels).",
2988 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07002989 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002990
Andreas Hubere6c40962009-09-10 14:13:30 -07002991 mOutputFormat->setCString(
2992 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07002993
2994 // Use the codec-advertised number of channels, as some
2995 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08002996 // mono. If we know the codec lies about this information,
2997 // use the actual number of channels instead.
2998 mOutputFormat->setInt32(
2999 kKeyChannelCount,
3000 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3001 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003002
3003 // The codec-reported sampleRate is not reliable...
3004 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3005 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003006 OMX_AUDIO_PARAM_AMRTYPE amr;
3007 InitOMXParams(&amr);
3008 amr.nPortIndex = kPortIndexOutput;
3009
Andreas Huber784202e2009-10-15 13:46:54 -07003010 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003011 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3012 CHECK_EQ(err, OK);
3013
3014 CHECK_EQ(amr.nChannels, 1);
3015 mOutputFormat->setInt32(kKeyChannelCount, 1);
3016
3017 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3018 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003019 mOutputFormat->setCString(
3020 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003021 mOutputFormat->setInt32(kKeySampleRate, 8000);
3022 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3023 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003024 mOutputFormat->setCString(
3025 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003026 mOutputFormat->setInt32(kKeySampleRate, 16000);
3027 } else {
3028 CHECK(!"Unknown AMR band mode.");
3029 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003030 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003031 mOutputFormat->setCString(
3032 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dongabed93a2010-04-22 17:27:04 -07003033 int32_t numChannels, sampleRate;
3034 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3035 inputFormat->findInt32(kKeySampleRate, &sampleRate);
3036 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3037 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003038 } else {
3039 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003040 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003041 break;
3042 }
3043
3044 case OMX_PortDomainVideo:
3045 {
3046 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3047
3048 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003049 mOutputFormat->setCString(
3050 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003051 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003052 mOutputFormat->setCString(
3053 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003054 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003055 mOutputFormat->setCString(
3056 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003057 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003058 mOutputFormat->setCString(
3059 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003060 } else {
3061 CHECK(!"Unknown compression format.");
3062 }
3063
3064 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3065 // This component appears to be lying to me.
3066 mOutputFormat->setInt32(
3067 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3068 mOutputFormat->setInt32(
3069 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3070 } else {
3071 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3072 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3073 }
3074
3075 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3076 break;
3077 }
3078
3079 default:
3080 {
3081 CHECK(!"should not be here, neither audio nor video.");
3082 break;
3083 }
3084 }
3085}
3086
Andreas Hubere6c40962009-09-10 14:13:30 -07003087////////////////////////////////////////////////////////////////////////////////
3088
3089status_t QueryCodecs(
3090 const sp<IOMX> &omx,
3091 const char *mime, bool queryDecoders,
3092 Vector<CodecCapabilities> *results) {
3093 results->clear();
3094
3095 for (int index = 0;; ++index) {
3096 const char *componentName;
3097
3098 if (!queryDecoders) {
3099 componentName = GetCodec(
3100 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3101 mime, index);
3102 } else {
3103 componentName = GetCodec(
3104 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3105 mime, index);
3106 }
3107
3108 if (!componentName) {
3109 return OK;
3110 }
3111
Andreas Huber1a189a82010-03-24 13:49:20 -07003112 if (strncmp(componentName, "OMX.", 4)) {
3113 // Not an OpenMax component but a software codec.
3114
3115 results->push();
3116 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3117 caps->mComponentName = componentName;
3118
3119 continue;
3120 }
3121
Andreas Huber784202e2009-10-15 13:46:54 -07003122 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003123 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003124 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003125
3126 if (err != OK) {
3127 continue;
3128 }
3129
James Dong722d5912010-04-13 10:56:59 -07003130 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003131
3132 results->push();
3133 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3134 caps->mComponentName = componentName;
3135
3136 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3137 InitOMXParams(&param);
3138
3139 param.nPortIndex = queryDecoders ? 0 : 1;
3140
3141 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003142 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003143 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3144 &param, sizeof(param));
3145
3146 if (err != OK) {
3147 break;
3148 }
3149
3150 CodecProfileLevel profileLevel;
3151 profileLevel.mProfile = param.eProfile;
3152 profileLevel.mLevel = param.eLevel;
3153
3154 caps->mProfileLevels.push(profileLevel);
3155 }
3156
Andreas Huber784202e2009-10-15 13:46:54 -07003157 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003158 }
3159}
3160
Andreas Huberbe06d262009-08-14 14:37:10 -07003161} // namespace android