blob: d18d146ee1740fe2d5f468126ecb78d8d81c29be [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 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 "NuPlayerDecoder"
19#include <utils/Log.h>
20
21#include "NuPlayerDecoder.h"
22
Andreas Huberf9334412010-12-15 15:17:42 -080023#include "ESDS.h"
24
25#include <media/stagefright/foundation/ABuffer.h>
26#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080027#include <media/stagefright/foundation/AMessage.h>
Andreas Huberf9334412010-12-15 15:17:42 -080028#include <media/stagefright/ACodec.h>
29#include <media/stagefright/MediaDefs.h>
30#include <media/stagefright/MetaData.h>
31#include <media/stagefright/Utils.h>
Andreas Huberf9334412010-12-15 15:17:42 -080032
33namespace android {
34
35NuPlayer::Decoder::Decoder(
Glenn Kasten11731182011-02-08 17:26:17 -080036 const sp<AMessage> &notify,
37 const sp<NativeWindowWrapper> &nativeWindow)
Andreas Huberf9334412010-12-15 15:17:42 -080038 : mNotify(notify),
Glenn Kasten11731182011-02-08 17:26:17 -080039 mNativeWindow(nativeWindow) {
Andreas Huberf9334412010-12-15 15:17:42 -080040}
41
42NuPlayer::Decoder::~Decoder() {
43}
44
Andreas Huber5bc087c2010-12-23 10:27:40 -080045void NuPlayer::Decoder::configure(const sp<MetaData> &meta) {
Andreas Huberf9334412010-12-15 15:17:42 -080046 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -080047
48 const char *mime;
49 CHECK(meta->findCString(kKeyMIMEType, &mime));
50
51 sp<AMessage> notifyMsg =
52 new AMessage(kWhatCodecNotify, id());
53
54 sp<AMessage> format = makeFormat(meta);
55
Glenn Kasten11731182011-02-08 17:26:17 -080056 if (mNativeWindow != NULL) {
57 format->setObject("native-window", mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -080058 }
59
Andreas Huber078cfcf2011-09-15 12:25:04 -070060 // Current video decoders do not return from OMX_FillThisBuffer
61 // quickly, violating the OpenMAX specs, until that is remedied
62 // we need to invest in an extra looper to free the main event
63 // queue.
64 bool needDedicatedLooper = !strncasecmp(mime, "video/", 6);
65
Andreas Huber00f49512011-05-11 14:15:13 -070066 mCodec = new ACodec;
Andreas Huber078cfcf2011-09-15 12:25:04 -070067
68 if (needDedicatedLooper && mCodecLooper == NULL) {
69 mCodecLooper = new ALooper;
70 mCodecLooper->setName("NuPlayerDecoder");
71 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
72 }
73
74 (needDedicatedLooper ? mCodecLooper : looper())->registerHandler(mCodec);
Andreas Huberf9334412010-12-15 15:17:42 -080075
Andreas Huber00f49512011-05-11 14:15:13 -070076 mCodec->setNotificationMessage(notifyMsg);
77 mCodec->initiateSetup(format);
Andreas Huberf9334412010-12-15 15:17:42 -080078}
79
80void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
81 switch (msg->what()) {
82 case kWhatCodecNotify:
83 {
84 int32_t what;
85 CHECK(msg->findInt32("what", &what));
86
87 if (what == ACodec::kWhatFillThisBuffer) {
88 onFillThisBuffer(msg);
89 } else {
90 sp<AMessage> notify = mNotify->dup();
91 notify->setMessage("codec-request", msg);
92 notify->post();
93 }
94 break;
95 }
96
97 default:
98 TRESPASS();
99 break;
100 }
101}
102
103sp<AMessage> NuPlayer::Decoder::makeFormat(const sp<MetaData> &meta) {
104 CHECK(mCSD.isEmpty());
105
106 const char *mime;
107 CHECK(meta->findCString(kKeyMIMEType, &mime));
108
109 sp<AMessage> msg = new AMessage;
110 msg->setString("mime", mime);
111
112 if (!strncasecmp("video/", mime, 6)) {
113 int32_t width, height;
114 CHECK(meta->findInt32(kKeyWidth, &width));
115 CHECK(meta->findInt32(kKeyHeight, &height));
116
117 msg->setInt32("width", width);
118 msg->setInt32("height", height);
Andreas Hubere370bb62012-04-25 14:24:30 -0700119 } else if (!strncasecmp("audio/", mime, 6)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800120 int32_t numChannels, sampleRate;
121 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
122 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
123
124 msg->setInt32("channel-count", numChannels);
125 msg->setInt32("sample-rate", sampleRate);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700126
Marco Nelissen8b712412012-04-27 09:33:24 -0700127 int32_t delay = 0;
128 if (meta->findInt32(kKeyEncoderDelay, &delay)) {
129 msg->setInt32("encoder-delay", delay);
130 }
131 int32_t padding = 0;
132 if (meta->findInt32(kKeyEncoderPadding, &padding)) {
133 msg->setInt32("encoder-padding", padding);
134 }
135
Andreas Hubered3e3e02012-03-26 11:13:27 -0700136 int32_t isADTS;
137 if (meta->findInt32(kKeyIsADTS, &isADTS) && isADTS != 0) {
138 msg->setInt32("is-adts", true);
139 }
Andreas Huberf9334412010-12-15 15:17:42 -0800140 }
141
Andreas Huber5bc087c2010-12-23 10:27:40 -0800142 int32_t maxInputSize;
143 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
144 msg->setInt32("max-input-size", maxInputSize);
145 }
146
147 mCSDIndex = 0;
148
Andreas Huberf9334412010-12-15 15:17:42 -0800149 uint32_t type;
150 const void *data;
151 size_t size;
152 if (meta->findData(kKeyAVCC, &type, &data, &size)) {
153 // Parse the AVCDecoderConfigurationRecord
154
155 const uint8_t *ptr = (const uint8_t *)data;
156
157 CHECK(size >= 7);
158 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1
159 uint8_t profile = ptr[1];
160 uint8_t level = ptr[3];
161
162 // There is decodable content out there that fails the following
163 // assertion, let's be lenient for now...
164 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
165
166 size_t lengthSize = 1 + (ptr[4] & 3);
167
168 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
169 // violates it...
170 // CHECK((ptr[5] >> 5) == 7); // reserved
171
172 size_t numSeqParameterSets = ptr[5] & 31;
173
174 ptr += 6;
175 size -= 6;
176
177 sp<ABuffer> buffer = new ABuffer(1024);
178 buffer->setRange(0, 0);
179
180 for (size_t i = 0; i < numSeqParameterSets; ++i) {
181 CHECK(size >= 2);
182 size_t length = U16_AT(ptr);
183
184 ptr += 2;
185 size -= 2;
186
187 CHECK(size >= length);
188
189 memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
190 memcpy(buffer->data() + buffer->size() + 4, ptr, length);
191 buffer->setRange(0, buffer->size() + 4 + length);
192
193 ptr += length;
194 size -= length;
195 }
196
197 buffer->meta()->setInt32("csd", true);
198 mCSD.push(buffer);
199
200 buffer = new ABuffer(1024);
201 buffer->setRange(0, 0);
202
203 CHECK(size >= 1);
204 size_t numPictureParameterSets = *ptr;
205 ++ptr;
206 --size;
207
208 for (size_t i = 0; i < numPictureParameterSets; ++i) {
209 CHECK(size >= 2);
210 size_t length = U16_AT(ptr);
211
212 ptr += 2;
213 size -= 2;
214
215 CHECK(size >= length);
216
217 memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
218 memcpy(buffer->data() + buffer->size() + 4, ptr, length);
219 buffer->setRange(0, buffer->size() + 4 + length);
220
221 ptr += length;
222 size -= length;
223 }
224
225 buffer->meta()->setInt32("csd", true);
226 mCSD.push(buffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800227 } else if (meta->findData(kKeyESDS, &type, &data, &size)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800228 ESDS esds((const char *)data, size);
229 CHECK_EQ(esds.InitCheck(), (status_t)OK);
230
231 const void *codec_specific_data;
232 size_t codec_specific_data_size;
233 esds.getCodecSpecificInfo(
234 &codec_specific_data, &codec_specific_data_size);
235
236 sp<ABuffer> buffer = new ABuffer(codec_specific_data_size);
237
238 memcpy(buffer->data(), codec_specific_data,
239 codec_specific_data_size);
240
241 buffer->meta()->setInt32("csd", true);
242 mCSD.push(buffer);
Andreas Huberafed0e12011-09-20 15:39:58 -0700243 } else if (meta->findData(kKeyVorbisInfo, &type, &data, &size)) {
244 sp<ABuffer> buffer = new ABuffer(size);
245 memcpy(buffer->data(), data, size);
246
247 buffer->meta()->setInt32("csd", true);
248 mCSD.push(buffer);
249
250 CHECK(meta->findData(kKeyVorbisBooks, &type, &data, &size));
251
252 buffer = new ABuffer(size);
253 memcpy(buffer->data(), data, size);
254
255 buffer->meta()->setInt32("csd", true);
256 mCSD.push(buffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800257 }
258
Andreas Huberf9334412010-12-15 15:17:42 -0800259 return msg;
260}
261
262void NuPlayer::Decoder::onFillThisBuffer(const sp<AMessage> &msg) {
263 sp<AMessage> reply;
264 CHECK(msg->findMessage("reply", &reply));
265
266#if 0
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800267 sp<ABuffer> outBuffer;
268 CHECK(msg->findBuffer("buffer", &outBuffer));
Andreas Huberf9334412010-12-15 15:17:42 -0800269#else
270 sp<ABuffer> outBuffer;
271#endif
272
273 if (mCSDIndex < mCSD.size()) {
274 outBuffer = mCSD.editItemAt(mCSDIndex++);
275 outBuffer->meta()->setInt64("timeUs", 0);
276
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800277 reply->setBuffer("buffer", outBuffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800278 reply->post();
279 return;
280 }
281
282 sp<AMessage> notify = mNotify->dup();
283 notify->setMessage("codec-request", msg);
284 notify->post();
285}
286
287void NuPlayer::Decoder::signalFlush() {
288 if (mCodec != NULL) {
289 mCodec->signalFlush();
Andreas Huberf9334412010-12-15 15:17:42 -0800290 }
291}
292
293void NuPlayer::Decoder::signalResume() {
294 if (mCodec != NULL) {
295 mCodec->signalResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800296 }
297}
298
Andreas Huber3831a062010-12-21 10:22:33 -0800299void NuPlayer::Decoder::initiateShutdown() {
300 if (mCodec != NULL) {
301 mCodec->initiateShutdown();
Andreas Huber3831a062010-12-21 10:22:33 -0800302 }
303}
304
Andreas Huberf9334412010-12-15 15:17:42 -0800305} // namespace android
306