blob: 25974b678b989cfe6b4e8961a49069dab79b3744 [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
127 int32_t isADTS;
128 if (meta->findInt32(kKeyIsADTS, &isADTS) && isADTS != 0) {
129 msg->setInt32("is-adts", true);
130 }
Andreas Huberf9334412010-12-15 15:17:42 -0800131 }
132
Andreas Huber5bc087c2010-12-23 10:27:40 -0800133 int32_t maxInputSize;
134 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
135 msg->setInt32("max-input-size", maxInputSize);
136 }
137
138 mCSDIndex = 0;
139
Andreas Huberf9334412010-12-15 15:17:42 -0800140 uint32_t type;
141 const void *data;
142 size_t size;
143 if (meta->findData(kKeyAVCC, &type, &data, &size)) {
144 // Parse the AVCDecoderConfigurationRecord
145
146 const uint8_t *ptr = (const uint8_t *)data;
147
148 CHECK(size >= 7);
149 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1
150 uint8_t profile = ptr[1];
151 uint8_t level = ptr[3];
152
153 // There is decodable content out there that fails the following
154 // assertion, let's be lenient for now...
155 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
156
157 size_t lengthSize = 1 + (ptr[4] & 3);
158
159 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
160 // violates it...
161 // CHECK((ptr[5] >> 5) == 7); // reserved
162
163 size_t numSeqParameterSets = ptr[5] & 31;
164
165 ptr += 6;
166 size -= 6;
167
168 sp<ABuffer> buffer = new ABuffer(1024);
169 buffer->setRange(0, 0);
170
171 for (size_t i = 0; i < numSeqParameterSets; ++i) {
172 CHECK(size >= 2);
173 size_t length = U16_AT(ptr);
174
175 ptr += 2;
176 size -= 2;
177
178 CHECK(size >= length);
179
180 memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
181 memcpy(buffer->data() + buffer->size() + 4, ptr, length);
182 buffer->setRange(0, buffer->size() + 4 + length);
183
184 ptr += length;
185 size -= length;
186 }
187
188 buffer->meta()->setInt32("csd", true);
189 mCSD.push(buffer);
190
191 buffer = new ABuffer(1024);
192 buffer->setRange(0, 0);
193
194 CHECK(size >= 1);
195 size_t numPictureParameterSets = *ptr;
196 ++ptr;
197 --size;
198
199 for (size_t i = 0; i < numPictureParameterSets; ++i) {
200 CHECK(size >= 2);
201 size_t length = U16_AT(ptr);
202
203 ptr += 2;
204 size -= 2;
205
206 CHECK(size >= length);
207
208 memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
209 memcpy(buffer->data() + buffer->size() + 4, ptr, length);
210 buffer->setRange(0, buffer->size() + 4 + length);
211
212 ptr += length;
213 size -= length;
214 }
215
216 buffer->meta()->setInt32("csd", true);
217 mCSD.push(buffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800218 } else if (meta->findData(kKeyESDS, &type, &data, &size)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800219 ESDS esds((const char *)data, size);
220 CHECK_EQ(esds.InitCheck(), (status_t)OK);
221
222 const void *codec_specific_data;
223 size_t codec_specific_data_size;
224 esds.getCodecSpecificInfo(
225 &codec_specific_data, &codec_specific_data_size);
226
227 sp<ABuffer> buffer = new ABuffer(codec_specific_data_size);
228
229 memcpy(buffer->data(), codec_specific_data,
230 codec_specific_data_size);
231
232 buffer->meta()->setInt32("csd", true);
233 mCSD.push(buffer);
Andreas Huberafed0e12011-09-20 15:39:58 -0700234 } else if (meta->findData(kKeyVorbisInfo, &type, &data, &size)) {
235 sp<ABuffer> buffer = new ABuffer(size);
236 memcpy(buffer->data(), data, size);
237
238 buffer->meta()->setInt32("csd", true);
239 mCSD.push(buffer);
240
241 CHECK(meta->findData(kKeyVorbisBooks, &type, &data, &size));
242
243 buffer = new ABuffer(size);
244 memcpy(buffer->data(), data, size);
245
246 buffer->meta()->setInt32("csd", true);
247 mCSD.push(buffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800248 }
249
Andreas Huberf9334412010-12-15 15:17:42 -0800250 return msg;
251}
252
253void NuPlayer::Decoder::onFillThisBuffer(const sp<AMessage> &msg) {
254 sp<AMessage> reply;
255 CHECK(msg->findMessage("reply", &reply));
256
257#if 0
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800258 sp<ABuffer> outBuffer;
259 CHECK(msg->findBuffer("buffer", &outBuffer));
Andreas Huberf9334412010-12-15 15:17:42 -0800260#else
261 sp<ABuffer> outBuffer;
262#endif
263
264 if (mCSDIndex < mCSD.size()) {
265 outBuffer = mCSD.editItemAt(mCSDIndex++);
266 outBuffer->meta()->setInt64("timeUs", 0);
267
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800268 reply->setBuffer("buffer", outBuffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800269 reply->post();
270 return;
271 }
272
273 sp<AMessage> notify = mNotify->dup();
274 notify->setMessage("codec-request", msg);
275 notify->post();
276}
277
278void NuPlayer::Decoder::signalFlush() {
279 if (mCodec != NULL) {
280 mCodec->signalFlush();
Andreas Huberf9334412010-12-15 15:17:42 -0800281 }
282}
283
284void NuPlayer::Decoder::signalResume() {
285 if (mCodec != NULL) {
286 mCodec->signalResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800287 }
288}
289
Andreas Huber3831a062010-12-21 10:22:33 -0800290void NuPlayer::Decoder::initiateShutdown() {
291 if (mCodec != NULL) {
292 mCodec->initiateShutdown();
Andreas Huber3831a062010-12-21 10:22:33 -0800293 }
294}
295
Andreas Huberf9334412010-12-15 15:17:42 -0800296} // namespace android
297