blob: 460fc98393143a5197af849a68decac6ade4930d [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);
119 } else {
120 CHECK(!strncasecmp("audio/", mime, 6));
121
122 int32_t numChannels, sampleRate;
123 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
124 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
125
126 msg->setInt32("channel-count", numChannels);
127 msg->setInt32("sample-rate", sampleRate);
128 }
129
Andreas Huber5bc087c2010-12-23 10:27:40 -0800130 int32_t maxInputSize;
131 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
132 msg->setInt32("max-input-size", maxInputSize);
133 }
134
135 mCSDIndex = 0;
136
Andreas Huberf9334412010-12-15 15:17:42 -0800137 uint32_t type;
138 const void *data;
139 size_t size;
140 if (meta->findData(kKeyAVCC, &type, &data, &size)) {
141 // Parse the AVCDecoderConfigurationRecord
142
143 const uint8_t *ptr = (const uint8_t *)data;
144
145 CHECK(size >= 7);
146 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1
147 uint8_t profile = ptr[1];
148 uint8_t level = ptr[3];
149
150 // There is decodable content out there that fails the following
151 // assertion, let's be lenient for now...
152 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
153
154 size_t lengthSize = 1 + (ptr[4] & 3);
155
156 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
157 // violates it...
158 // CHECK((ptr[5] >> 5) == 7); // reserved
159
160 size_t numSeqParameterSets = ptr[5] & 31;
161
162 ptr += 6;
163 size -= 6;
164
165 sp<ABuffer> buffer = new ABuffer(1024);
166 buffer->setRange(0, 0);
167
168 for (size_t i = 0; i < numSeqParameterSets; ++i) {
169 CHECK(size >= 2);
170 size_t length = U16_AT(ptr);
171
172 ptr += 2;
173 size -= 2;
174
175 CHECK(size >= length);
176
177 memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
178 memcpy(buffer->data() + buffer->size() + 4, ptr, length);
179 buffer->setRange(0, buffer->size() + 4 + length);
180
181 ptr += length;
182 size -= length;
183 }
184
185 buffer->meta()->setInt32("csd", true);
186 mCSD.push(buffer);
187
188 buffer = new ABuffer(1024);
189 buffer->setRange(0, 0);
190
191 CHECK(size >= 1);
192 size_t numPictureParameterSets = *ptr;
193 ++ptr;
194 --size;
195
196 for (size_t i = 0; i < numPictureParameterSets; ++i) {
197 CHECK(size >= 2);
198 size_t length = U16_AT(ptr);
199
200 ptr += 2;
201 size -= 2;
202
203 CHECK(size >= length);
204
205 memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
206 memcpy(buffer->data() + buffer->size() + 4, ptr, length);
207 buffer->setRange(0, buffer->size() + 4 + length);
208
209 ptr += length;
210 size -= length;
211 }
212
213 buffer->meta()->setInt32("csd", true);
214 mCSD.push(buffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800215 } else if (meta->findData(kKeyESDS, &type, &data, &size)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800216 ESDS esds((const char *)data, size);
217 CHECK_EQ(esds.InitCheck(), (status_t)OK);
218
219 const void *codec_specific_data;
220 size_t codec_specific_data_size;
221 esds.getCodecSpecificInfo(
222 &codec_specific_data, &codec_specific_data_size);
223
224 sp<ABuffer> buffer = new ABuffer(codec_specific_data_size);
225
226 memcpy(buffer->data(), codec_specific_data,
227 codec_specific_data_size);
228
229 buffer->meta()->setInt32("csd", true);
230 mCSD.push(buffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800231 }
232
Andreas Huberf9334412010-12-15 15:17:42 -0800233 return msg;
234}
235
236void NuPlayer::Decoder::onFillThisBuffer(const sp<AMessage> &msg) {
237 sp<AMessage> reply;
238 CHECK(msg->findMessage("reply", &reply));
239
240#if 0
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800241 sp<ABuffer> outBuffer;
242 CHECK(msg->findBuffer("buffer", &outBuffer));
Andreas Huberf9334412010-12-15 15:17:42 -0800243#else
244 sp<ABuffer> outBuffer;
245#endif
246
247 if (mCSDIndex < mCSD.size()) {
248 outBuffer = mCSD.editItemAt(mCSDIndex++);
249 outBuffer->meta()->setInt64("timeUs", 0);
250
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800251 reply->setBuffer("buffer", outBuffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800252 reply->post();
253 return;
254 }
255
256 sp<AMessage> notify = mNotify->dup();
257 notify->setMessage("codec-request", msg);
258 notify->post();
259}
260
261void NuPlayer::Decoder::signalFlush() {
262 if (mCodec != NULL) {
263 mCodec->signalFlush();
Andreas Huberf9334412010-12-15 15:17:42 -0800264 }
265}
266
267void NuPlayer::Decoder::signalResume() {
268 if (mCodec != NULL) {
269 mCodec->signalResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800270 }
271}
272
Andreas Huber3831a062010-12-21 10:22:33 -0800273void NuPlayer::Decoder::initiateShutdown() {
274 if (mCodec != NULL) {
275 mCodec->initiateShutdown();
Andreas Huber3831a062010-12-21 10:22:33 -0800276 }
277}
278
Andreas Huberf9334412010-12-15 15:17:42 -0800279} // namespace android
280