1 /*
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 "Utils"
19 #include <utils/Log.h>
20 #include <ctype.h>
21
22 #include "include/ESDS.h"
23
24 #include <arpa/inet.h>
25 #include <cutils/properties.h>
26 #include <media/openmax/OMX_Audio.h>
27 #include <media/stagefright/foundation/ABuffer.h>
28 #include <media/stagefright/foundation/ADebug.h>
29 #include <media/stagefright/foundation/AMessage.h>
30 #include <media/stagefright/MetaData.h>
31 #include <media/stagefright/MediaDefs.h>
32 #include <media/AudioSystem.h>
33 #include <media/MediaPlayerInterface.h>
34 #include <hardware/audio.h>
35 #include <media/stagefright/Utils.h>
36 #include <media/AudioParameter.h>
37
38 namespace android {
39
U16_AT(const uint8_t * ptr)40 uint16_t U16_AT(const uint8_t *ptr) {
41 return ptr[0] << 8 | ptr[1];
42 }
43
U32_AT(const uint8_t * ptr)44 uint32_t U32_AT(const uint8_t *ptr) {
45 return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
46 }
47
U64_AT(const uint8_t * ptr)48 uint64_t U64_AT(const uint8_t *ptr) {
49 return ((uint64_t)U32_AT(ptr)) << 32 | U32_AT(ptr + 4);
50 }
51
U16LE_AT(const uint8_t * ptr)52 uint16_t U16LE_AT(const uint8_t *ptr) {
53 return ptr[0] | (ptr[1] << 8);
54 }
55
U32LE_AT(const uint8_t * ptr)56 uint32_t U32LE_AT(const uint8_t *ptr) {
57 return ptr[3] << 24 | ptr[2] << 16 | ptr[1] << 8 | ptr[0];
58 }
59
U64LE_AT(const uint8_t * ptr)60 uint64_t U64LE_AT(const uint8_t *ptr) {
61 return ((uint64_t)U32LE_AT(ptr + 4)) << 32 | U32LE_AT(ptr);
62 }
63
64 // XXX warning: these won't work on big-endian host.
ntoh64(uint64_t x)65 uint64_t ntoh64(uint64_t x) {
66 return ((uint64_t)ntohl(x & 0xffffffff) << 32) | ntohl(x >> 32);
67 }
68
hton64(uint64_t x)69 uint64_t hton64(uint64_t x) {
70 return ((uint64_t)htonl(x & 0xffffffff) << 32) | htonl(x >> 32);
71 }
72
copyNALUToABuffer(sp<ABuffer> * buffer,const uint8_t * ptr,size_t length)73 static status_t copyNALUToABuffer(sp<ABuffer> *buffer, const uint8_t *ptr, size_t length) {
74 if (((*buffer)->size() + 4 + length) > ((*buffer)->capacity() - (*buffer)->offset())) {
75 sp<ABuffer> tmpBuffer = new (std::nothrow) ABuffer((*buffer)->size() + 4 + length + 1024);
76 if (tmpBuffer.get() == NULL || tmpBuffer->base() == NULL) {
77 return NO_MEMORY;
78 }
79 memcpy(tmpBuffer->data(), (*buffer)->data(), (*buffer)->size());
80 tmpBuffer->setRange(0, (*buffer)->size());
81 (*buffer) = tmpBuffer;
82 }
83
84 memcpy((*buffer)->data() + (*buffer)->size(), "\x00\x00\x00\x01", 4);
85 memcpy((*buffer)->data() + (*buffer)->size() + 4, ptr, length);
86 (*buffer)->setRange((*buffer)->offset(), (*buffer)->size() + 4 + length);
87 return OK;
88 }
89
convertMetaDataToMessage(const sp<MetaData> & meta,sp<AMessage> * format)90 status_t convertMetaDataToMessage(
91 const sp<MetaData> &meta, sp<AMessage> *format) {
92 format->clear();
93
94 const char *mime;
95 CHECK(meta->findCString(kKeyMIMEType, &mime));
96
97 sp<AMessage> msg = new AMessage;
98 msg->setString("mime", mime);
99
100 int64_t durationUs;
101 if (meta->findInt64(kKeyDuration, &durationUs)) {
102 msg->setInt64("durationUs", durationUs);
103 }
104
105 int avgBitRate;
106 if (meta->findInt32(kKeyBitRate, &avgBitRate)) {
107 msg->setInt32("bit-rate", avgBitRate);
108 }
109
110 int32_t isSync;
111 if (meta->findInt32(kKeyIsSyncFrame, &isSync) && isSync != 0) {
112 msg->setInt32("is-sync-frame", 1);
113 }
114
115 if (!strncasecmp("video/", mime, 6)) {
116 int32_t width, height;
117 CHECK(meta->findInt32(kKeyWidth, &width));
118 CHECK(meta->findInt32(kKeyHeight, &height));
119
120 msg->setInt32("width", width);
121 msg->setInt32("height", height);
122
123 int32_t sarWidth, sarHeight;
124 if (meta->findInt32(kKeySARWidth, &sarWidth)
125 && meta->findInt32(kKeySARHeight, &sarHeight)) {
126 msg->setInt32("sar-width", sarWidth);
127 msg->setInt32("sar-height", sarHeight);
128 }
129
130 int32_t colorFormat;
131 if (meta->findInt32(kKeyColorFormat, &colorFormat)) {
132 msg->setInt32("color-format", colorFormat);
133 }
134
135 int32_t cropLeft, cropTop, cropRight, cropBottom;
136 if (meta->findRect(kKeyCropRect,
137 &cropLeft,
138 &cropTop,
139 &cropRight,
140 &cropBottom)) {
141 msg->setRect("crop", cropLeft, cropTop, cropRight, cropBottom);
142 }
143
144 int32_t rotationDegrees;
145 if (meta->findInt32(kKeyRotation, &rotationDegrees)) {
146 msg->setInt32("rotation-degrees", rotationDegrees);
147 }
148 } else if (!strncasecmp("audio/", mime, 6)) {
149 int32_t numChannels, sampleRate;
150 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
151 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
152
153 msg->setInt32("channel-count", numChannels);
154 msg->setInt32("sample-rate", sampleRate);
155
156 int32_t channelMask;
157 if (meta->findInt32(kKeyChannelMask, &channelMask)) {
158 msg->setInt32("channel-mask", channelMask);
159 }
160
161 int32_t delay = 0;
162 if (meta->findInt32(kKeyEncoderDelay, &delay)) {
163 msg->setInt32("encoder-delay", delay);
164 }
165 int32_t padding = 0;
166 if (meta->findInt32(kKeyEncoderPadding, &padding)) {
167 msg->setInt32("encoder-padding", padding);
168 }
169
170 int32_t isADTS;
171 if (meta->findInt32(kKeyIsADTS, &isADTS)) {
172 msg->setInt32("is-adts", true);
173 }
174
175 int32_t aacProfile = -1;
176 if (meta->findInt32(kKeyAACAOT, &aacProfile)) {
177 msg->setInt32("aac-profile", aacProfile);
178 }
179 }
180
181 int32_t maxInputSize;
182 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
183 msg->setInt32("max-input-size", maxInputSize);
184 }
185
186 int32_t maxWidth;
187 if (meta->findInt32(kKeyMaxWidth, &maxWidth)) {
188 msg->setInt32("max-width", maxWidth);
189 }
190
191 int32_t maxHeight;
192 if (meta->findInt32(kKeyMaxHeight, &maxHeight)) {
193 msg->setInt32("max-height", maxHeight);
194 }
195
196 int32_t rotationDegrees;
197 if (meta->findInt32(kKeyRotation, &rotationDegrees)) {
198 msg->setInt32("rotation-degrees", rotationDegrees);
199 }
200
201 int32_t fps;
202 if (meta->findInt32(kKeyFrameRate, &fps) && fps > 0) {
203 msg->setInt32("frame-rate", fps);
204 }
205
206 uint32_t type;
207 const void *data;
208 size_t size;
209 if (meta->findData(kKeyAVCC, &type, &data, &size)) {
210 // Parse the AVCDecoderConfigurationRecord
211
212 const uint8_t *ptr = (const uint8_t *)data;
213
214 if (size < 7 || ptr[0] != 1) { // configurationVersion == 1
215 ALOGE("b/23680780");
216 return BAD_VALUE;
217 }
218 uint8_t profile __unused = ptr[1];
219 uint8_t level __unused = ptr[3];
220
221 // There is decodable content out there that fails the following
222 // assertion, let's be lenient for now...
223 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
224
225 size_t lengthSize __unused = 1 + (ptr[4] & 3);
226
227 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
228 // violates it...
229 // CHECK((ptr[5] >> 5) == 7); // reserved
230
231 size_t numSeqParameterSets = ptr[5] & 31;
232
233 ptr += 6;
234 size -= 6;
235
236 sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024);
237 if (buffer.get() == NULL || buffer->base() == NULL) {
238 return NO_MEMORY;
239 }
240 buffer->setRange(0, 0);
241
242 for (size_t i = 0; i < numSeqParameterSets; ++i) {
243 if (size < 2) {
244 ALOGE("b/23680780");
245 return BAD_VALUE;
246 }
247 size_t length = U16_AT(ptr);
248
249 ptr += 2;
250 size -= 2;
251
252 if (size < length) {
253 return BAD_VALUE;
254 }
255 status_t err = copyNALUToABuffer(&buffer, ptr, length);
256 if (err != OK) {
257 return err;
258 }
259
260 ptr += length;
261 size -= length;
262 }
263
264 buffer->meta()->setInt32("csd", true);
265 buffer->meta()->setInt64("timeUs", 0);
266
267 msg->setBuffer("csd-0", buffer);
268
269 buffer = new (std::nothrow) ABuffer(1024);
270 if (buffer.get() == NULL || buffer->base() == NULL) {
271 return NO_MEMORY;
272 }
273 buffer->setRange(0, 0);
274
275 if (size < 1) {
276 ALOGE("b/23680780");
277 return BAD_VALUE;
278 }
279 size_t numPictureParameterSets = *ptr;
280 ++ptr;
281 --size;
282
283 for (size_t i = 0; i < numPictureParameterSets; ++i) {
284 if (size < 2) {
285 ALOGE("b/23680780");
286 return BAD_VALUE;
287 }
288 size_t length = U16_AT(ptr);
289
290 ptr += 2;
291 size -= 2;
292
293 if (size < length) {
294 return BAD_VALUE;
295 }
296 status_t err = copyNALUToABuffer(&buffer, ptr, length);
297 if (err != OK) {
298 return err;
299 }
300
301 ptr += length;
302 size -= length;
303 }
304
305 buffer->meta()->setInt32("csd", true);
306 buffer->meta()->setInt64("timeUs", 0);
307 msg->setBuffer("csd-1", buffer);
308 } else if (meta->findData(kKeyHVCC, &type, &data, &size)) {
309 const uint8_t *ptr = (const uint8_t *)data;
310
311 if (size < 23 || ptr[0] != 1) { // configurationVersion == 1
312 ALOGE("b/23680780");
313 return BAD_VALUE;
314 }
315 uint8_t profile __unused = ptr[1] & 31;
316 uint8_t level __unused = ptr[12];
317 ptr += 22;
318 size -= 22;
319
320
321 size_t numofArrays = (char)ptr[0];
322 ptr += 1;
323 size -= 1;
324 size_t j = 0, i = 0;
325
326 sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024);
327 if (buffer.get() == NULL || buffer->base() == NULL) {
328 return NO_MEMORY;
329 }
330 buffer->setRange(0, 0);
331
332 for (i = 0; i < numofArrays; i++) {
333 if (size < 3) {
334 ALOGE("b/23680780");
335 return BAD_VALUE;
336 }
337 ptr += 1;
338 size -= 1;
339
340 //Num of nals
341 size_t numofNals = U16_AT(ptr);
342
343 ptr += 2;
344 size -= 2;
345
346 for (j = 0; j < numofNals; j++) {
347 if (size < 2) {
348 ALOGE("b/23680780");
349 return BAD_VALUE;
350 }
351 size_t length = U16_AT(ptr);
352
353 ptr += 2;
354 size -= 2;
355
356 if (size < length) {
357 return BAD_VALUE;
358 }
359 status_t err = copyNALUToABuffer(&buffer, ptr, length);
360 if (err != OK) {
361 return err;
362 }
363
364 ptr += length;
365 size -= length;
366 }
367 }
368 buffer->meta()->setInt32("csd", true);
369 buffer->meta()->setInt64("timeUs", 0);
370 msg->setBuffer("csd-0", buffer);
371
372 } else if (meta->findData(kKeyESDS, &type, &data, &size)) {
373 ESDS esds((const char *)data, size);
374 CHECK_EQ(esds.InitCheck(), (status_t)OK);
375
376 const void *codec_specific_data;
377 size_t codec_specific_data_size;
378 esds.getCodecSpecificInfo(
379 &codec_specific_data, &codec_specific_data_size);
380
381 sp<ABuffer> buffer = new (std::nothrow) ABuffer(codec_specific_data_size);
382 if (buffer.get() == NULL || buffer->base() == NULL) {
383 return NO_MEMORY;
384 }
385
386 memcpy(buffer->data(), codec_specific_data,
387 codec_specific_data_size);
388
389 buffer->meta()->setInt32("csd", true);
390 buffer->meta()->setInt64("timeUs", 0);
391 msg->setBuffer("csd-0", buffer);
392 } else if (meta->findData(kKeyVorbisInfo, &type, &data, &size)) {
393 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
394 if (buffer.get() == NULL || buffer->base() == NULL) {
395 return NO_MEMORY;
396 }
397 memcpy(buffer->data(), data, size);
398
399 buffer->meta()->setInt32("csd", true);
400 buffer->meta()->setInt64("timeUs", 0);
401 msg->setBuffer("csd-0", buffer);
402
403 if (!meta->findData(kKeyVorbisBooks, &type, &data, &size)) {
404 return -EINVAL;
405 }
406
407 buffer = new (std::nothrow) ABuffer(size);
408 if (buffer.get() == NULL || buffer->base() == NULL) {
409 return NO_MEMORY;
410 }
411 memcpy(buffer->data(), data, size);
412
413 buffer->meta()->setInt32("csd", true);
414 buffer->meta()->setInt64("timeUs", 0);
415 msg->setBuffer("csd-1", buffer);
416 } else if (meta->findData(kKeyOpusHeader, &type, &data, &size)) {
417 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
418 if (buffer.get() == NULL || buffer->base() == NULL) {
419 return NO_MEMORY;
420 }
421 memcpy(buffer->data(), data, size);
422
423 buffer->meta()->setInt32("csd", true);
424 buffer->meta()->setInt64("timeUs", 0);
425 msg->setBuffer("csd-0", buffer);
426
427 if (!meta->findData(kKeyOpusCodecDelay, &type, &data, &size)) {
428 return -EINVAL;
429 }
430
431 buffer = new (std::nothrow) ABuffer(size);
432 if (buffer.get() == NULL || buffer->base() == NULL) {
433 return NO_MEMORY;
434 }
435 memcpy(buffer->data(), data, size);
436
437 buffer->meta()->setInt32("csd", true);
438 buffer->meta()->setInt64("timeUs", 0);
439 msg->setBuffer("csd-1", buffer);
440
441 if (!meta->findData(kKeyOpusSeekPreRoll, &type, &data, &size)) {
442 return -EINVAL;
443 }
444
445 buffer = new (std::nothrow) ABuffer(size);
446 if (buffer.get() == NULL || buffer->base() == NULL) {
447 return NO_MEMORY;
448 }
449 memcpy(buffer->data(), data, size);
450
451 buffer->meta()->setInt32("csd", true);
452 buffer->meta()->setInt64("timeUs", 0);
453 msg->setBuffer("csd-2", buffer);
454 }
455
456 *format = msg;
457
458 return OK;
459 }
460
reassembleAVCC(const sp<ABuffer> & csd0,const sp<ABuffer> csd1,char * avcc)461 static size_t reassembleAVCC(const sp<ABuffer> &csd0, const sp<ABuffer> csd1, char *avcc) {
462
463 avcc[0] = 1; // version
464 avcc[1] = 0x64; // profile
465 avcc[2] = 0; // unused (?)
466 avcc[3] = 0xd; // level
467 avcc[4] = 0xff; // reserved+size
468
469 size_t i = 0;
470 int numparams = 0;
471 int lastparamoffset = 0;
472 int avccidx = 6;
473 do {
474 if (i >= csd0->size() - 4 ||
475 memcmp(csd0->data() + i, "\x00\x00\x00\x01", 4) == 0) {
476 if (i >= csd0->size() - 4) {
477 // there can't be another param here, so use all the rest
478 i = csd0->size();
479 }
480 ALOGV("block at %zu, last was %d", i, lastparamoffset);
481 if (lastparamoffset > 0) {
482 int size = i - lastparamoffset;
483 avcc[avccidx++] = size >> 8;
484 avcc[avccidx++] = size & 0xff;
485 memcpy(avcc+avccidx, csd0->data() + lastparamoffset, size);
486 avccidx += size;
487 numparams++;
488 }
489 i += 4;
490 lastparamoffset = i;
491 } else {
492 i++;
493 }
494 } while(i < csd0->size());
495 ALOGV("csd0 contains %d params", numparams);
496
497 avcc[5] = 0xe0 | numparams;
498 //and now csd-1
499 i = 0;
500 numparams = 0;
501 lastparamoffset = 0;
502 int numpicparamsoffset = avccidx;
503 avccidx++;
504 do {
505 if (i >= csd1->size() - 4 ||
506 memcmp(csd1->data() + i, "\x00\x00\x00\x01", 4) == 0) {
507 if (i >= csd1->size() - 4) {
508 // there can't be another param here, so use all the rest
509 i = csd1->size();
510 }
511 ALOGV("block at %zu, last was %d", i, lastparamoffset);
512 if (lastparamoffset > 0) {
513 int size = i - lastparamoffset;
514 avcc[avccidx++] = size >> 8;
515 avcc[avccidx++] = size & 0xff;
516 memcpy(avcc+avccidx, csd1->data() + lastparamoffset, size);
517 avccidx += size;
518 numparams++;
519 }
520 i += 4;
521 lastparamoffset = i;
522 } else {
523 i++;
524 }
525 } while(i < csd1->size());
526 avcc[numpicparamsoffset] = numparams;
527 return avccidx;
528 }
529
reassembleESDS(const sp<ABuffer> & csd0,char * esds)530 static void reassembleESDS(const sp<ABuffer> &csd0, char *esds) {
531 int csd0size = csd0->size();
532 esds[0] = 3; // kTag_ESDescriptor;
533 int esdescriptorsize = 26 + csd0size;
534 CHECK(esdescriptorsize < 268435456); // 7 bits per byte, so max is 2^28-1
535 esds[1] = 0x80 | (esdescriptorsize >> 21);
536 esds[2] = 0x80 | ((esdescriptorsize >> 14) & 0x7f);
537 esds[3] = 0x80 | ((esdescriptorsize >> 7) & 0x7f);
538 esds[4] = (esdescriptorsize & 0x7f);
539 esds[5] = esds[6] = 0; // es id
540 esds[7] = 0; // flags
541 esds[8] = 4; // kTag_DecoderConfigDescriptor
542 int configdescriptorsize = 18 + csd0size;
543 esds[9] = 0x80 | (configdescriptorsize >> 21);
544 esds[10] = 0x80 | ((configdescriptorsize >> 14) & 0x7f);
545 esds[11] = 0x80 | ((configdescriptorsize >> 7) & 0x7f);
546 esds[12] = (configdescriptorsize & 0x7f);
547 esds[13] = 0x40; // objectTypeIndication
548 esds[14] = 0x15; // not sure what 14-25 mean, they are ignored by ESDS.cpp,
549 esds[15] = 0x00; // but the actual values here were taken from a real file.
550 esds[16] = 0x18;
551 esds[17] = 0x00;
552 esds[18] = 0x00;
553 esds[19] = 0x00;
554 esds[20] = 0xfa;
555 esds[21] = 0x00;
556 esds[22] = 0x00;
557 esds[23] = 0x00;
558 esds[24] = 0xfa;
559 esds[25] = 0x00;
560 esds[26] = 5; // kTag_DecoderSpecificInfo;
561 esds[27] = 0x80 | (csd0size >> 21);
562 esds[28] = 0x80 | ((csd0size >> 14) & 0x7f);
563 esds[29] = 0x80 | ((csd0size >> 7) & 0x7f);
564 esds[30] = (csd0size & 0x7f);
565 memcpy((void*)&esds[31], csd0->data(), csd0size);
566 // data following this is ignored, so don't bother appending it
567
568 }
569
convertMessageToMetaData(const sp<AMessage> & msg,sp<MetaData> & meta)570 void convertMessageToMetaData(const sp<AMessage> &msg, sp<MetaData> &meta) {
571 AString mime;
572 if (msg->findString("mime", &mime)) {
573 meta->setCString(kKeyMIMEType, mime.c_str());
574 } else {
575 ALOGW("did not find mime type");
576 }
577
578 int64_t durationUs;
579 if (msg->findInt64("durationUs", &durationUs)) {
580 meta->setInt64(kKeyDuration, durationUs);
581 }
582
583 int32_t isSync;
584 if (msg->findInt32("is-sync-frame", &isSync) && isSync != 0) {
585 meta->setInt32(kKeyIsSyncFrame, 1);
586 }
587
588 if (mime.startsWith("video/")) {
589 int32_t width;
590 int32_t height;
591 if (msg->findInt32("width", &width) && msg->findInt32("height", &height)) {
592 meta->setInt32(kKeyWidth, width);
593 meta->setInt32(kKeyHeight, height);
594 } else {
595 ALOGW("did not find width and/or height");
596 }
597
598 int32_t sarWidth, sarHeight;
599 if (msg->findInt32("sar-width", &sarWidth)
600 && msg->findInt32("sar-height", &sarHeight)) {
601 meta->setInt32(kKeySARWidth, sarWidth);
602 meta->setInt32(kKeySARHeight, sarHeight);
603 }
604
605 int32_t colorFormat;
606 if (msg->findInt32("color-format", &colorFormat)) {
607 meta->setInt32(kKeyColorFormat, colorFormat);
608 }
609
610 int32_t cropLeft, cropTop, cropRight, cropBottom;
611 if (msg->findRect("crop",
612 &cropLeft,
613 &cropTop,
614 &cropRight,
615 &cropBottom)) {
616 meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom);
617 }
618
619 int32_t rotationDegrees;
620 if (msg->findInt32("rotation-degrees", &rotationDegrees)) {
621 meta->setInt32(kKeyRotation, rotationDegrees);
622 }
623 } else if (mime.startsWith("audio/")) {
624 int32_t numChannels;
625 if (msg->findInt32("channel-count", &numChannels)) {
626 meta->setInt32(kKeyChannelCount, numChannels);
627 }
628 int32_t sampleRate;
629 if (msg->findInt32("sample-rate", &sampleRate)) {
630 meta->setInt32(kKeySampleRate, sampleRate);
631 }
632 int32_t channelMask;
633 if (msg->findInt32("channel-mask", &channelMask)) {
634 meta->setInt32(kKeyChannelMask, channelMask);
635 }
636 int32_t delay = 0;
637 if (msg->findInt32("encoder-delay", &delay)) {
638 meta->setInt32(kKeyEncoderDelay, delay);
639 }
640 int32_t padding = 0;
641 if (msg->findInt32("encoder-padding", &padding)) {
642 meta->setInt32(kKeyEncoderPadding, padding);
643 }
644
645 int32_t isADTS;
646 if (msg->findInt32("is-adts", &isADTS)) {
647 meta->setInt32(kKeyIsADTS, isADTS);
648 }
649 }
650
651 int32_t maxInputSize;
652 if (msg->findInt32("max-input-size", &maxInputSize)) {
653 meta->setInt32(kKeyMaxInputSize, maxInputSize);
654 }
655
656 int32_t maxWidth;
657 if (msg->findInt32("max-width", &maxWidth)) {
658 meta->setInt32(kKeyMaxWidth, maxWidth);
659 }
660
661 int32_t maxHeight;
662 if (msg->findInt32("max-height", &maxHeight)) {
663 meta->setInt32(kKeyMaxHeight, maxHeight);
664 }
665
666 int32_t fps;
667 if (msg->findInt32("frame-rate", &fps) && fps > 0) {
668 meta->setInt32(kKeyFrameRate, fps);
669 }
670
671 // reassemble the csd data into its original form
672 sp<ABuffer> csd0;
673 if (msg->findBuffer("csd-0", &csd0)) {
674 int csd0size = csd0->size();
675 if (mime == MEDIA_MIMETYPE_VIDEO_AVC) {
676 sp<ABuffer> csd1;
677 if (msg->findBuffer("csd-1", &csd1)) {
678 Vector<char> avcc;
679 int avccSize = csd0size + csd1->size() + 1024;
680 if (avcc.resize(avccSize) < 0) {
681 ALOGE("error allocating avcc (size %d); abort setting avcc.", avccSize);
682 } else {
683 size_t outsize = reassembleAVCC(csd0, csd1, avcc.editArray());
684 meta->setData(kKeyAVCC, kKeyAVCC, avcc.array(), outsize);
685 }
686 }
687 } else if (mime == MEDIA_MIMETYPE_AUDIO_AAC || mime == MEDIA_MIMETYPE_VIDEO_MPEG4) {
688 Vector<char> esds;
689 int esdsSize = csd0size + 31;
690 if (esds.resize(esdsSize) < 0) {
691 ALOGE("error allocating esds (size %d); abort setting esds.", esdsSize);
692 } else {
693 // The written ESDS is actually for an audio stream, but it's enough
694 // for transporting the CSD to muxers.
695 reassembleESDS(csd0, esds.editArray());
696 meta->setData(kKeyESDS, kKeyESDS, esds.array(), esds.size());
697 }
698 }
699 }
700
701 int32_t timeScale;
702 if (msg->findInt32("time-scale", &timeScale)) {
703 meta->setInt32(kKeyTimeScale, timeScale);
704 }
705
706 // XXX TODO add whatever other keys there are
707
708 #if 0
709 ALOGI("converted %s to:", msg->debugString(0).c_str());
710 meta->dumpToLog();
711 #endif
712 }
713
MakeUserAgent()714 AString MakeUserAgent() {
715 AString ua;
716 ua.append("stagefright/1.2 (Linux;Android ");
717
718 #if (PROPERTY_VALUE_MAX < 8)
719 #error "PROPERTY_VALUE_MAX must be at least 8"
720 #endif
721
722 char value[PROPERTY_VALUE_MAX];
723 property_get("ro.build.version.release", value, "Unknown");
724 ua.append(value);
725 ua.append(")");
726
727 return ua;
728 }
729
sendMetaDataToHal(sp<MediaPlayerBase::AudioSink> & sink,const sp<MetaData> & meta)730 status_t sendMetaDataToHal(sp<MediaPlayerBase::AudioSink>& sink,
731 const sp<MetaData>& meta)
732 {
733 int32_t sampleRate = 0;
734 int32_t bitRate = 0;
735 int32_t channelMask = 0;
736 int32_t delaySamples = 0;
737 int32_t paddingSamples = 0;
738
739 AudioParameter param = AudioParameter();
740
741 if (meta->findInt32(kKeySampleRate, &sampleRate)) {
742 param.addInt(String8(AUDIO_OFFLOAD_CODEC_SAMPLE_RATE), sampleRate);
743 }
744 if (meta->findInt32(kKeyChannelMask, &channelMask)) {
745 param.addInt(String8(AUDIO_OFFLOAD_CODEC_NUM_CHANNEL), channelMask);
746 }
747 if (meta->findInt32(kKeyBitRate, &bitRate)) {
748 param.addInt(String8(AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE), bitRate);
749 }
750 if (meta->findInt32(kKeyEncoderDelay, &delaySamples)) {
751 param.addInt(String8(AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES), delaySamples);
752 }
753 if (meta->findInt32(kKeyEncoderPadding, &paddingSamples)) {
754 param.addInt(String8(AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES), paddingSamples);
755 }
756
757 ALOGV("sendMetaDataToHal: bitRate %d, sampleRate %d, chanMask %d,"
758 "delaySample %d, paddingSample %d", bitRate, sampleRate,
759 channelMask, delaySamples, paddingSamples);
760
761 sink->setParameters(param.toString());
762 return OK;
763 }
764
765 struct mime_conv_t {
766 const char* mime;
767 audio_format_t format;
768 };
769
770 static const struct mime_conv_t mimeLookup[] = {
771 { MEDIA_MIMETYPE_AUDIO_MPEG, AUDIO_FORMAT_MP3 },
772 { MEDIA_MIMETYPE_AUDIO_RAW, AUDIO_FORMAT_PCM_16_BIT },
773 { MEDIA_MIMETYPE_AUDIO_AMR_NB, AUDIO_FORMAT_AMR_NB },
774 { MEDIA_MIMETYPE_AUDIO_AMR_WB, AUDIO_FORMAT_AMR_WB },
775 { MEDIA_MIMETYPE_AUDIO_AAC, AUDIO_FORMAT_AAC },
776 { MEDIA_MIMETYPE_AUDIO_VORBIS, AUDIO_FORMAT_VORBIS },
777 { MEDIA_MIMETYPE_AUDIO_OPUS, AUDIO_FORMAT_OPUS},
778 { 0, AUDIO_FORMAT_INVALID }
779 };
780
mapMimeToAudioFormat(audio_format_t & format,const char * mime)781 status_t mapMimeToAudioFormat( audio_format_t& format, const char* mime )
782 {
783 const struct mime_conv_t* p = &mimeLookup[0];
784 while (p->mime != NULL) {
785 if (0 == strcasecmp(mime, p->mime)) {
786 format = p->format;
787 return OK;
788 }
789 ++p;
790 }
791
792 return BAD_VALUE;
793 }
794
795 struct aac_format_conv_t {
796 OMX_AUDIO_AACPROFILETYPE eAacProfileType;
797 audio_format_t format;
798 };
799
800 static const struct aac_format_conv_t profileLookup[] = {
801 { OMX_AUDIO_AACObjectMain, AUDIO_FORMAT_AAC_MAIN},
802 { OMX_AUDIO_AACObjectLC, AUDIO_FORMAT_AAC_LC},
803 { OMX_AUDIO_AACObjectSSR, AUDIO_FORMAT_AAC_SSR},
804 { OMX_AUDIO_AACObjectLTP, AUDIO_FORMAT_AAC_LTP},
805 { OMX_AUDIO_AACObjectHE, AUDIO_FORMAT_AAC_HE_V1},
806 { OMX_AUDIO_AACObjectScalable, AUDIO_FORMAT_AAC_SCALABLE},
807 { OMX_AUDIO_AACObjectERLC, AUDIO_FORMAT_AAC_ERLC},
808 { OMX_AUDIO_AACObjectLD, AUDIO_FORMAT_AAC_LD},
809 { OMX_AUDIO_AACObjectHE_PS, AUDIO_FORMAT_AAC_HE_V2},
810 { OMX_AUDIO_AACObjectELD, AUDIO_FORMAT_AAC_ELD},
811 { OMX_AUDIO_AACObjectNull, AUDIO_FORMAT_AAC},
812 };
813
mapAACProfileToAudioFormat(audio_format_t & format,uint64_t eAacProfile)814 void mapAACProfileToAudioFormat( audio_format_t& format, uint64_t eAacProfile)
815 {
816 const struct aac_format_conv_t* p = &profileLookup[0];
817 while (p->eAacProfileType != OMX_AUDIO_AACObjectNull) {
818 if (eAacProfile == p->eAacProfileType) {
819 format = p->format;
820 return;
821 }
822 ++p;
823 }
824 format = AUDIO_FORMAT_AAC;
825 return;
826 }
827
canOffloadStream(const sp<MetaData> & meta,bool hasVideo,bool isStreaming,audio_stream_type_t streamType)828 bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo,
829 bool isStreaming, audio_stream_type_t streamType)
830 {
831 const char *mime;
832 if (meta == NULL) {
833 return false;
834 }
835 CHECK(meta->findCString(kKeyMIMEType, &mime));
836
837 audio_offload_info_t info = AUDIO_INFO_INITIALIZER;
838
839 info.format = AUDIO_FORMAT_INVALID;
840 if (mapMimeToAudioFormat(info.format, mime) != OK) {
841 ALOGE(" Couldn't map mime type \"%s\" to a valid AudioSystem::audio_format !", mime);
842 return false;
843 } else {
844 ALOGV("Mime type \"%s\" mapped to audio_format %d", mime, info.format);
845 }
846
847 if (AUDIO_FORMAT_INVALID == info.format) {
848 // can't offload if we don't know what the source format is
849 ALOGE("mime type \"%s\" not a known audio format", mime);
850 return false;
851 }
852
853 // Redefine aac format according to its profile
854 // Offloading depends on audio DSP capabilities.
855 int32_t aacaot = -1;
856 if (meta->findInt32(kKeyAACAOT, &aacaot)) {
857 mapAACProfileToAudioFormat(info.format,(OMX_AUDIO_AACPROFILETYPE) aacaot);
858 }
859
860 int32_t srate = -1;
861 if (!meta->findInt32(kKeySampleRate, &srate)) {
862 ALOGV("track of type '%s' does not publish sample rate", mime);
863 }
864 info.sample_rate = srate;
865
866 int32_t cmask = 0;
867 if (!meta->findInt32(kKeyChannelMask, &cmask)) {
868 ALOGV("track of type '%s' does not publish channel mask", mime);
869
870 // Try a channel count instead
871 int32_t channelCount;
872 if (!meta->findInt32(kKeyChannelCount, &channelCount)) {
873 ALOGV("track of type '%s' does not publish channel count", mime);
874 } else {
875 cmask = audio_channel_out_mask_from_count(channelCount);
876 }
877 }
878 info.channel_mask = cmask;
879
880 int64_t duration = 0;
881 if (!meta->findInt64(kKeyDuration, &duration)) {
882 ALOGV("track of type '%s' does not publish duration", mime);
883 }
884 info.duration_us = duration;
885
886 int32_t brate = -1;
887 if (!meta->findInt32(kKeyBitRate, &brate)) {
888 ALOGV("track of type '%s' does not publish bitrate", mime);
889 }
890 info.bit_rate = brate;
891
892
893 info.stream_type = streamType;
894 info.has_video = hasVideo;
895 info.is_streaming = isStreaming;
896
897 // Check if offload is possible for given format, stream type, sample rate,
898 // bit rate, duration, video and streaming
899 return AudioSystem::isOffloadSupported(info);
900 }
901
uriDebugString(const AString & uri,bool incognito)902 AString uriDebugString(const AString &uri, bool incognito) {
903 if (incognito) {
904 return AString("<URI suppressed>");
905 }
906
907 char prop[PROPERTY_VALUE_MAX];
908 if (property_get("media.stagefright.log-uri", prop, "false") &&
909 (!strcmp(prop, "1") || !strcmp(prop, "true"))) {
910 return uri;
911 }
912
913 // find scheme
914 AString scheme;
915 const char *chars = uri.c_str();
916 for (size_t i = 0; i < uri.size(); i++) {
917 const char c = chars[i];
918 if (!isascii(c)) {
919 break;
920 } else if (isalpha(c)) {
921 continue;
922 } else if (i == 0) {
923 // first character must be a letter
924 break;
925 } else if (isdigit(c) || c == '+' || c == '.' || c =='-') {
926 continue;
927 } else if (c != ':') {
928 break;
929 }
930 scheme = AString(uri, 0, i);
931 scheme.append("://<suppressed>");
932 return scheme;
933 }
934 return AString("<no-scheme URI suppressed>");
935 }
936
HLSTime(const sp<AMessage> & meta)937 HLSTime::HLSTime(const sp<AMessage>& meta) :
938 mSeq(-1),
939 mTimeUs(-1ll),
940 mMeta(meta) {
941 if (meta != NULL) {
942 CHECK(meta->findInt32("discontinuitySeq", &mSeq));
943 CHECK(meta->findInt64("timeUs", &mTimeUs));
944 }
945 }
946
getSegmentTimeUs() const947 int64_t HLSTime::getSegmentTimeUs() const {
948 int64_t segmentStartTimeUs = -1ll;
949 if (mMeta != NULL) {
950 CHECK(mMeta->findInt64("segmentStartTimeUs", &segmentStartTimeUs));
951
952 int64_t segmentFirstTimeUs;
953 if (mMeta->findInt64("segmentFirstTimeUs", &segmentFirstTimeUs)) {
954 segmentStartTimeUs += mTimeUs - segmentFirstTimeUs;
955 }
956
957 // adjust segment time by playlist age (for live streaming)
958 int64_t playlistTimeUs;
959 if (mMeta->findInt64("playlistTimeUs", &playlistTimeUs)) {
960 int64_t playlistAgeUs = ALooper::GetNowUs() - playlistTimeUs;
961
962 int64_t durationUs;
963 CHECK(mMeta->findInt64("segmentDurationUs", &durationUs));
964
965 // round to nearest whole segment
966 playlistAgeUs = (playlistAgeUs + durationUs / 2)
967 / durationUs * durationUs;
968
969 segmentStartTimeUs -= playlistAgeUs;
970 if (segmentStartTimeUs < 0) {
971 segmentStartTimeUs = 0;
972 }
973 }
974 }
975 return segmentStartTimeUs;
976 }
977
operator <(const HLSTime & t0,const HLSTime & t1)978 bool operator <(const HLSTime &t0, const HLSTime &t1) {
979 // we can only compare discontinuity sequence and timestamp.
980 // (mSegmentTimeUs is not reliable in live streaming case, it's the
981 // time starting from beginning of playlist but playlist could change.)
982 return t0.mSeq < t1.mSeq
983 || (t0.mSeq == t1.mSeq && t0.mTimeUs < t1.mTimeUs);
984 }
985
writeToAMessage(sp<AMessage> msg,const AudioPlaybackRate & rate)986 void writeToAMessage(sp<AMessage> msg, const AudioPlaybackRate &rate) {
987 msg->setFloat("speed", rate.mSpeed);
988 msg->setFloat("pitch", rate.mPitch);
989 msg->setInt32("audio-fallback-mode", rate.mFallbackMode);
990 msg->setInt32("audio-stretch-mode", rate.mStretchMode);
991 }
992
readFromAMessage(const sp<AMessage> & msg,AudioPlaybackRate * rate)993 void readFromAMessage(const sp<AMessage> &msg, AudioPlaybackRate *rate /* nonnull */) {
994 *rate = AUDIO_PLAYBACK_RATE_DEFAULT;
995 CHECK(msg->findFloat("speed", &rate->mSpeed));
996 CHECK(msg->findFloat("pitch", &rate->mPitch));
997 CHECK(msg->findInt32("audio-fallback-mode", (int32_t *)&rate->mFallbackMode));
998 CHECK(msg->findInt32("audio-stretch-mode", (int32_t *)&rate->mStretchMode));
999 }
1000
writeToAMessage(sp<AMessage> msg,const AVSyncSettings & sync,float videoFpsHint)1001 void writeToAMessage(sp<AMessage> msg, const AVSyncSettings &sync, float videoFpsHint) {
1002 msg->setInt32("sync-source", sync.mSource);
1003 msg->setInt32("audio-adjust-mode", sync.mAudioAdjustMode);
1004 msg->setFloat("tolerance", sync.mTolerance);
1005 msg->setFloat("video-fps", videoFpsHint);
1006 }
1007
readFromAMessage(const sp<AMessage> & msg,AVSyncSettings * sync,float * videoFps)1008 void readFromAMessage(
1009 const sp<AMessage> &msg,
1010 AVSyncSettings *sync /* nonnull */,
1011 float *videoFps /* nonnull */) {
1012 AVSyncSettings settings;
1013 CHECK(msg->findInt32("sync-source", (int32_t *)&settings.mSource));
1014 CHECK(msg->findInt32("audio-adjust-mode", (int32_t *)&settings.mAudioAdjustMode));
1015 CHECK(msg->findFloat("tolerance", &settings.mTolerance));
1016 CHECK(msg->findFloat("video-fps", videoFps));
1017 *sync = settings;
1018 }
1019
1020 } // namespace android
1021
1022