blob: 2a518292b19b0a9fe273eb9aa6d463818cf90913 [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>
32#include <surfaceflinger/Surface.h>
Glenn Kasten11731182011-02-08 17:26:17 -080033#include <gui/ISurfaceTexture.h>
Andreas Huberf9334412010-12-15 15:17:42 -080034
35namespace android {
36
37NuPlayer::Decoder::Decoder(
Glenn Kasten11731182011-02-08 17:26:17 -080038 const sp<AMessage> &notify,
39 const sp<NativeWindowWrapper> &nativeWindow)
Andreas Huberf9334412010-12-15 15:17:42 -080040 : mNotify(notify),
Glenn Kasten11731182011-02-08 17:26:17 -080041 mNativeWindow(nativeWindow) {
Andreas Huberf9334412010-12-15 15:17:42 -080042}
43
44NuPlayer::Decoder::~Decoder() {
45}
46
Andreas Huber5bc087c2010-12-23 10:27:40 -080047void NuPlayer::Decoder::configure(const sp<MetaData> &meta) {
Andreas Huberf9334412010-12-15 15:17:42 -080048 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -080049
50 const char *mime;
51 CHECK(meta->findCString(kKeyMIMEType, &mime));
52
53 sp<AMessage> notifyMsg =
54 new AMessage(kWhatCodecNotify, id());
55
56 sp<AMessage> format = makeFormat(meta);
57
Glenn Kasten11731182011-02-08 17:26:17 -080058 if (mNativeWindow != NULL) {
59 format->setObject("native-window", mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -080060 }
61
Andreas Huber078cfcf2011-09-15 12:25:04 -070062 // Current video decoders do not return from OMX_FillThisBuffer
63 // quickly, violating the OpenMAX specs, until that is remedied
64 // we need to invest in an extra looper to free the main event
65 // queue.
66 bool needDedicatedLooper = !strncasecmp(mime, "video/", 6);
67
Andreas Huber00f49512011-05-11 14:15:13 -070068 mCodec = new ACodec;
Andreas Huber078cfcf2011-09-15 12:25:04 -070069
70 if (needDedicatedLooper && mCodecLooper == NULL) {
71 mCodecLooper = new ALooper;
72 mCodecLooper->setName("NuPlayerDecoder");
73 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
74 }
75
76 (needDedicatedLooper ? mCodecLooper : looper())->registerHandler(mCodec);
Andreas Huberf9334412010-12-15 15:17:42 -080077
Andreas Huber00f49512011-05-11 14:15:13 -070078 mCodec->setNotificationMessage(notifyMsg);
79 mCodec->initiateSetup(format);
Andreas Huberf9334412010-12-15 15:17:42 -080080}
81
82void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
83 switch (msg->what()) {
84 case kWhatCodecNotify:
85 {
86 int32_t what;
87 CHECK(msg->findInt32("what", &what));
88
89 if (what == ACodec::kWhatFillThisBuffer) {
90 onFillThisBuffer(msg);
91 } else {
92 sp<AMessage> notify = mNotify->dup();
93 notify->setMessage("codec-request", msg);
94 notify->post();
95 }
96 break;
97 }
98
99 default:
100 TRESPASS();
101 break;
102 }
103}
104
105sp<AMessage> NuPlayer::Decoder::makeFormat(const sp<MetaData> &meta) {
106 CHECK(mCSD.isEmpty());
107
108 const char *mime;
109 CHECK(meta->findCString(kKeyMIMEType, &mime));
110
111 sp<AMessage> msg = new AMessage;
112 msg->setString("mime", mime);
113
114 if (!strncasecmp("video/", mime, 6)) {
115 int32_t width, height;
116 CHECK(meta->findInt32(kKeyWidth, &width));
117 CHECK(meta->findInt32(kKeyHeight, &height));
118
119 msg->setInt32("width", width);
120 msg->setInt32("height", height);
121 } else {
122 CHECK(!strncasecmp("audio/", mime, 6));
123
124 int32_t numChannels, sampleRate;
125 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
126 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
127
128 msg->setInt32("channel-count", numChannels);
129 msg->setInt32("sample-rate", sampleRate);
130 }
131
Andreas Huber5bc087c2010-12-23 10:27:40 -0800132 int32_t maxInputSize;
133 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
134 msg->setInt32("max-input-size", maxInputSize);
135 }
136
137 mCSDIndex = 0;
138
Andreas Huberf9334412010-12-15 15:17:42 -0800139 uint32_t type;
140 const void *data;
141 size_t size;
142 if (meta->findData(kKeyAVCC, &type, &data, &size)) {
143 // Parse the AVCDecoderConfigurationRecord
144
145 const uint8_t *ptr = (const uint8_t *)data;
146
147 CHECK(size >= 7);
148 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1
149 uint8_t profile = ptr[1];
150 uint8_t level = ptr[3];
151
152 // There is decodable content out there that fails the following
153 // assertion, let's be lenient for now...
154 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
155
156 size_t lengthSize = 1 + (ptr[4] & 3);
157
158 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
159 // violates it...
160 // CHECK((ptr[5] >> 5) == 7); // reserved
161
162 size_t numSeqParameterSets = ptr[5] & 31;
163
164 ptr += 6;
165 size -= 6;
166
167 sp<ABuffer> buffer = new ABuffer(1024);
168 buffer->setRange(0, 0);
169
170 for (size_t i = 0; i < numSeqParameterSets; ++i) {
171 CHECK(size >= 2);
172 size_t length = U16_AT(ptr);
173
174 ptr += 2;
175 size -= 2;
176
177 CHECK(size >= length);
178
179 memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
180 memcpy(buffer->data() + buffer->size() + 4, ptr, length);
181 buffer->setRange(0, buffer->size() + 4 + length);
182
183 ptr += length;
184 size -= length;
185 }
186
187 buffer->meta()->setInt32("csd", true);
188 mCSD.push(buffer);
189
190 buffer = new ABuffer(1024);
191 buffer->setRange(0, 0);
192
193 CHECK(size >= 1);
194 size_t numPictureParameterSets = *ptr;
195 ++ptr;
196 --size;
197
198 for (size_t i = 0; i < numPictureParameterSets; ++i) {
199 CHECK(size >= 2);
200 size_t length = U16_AT(ptr);
201
202 ptr += 2;
203 size -= 2;
204
205 CHECK(size >= length);
206
207 memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
208 memcpy(buffer->data() + buffer->size() + 4, ptr, length);
209 buffer->setRange(0, buffer->size() + 4 + length);
210
211 ptr += length;
212 size -= length;
213 }
214
215 buffer->meta()->setInt32("csd", true);
216 mCSD.push(buffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800217 } else if (meta->findData(kKeyESDS, &type, &data, &size)) {
Andreas Huberf9334412010-12-15 15:17:42 -0800218 ESDS esds((const char *)data, size);
219 CHECK_EQ(esds.InitCheck(), (status_t)OK);
220
221 const void *codec_specific_data;
222 size_t codec_specific_data_size;
223 esds.getCodecSpecificInfo(
224 &codec_specific_data, &codec_specific_data_size);
225
226 sp<ABuffer> buffer = new ABuffer(codec_specific_data_size);
227
228 memcpy(buffer->data(), codec_specific_data,
229 codec_specific_data_size);
230
231 buffer->meta()->setInt32("csd", true);
232 mCSD.push(buffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800233 }
234
Andreas Huberf9334412010-12-15 15:17:42 -0800235 return msg;
236}
237
238void NuPlayer::Decoder::onFillThisBuffer(const sp<AMessage> &msg) {
239 sp<AMessage> reply;
240 CHECK(msg->findMessage("reply", &reply));
241
242#if 0
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800243 sp<ABuffer> outBuffer;
244 CHECK(msg->findBuffer("buffer", &outBuffer));
Andreas Huberf9334412010-12-15 15:17:42 -0800245#else
246 sp<ABuffer> outBuffer;
247#endif
248
249 if (mCSDIndex < mCSD.size()) {
250 outBuffer = mCSD.editItemAt(mCSDIndex++);
251 outBuffer->meta()->setInt64("timeUs", 0);
252
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800253 reply->setBuffer("buffer", outBuffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800254 reply->post();
255 return;
256 }
257
258 sp<AMessage> notify = mNotify->dup();
259 notify->setMessage("codec-request", msg);
260 notify->post();
261}
262
263void NuPlayer::Decoder::signalFlush() {
264 if (mCodec != NULL) {
265 mCodec->signalFlush();
Andreas Huberf9334412010-12-15 15:17:42 -0800266 }
267}
268
269void NuPlayer::Decoder::signalResume() {
270 if (mCodec != NULL) {
271 mCodec->signalResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800272 }
273}
274
Andreas Huber3831a062010-12-21 10:22:33 -0800275void NuPlayer::Decoder::initiateShutdown() {
276 if (mCodec != NULL) {
277 mCodec->initiateShutdown();
Andreas Huber3831a062010-12-21 10:22:33 -0800278 }
279}
280
Andreas Huberf9334412010-12-15 15:17:42 -0800281} // namespace android
282