blob: 2686489e3dc2fa8a010802d5932cff41bbedbfda [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 Huber8c7ab032009-12-07 11:23:44 -080021#if BUILD_WITH_FULL_STAGEFRIGHT
Andreas Huberdacaa732009-12-07 09:56:32 -080022#include "include/AACDecoder.h"
Andreas Hubera30d4002009-12-08 15:40:06 -080023#include "include/AMRNBDecoder.h"
Andreas Huberd49b526dd2009-12-11 15:07:25 -080024#include "include/AMRNBEncoder.h"
Andreas Hubera30d4002009-12-08 15:40:06 -080025#include "include/AMRWBDecoder.h"
Andreas Huber4a0ec3f2009-12-10 09:44:29 -080026#include "include/AVCDecoder.h"
James Dong02f5b542009-12-15 16:26:55 -080027#include "include/M4vH263Decoder.h"
Andreas Huber250f2432009-12-07 14:22:35 -080028#include "include/MP3Decoder.h"
Andreas Huber8c7ab032009-12-07 11:23:44 -080029#endif
30
Andreas Huberbd7b43b2009-10-13 10:22:55 -070031#include "include/ESDS.h"
32
Andreas Huberbe06d262009-08-14 14:37:10 -070033#include <binder/IServiceManager.h>
34#include <binder/MemoryDealer.h>
35#include <binder/ProcessState.h>
36#include <media/IMediaPlayerService.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070037#include <media/stagefright/MediaBuffer.h>
38#include <media/stagefright/MediaBufferGroup.h>
39#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070040#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070041#include <media/stagefright/MediaExtractor.h>
42#include <media/stagefright/MetaData.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070043#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070044#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070045#include <utils/Vector.h>
46
47#include <OMX_Audio.h>
48#include <OMX_Component.h>
49
50namespace android {
51
Andreas Huber8b432b12009-10-07 13:36:52 -070052static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
53
Andreas Huberbe06d262009-08-14 14:37:10 -070054struct CodecInfo {
55 const char *mime;
56 const char *codec;
57};
58
Andreas Huberfb1c2f82009-12-15 13:25:11 -080059#if BUILD_WITH_FULL_STAGEFRIGHT
60#define OPTIONAL(x,y) { x, y },
61
62#define FACTORY_CREATE(name) \
63static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
64 return new name(source); \
65}
66
67#define FACTORY_REF(name) { #name, Make##name },
68
69FACTORY_CREATE(MP3Decoder)
70FACTORY_CREATE(AMRNBDecoder)
71FACTORY_CREATE(AMRWBDecoder)
72FACTORY_CREATE(AACDecoder)
73FACTORY_CREATE(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -080074FACTORY_CREATE(M4vH263Decoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -080075FACTORY_CREATE(AMRNBEncoder)
76
77static sp<MediaSource> InstantiateSoftwareCodec(
78 const char *name, const sp<MediaSource> &source) {
79 struct FactoryInfo {
80 const char *name;
81 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
82 };
83
84 static const FactoryInfo kFactoryInfo[] = {
85 FACTORY_REF(MP3Decoder)
86 FACTORY_REF(AMRNBDecoder)
87 FACTORY_REF(AMRWBDecoder)
88 FACTORY_REF(AACDecoder)
89 FACTORY_REF(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -080090 FACTORY_REF(M4vH263Decoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -080091 FACTORY_REF(AMRNBEncoder)
92 };
93 for (size_t i = 0;
94 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
95 if (!strcmp(name, kFactoryInfo[i].name)) {
96 return (*kFactoryInfo[i].CreateFunc)(source);
97 }
98 }
99
100 return NULL;
101}
102
103#undef FACTORY_REF
104#undef FACTORY_CREATE
105
106#else
107#define OPTIONAL(x,y)
108#endif
109
Andreas Huberbe06d262009-08-14 14:37:10 -0700110static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700111 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
112 { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800113 OPTIONAL(MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder")
Andreas Hubere6c40962009-09-10 14:13:30 -0700114 { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
115 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800116 OPTIONAL(MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder")
Andreas Hubere6c40962009-09-10 14:13:30 -0700117 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
118 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800119 OPTIONAL(MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder")
Andreas Hubere6c40962009-09-10 14:13:30 -0700120 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
121 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800122 OPTIONAL(MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder")
Andreas Hubere6c40962009-09-10 14:13:30 -0700123 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
124 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
125 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
James Dong4e4795c2009-12-18 18:40:43 -0800126 OPTIONAL(MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder")
Andreas Hubere6c40962009-09-10 14:13:30 -0700127 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
128 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
129 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.Decoder" },
James Dong4e4795c2009-12-18 18:40:43 -0800130 OPTIONAL(MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder")
Andreas Hubere6c40962009-09-10 14:13:30 -0700131 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
132 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
133 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800134 OPTIONAL(MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder")
Andreas Hubere6c40962009-09-10 14:13:30 -0700135 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700136};
137
138static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700139 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800140 OPTIONAL(MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder")
Andreas Hubere6c40962009-09-10 14:13:30 -0700141 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrencnb" },
142 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
143 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
144 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
145 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
146 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
147 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
148 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
149 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
150 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
151 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
152 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700153};
154
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800155#undef OPTIONAL
156
Andreas Hubere0873732009-09-10 09:57:53 -0700157#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700158#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
159
Andreas Huberbe06d262009-08-14 14:37:10 -0700160struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700161 OMXCodecObserver() {
162 }
163
164 void setCodec(const sp<OMXCodec> &target) {
165 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700166 }
167
168 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700169 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700170 sp<OMXCodec> codec = mTarget.promote();
171
172 if (codec.get() != NULL) {
173 codec->on_message(msg);
174 }
175 }
176
177protected:
178 virtual ~OMXCodecObserver() {}
179
180private:
181 wp<OMXCodec> mTarget;
182
183 OMXCodecObserver(const OMXCodecObserver &);
184 OMXCodecObserver &operator=(const OMXCodecObserver &);
185};
186
187static const char *GetCodec(const CodecInfo *info, size_t numInfos,
188 const char *mime, int index) {
189 CHECK(index >= 0);
190 for(size_t i = 0; i < numInfos; ++i) {
191 if (!strcasecmp(mime, info[i].mime)) {
192 if (index == 0) {
193 return info[i].codec;
194 }
195
196 --index;
197 }
198 }
199
200 return NULL;
201}
202
Andreas Huberebf66ea2009-08-19 13:32:58 -0700203enum {
204 kAVCProfileBaseline = 0x42,
205 kAVCProfileMain = 0x4d,
206 kAVCProfileExtended = 0x58,
207 kAVCProfileHigh = 0x64,
208 kAVCProfileHigh10 = 0x6e,
209 kAVCProfileHigh422 = 0x7a,
210 kAVCProfileHigh444 = 0xf4,
211 kAVCProfileCAVLC444Intra = 0x2c
212};
213
214static const char *AVCProfileToString(uint8_t profile) {
215 switch (profile) {
216 case kAVCProfileBaseline:
217 return "Baseline";
218 case kAVCProfileMain:
219 return "Main";
220 case kAVCProfileExtended:
221 return "Extended";
222 case kAVCProfileHigh:
223 return "High";
224 case kAVCProfileHigh10:
225 return "High 10";
226 case kAVCProfileHigh422:
227 return "High 422";
228 case kAVCProfileHigh444:
229 return "High 444";
230 case kAVCProfileCAVLC444Intra:
231 return "CAVLC 444 Intra";
232 default: return "Unknown";
233 }
234}
235
Andreas Huber4c483422009-09-02 16:05:36 -0700236template<class T>
237static void InitOMXParams(T *params) {
238 params->nSize = sizeof(T);
239 params->nVersion.s.nVersionMajor = 1;
240 params->nVersion.s.nVersionMinor = 0;
241 params->nVersion.s.nRevision = 0;
242 params->nVersion.s.nStep = 0;
243}
244
Andreas Hubere13526a2009-10-22 10:43:34 -0700245static bool IsSoftwareCodec(const char *componentName) {
246 if (!strncmp("OMX.PV.", componentName, 7)) {
247 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700248 }
249
Andreas Hubere13526a2009-10-22 10:43:34 -0700250 return false;
251}
252
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800253// A sort order in which non-OMX components are first,
254// followed by software codecs, i.e. OMX.PV.*, followed
255// by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700256static int CompareSoftwareCodecsFirst(
257 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800258 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
259 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
260
261 if (isNotOMX1) {
262 if (isNotOMX2) { return 0; }
263 return -1;
264 }
265 if (isNotOMX2) {
266 return 1;
267 }
268
Andreas Hubere13526a2009-10-22 10:43:34 -0700269 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
270 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
271
272 if (isSoftwareCodec1) {
273 if (isSoftwareCodec2) { return 0; }
274 return -1;
275 }
276
277 if (isSoftwareCodec2) {
278 return 1;
279 }
280
281 return 0;
282}
283
284// static
285uint32_t OMXCodec::getComponentQuirks(const char *componentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700286 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700287
Andreas Huberbe06d262009-08-14 14:37:10 -0700288 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700289 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700290 }
291 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
292 quirks |= kNeedsFlushBeforeDisable;
293 }
294 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
295 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700296 quirks |= kRequiresFlushCompleteEmulation;
Andreas Huberbe06d262009-08-14 14:37:10 -0700297 }
298 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
299 quirks |= kRequiresLoadedToIdleAfterAllocation;
300 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700301 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huberbe06d262009-08-14 14:37:10 -0700302 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700303 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
304 // XXX Required on P....on only.
305 quirks |= kRequiresAllocateBufferOnOutputPorts;
306 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700307
Andreas Huber2dc64d82009-09-11 12:58:53 -0700308 if (!strncmp(componentName, "OMX.TI.", 7)) {
309 // Apparently I must not use OMX_UseBuffer on either input or
310 // output ports on any of the TI components or quote:
311 // "(I) may have unexpected problem (sic) which can be timing related
312 // and hard to reproduce."
313
314 quirks |= kRequiresAllocateBufferOnInputPorts;
315 quirks |= kRequiresAllocateBufferOnOutputPorts;
316 }
317
Andreas Hubere13526a2009-10-22 10:43:34 -0700318 return quirks;
319}
320
321// static
322void OMXCodec::findMatchingCodecs(
323 const char *mime,
324 bool createEncoder, const char *matchComponentName,
325 uint32_t flags,
326 Vector<String8> *matchingCodecs) {
327 matchingCodecs->clear();
328
329 for (int index = 0;; ++index) {
330 const char *componentName;
331
332 if (createEncoder) {
333 componentName = GetCodec(
334 kEncoderInfo,
335 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
336 mime, index);
337 } else {
338 componentName = GetCodec(
339 kDecoderInfo,
340 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
341 mime, index);
342 }
343
344 if (!componentName) {
345 break;
346 }
347
348 // If a specific codec is requested, skip the non-matching ones.
349 if (matchComponentName && strcmp(componentName, matchComponentName)) {
350 continue;
351 }
352
353 matchingCodecs->push(String8(componentName));
354 }
355
356 if (flags & kPreferSoftwareCodecs) {
357 matchingCodecs->sort(CompareSoftwareCodecsFirst);
358 }
359}
360
361// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800362sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700363 const sp<IOMX> &omx,
364 const sp<MetaData> &meta, bool createEncoder,
365 const sp<MediaSource> &source,
366 const char *matchComponentName,
367 uint32_t flags) {
368 const char *mime;
369 bool success = meta->findCString(kKeyMIMEType, &mime);
370 CHECK(success);
371
372 Vector<String8> matchingCodecs;
373 findMatchingCodecs(
374 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
375
376 if (matchingCodecs.isEmpty()) {
377 return NULL;
378 }
379
380 sp<OMXCodecObserver> observer = new OMXCodecObserver;
381 IOMX::node_id node = 0;
382 success = false;
383
384 const char *componentName;
385 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
386 componentName = matchingCodecs[i].string();
387
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800388#if BUILD_WITH_FULL_STAGEFRIGHT
389 sp<MediaSource> softwareCodec =
390 InstantiateSoftwareCodec(componentName, source);
391
392 if (softwareCodec != NULL) {
393 LOGV("Successfully allocated software codec '%s'", componentName);
394
395 return softwareCodec;
396 }
397#endif
398
Andreas Hubere13526a2009-10-22 10:43:34 -0700399 LOGV("Attempting to allocate OMX node '%s'", componentName);
400
401 status_t err = omx->allocateNode(componentName, observer, &node);
402 if (err == OK) {
403 LOGV("Successfully allocated OMX node '%s'", componentName);
404
405 success = true;
406 break;
407 }
408 }
409
410 if (!success) {
411 return NULL;
412 }
413
Andreas Huberbe06d262009-08-14 14:37:10 -0700414 sp<OMXCodec> codec = new OMXCodec(
Andreas Hubere13526a2009-10-22 10:43:34 -0700415 omx, node, getComponentQuirks(componentName),
416 createEncoder, mime, componentName,
Andreas Huberbe06d262009-08-14 14:37:10 -0700417 source);
418
Andreas Huber784202e2009-10-15 13:46:54 -0700419 observer->setCodec(codec);
420
Andreas Huberbe06d262009-08-14 14:37:10 -0700421 uint32_t type;
422 const void *data;
423 size_t size;
424 if (meta->findData(kKeyESDS, &type, &data, &size)) {
425 ESDS esds((const char *)data, size);
426 CHECK_EQ(esds.InitCheck(), OK);
427
428 const void *codec_specific_data;
429 size_t codec_specific_data_size;
430 esds.getCodecSpecificInfo(
431 &codec_specific_data, &codec_specific_data_size);
432
Andreas Huberbe06d262009-08-14 14:37:10 -0700433 codec->addCodecSpecificData(
434 codec_specific_data, codec_specific_data_size);
435 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
Andreas Huberebf66ea2009-08-19 13:32:58 -0700436 // Parse the AVCDecoderConfigurationRecord
437
438 const uint8_t *ptr = (const uint8_t *)data;
439
440 CHECK(size >= 7);
441 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
442 uint8_t profile = ptr[1];
443 uint8_t level = ptr[3];
444
Andreas Huber44e15c42009-11-23 14:39:38 -0800445 // There is decodable content out there that fails the following
446 // assertion, let's be lenient for now...
447 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700448
449 size_t lengthSize = 1 + (ptr[4] & 3);
450
451 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
452 // violates it...
453 // CHECK((ptr[5] >> 5) == 7); // reserved
454
455 size_t numSeqParameterSets = ptr[5] & 31;
456
457 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700458 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700459
460 for (size_t i = 0; i < numSeqParameterSets; ++i) {
461 CHECK(size >= 2);
462 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700463
464 ptr += 2;
465 size -= 2;
466
Andreas Huberbe06d262009-08-14 14:37:10 -0700467 CHECK(size >= length);
468
469 codec->addCodecSpecificData(ptr, length);
470
471 ptr += length;
472 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700473 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700474
Andreas Huberebf66ea2009-08-19 13:32:58 -0700475 CHECK(size >= 1);
476 size_t numPictureParameterSets = *ptr;
477 ++ptr;
478 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700479
Andreas Huberebf66ea2009-08-19 13:32:58 -0700480 for (size_t i = 0; i < numPictureParameterSets; ++i) {
481 CHECK(size >= 2);
482 size_t length = U16_AT(ptr);
483
484 ptr += 2;
485 size -= 2;
486
487 CHECK(size >= length);
488
489 codec->addCodecSpecificData(ptr, length);
490
491 ptr += length;
492 size -= length;
493 }
494
Andreas Huber53a76bd2009-10-06 16:20:44 -0700495 LOGV("AVC profile = %d (%s), level = %d",
Andreas Huberebf66ea2009-08-19 13:32:58 -0700496 (int)profile, AVCProfileToString(profile), (int)level / 10);
497
498 if (!strcmp(componentName, "OMX.TI.Video.Decoder")
499 && (profile != kAVCProfileBaseline || level > 39)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700500 // This stream exceeds the decoder's capabilities. The decoder
501 // does not handle this gracefully and would clobber the heap
502 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700503
504 LOGE("Profile and/or level exceed the decoder's capabilities.");
505 return NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -0700506 }
507 }
508
Andreas Hubere6c40962009-09-10 14:13:30 -0700509 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime)) {
Andreas Huber8768f2c2009-12-01 15:26:54 -0800510 codec->setAMRFormat(false /* isWAMR */);
Andreas Huberbe06d262009-08-14 14:37:10 -0700511 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700512 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) {
Andreas Huber8768f2c2009-12-01 15:26:54 -0800513 codec->setAMRFormat(true /* isWAMR */);
Andreas Huberee606e62009-09-08 10:19:21 -0700514 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700515 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700516 int32_t numChannels, sampleRate;
517 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
518 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
519
520 codec->setAACFormat(numChannels, sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700521 }
522 if (!strncasecmp(mime, "video/", 6)) {
523 int32_t width, height;
524 bool success = meta->findInt32(kKeyWidth, &width);
525 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700526 CHECK(success);
Andreas Huberbe06d262009-08-14 14:37:10 -0700527
528 if (createEncoder) {
529 codec->setVideoInputFormat(mime, width, height);
530 } else {
531 codec->setVideoOutputFormat(mime, width, height);
532 }
533 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700534 if (!strcasecmp(mime, MEDIA_MIMETYPE_IMAGE_JPEG)
Andreas Huberbe06d262009-08-14 14:37:10 -0700535 && !strcmp(componentName, "OMX.TI.JPEG.decode")) {
536 OMX_COLOR_FORMATTYPE format =
537 OMX_COLOR_Format32bitARGB8888;
538 // OMX_COLOR_FormatYUV420PackedPlanar;
539 // OMX_COLOR_FormatCbYCrY;
540 // OMX_COLOR_FormatYUV411Planar;
541
542 int32_t width, height;
543 bool success = meta->findInt32(kKeyWidth, &width);
544 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700545
546 int32_t compressedSize;
547 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700548 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700549
550 CHECK(success);
551 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700552
553 codec->setImageOutputFormat(format, width, height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700554 codec->setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700555 }
556
Andreas Huberda050cf22009-09-02 14:01:43 -0700557 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800558 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700559 codec->setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
560 }
561
562 if (!strcmp(componentName, "OMX.TI.AMR.encode")
563 || !strcmp(componentName, "OMX.TI.WBAMR.encode")) {
564 codec->setMinBufferSize(kPortIndexOutput, 8192); // XXX
565 }
566
Andreas Huberbe06d262009-08-14 14:37:10 -0700567 codec->initOutputFormat(meta);
568
569 return codec;
570}
571
Andreas Huberda050cf22009-09-02 14:01:43 -0700572void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
573 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700574 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700575 def.nPortIndex = portIndex;
576
Andreas Huber784202e2009-10-15 13:46:54 -0700577 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700578 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
579 CHECK_EQ(err, OK);
580
581 if (def.nBufferSize < size) {
582 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700583 }
584
Andreas Huber784202e2009-10-15 13:46:54 -0700585 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700586 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
587 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800588
589 err = mOMX->getParameter(
590 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
591 CHECK_EQ(err, OK);
592
593 // Make sure the setting actually stuck.
594 CHECK(def.nBufferSize >= size);
Andreas Huberda050cf22009-09-02 14:01:43 -0700595}
596
Andreas Huberbe06d262009-08-14 14:37:10 -0700597status_t OMXCodec::setVideoPortFormatType(
598 OMX_U32 portIndex,
599 OMX_VIDEO_CODINGTYPE compressionFormat,
600 OMX_COLOR_FORMATTYPE colorFormat) {
601 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700602 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700603 format.nPortIndex = portIndex;
604 format.nIndex = 0;
605 bool found = false;
606
607 OMX_U32 index = 0;
608 for (;;) {
609 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700610 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700611 mNode, OMX_IndexParamVideoPortFormat,
612 &format, sizeof(format));
613
614 if (err != OK) {
615 return err;
616 }
617
618 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700619 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700620
621#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700622 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700623 portIndex,
624 index, format.eCompressionFormat, format.eColorFormat);
625#endif
626
627 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
628 if (portIndex == kPortIndexInput
629 && colorFormat == format.eColorFormat) {
630 // eCompressionFormat does not seem right.
631 found = true;
632 break;
633 }
634 if (portIndex == kPortIndexOutput
635 && compressionFormat == format.eCompressionFormat) {
636 // eColorFormat does not seem right.
637 found = true;
638 break;
639 }
640 }
641
642 if (format.eCompressionFormat == compressionFormat
643 && format.eColorFormat == colorFormat) {
644 found = true;
645 break;
646 }
647
648 ++index;
649 }
650
651 if (!found) {
652 return UNKNOWN_ERROR;
653 }
654
Andreas Huber53a76bd2009-10-06 16:20:44 -0700655 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700656 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700657 mNode, OMX_IndexParamVideoPortFormat,
658 &format, sizeof(format));
659
660 return err;
661}
662
Andreas Huberb482ce82009-10-29 12:02:48 -0700663static size_t getFrameSize(
664 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
665 switch (colorFormat) {
666 case OMX_COLOR_FormatYCbYCr:
667 case OMX_COLOR_FormatCbYCrY:
668 return width * height * 2;
669
670 case OMX_COLOR_FormatYUV420SemiPlanar:
671 return (width * height * 3) / 2;
672
673 default:
674 CHECK(!"Should not be here. Unsupported color format.");
675 break;
676 }
677}
678
Andreas Huberbe06d262009-08-14 14:37:10 -0700679void OMXCodec::setVideoInputFormat(
680 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700681 CODEC_LOGV("setVideoInputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700682
683 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700684 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700685 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700686 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700687 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700688 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700689 compressionFormat = OMX_VIDEO_CodingH263;
690 } else {
691 LOGE("Not a supported video mime type: %s", mime);
692 CHECK(!"Should not be here. Not a supported video mime type.");
693 }
694
Andreas Huberea6a38c2009-11-16 15:43:38 -0800695 OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
696 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
697 colorFormat = OMX_COLOR_FormatYCbYCr;
Andreas Huberbe06d262009-08-14 14:37:10 -0700698 }
699
Andreas Huberb482ce82009-10-29 12:02:48 -0700700 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700701 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700702 colorFormat), OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700703
Andreas Huberb482ce82009-10-29 12:02:48 -0700704 CHECK_EQ(setVideoPortFormatType(
705 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
706 OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700707
708 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700709 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700710 def.nPortIndex = kPortIndexOutput;
711
Andreas Huber4c483422009-09-02 16:05:36 -0700712 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
713
Andreas Huber784202e2009-10-15 13:46:54 -0700714 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700715 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
716
717 CHECK_EQ(err, OK);
718 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
719
720 video_def->nFrameWidth = width;
721 video_def->nFrameHeight = height;
722
723 video_def->eCompressionFormat = compressionFormat;
724 video_def->eColorFormat = OMX_COLOR_FormatUnused;
725
Andreas Huber784202e2009-10-15 13:46:54 -0700726 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700727 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
728 CHECK_EQ(err, OK);
729
730 ////////////////////////////////////////////////////////////////////////////
731
Andreas Huber4c483422009-09-02 16:05:36 -0700732 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700733 def.nPortIndex = kPortIndexInput;
734
Andreas Huber784202e2009-10-15 13:46:54 -0700735 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700736 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
737 CHECK_EQ(err, OK);
738
Andreas Huberb482ce82009-10-29 12:02:48 -0700739 def.nBufferSize = getFrameSize(colorFormat, width, height);
Andreas Huber53a76bd2009-10-06 16:20:44 -0700740 CODEC_LOGV("Setting nBufferSize = %ld", def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700741
742 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
743
744 video_def->nFrameWidth = width;
745 video_def->nFrameHeight = height;
746 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
747 video_def->eColorFormat = colorFormat;
748
Andreas Huberb482ce82009-10-29 12:02:48 -0700749 video_def->xFramerate = 24 << 16; // XXX crucial!
750
Andreas Huber784202e2009-10-15 13:46:54 -0700751 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700752 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
753 CHECK_EQ(err, OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700754
755 switch (compressionFormat) {
756 case OMX_VIDEO_CodingMPEG4:
757 {
758 CHECK_EQ(setupMPEG4EncoderParameters(), OK);
759 break;
760 }
761
762 case OMX_VIDEO_CodingH263:
763 break;
764
Andreas Huberea6a38c2009-11-16 15:43:38 -0800765 case OMX_VIDEO_CodingAVC:
766 {
767 CHECK_EQ(setupAVCEncoderParameters(), OK);
768 break;
769 }
770
Andreas Huberb482ce82009-10-29 12:02:48 -0700771 default:
772 CHECK(!"Support for this compressionFormat to be implemented.");
773 break;
774 }
775}
776
777status_t OMXCodec::setupMPEG4EncoderParameters() {
778 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
779 InitOMXParams(&mpeg4type);
780 mpeg4type.nPortIndex = kPortIndexOutput;
781
782 status_t err = mOMX->getParameter(
783 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
784 CHECK_EQ(err, OK);
785
786 mpeg4type.nSliceHeaderSpacing = 0;
787 mpeg4type.bSVH = OMX_FALSE;
788 mpeg4type.bGov = OMX_FALSE;
789
790 mpeg4type.nAllowedPictureTypes =
791 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
792
793 mpeg4type.nPFrames = 23;
794 mpeg4type.nBFrames = 0;
795
796 mpeg4type.nIDCVLCThreshold = 0;
797 mpeg4type.bACPred = OMX_TRUE;
798 mpeg4type.nMaxPacketSize = 256;
799 mpeg4type.nTimeIncRes = 1000;
800 mpeg4type.nHeaderExtension = 0;
801 mpeg4type.bReversibleVLC = OMX_FALSE;
802
803 mpeg4type.eProfile = OMX_VIDEO_MPEG4ProfileCore;
804 mpeg4type.eLevel = OMX_VIDEO_MPEG4Level2;
805
806 err = mOMX->setParameter(
807 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
808 CHECK_EQ(err, OK);
809
810 // ----------------
811
812 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
813 InitOMXParams(&bitrateType);
814 bitrateType.nPortIndex = kPortIndexOutput;
815
816 err = mOMX->getParameter(
817 mNode, OMX_IndexParamVideoBitrate,
818 &bitrateType, sizeof(bitrateType));
819 CHECK_EQ(err, OK);
820
821 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
822 bitrateType.nTargetBitrate = 1000000;
823
824 err = mOMX->setParameter(
825 mNode, OMX_IndexParamVideoBitrate,
826 &bitrateType, sizeof(bitrateType));
827 CHECK_EQ(err, OK);
828
829 // ----------------
830
831 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
832 InitOMXParams(&errorCorrectionType);
833 errorCorrectionType.nPortIndex = kPortIndexOutput;
834
835 err = mOMX->getParameter(
836 mNode, OMX_IndexParamVideoErrorCorrection,
837 &errorCorrectionType, sizeof(errorCorrectionType));
838 CHECK_EQ(err, OK);
839
840 errorCorrectionType.bEnableHEC = OMX_FALSE;
841 errorCorrectionType.bEnableResync = OMX_TRUE;
842 errorCorrectionType.nResynchMarkerSpacing = 256;
843 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
844 errorCorrectionType.bEnableRVLC = OMX_FALSE;
845
846 err = mOMX->setParameter(
847 mNode, OMX_IndexParamVideoErrorCorrection,
848 &errorCorrectionType, sizeof(errorCorrectionType));
849 CHECK_EQ(err, OK);
850
851 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700852}
853
Andreas Huberea6a38c2009-11-16 15:43:38 -0800854status_t OMXCodec::setupAVCEncoderParameters() {
855 OMX_VIDEO_PARAM_AVCTYPE h264type;
856 InitOMXParams(&h264type);
857 h264type.nPortIndex = kPortIndexOutput;
858
859 status_t err = mOMX->getParameter(
860 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
861 CHECK_EQ(err, OK);
862
863 h264type.nAllowedPictureTypes =
864 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
865
866 h264type.nSliceHeaderSpacing = 0;
867 h264type.nBFrames = 0;
868 h264type.bUseHadamard = OMX_TRUE;
869 h264type.nRefFrames = 1;
870 h264type.nRefIdx10ActiveMinus1 = 0;
871 h264type.nRefIdx11ActiveMinus1 = 0;
872 h264type.bEnableUEP = OMX_FALSE;
873 h264type.bEnableFMO = OMX_FALSE;
874 h264type.bEnableASO = OMX_FALSE;
875 h264type.bEnableRS = OMX_FALSE;
876 h264type.eProfile = OMX_VIDEO_AVCProfileBaseline;
877 h264type.eLevel = OMX_VIDEO_AVCLevel1b;
878 h264type.bFrameMBsOnly = OMX_TRUE;
879 h264type.bMBAFF = OMX_FALSE;
880 h264type.bEntropyCodingCABAC = OMX_FALSE;
881 h264type.bWeightedPPrediction = OMX_FALSE;
882 h264type.bconstIpred = OMX_FALSE;
883 h264type.bDirect8x8Inference = OMX_FALSE;
884 h264type.bDirectSpatialTemporal = OMX_FALSE;
885 h264type.nCabacInitIdc = 0;
886 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
887
888 err = mOMX->setParameter(
889 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
890 CHECK_EQ(err, OK);
891
892 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
893 InitOMXParams(&bitrateType);
894 bitrateType.nPortIndex = kPortIndexOutput;
895
896 err = mOMX->getParameter(
897 mNode, OMX_IndexParamVideoBitrate,
898 &bitrateType, sizeof(bitrateType));
899 CHECK_EQ(err, OK);
900
901 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
902 bitrateType.nTargetBitrate = 1000000;
903
904 err = mOMX->setParameter(
905 mNode, OMX_IndexParamVideoBitrate,
906 &bitrateType, sizeof(bitrateType));
907 CHECK_EQ(err, OK);
908
909 return OK;
910}
911
Andreas Huberbe06d262009-08-14 14:37:10 -0700912void OMXCodec::setVideoOutputFormat(
913 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700914 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700915
Andreas Huberbe06d262009-08-14 14:37:10 -0700916 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700917 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700918 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700919 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700920 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700921 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700922 compressionFormat = OMX_VIDEO_CodingH263;
923 } else {
924 LOGE("Not a supported video mime type: %s", mime);
925 CHECK(!"Should not be here. Not a supported video mime type.");
926 }
927
928 setVideoPortFormatType(
929 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
930
931#if 1
932 {
933 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700934 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700935 format.nPortIndex = kPortIndexOutput;
936 format.nIndex = 0;
937
Andreas Huber784202e2009-10-15 13:46:54 -0700938 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700939 mNode, OMX_IndexParamVideoPortFormat,
940 &format, sizeof(format));
941 CHECK_EQ(err, OK);
942 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
943
944 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
945
946 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
947 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
948 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
949 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
950
Andreas Huber784202e2009-10-15 13:46:54 -0700951 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700952 mNode, OMX_IndexParamVideoPortFormat,
953 &format, sizeof(format));
954 CHECK_EQ(err, OK);
955 }
956#endif
957
958 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700959 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700960 def.nPortIndex = kPortIndexInput;
961
Andreas Huber4c483422009-09-02 16:05:36 -0700962 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
963
Andreas Huber784202e2009-10-15 13:46:54 -0700964 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700965 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
966
967 CHECK_EQ(err, OK);
968
969#if 1
970 // XXX Need a (much) better heuristic to compute input buffer sizes.
971 const size_t X = 64 * 1024;
972 if (def.nBufferSize < X) {
973 def.nBufferSize = X;
974 }
975#endif
976
977 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
978
979 video_def->nFrameWidth = width;
980 video_def->nFrameHeight = height;
981
Andreas Huberb482ce82009-10-29 12:02:48 -0700982 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -0700983 video_def->eColorFormat = OMX_COLOR_FormatUnused;
984
Andreas Huber784202e2009-10-15 13:46:54 -0700985 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700986 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
987 CHECK_EQ(err, OK);
988
989 ////////////////////////////////////////////////////////////////////////////
990
Andreas Huber4c483422009-09-02 16:05:36 -0700991 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700992 def.nPortIndex = kPortIndexOutput;
993
Andreas Huber784202e2009-10-15 13:46:54 -0700994 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700995 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
996 CHECK_EQ(err, OK);
997 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
998
999#if 0
1000 def.nBufferSize =
1001 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1002#endif
1003
1004 video_def->nFrameWidth = width;
1005 video_def->nFrameHeight = height;
1006
Andreas Huber784202e2009-10-15 13:46:54 -07001007 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001008 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1009 CHECK_EQ(err, OK);
1010}
1011
Andreas Huberbe06d262009-08-14 14:37:10 -07001012OMXCodec::OMXCodec(
1013 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001014 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001015 const char *mime,
1016 const char *componentName,
1017 const sp<MediaSource> &source)
1018 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001019 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001020 mNode(node),
1021 mQuirks(quirks),
1022 mIsEncoder(isEncoder),
1023 mMIME(strdup(mime)),
1024 mComponentName(strdup(componentName)),
1025 mSource(source),
1026 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001027 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001028 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001029 mSignalledEOS(false),
1030 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001031 mOutputPortSettingsHaveChanged(false),
Andreas Huberbe06d262009-08-14 14:37:10 -07001032 mSeekTimeUs(-1) {
1033 mPortStatus[kPortIndexInput] = ENABLED;
1034 mPortStatus[kPortIndexOutput] = ENABLED;
1035
Andreas Huber4c483422009-09-02 16:05:36 -07001036 setComponentRole();
1037}
1038
Andreas Hubere6c40962009-09-10 14:13:30 -07001039// static
1040void OMXCodec::setComponentRole(
1041 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1042 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001043 struct MimeToRole {
1044 const char *mime;
1045 const char *decoderRole;
1046 const char *encoderRole;
1047 };
1048
1049 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001050 { MEDIA_MIMETYPE_AUDIO_MPEG,
1051 "audio_decoder.mp3", "audio_encoder.mp3" },
1052 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1053 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1054 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1055 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1056 { MEDIA_MIMETYPE_AUDIO_AAC,
1057 "audio_decoder.aac", "audio_encoder.aac" },
1058 { MEDIA_MIMETYPE_VIDEO_AVC,
1059 "video_decoder.avc", "video_encoder.avc" },
1060 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1061 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1062 { MEDIA_MIMETYPE_VIDEO_H263,
1063 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001064 };
1065
1066 static const size_t kNumMimeToRole =
1067 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1068
1069 size_t i;
1070 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001071 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001072 break;
1073 }
1074 }
1075
1076 if (i == kNumMimeToRole) {
1077 return;
1078 }
1079
1080 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001081 isEncoder ? kMimeToRole[i].encoderRole
1082 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001083
1084 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001085 OMX_PARAM_COMPONENTROLETYPE roleParams;
1086 InitOMXParams(&roleParams);
1087
1088 strncpy((char *)roleParams.cRole,
1089 role, OMX_MAX_STRINGNAME_SIZE - 1);
1090
1091 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1092
Andreas Huber784202e2009-10-15 13:46:54 -07001093 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001094 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001095 &roleParams, sizeof(roleParams));
1096
1097 if (err != OK) {
1098 LOGW("Failed to set standard component role '%s'.", role);
1099 }
1100 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001101}
1102
Andreas Hubere6c40962009-09-10 14:13:30 -07001103void OMXCodec::setComponentRole() {
1104 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1105}
1106
Andreas Huberbe06d262009-08-14 14:37:10 -07001107OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001108 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001109
Andreas Huber784202e2009-10-15 13:46:54 -07001110 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001111 CHECK_EQ(err, OK);
1112
1113 mNode = NULL;
1114 setState(DEAD);
1115
1116 clearCodecSpecificData();
1117
1118 free(mComponentName);
1119 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001120
Andreas Huberbe06d262009-08-14 14:37:10 -07001121 free(mMIME);
1122 mMIME = NULL;
1123}
1124
1125status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001126 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001127
1128 CHECK_EQ(mState, LOADED);
1129
1130 status_t err;
1131 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001132 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001133 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001134 setState(LOADED_TO_IDLE);
1135 }
1136
1137 err = allocateBuffers();
1138 CHECK_EQ(err, OK);
1139
1140 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001141 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001142 CHECK_EQ(err, OK);
1143
1144 setState(LOADED_TO_IDLE);
1145 }
1146
1147 while (mState != EXECUTING && mState != ERROR) {
1148 mAsyncCompletion.wait(mLock);
1149 }
1150
1151 return mState == ERROR ? UNKNOWN_ERROR : OK;
1152}
1153
1154// static
1155bool OMXCodec::isIntermediateState(State state) {
1156 return state == LOADED_TO_IDLE
1157 || state == IDLE_TO_EXECUTING
1158 || state == EXECUTING_TO_IDLE
1159 || state == IDLE_TO_LOADED
1160 || state == RECONFIGURING;
1161}
1162
1163status_t OMXCodec::allocateBuffers() {
1164 status_t err = allocateBuffersOnPort(kPortIndexInput);
1165
1166 if (err != OK) {
1167 return err;
1168 }
1169
1170 return allocateBuffersOnPort(kPortIndexOutput);
1171}
1172
1173status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1174 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001175 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001176 def.nPortIndex = portIndex;
1177
Andreas Huber784202e2009-10-15 13:46:54 -07001178 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001179 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1180
1181 if (err != OK) {
1182 return err;
1183 }
1184
Andreas Huber5c0a9132009-08-20 11:16:40 -07001185 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
1186 mDealer[portIndex] = new MemoryDealer(totalSize);
1187
Andreas Huberbe06d262009-08-14 14:37:10 -07001188 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001189 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001190 CHECK(mem.get() != NULL);
1191
Andreas Huberc712b9f2010-01-20 15:05:46 -08001192 BufferInfo info;
1193 info.mData = NULL;
1194 info.mSize = def.nBufferSize;
1195
Andreas Huberbe06d262009-08-14 14:37:10 -07001196 IOMX::buffer_id buffer;
1197 if (portIndex == kPortIndexInput
1198 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001199 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001200 mem.clear();
1201
Andreas Huberf1fe0642010-01-15 15:28:19 -08001202 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001203 mNode, portIndex, def.nBufferSize, &buffer,
1204 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001205 } else {
1206 err = mOMX->allocateBufferWithBackup(
1207 mNode, portIndex, mem, &buffer);
1208 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001209 } else if (portIndex == kPortIndexOutput
1210 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001211 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001212 mem.clear();
1213
Andreas Huberf1fe0642010-01-15 15:28:19 -08001214 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001215 mNode, portIndex, def.nBufferSize, &buffer,
1216 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001217 } else {
1218 err = mOMX->allocateBufferWithBackup(
1219 mNode, portIndex, mem, &buffer);
1220 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001221 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001222 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001223 }
1224
1225 if (err != OK) {
1226 LOGE("allocate_buffer_with_backup failed");
1227 return err;
1228 }
1229
Andreas Huberc712b9f2010-01-20 15:05:46 -08001230 if (mem != NULL) {
1231 info.mData = mem->pointer();
1232 }
1233
Andreas Huberbe06d262009-08-14 14:37:10 -07001234 info.mBuffer = buffer;
1235 info.mOwnedByComponent = false;
1236 info.mMem = mem;
1237 info.mMediaBuffer = NULL;
1238
1239 if (portIndex == kPortIndexOutput) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001240 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001241 info.mMediaBuffer->setObserver(this);
1242 }
1243
1244 mPortBuffers[portIndex].push(info);
1245
Andreas Huber4c483422009-09-02 16:05:36 -07001246 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001247 portIndex == kPortIndexInput ? "input" : "output");
1248 }
1249
Andreas Huber2ea14e22009-12-16 09:30:55 -08001250 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001251
1252 return OK;
1253}
1254
1255void OMXCodec::on_message(const omx_message &msg) {
1256 Mutex::Autolock autoLock(mLock);
1257
1258 switch (msg.type) {
1259 case omx_message::EVENT:
1260 {
1261 onEvent(
1262 msg.u.event_data.event, msg.u.event_data.data1,
1263 msg.u.event_data.data2);
1264
1265 break;
1266 }
1267
1268 case omx_message::EMPTY_BUFFER_DONE:
1269 {
1270 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1271
Andreas Huber4c483422009-09-02 16:05:36 -07001272 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001273
1274 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1275 size_t i = 0;
1276 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1277 ++i;
1278 }
1279
1280 CHECK(i < buffers->size());
1281 if (!(*buffers)[i].mOwnedByComponent) {
1282 LOGW("We already own input buffer %p, yet received "
1283 "an EMPTY_BUFFER_DONE.", buffer);
1284 }
1285
1286 buffers->editItemAt(i).mOwnedByComponent = false;
1287
1288 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001289 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001290
1291 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001292 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001293 CHECK_EQ(err, OK);
1294
1295 buffers->removeAt(i);
1296 } else if (mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
1297 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1298 drainInputBuffer(&buffers->editItemAt(i));
1299 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001300 break;
1301 }
1302
1303 case omx_message::FILL_BUFFER_DONE:
1304 {
1305 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1306 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1307
Andreas Huber2ea14e22009-12-16 09:30:55 -08001308 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001309 buffer,
1310 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001311 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001312 msg.u.extended_buffer_data.timestamp,
1313 msg.u.extended_buffer_data.timestamp / 1E6);
1314
1315 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1316 size_t i = 0;
1317 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1318 ++i;
1319 }
1320
1321 CHECK(i < buffers->size());
1322 BufferInfo *info = &buffers->editItemAt(i);
1323
1324 if (!info->mOwnedByComponent) {
1325 LOGW("We already own output buffer %p, yet received "
1326 "a FILL_BUFFER_DONE.", buffer);
1327 }
1328
1329 info->mOwnedByComponent = false;
1330
1331 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001332 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001333
1334 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001335 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001336 CHECK_EQ(err, OK);
1337
1338 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001339#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001340 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1341 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001342 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001343 mNoMoreOutputData = true;
1344 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001345#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001346 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1347 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001348
Andreas Huberbe06d262009-08-14 14:37:10 -07001349 MediaBuffer *buffer = info->mMediaBuffer;
1350
1351 buffer->set_range(
1352 msg.u.extended_buffer_data.range_offset,
1353 msg.u.extended_buffer_data.range_length);
1354
1355 buffer->meta_data()->clear();
1356
Andreas Huberfa8de752009-10-08 10:07:49 -07001357 buffer->meta_data()->setInt64(
1358 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001359
1360 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1361 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1362 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001363 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1364 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1365 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001366
1367 buffer->meta_data()->setPointer(
1368 kKeyPlatformPrivate,
1369 msg.u.extended_buffer_data.platform_private);
1370
1371 buffer->meta_data()->setPointer(
1372 kKeyBufferID,
1373 msg.u.extended_buffer_data.buffer);
1374
1375 mFilledBuffers.push_back(i);
1376 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001377
1378 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1379 CODEC_LOGV("No more output data.");
1380 mNoMoreOutputData = true;
1381 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001382 }
1383
1384 break;
1385 }
1386
1387 default:
1388 {
1389 CHECK(!"should not be here.");
1390 break;
1391 }
1392 }
1393}
1394
1395void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1396 switch (event) {
1397 case OMX_EventCmdComplete:
1398 {
1399 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1400 break;
1401 }
1402
1403 case OMX_EventError:
1404 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001405 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001406
1407 setState(ERROR);
1408 break;
1409 }
1410
1411 case OMX_EventPortSettingsChanged:
1412 {
1413 onPortSettingsChanged(data1);
1414 break;
1415 }
1416
Andreas Huber2ea14e22009-12-16 09:30:55 -08001417#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001418 case OMX_EventBufferFlag:
1419 {
Andreas Huber4c483422009-09-02 16:05:36 -07001420 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001421
1422 if (data1 == kPortIndexOutput) {
1423 mNoMoreOutputData = true;
1424 }
1425 break;
1426 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001427#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001428
1429 default:
1430 {
Andreas Huber4c483422009-09-02 16:05:36 -07001431 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001432 break;
1433 }
1434 }
1435}
1436
Andreas Huberb1678602009-10-19 13:06:40 -07001437// Has the format changed in any way that the client would have to be aware of?
1438static bool formatHasNotablyChanged(
1439 const sp<MetaData> &from, const sp<MetaData> &to) {
1440 if (from.get() == NULL && to.get() == NULL) {
1441 return false;
1442 }
1443
Andreas Huberf68c1682009-10-21 14:01:30 -07001444 if ((from.get() == NULL && to.get() != NULL)
1445 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001446 return true;
1447 }
1448
1449 const char *mime_from, *mime_to;
1450 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1451 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1452
1453 if (strcasecmp(mime_from, mime_to)) {
1454 return true;
1455 }
1456
1457 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1458 int32_t colorFormat_from, colorFormat_to;
1459 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1460 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1461
1462 if (colorFormat_from != colorFormat_to) {
1463 return true;
1464 }
1465
1466 int32_t width_from, width_to;
1467 CHECK(from->findInt32(kKeyWidth, &width_from));
1468 CHECK(to->findInt32(kKeyWidth, &width_to));
1469
1470 if (width_from != width_to) {
1471 return true;
1472 }
1473
1474 int32_t height_from, height_to;
1475 CHECK(from->findInt32(kKeyHeight, &height_from));
1476 CHECK(to->findInt32(kKeyHeight, &height_to));
1477
1478 if (height_from != height_to) {
1479 return true;
1480 }
1481 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1482 int32_t numChannels_from, numChannels_to;
1483 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1484 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1485
1486 if (numChannels_from != numChannels_to) {
1487 return true;
1488 }
1489
1490 int32_t sampleRate_from, sampleRate_to;
1491 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1492 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1493
1494 if (sampleRate_from != sampleRate_to) {
1495 return true;
1496 }
1497 }
1498
1499 return false;
1500}
1501
Andreas Huberbe06d262009-08-14 14:37:10 -07001502void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1503 switch (cmd) {
1504 case OMX_CommandStateSet:
1505 {
1506 onStateChange((OMX_STATETYPE)data);
1507 break;
1508 }
1509
1510 case OMX_CommandPortDisable:
1511 {
1512 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001513 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001514
1515 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1516 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1517 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1518
1519 mPortStatus[portIndex] = DISABLED;
1520
1521 if (mState == RECONFIGURING) {
1522 CHECK_EQ(portIndex, kPortIndexOutput);
1523
Andreas Huberb1678602009-10-19 13:06:40 -07001524 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001525 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001526
1527 // Don't notify clients if the output port settings change
1528 // wasn't of importance to them, i.e. it may be that just the
1529 // number of buffers has changed and nothing else.
1530 mOutputPortSettingsHaveChanged =
1531 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001532
Andreas Huberbe06d262009-08-14 14:37:10 -07001533 enablePortAsync(portIndex);
1534
1535 status_t err = allocateBuffersOnPort(portIndex);
1536 CHECK_EQ(err, OK);
1537 }
1538 break;
1539 }
1540
1541 case OMX_CommandPortEnable:
1542 {
1543 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001544 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001545
1546 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1547 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1548
1549 mPortStatus[portIndex] = ENABLED;
1550
1551 if (mState == RECONFIGURING) {
1552 CHECK_EQ(portIndex, kPortIndexOutput);
1553
1554 setState(EXECUTING);
1555
1556 fillOutputBuffers();
1557 }
1558 break;
1559 }
1560
1561 case OMX_CommandFlush:
1562 {
1563 OMX_U32 portIndex = data;
1564
Andreas Huber4c483422009-09-02 16:05:36 -07001565 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001566
1567 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1568 mPortStatus[portIndex] = ENABLED;
1569
1570 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1571 mPortBuffers[portIndex].size());
1572
1573 if (mState == RECONFIGURING) {
1574 CHECK_EQ(portIndex, kPortIndexOutput);
1575
1576 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001577 } else if (mState == EXECUTING_TO_IDLE) {
1578 if (mPortStatus[kPortIndexInput] == ENABLED
1579 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001580 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001581 "transition from EXECUTING to IDLE.");
1582
1583 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1584 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1585
1586 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001587 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001588 CHECK_EQ(err, OK);
1589 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001590 } else {
1591 // We're flushing both ports in preparation for seeking.
1592
1593 if (mPortStatus[kPortIndexInput] == ENABLED
1594 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001595 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001596 " seek-time.");
1597
1598 drainInputBuffers();
1599 fillOutputBuffers();
1600 }
1601 }
1602
1603 break;
1604 }
1605
1606 default:
1607 {
Andreas Huber4c483422009-09-02 16:05:36 -07001608 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001609 break;
1610 }
1611 }
1612}
1613
1614void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001615 CODEC_LOGV("onStateChange %d", newState);
1616
Andreas Huberbe06d262009-08-14 14:37:10 -07001617 switch (newState) {
1618 case OMX_StateIdle:
1619 {
Andreas Huber4c483422009-09-02 16:05:36 -07001620 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001621 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001622 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001623 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1624
1625 CHECK_EQ(err, OK);
1626
1627 setState(IDLE_TO_EXECUTING);
1628 } else {
1629 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1630
1631 CHECK_EQ(
1632 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1633 mPortBuffers[kPortIndexInput].size());
1634
1635 CHECK_EQ(
1636 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1637 mPortBuffers[kPortIndexOutput].size());
1638
Andreas Huber784202e2009-10-15 13:46:54 -07001639 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001640 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1641
1642 CHECK_EQ(err, OK);
1643
1644 err = freeBuffersOnPort(kPortIndexInput);
1645 CHECK_EQ(err, OK);
1646
1647 err = freeBuffersOnPort(kPortIndexOutput);
1648 CHECK_EQ(err, OK);
1649
1650 mPortStatus[kPortIndexInput] = ENABLED;
1651 mPortStatus[kPortIndexOutput] = ENABLED;
1652
1653 setState(IDLE_TO_LOADED);
1654 }
1655 break;
1656 }
1657
1658 case OMX_StateExecuting:
1659 {
1660 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1661
Andreas Huber4c483422009-09-02 16:05:36 -07001662 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001663
1664 setState(EXECUTING);
1665
Andreas Huber42978e52009-08-27 10:08:39 -07001666 // Buffers will be submitted to the component in the first
1667 // call to OMXCodec::read as mInitialBufferSubmit is true at
1668 // this point. This ensures that this on_message call returns,
1669 // releases the lock and ::init can notice the state change and
1670 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001671 break;
1672 }
1673
1674 case OMX_StateLoaded:
1675 {
1676 CHECK_EQ(mState, IDLE_TO_LOADED);
1677
Andreas Huber4c483422009-09-02 16:05:36 -07001678 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001679
1680 setState(LOADED);
1681 break;
1682 }
1683
Andreas Huberc712b9f2010-01-20 15:05:46 -08001684 case OMX_StateInvalid:
1685 {
1686 setState(ERROR);
1687 break;
1688 }
1689
Andreas Huberbe06d262009-08-14 14:37:10 -07001690 default:
1691 {
1692 CHECK(!"should not be here.");
1693 break;
1694 }
1695 }
1696}
1697
1698// static
1699size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1700 size_t n = 0;
1701 for (size_t i = 0; i < buffers.size(); ++i) {
1702 if (!buffers[i].mOwnedByComponent) {
1703 ++n;
1704 }
1705 }
1706
1707 return n;
1708}
1709
1710status_t OMXCodec::freeBuffersOnPort(
1711 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1712 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1713
1714 status_t stickyErr = OK;
1715
1716 for (size_t i = buffers->size(); i-- > 0;) {
1717 BufferInfo *info = &buffers->editItemAt(i);
1718
1719 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1720 continue;
1721 }
1722
1723 CHECK_EQ(info->mOwnedByComponent, false);
1724
Andreas Huber92022852009-09-14 15:24:14 -07001725 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1726
Andreas Huberbe06d262009-08-14 14:37:10 -07001727 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001728 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001729
1730 if (err != OK) {
1731 stickyErr = err;
1732 }
1733
1734 if (info->mMediaBuffer != NULL) {
1735 info->mMediaBuffer->setObserver(NULL);
1736
1737 // Make sure nobody but us owns this buffer at this point.
1738 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1739
1740 info->mMediaBuffer->release();
1741 }
1742
1743 buffers->removeAt(i);
1744 }
1745
1746 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1747
1748 return stickyErr;
1749}
1750
1751void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001752 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001753
1754 CHECK_EQ(mState, EXECUTING);
1755 CHECK_EQ(portIndex, kPortIndexOutput);
1756 setState(RECONFIGURING);
1757
1758 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001759 if (!flushPortAsync(portIndex)) {
1760 onCmdComplete(OMX_CommandFlush, portIndex);
1761 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001762 } else {
1763 disablePortAsync(portIndex);
1764 }
1765}
1766
Andreas Huber404cc412009-08-25 14:26:05 -07001767bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001768 CHECK(mState == EXECUTING || mState == RECONFIGURING
1769 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001770
Andreas Huber4c483422009-09-02 16:05:36 -07001771 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001772 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1773 mPortBuffers[portIndex].size());
1774
Andreas Huberbe06d262009-08-14 14:37:10 -07001775 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1776 mPortStatus[portIndex] = SHUTTING_DOWN;
1777
Andreas Huber404cc412009-08-25 14:26:05 -07001778 if ((mQuirks & kRequiresFlushCompleteEmulation)
1779 && countBuffersWeOwn(mPortBuffers[portIndex])
1780 == mPortBuffers[portIndex].size()) {
1781 // No flush is necessary and this component fails to send a
1782 // flush-complete event in this case.
1783
1784 return false;
1785 }
1786
Andreas Huberbe06d262009-08-14 14:37:10 -07001787 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001788 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001789 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001790
1791 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001792}
1793
1794void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1795 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1796
1797 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1798 mPortStatus[portIndex] = DISABLING;
1799
1800 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001801 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001802 CHECK_EQ(err, OK);
1803
1804 freeBuffersOnPort(portIndex, true);
1805}
1806
1807void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1808 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1809
1810 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1811 mPortStatus[portIndex] = ENABLING;
1812
1813 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001814 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001815 CHECK_EQ(err, OK);
1816}
1817
1818void OMXCodec::fillOutputBuffers() {
1819 CHECK_EQ(mState, EXECUTING);
1820
Andreas Huberdbcb2c62010-01-14 11:32:13 -08001821 // This is a workaround for some decoders not properly reporting
1822 // end-of-output-stream. If we own all input buffers and also own
1823 // all output buffers and we already signalled end-of-input-stream,
1824 // the end-of-output-stream is implied.
1825 if (mSignalledEOS
1826 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
1827 == mPortBuffers[kPortIndexInput].size()
1828 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
1829 == mPortBuffers[kPortIndexOutput].size()) {
1830 mNoMoreOutputData = true;
1831 mBufferFilled.signal();
1832
1833 return;
1834 }
1835
Andreas Huberbe06d262009-08-14 14:37:10 -07001836 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1837 for (size_t i = 0; i < buffers->size(); ++i) {
1838 fillOutputBuffer(&buffers->editItemAt(i));
1839 }
1840}
1841
1842void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001843 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001844
1845 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1846 for (size_t i = 0; i < buffers->size(); ++i) {
1847 drainInputBuffer(&buffers->editItemAt(i));
1848 }
1849}
1850
1851void OMXCodec::drainInputBuffer(BufferInfo *info) {
1852 CHECK_EQ(info->mOwnedByComponent, false);
1853
1854 if (mSignalledEOS) {
1855 return;
1856 }
1857
1858 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1859 const CodecSpecificData *specific =
1860 mCodecSpecificData[mCodecSpecificDataIndex];
1861
1862 size_t size = specific->mSize;
1863
Andreas Hubere6c40962009-09-10 14:13:30 -07001864 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07001865 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001866 static const uint8_t kNALStartCode[4] =
1867 { 0x00, 0x00, 0x00, 0x01 };
1868
Andreas Huberc712b9f2010-01-20 15:05:46 -08001869 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07001870
1871 size += 4;
1872
Andreas Huberc712b9f2010-01-20 15:05:46 -08001873 memcpy(info->mData, kNALStartCode, 4);
1874 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07001875 specific->mData, specific->mSize);
1876 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001877 CHECK(info->mSize >= specific->mSize);
1878 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001879 }
1880
Andreas Huber2ea14e22009-12-16 09:30:55 -08001881 mNoMoreOutputData = false;
1882
Andreas Huberdbcb2c62010-01-14 11:32:13 -08001883 CODEC_LOGV("calling emptyBuffer with codec specific data");
1884
Andreas Huber784202e2009-10-15 13:46:54 -07001885 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07001886 mNode, info->mBuffer, 0, size,
1887 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
1888 0);
Andreas Huber3f427072009-10-08 11:02:27 -07001889 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001890
1891 info->mOwnedByComponent = true;
1892
1893 ++mCodecSpecificDataIndex;
1894 return;
1895 }
1896
1897 MediaBuffer *srcBuffer;
1898 status_t err;
1899 if (mSeekTimeUs >= 0) {
1900 MediaSource::ReadOptions options;
1901 options.setSeekTo(mSeekTimeUs);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001902
Andreas Huberbe06d262009-08-14 14:37:10 -07001903 mSeekTimeUs = -1;
Andreas Huber2ea14e22009-12-16 09:30:55 -08001904 mBufferFilled.signal();
Andreas Huberbe06d262009-08-14 14:37:10 -07001905
1906 err = mSource->read(&srcBuffer, &options);
1907 } else {
1908 err = mSource->read(&srcBuffer);
1909 }
1910
1911 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberfa8de752009-10-08 10:07:49 -07001912 OMX_TICKS timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07001913 size_t srcLength = 0;
1914
1915 if (err != OK) {
Andreas Huber4c483422009-09-02 16:05:36 -07001916 CODEC_LOGV("signalling end of input stream.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001917 flags |= OMX_BUFFERFLAG_EOS;
1918
1919 mSignalledEOS = true;
1920 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001921 mNoMoreOutputData = false;
1922
Andreas Huberbe06d262009-08-14 14:37:10 -07001923 srcLength = srcBuffer->range_length();
1924
Andreas Huberc712b9f2010-01-20 15:05:46 -08001925 if (info->mSize < srcLength) {
1926 LOGE("info->mSize = %d, srcLength = %d",
1927 info->mSize, srcLength);
Andreas Huberbe06d262009-08-14 14:37:10 -07001928 }
Andreas Huberc712b9f2010-01-20 15:05:46 -08001929 CHECK(info->mSize >= srcLength);
1930 memcpy(info->mData,
Andreas Huberbe06d262009-08-14 14:37:10 -07001931 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
1932 srcLength);
1933
Andreas Huberfa8de752009-10-08 10:07:49 -07001934 if (srcBuffer->meta_data()->findInt64(kKeyTime, &timestampUs)) {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001935 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
1936 "timestamp %lld us (%.2f secs)",
1937 info->mBuffer, srcLength,
1938 timestampUs, timestampUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07001939 }
1940 }
1941
Andreas Huberbe06d262009-08-14 14:37:10 -07001942 if (srcBuffer != NULL) {
1943 srcBuffer->release();
1944 srcBuffer = NULL;
1945 }
Andreas Huber3f427072009-10-08 11:02:27 -07001946
Andreas Huber784202e2009-10-15 13:46:54 -07001947 err = mOMX->emptyBuffer(
Andreas Huber3f427072009-10-08 11:02:27 -07001948 mNode, info->mBuffer, 0, srcLength,
Andreas Huberfa8de752009-10-08 10:07:49 -07001949 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07001950
1951 if (err != OK) {
1952 setState(ERROR);
1953 return;
1954 }
1955
1956 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001957
1958 // This component does not ever signal the EOS flag on output buffers,
1959 // Thanks for nothing.
1960 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
1961 mNoMoreOutputData = true;
1962 mBufferFilled.signal();
1963 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001964}
1965
1966void OMXCodec::fillOutputBuffer(BufferInfo *info) {
1967 CHECK_EQ(info->mOwnedByComponent, false);
1968
Andreas Huber404cc412009-08-25 14:26:05 -07001969 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07001970 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07001971 "calling fillOutputBuffer");
1972 return;
1973 }
1974
Andreas Huber4c483422009-09-02 16:05:36 -07001975 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07001976 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber3f427072009-10-08 11:02:27 -07001977 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001978
1979 info->mOwnedByComponent = true;
1980}
1981
1982void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
1983 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1984 for (size_t i = 0; i < buffers->size(); ++i) {
1985 if ((*buffers)[i].mBuffer == buffer) {
1986 drainInputBuffer(&buffers->editItemAt(i));
1987 return;
1988 }
1989 }
1990
1991 CHECK(!"should not be here.");
1992}
1993
1994void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
1995 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1996 for (size_t i = 0; i < buffers->size(); ++i) {
1997 if ((*buffers)[i].mBuffer == buffer) {
1998 fillOutputBuffer(&buffers->editItemAt(i));
1999 return;
2000 }
2001 }
2002
2003 CHECK(!"should not be here.");
2004}
2005
2006void OMXCodec::setState(State newState) {
2007 mState = newState;
2008 mAsyncCompletion.signal();
2009
2010 // This may cause some spurious wakeups but is necessary to
2011 // unblock the reader if we enter ERROR state.
2012 mBufferFilled.signal();
2013}
2014
Andreas Huberda050cf22009-09-02 14:01:43 -07002015void OMXCodec::setRawAudioFormat(
2016 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
2017 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002018 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002019 pcmParams.nPortIndex = portIndex;
2020
Andreas Huber784202e2009-10-15 13:46:54 -07002021 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002022 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2023
2024 CHECK_EQ(err, OK);
2025
2026 pcmParams.nChannels = numChannels;
2027 pcmParams.eNumData = OMX_NumericalDataSigned;
2028 pcmParams.bInterleaved = OMX_TRUE;
2029 pcmParams.nBitPerSample = 16;
2030 pcmParams.nSamplingRate = sampleRate;
2031 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2032
2033 if (numChannels == 1) {
2034 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2035 } else {
2036 CHECK_EQ(numChannels, 2);
2037
2038 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2039 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2040 }
2041
Andreas Huber784202e2009-10-15 13:46:54 -07002042 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002043 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2044
2045 CHECK_EQ(err, OK);
2046}
2047
Andreas Huber8768f2c2009-12-01 15:26:54 -08002048void OMXCodec::setAMRFormat(bool isWAMR) {
2049 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002050
Andreas Huber8768f2c2009-12-01 15:26:54 -08002051 OMX_AUDIO_PARAM_AMRTYPE def;
2052 InitOMXParams(&def);
2053 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002054
Andreas Huber8768f2c2009-12-01 15:26:54 -08002055 status_t err =
2056 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002057
Andreas Huber8768f2c2009-12-01 15:26:54 -08002058 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002059
Andreas Huber8768f2c2009-12-01 15:26:54 -08002060 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
2061 def.eAMRBandMode =
2062 isWAMR ? OMX_AUDIO_AMRBandModeWB0 : OMX_AUDIO_AMRBandModeNB0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002063
Andreas Huber8768f2c2009-12-01 15:26:54 -08002064 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2065 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002066
2067 ////////////////////////
2068
2069 if (mIsEncoder) {
2070 sp<MetaData> format = mSource->getFormat();
2071 int32_t sampleRate;
2072 int32_t numChannels;
2073 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2074 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2075
2076 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2077 }
2078}
2079
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07002080void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate) {
Andreas Huberda050cf22009-09-02 14:01:43 -07002081 if (mIsEncoder) {
2082 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2083 } else {
2084 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002085 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002086 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002087
Andreas Huber784202e2009-10-15 13:46:54 -07002088 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002089 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2090 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002091
Andreas Huberda050cf22009-09-02 14:01:43 -07002092 profile.nChannels = numChannels;
2093 profile.nSampleRate = sampleRate;
2094 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002095
Andreas Huber784202e2009-10-15 13:46:54 -07002096 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002097 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2098 CHECK_EQ(err, OK);
2099 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002100}
2101
2102void OMXCodec::setImageOutputFormat(
2103 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002104 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002105
2106#if 0
2107 OMX_INDEXTYPE index;
2108 status_t err = mOMX->get_extension_index(
2109 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2110 CHECK_EQ(err, OK);
2111
2112 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2113 CHECK_EQ(err, OK);
2114#endif
2115
2116 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002117 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002118 def.nPortIndex = kPortIndexOutput;
2119
Andreas Huber784202e2009-10-15 13:46:54 -07002120 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002121 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2122 CHECK_EQ(err, OK);
2123
2124 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2125
2126 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002127
Andreas Huberbe06d262009-08-14 14:37:10 -07002128 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2129 imageDef->eColorFormat = format;
2130 imageDef->nFrameWidth = width;
2131 imageDef->nFrameHeight = height;
2132
2133 switch (format) {
2134 case OMX_COLOR_FormatYUV420PackedPlanar:
2135 case OMX_COLOR_FormatYUV411Planar:
2136 {
2137 def.nBufferSize = (width * height * 3) / 2;
2138 break;
2139 }
2140
2141 case OMX_COLOR_FormatCbYCrY:
2142 {
2143 def.nBufferSize = width * height * 2;
2144 break;
2145 }
2146
2147 case OMX_COLOR_Format32bitARGB8888:
2148 {
2149 def.nBufferSize = width * height * 4;
2150 break;
2151 }
2152
Andreas Huber201511c2009-09-08 14:01:44 -07002153 case OMX_COLOR_Format16bitARGB4444:
2154 case OMX_COLOR_Format16bitARGB1555:
2155 case OMX_COLOR_Format16bitRGB565:
2156 case OMX_COLOR_Format16bitBGR565:
2157 {
2158 def.nBufferSize = width * height * 2;
2159 break;
2160 }
2161
Andreas Huberbe06d262009-08-14 14:37:10 -07002162 default:
2163 CHECK(!"Should not be here. Unknown color format.");
2164 break;
2165 }
2166
Andreas Huber5c0a9132009-08-20 11:16:40 -07002167 def.nBufferCountActual = def.nBufferCountMin;
2168
Andreas Huber784202e2009-10-15 13:46:54 -07002169 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002170 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2171 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002172}
Andreas Huberbe06d262009-08-14 14:37:10 -07002173
Andreas Huber5c0a9132009-08-20 11:16:40 -07002174void OMXCodec::setJPEGInputFormat(
2175 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2176 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002177 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002178 def.nPortIndex = kPortIndexInput;
2179
Andreas Huber784202e2009-10-15 13:46:54 -07002180 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002181 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2182 CHECK_EQ(err, OK);
2183
Andreas Huber5c0a9132009-08-20 11:16:40 -07002184 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2185 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2186
Andreas Huberbe06d262009-08-14 14:37:10 -07002187 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2188 imageDef->nFrameWidth = width;
2189 imageDef->nFrameHeight = height;
2190
Andreas Huber5c0a9132009-08-20 11:16:40 -07002191 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002192 def.nBufferCountActual = def.nBufferCountMin;
2193
Andreas Huber784202e2009-10-15 13:46:54 -07002194 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002195 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2196 CHECK_EQ(err, OK);
2197}
2198
2199void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2200 CodecSpecificData *specific =
2201 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2202
2203 specific->mSize = size;
2204 memcpy(specific->mData, data, size);
2205
2206 mCodecSpecificData.push(specific);
2207}
2208
2209void OMXCodec::clearCodecSpecificData() {
2210 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2211 free(mCodecSpecificData.editItemAt(i));
2212 }
2213 mCodecSpecificData.clear();
2214 mCodecSpecificDataIndex = 0;
2215}
2216
2217status_t OMXCodec::start(MetaData *) {
Andreas Huber42978e52009-08-27 10:08:39 -07002218 Mutex::Autolock autoLock(mLock);
2219
Andreas Huberbe06d262009-08-14 14:37:10 -07002220 if (mState != LOADED) {
2221 return UNKNOWN_ERROR;
2222 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002223
Andreas Huberbe06d262009-08-14 14:37:10 -07002224 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002225 if (mQuirks & kWantsNALFragments) {
2226 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002227 }
2228 status_t err = mSource->start(params.get());
2229
2230 if (err != OK) {
2231 return err;
2232 }
2233
2234 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002235 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002236 mSignalledEOS = false;
2237 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002238 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002239 mSeekTimeUs = -1;
2240 mFilledBuffers.clear();
2241
2242 return init();
2243}
2244
2245status_t OMXCodec::stop() {
Andreas Huber4c483422009-09-02 16:05:36 -07002246 CODEC_LOGV("stop");
Andreas Huberbe06d262009-08-14 14:37:10 -07002247
2248 Mutex::Autolock autoLock(mLock);
2249
2250 while (isIntermediateState(mState)) {
2251 mAsyncCompletion.wait(mLock);
2252 }
2253
2254 switch (mState) {
2255 case LOADED:
2256 case ERROR:
2257 break;
2258
2259 case EXECUTING:
2260 {
2261 setState(EXECUTING_TO_IDLE);
2262
Andreas Huber127fcdc2009-08-26 16:27:02 -07002263 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002264 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002265 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002266
Andreas Huber127fcdc2009-08-26 16:27:02 -07002267 bool emulateInputFlushCompletion =
2268 !flushPortAsync(kPortIndexInput);
2269
2270 bool emulateOutputFlushCompletion =
2271 !flushPortAsync(kPortIndexOutput);
2272
2273 if (emulateInputFlushCompletion) {
2274 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2275 }
2276
2277 if (emulateOutputFlushCompletion) {
2278 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2279 }
2280 } else {
2281 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2282 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2283
2284 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002285 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002286 CHECK_EQ(err, OK);
2287 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002288
2289 while (mState != LOADED && mState != ERROR) {
2290 mAsyncCompletion.wait(mLock);
2291 }
2292
2293 break;
2294 }
2295
2296 default:
2297 {
2298 CHECK(!"should not be here.");
2299 break;
2300 }
2301 }
2302
2303 mSource->stop();
2304
2305 return OK;
2306}
2307
2308sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002309 Mutex::Autolock autoLock(mLock);
2310
Andreas Huberbe06d262009-08-14 14:37:10 -07002311 return mOutputFormat;
2312}
2313
2314status_t OMXCodec::read(
2315 MediaBuffer **buffer, const ReadOptions *options) {
2316 *buffer = NULL;
2317
2318 Mutex::Autolock autoLock(mLock);
2319
Andreas Huberd06e5b82009-08-28 13:18:14 -07002320 if (mState != EXECUTING && mState != RECONFIGURING) {
2321 return UNKNOWN_ERROR;
2322 }
2323
Andreas Hubere981c332009-10-22 13:49:30 -07002324 bool seeking = false;
2325 int64_t seekTimeUs;
2326 if (options && options->getSeekTo(&seekTimeUs)) {
2327 seeking = true;
2328 }
2329
Andreas Huber42978e52009-08-27 10:08:39 -07002330 if (mInitialBufferSubmit) {
2331 mInitialBufferSubmit = false;
2332
Andreas Hubere981c332009-10-22 13:49:30 -07002333 if (seeking) {
2334 CHECK(seekTimeUs >= 0);
2335 mSeekTimeUs = seekTimeUs;
2336
2337 // There's no reason to trigger the code below, there's
2338 // nothing to flush yet.
2339 seeking = false;
2340 }
2341
Andreas Huber42978e52009-08-27 10:08:39 -07002342 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002343
Andreas Huberd06e5b82009-08-28 13:18:14 -07002344 if (mState == EXECUTING) {
2345 // Otherwise mState == RECONFIGURING and this code will trigger
2346 // after the output port is reenabled.
2347 fillOutputBuffers();
2348 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002349 }
2350
Andreas Hubere981c332009-10-22 13:49:30 -07002351 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002352 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002353
2354 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002355
2356 CHECK(seekTimeUs >= 0);
2357 mSeekTimeUs = seekTimeUs;
2358
2359 mFilledBuffers.clear();
2360
2361 CHECK_EQ(mState, EXECUTING);
2362
Andreas Huber404cc412009-08-25 14:26:05 -07002363 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2364 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2365
2366 if (emulateInputFlushCompletion) {
2367 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2368 }
2369
2370 if (emulateOutputFlushCompletion) {
2371 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2372 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002373
2374 while (mSeekTimeUs >= 0) {
2375 mBufferFilled.wait(mLock);
2376 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002377 }
2378
2379 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2380 mBufferFilled.wait(mLock);
2381 }
2382
2383 if (mState == ERROR) {
2384 return UNKNOWN_ERROR;
2385 }
2386
2387 if (mFilledBuffers.empty()) {
2388 return ERROR_END_OF_STREAM;
2389 }
2390
Andreas Hubercfd55572009-10-09 14:11:28 -07002391 if (mOutputPortSettingsHaveChanged) {
2392 mOutputPortSettingsHaveChanged = false;
2393
2394 return INFO_FORMAT_CHANGED;
2395 }
2396
Andreas Huberbe06d262009-08-14 14:37:10 -07002397 size_t index = *mFilledBuffers.begin();
2398 mFilledBuffers.erase(mFilledBuffers.begin());
2399
2400 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2401 info->mMediaBuffer->add_ref();
2402 *buffer = info->mMediaBuffer;
2403
2404 return OK;
2405}
2406
2407void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2408 Mutex::Autolock autoLock(mLock);
2409
2410 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2411 for (size_t i = 0; i < buffers->size(); ++i) {
2412 BufferInfo *info = &buffers->editItemAt(i);
2413
2414 if (info->mMediaBuffer == buffer) {
2415 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2416 fillOutputBuffer(info);
2417 return;
2418 }
2419 }
2420
2421 CHECK(!"should not be here.");
2422}
2423
2424static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2425 static const char *kNames[] = {
2426 "OMX_IMAGE_CodingUnused",
2427 "OMX_IMAGE_CodingAutoDetect",
2428 "OMX_IMAGE_CodingJPEG",
2429 "OMX_IMAGE_CodingJPEG2K",
2430 "OMX_IMAGE_CodingEXIF",
2431 "OMX_IMAGE_CodingTIFF",
2432 "OMX_IMAGE_CodingGIF",
2433 "OMX_IMAGE_CodingPNG",
2434 "OMX_IMAGE_CodingLZW",
2435 "OMX_IMAGE_CodingBMP",
2436 };
2437
2438 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2439
2440 if (type < 0 || (size_t)type >= numNames) {
2441 return "UNKNOWN";
2442 } else {
2443 return kNames[type];
2444 }
2445}
2446
2447static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2448 static const char *kNames[] = {
2449 "OMX_COLOR_FormatUnused",
2450 "OMX_COLOR_FormatMonochrome",
2451 "OMX_COLOR_Format8bitRGB332",
2452 "OMX_COLOR_Format12bitRGB444",
2453 "OMX_COLOR_Format16bitARGB4444",
2454 "OMX_COLOR_Format16bitARGB1555",
2455 "OMX_COLOR_Format16bitRGB565",
2456 "OMX_COLOR_Format16bitBGR565",
2457 "OMX_COLOR_Format18bitRGB666",
2458 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002459 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002460 "OMX_COLOR_Format24bitRGB888",
2461 "OMX_COLOR_Format24bitBGR888",
2462 "OMX_COLOR_Format24bitARGB1887",
2463 "OMX_COLOR_Format25bitARGB1888",
2464 "OMX_COLOR_Format32bitBGRA8888",
2465 "OMX_COLOR_Format32bitARGB8888",
2466 "OMX_COLOR_FormatYUV411Planar",
2467 "OMX_COLOR_FormatYUV411PackedPlanar",
2468 "OMX_COLOR_FormatYUV420Planar",
2469 "OMX_COLOR_FormatYUV420PackedPlanar",
2470 "OMX_COLOR_FormatYUV420SemiPlanar",
2471 "OMX_COLOR_FormatYUV422Planar",
2472 "OMX_COLOR_FormatYUV422PackedPlanar",
2473 "OMX_COLOR_FormatYUV422SemiPlanar",
2474 "OMX_COLOR_FormatYCbYCr",
2475 "OMX_COLOR_FormatYCrYCb",
2476 "OMX_COLOR_FormatCbYCrY",
2477 "OMX_COLOR_FormatCrYCbY",
2478 "OMX_COLOR_FormatYUV444Interleaved",
2479 "OMX_COLOR_FormatRawBayer8bit",
2480 "OMX_COLOR_FormatRawBayer10bit",
2481 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002482 "OMX_COLOR_FormatL2",
2483 "OMX_COLOR_FormatL4",
2484 "OMX_COLOR_FormatL8",
2485 "OMX_COLOR_FormatL16",
2486 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002487 "OMX_COLOR_FormatL32",
2488 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2489 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2490 "OMX_COLOR_Format18BitBGR666",
2491 "OMX_COLOR_Format24BitARGB6666",
2492 "OMX_COLOR_Format24BitABGR6666",
2493 };
2494
2495 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2496
Andreas Huberbe06d262009-08-14 14:37:10 -07002497 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2498 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2499 } else if (type < 0 || (size_t)type >= numNames) {
2500 return "UNKNOWN";
2501 } else {
2502 return kNames[type];
2503 }
2504}
2505
2506static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2507 static const char *kNames[] = {
2508 "OMX_VIDEO_CodingUnused",
2509 "OMX_VIDEO_CodingAutoDetect",
2510 "OMX_VIDEO_CodingMPEG2",
2511 "OMX_VIDEO_CodingH263",
2512 "OMX_VIDEO_CodingMPEG4",
2513 "OMX_VIDEO_CodingWMV",
2514 "OMX_VIDEO_CodingRV",
2515 "OMX_VIDEO_CodingAVC",
2516 "OMX_VIDEO_CodingMJPEG",
2517 };
2518
2519 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2520
2521 if (type < 0 || (size_t)type >= numNames) {
2522 return "UNKNOWN";
2523 } else {
2524 return kNames[type];
2525 }
2526}
2527
2528static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2529 static const char *kNames[] = {
2530 "OMX_AUDIO_CodingUnused",
2531 "OMX_AUDIO_CodingAutoDetect",
2532 "OMX_AUDIO_CodingPCM",
2533 "OMX_AUDIO_CodingADPCM",
2534 "OMX_AUDIO_CodingAMR",
2535 "OMX_AUDIO_CodingGSMFR",
2536 "OMX_AUDIO_CodingGSMEFR",
2537 "OMX_AUDIO_CodingGSMHR",
2538 "OMX_AUDIO_CodingPDCFR",
2539 "OMX_AUDIO_CodingPDCEFR",
2540 "OMX_AUDIO_CodingPDCHR",
2541 "OMX_AUDIO_CodingTDMAFR",
2542 "OMX_AUDIO_CodingTDMAEFR",
2543 "OMX_AUDIO_CodingQCELP8",
2544 "OMX_AUDIO_CodingQCELP13",
2545 "OMX_AUDIO_CodingEVRC",
2546 "OMX_AUDIO_CodingSMV",
2547 "OMX_AUDIO_CodingG711",
2548 "OMX_AUDIO_CodingG723",
2549 "OMX_AUDIO_CodingG726",
2550 "OMX_AUDIO_CodingG729",
2551 "OMX_AUDIO_CodingAAC",
2552 "OMX_AUDIO_CodingMP3",
2553 "OMX_AUDIO_CodingSBC",
2554 "OMX_AUDIO_CodingVORBIS",
2555 "OMX_AUDIO_CodingWMA",
2556 "OMX_AUDIO_CodingRA",
2557 "OMX_AUDIO_CodingMIDI",
2558 };
2559
2560 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2561
2562 if (type < 0 || (size_t)type >= numNames) {
2563 return "UNKNOWN";
2564 } else {
2565 return kNames[type];
2566 }
2567}
2568
2569static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2570 static const char *kNames[] = {
2571 "OMX_AUDIO_PCMModeLinear",
2572 "OMX_AUDIO_PCMModeALaw",
2573 "OMX_AUDIO_PCMModeMULaw",
2574 };
2575
2576 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2577
2578 if (type < 0 || (size_t)type >= numNames) {
2579 return "UNKNOWN";
2580 } else {
2581 return kNames[type];
2582 }
2583}
2584
Andreas Huber7ae02c82009-09-09 16:29:47 -07002585static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2586 static const char *kNames[] = {
2587 "OMX_AUDIO_AMRBandModeUnused",
2588 "OMX_AUDIO_AMRBandModeNB0",
2589 "OMX_AUDIO_AMRBandModeNB1",
2590 "OMX_AUDIO_AMRBandModeNB2",
2591 "OMX_AUDIO_AMRBandModeNB3",
2592 "OMX_AUDIO_AMRBandModeNB4",
2593 "OMX_AUDIO_AMRBandModeNB5",
2594 "OMX_AUDIO_AMRBandModeNB6",
2595 "OMX_AUDIO_AMRBandModeNB7",
2596 "OMX_AUDIO_AMRBandModeWB0",
2597 "OMX_AUDIO_AMRBandModeWB1",
2598 "OMX_AUDIO_AMRBandModeWB2",
2599 "OMX_AUDIO_AMRBandModeWB3",
2600 "OMX_AUDIO_AMRBandModeWB4",
2601 "OMX_AUDIO_AMRBandModeWB5",
2602 "OMX_AUDIO_AMRBandModeWB6",
2603 "OMX_AUDIO_AMRBandModeWB7",
2604 "OMX_AUDIO_AMRBandModeWB8",
2605 };
2606
2607 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2608
2609 if (type < 0 || (size_t)type >= numNames) {
2610 return "UNKNOWN";
2611 } else {
2612 return kNames[type];
2613 }
2614}
2615
2616static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2617 static const char *kNames[] = {
2618 "OMX_AUDIO_AMRFrameFormatConformance",
2619 "OMX_AUDIO_AMRFrameFormatIF1",
2620 "OMX_AUDIO_AMRFrameFormatIF2",
2621 "OMX_AUDIO_AMRFrameFormatFSF",
2622 "OMX_AUDIO_AMRFrameFormatRTPPayload",
2623 "OMX_AUDIO_AMRFrameFormatITU",
2624 };
2625
2626 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2627
2628 if (type < 0 || (size_t)type >= numNames) {
2629 return "UNKNOWN";
2630 } else {
2631 return kNames[type];
2632 }
2633}
Andreas Huberbe06d262009-08-14 14:37:10 -07002634
2635void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2636 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002637 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002638 def.nPortIndex = portIndex;
2639
Andreas Huber784202e2009-10-15 13:46:54 -07002640 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002641 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2642 CHECK_EQ(err, OK);
2643
2644 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2645
2646 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2647 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2648
2649 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2650 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2651 printf(" nBufferSize = %ld\n", def.nBufferSize);
2652
2653 switch (def.eDomain) {
2654 case OMX_PortDomainImage:
2655 {
2656 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2657
2658 printf("\n");
2659 printf(" // Image\n");
2660 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
2661 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
2662 printf(" nStride = %ld\n", imageDef->nStride);
2663
2664 printf(" eCompressionFormat = %s\n",
2665 imageCompressionFormatString(imageDef->eCompressionFormat));
2666
2667 printf(" eColorFormat = %s\n",
2668 colorFormatString(imageDef->eColorFormat));
2669
2670 break;
2671 }
2672
2673 case OMX_PortDomainVideo:
2674 {
2675 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2676
2677 printf("\n");
2678 printf(" // Video\n");
2679 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
2680 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
2681 printf(" nStride = %ld\n", videoDef->nStride);
2682
2683 printf(" eCompressionFormat = %s\n",
2684 videoCompressionFormatString(videoDef->eCompressionFormat));
2685
2686 printf(" eColorFormat = %s\n",
2687 colorFormatString(videoDef->eColorFormat));
2688
2689 break;
2690 }
2691
2692 case OMX_PortDomainAudio:
2693 {
2694 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
2695
2696 printf("\n");
2697 printf(" // Audio\n");
2698 printf(" eEncoding = %s\n",
2699 audioCodingTypeString(audioDef->eEncoding));
2700
2701 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
2702 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002703 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07002704 params.nPortIndex = portIndex;
2705
Andreas Huber784202e2009-10-15 13:46:54 -07002706 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002707 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2708 CHECK_EQ(err, OK);
2709
2710 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
2711 printf(" nChannels = %ld\n", params.nChannels);
2712 printf(" bInterleaved = %d\n", params.bInterleaved);
2713 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
2714
2715 printf(" eNumData = %s\n",
2716 params.eNumData == OMX_NumericalDataSigned
2717 ? "signed" : "unsigned");
2718
2719 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07002720 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
2721 OMX_AUDIO_PARAM_AMRTYPE amr;
2722 InitOMXParams(&amr);
2723 amr.nPortIndex = portIndex;
2724
Andreas Huber784202e2009-10-15 13:46:54 -07002725 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07002726 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2727 CHECK_EQ(err, OK);
2728
2729 printf(" nChannels = %ld\n", amr.nChannels);
2730 printf(" eAMRBandMode = %s\n",
2731 amrBandModeString(amr.eAMRBandMode));
2732 printf(" eAMRFrameFormat = %s\n",
2733 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07002734 }
2735
2736 break;
2737 }
2738
2739 default:
2740 {
2741 printf(" // Unknown\n");
2742 break;
2743 }
2744 }
2745
2746 printf("}\n");
2747}
2748
2749void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
2750 mOutputFormat = new MetaData;
2751 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
2752
2753 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002754 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002755 def.nPortIndex = kPortIndexOutput;
2756
Andreas Huber784202e2009-10-15 13:46:54 -07002757 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002758 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2759 CHECK_EQ(err, OK);
2760
2761 switch (def.eDomain) {
2762 case OMX_PortDomainImage:
2763 {
2764 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2765 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2766
Andreas Hubere6c40962009-09-10 14:13:30 -07002767 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002768 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
2769 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
2770 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
2771 break;
2772 }
2773
2774 case OMX_PortDomainAudio:
2775 {
2776 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
2777
Andreas Huberda050cf22009-09-02 14:01:43 -07002778 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
2779 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002780 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07002781 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002782
Andreas Huber784202e2009-10-15 13:46:54 -07002783 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002784 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2785 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002786
Andreas Huberda050cf22009-09-02 14:01:43 -07002787 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
2788 CHECK_EQ(params.nBitPerSample, 16);
2789 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07002790
Andreas Huberda050cf22009-09-02 14:01:43 -07002791 int32_t numChannels, sampleRate;
2792 inputFormat->findInt32(kKeyChannelCount, &numChannels);
2793 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07002794
Andreas Huberda050cf22009-09-02 14:01:43 -07002795 if ((OMX_U32)numChannels != params.nChannels) {
2796 LOGW("Codec outputs a different number of channels than "
2797 "the input stream contains.");
2798 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002799
Andreas Hubere6c40962009-09-10 14:13:30 -07002800 mOutputFormat->setCString(
2801 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07002802
2803 // Use the codec-advertised number of channels, as some
2804 // codecs appear to output stereo even if the input data is
2805 // mono.
2806 mOutputFormat->setInt32(kKeyChannelCount, params.nChannels);
2807
2808 // The codec-reported sampleRate is not reliable...
2809 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
2810 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07002811 OMX_AUDIO_PARAM_AMRTYPE amr;
2812 InitOMXParams(&amr);
2813 amr.nPortIndex = kPortIndexOutput;
2814
Andreas Huber784202e2009-10-15 13:46:54 -07002815 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07002816 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2817 CHECK_EQ(err, OK);
2818
2819 CHECK_EQ(amr.nChannels, 1);
2820 mOutputFormat->setInt32(kKeyChannelCount, 1);
2821
2822 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
2823 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002824 mOutputFormat->setCString(
2825 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07002826 mOutputFormat->setInt32(kKeySampleRate, 8000);
2827 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
2828 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002829 mOutputFormat->setCString(
2830 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07002831 mOutputFormat->setInt32(kKeySampleRate, 16000);
2832 } else {
2833 CHECK(!"Unknown AMR band mode.");
2834 }
Andreas Huberda050cf22009-09-02 14:01:43 -07002835 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002836 mOutputFormat->setCString(
2837 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
Andreas Huberda050cf22009-09-02 14:01:43 -07002838 } else {
2839 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07002840 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002841 break;
2842 }
2843
2844 case OMX_PortDomainVideo:
2845 {
2846 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
2847
2848 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002849 mOutputFormat->setCString(
2850 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002851 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002852 mOutputFormat->setCString(
2853 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002854 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002855 mOutputFormat->setCString(
2856 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07002857 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002858 mOutputFormat->setCString(
2859 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07002860 } else {
2861 CHECK(!"Unknown compression format.");
2862 }
2863
2864 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
2865 // This component appears to be lying to me.
2866 mOutputFormat->setInt32(
2867 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
2868 mOutputFormat->setInt32(
2869 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
2870 } else {
2871 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
2872 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
2873 }
2874
2875 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
2876 break;
2877 }
2878
2879 default:
2880 {
2881 CHECK(!"should not be here, neither audio nor video.");
2882 break;
2883 }
2884 }
2885}
2886
Andreas Hubere6c40962009-09-10 14:13:30 -07002887////////////////////////////////////////////////////////////////////////////////
2888
2889status_t QueryCodecs(
2890 const sp<IOMX> &omx,
2891 const char *mime, bool queryDecoders,
2892 Vector<CodecCapabilities> *results) {
2893 results->clear();
2894
2895 for (int index = 0;; ++index) {
2896 const char *componentName;
2897
2898 if (!queryDecoders) {
2899 componentName = GetCodec(
2900 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
2901 mime, index);
2902 } else {
2903 componentName = GetCodec(
2904 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
2905 mime, index);
2906 }
2907
2908 if (!componentName) {
2909 return OK;
2910 }
2911
Andreas Huber784202e2009-10-15 13:46:54 -07002912 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07002913 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07002914 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07002915
2916 if (err != OK) {
2917 continue;
2918 }
2919
2920 OMXCodec::setComponentRole(omx, node, queryDecoders, mime);
2921
2922 results->push();
2923 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
2924 caps->mComponentName = componentName;
2925
2926 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
2927 InitOMXParams(&param);
2928
2929 param.nPortIndex = queryDecoders ? 0 : 1;
2930
2931 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07002932 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07002933 node, OMX_IndexParamVideoProfileLevelQuerySupported,
2934 &param, sizeof(param));
2935
2936 if (err != OK) {
2937 break;
2938 }
2939
2940 CodecProfileLevel profileLevel;
2941 profileLevel.mProfile = param.eProfile;
2942 profileLevel.mLevel = param.eLevel;
2943
2944 caps->mProfileLevels.push(profileLevel);
2945 }
2946
Andreas Huber784202e2009-10-15 13:46:54 -07002947 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07002948 }
2949}
2950
Andreas Huberbe06d262009-08-14 14:37:10 -07002951} // namespace android