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 21 #include "include/ESDS.h" 22 23 #include <arpa/inet.h> 24 #include <cutils/properties.h> 25 #include <media/stagefright/foundation/ABuffer.h> 26 #include <media/stagefright/foundation/ADebug.h> 27 #include <media/stagefright/foundation/AMessage.h> 28 #include <media/stagefright/MetaData.h> 29 #include <media/stagefright/MediaDefs.h> 30 #include <media/AudioSystem.h> 31 #include <media/MediaPlayerInterface.h> 32 #include <hardware/audio.h> 33 #include <media/stagefright/Utils.h> 34 #include <media/AudioParameter.h> 35 36 namespace android { 37 38 uint16_t U16_AT(const uint8_t *ptr) { 39 return ptr[0] << 8 | ptr[1]; 40 } 41 42 uint32_t U32_AT(const uint8_t *ptr) { 43 return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3]; 44 } 45 46 uint64_t U64_AT(const uint8_t *ptr) { 47 return ((uint64_t)U32_AT(ptr)) << 32 | U32_AT(ptr + 4); 48 } 49 50 uint16_t U16LE_AT(const uint8_t *ptr) { 51 return ptr[0] | (ptr[1] << 8); 52 } 53 54 uint32_t U32LE_AT(const uint8_t *ptr) { 55 return ptr[3] << 24 | ptr[2] << 16 | ptr[1] << 8 | ptr[0]; 56 } 57 58 uint64_t U64LE_AT(const uint8_t *ptr) { 59 return ((uint64_t)U32LE_AT(ptr + 4)) << 32 | U32LE_AT(ptr); 60 } 61 62 // XXX warning: these won't work on big-endian host. 63 uint64_t ntoh64(uint64_t x) { 64 return ((uint64_t)ntohl(x & 0xffffffff) << 32) | ntohl(x >> 32); 65 } 66 67 uint64_t hton64(uint64_t x) { 68 return ((uint64_t)htonl(x & 0xffffffff) << 32) | htonl(x >> 32); 69 } 70 71 status_t convertMetaDataToMessage( 72 const sp<MetaData> &meta, sp<AMessage> *format) { 73 format->clear(); 74 75 const char *mime; 76 CHECK(meta->findCString(kKeyMIMEType, &mime)); 77 78 sp<AMessage> msg = new AMessage; 79 msg->setString("mime", mime); 80 81 int64_t durationUs; 82 if (meta->findInt64(kKeyDuration, &durationUs)) { 83 msg->setInt64("durationUs", durationUs); 84 } 85 86 int32_t isSync; 87 if (meta->findInt32(kKeyIsSyncFrame, &isSync) && isSync != 0) { 88 msg->setInt32("is-sync-frame", 1); 89 } 90 91 if (!strncasecmp("video/", mime, 6)) { 92 int32_t width, height; 93 CHECK(meta->findInt32(kKeyWidth, &width)); 94 CHECK(meta->findInt32(kKeyHeight, &height)); 95 96 msg->setInt32("width", width); 97 msg->setInt32("height", height); 98 99 int32_t sarWidth, sarHeight; 100 if (meta->findInt32(kKeySARWidth, &sarWidth) 101 && meta->findInt32(kKeySARHeight, &sarHeight)) { 102 msg->setInt32("sar-width", sarWidth); 103 msg->setInt32("sar-height", sarHeight); 104 } 105 } else if (!strncasecmp("audio/", mime, 6)) { 106 int32_t numChannels, sampleRate; 107 CHECK(meta->findInt32(kKeyChannelCount, &numChannels)); 108 CHECK(meta->findInt32(kKeySampleRate, &sampleRate)); 109 110 msg->setInt32("channel-count", numChannels); 111 msg->setInt32("sample-rate", sampleRate); 112 113 int32_t channelMask; 114 if (meta->findInt32(kKeyChannelMask, &channelMask)) { 115 msg->setInt32("channel-mask", channelMask); 116 } 117 118 int32_t delay = 0; 119 if (meta->findInt32(kKeyEncoderDelay, &delay)) { 120 msg->setInt32("encoder-delay", delay); 121 } 122 int32_t padding = 0; 123 if (meta->findInt32(kKeyEncoderPadding, &padding)) { 124 msg->setInt32("encoder-padding", padding); 125 } 126 127 int32_t isADTS; 128 if (meta->findInt32(kKeyIsADTS, &isADTS)) { 129 msg->setInt32("is-adts", true); 130 } 131 } 132 133 int32_t maxInputSize; 134 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) { 135 msg->setInt32("max-input-size", maxInputSize); 136 } 137 138 uint32_t type; 139 const void *data; 140 size_t size; 141 if (meta->findData(kKeyAVCC, &type, &data, &size)) { 142 // Parse the AVCDecoderConfigurationRecord 143 144 const uint8_t *ptr = (const uint8_t *)data; 145 146 CHECK(size >= 7); 147 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1 148 uint8_t profile = ptr[1]; 149 uint8_t level = ptr[3]; 150 151 // There is decodable content out there that fails the following 152 // assertion, let's be lenient for now... 153 // CHECK((ptr[4] >> 2) == 0x3f); // reserved 154 155 size_t lengthSize = 1 + (ptr[4] & 3); 156 157 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp 158 // violates it... 159 // CHECK((ptr[5] >> 5) == 7); // reserved 160 161 size_t numSeqParameterSets = ptr[5] & 31; 162 163 ptr += 6; 164 size -= 6; 165 166 sp<ABuffer> buffer = new ABuffer(1024); 167 buffer->setRange(0, 0); 168 169 for (size_t i = 0; i < numSeqParameterSets; ++i) { 170 CHECK(size >= 2); 171 size_t length = U16_AT(ptr); 172 173 ptr += 2; 174 size -= 2; 175 176 CHECK(size >= length); 177 178 memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4); 179 memcpy(buffer->data() + buffer->size() + 4, ptr, length); 180 buffer->setRange(0, buffer->size() + 4 + length); 181 182 ptr += length; 183 size -= length; 184 } 185 186 buffer->meta()->setInt32("csd", true); 187 buffer->meta()->setInt64("timeUs", 0); 188 189 msg->setBuffer("csd-0", 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 buffer->meta()->setInt64("timeUs", 0); 218 msg->setBuffer("csd-1", buffer); 219 } else if (meta->findData(kKeyESDS, &type, &data, &size)) { 220 ESDS esds((const char *)data, size); 221 CHECK_EQ(esds.InitCheck(), (status_t)OK); 222 223 const void *codec_specific_data; 224 size_t codec_specific_data_size; 225 esds.getCodecSpecificInfo( 226 &codec_specific_data, &codec_specific_data_size); 227 228 sp<ABuffer> buffer = new ABuffer(codec_specific_data_size); 229 230 memcpy(buffer->data(), codec_specific_data, 231 codec_specific_data_size); 232 233 buffer->meta()->setInt32("csd", true); 234 buffer->meta()->setInt64("timeUs", 0); 235 msg->setBuffer("csd-0", buffer); 236 } else if (meta->findData(kKeyVorbisInfo, &type, &data, &size)) { 237 sp<ABuffer> buffer = new ABuffer(size); 238 memcpy(buffer->data(), data, size); 239 240 buffer->meta()->setInt32("csd", true); 241 buffer->meta()->setInt64("timeUs", 0); 242 msg->setBuffer("csd-0", buffer); 243 244 if (!meta->findData(kKeyVorbisBooks, &type, &data, &size)) { 245 return -EINVAL; 246 } 247 248 buffer = new ABuffer(size); 249 memcpy(buffer->data(), data, size); 250 251 buffer->meta()->setInt32("csd", true); 252 buffer->meta()->setInt64("timeUs", 0); 253 msg->setBuffer("csd-1", buffer); 254 } 255 256 *format = msg; 257 258 return OK; 259 } 260 261 static size_t reassembleAVCC(const sp<ABuffer> &csd0, const sp<ABuffer> csd1, char *avcc) { 262 263 avcc[0] = 1; // version 264 avcc[1] = 0x64; // profile 265 avcc[2] = 0; // unused (?) 266 avcc[3] = 0xd; // level 267 avcc[4] = 0xff; // reserved+size 268 269 size_t i = 0; 270 int numparams = 0; 271 int lastparamoffset = 0; 272 int avccidx = 6; 273 do { 274 if (i >= csd0->size() - 4 || 275 memcmp(csd0->data() + i, "\x00\x00\x00\x01", 4) == 0) { 276 if (i >= csd0->size() - 4) { 277 // there can't be another param here, so use all the rest 278 i = csd0->size(); 279 } 280 ALOGV("block at %d, last was %d", i, lastparamoffset); 281 if (lastparamoffset > 0) { 282 int size = i - lastparamoffset; 283 avcc[avccidx++] = size >> 8; 284 avcc[avccidx++] = size & 0xff; 285 memcpy(avcc+avccidx, csd0->data() + lastparamoffset, size); 286 avccidx += size; 287 numparams++; 288 } 289 i += 4; 290 lastparamoffset = i; 291 } else { 292 i++; 293 } 294 } while(i < csd0->size()); 295 ALOGV("csd0 contains %d params", numparams); 296 297 avcc[5] = 0xe0 | numparams; 298 //and now csd-1 299 i = 0; 300 numparams = 0; 301 lastparamoffset = 0; 302 int numpicparamsoffset = avccidx; 303 avccidx++; 304 do { 305 if (i >= csd1->size() - 4 || 306 memcmp(csd1->data() + i, "\x00\x00\x00\x01", 4) == 0) { 307 if (i >= csd1->size() - 4) { 308 // there can't be another param here, so use all the rest 309 i = csd1->size(); 310 } 311 ALOGV("block at %d, last was %d", i, lastparamoffset); 312 if (lastparamoffset > 0) { 313 int size = i - lastparamoffset; 314 avcc[avccidx++] = size >> 8; 315 avcc[avccidx++] = size & 0xff; 316 memcpy(avcc+avccidx, csd1->data() + lastparamoffset, size); 317 avccidx += size; 318 numparams++; 319 } 320 i += 4; 321 lastparamoffset = i; 322 } else { 323 i++; 324 } 325 } while(i < csd1->size()); 326 avcc[numpicparamsoffset] = numparams; 327 return avccidx; 328 } 329 330 static void reassembleESDS(const sp<ABuffer> &csd0, char *esds) { 331 int csd0size = csd0->size(); 332 esds[0] = 3; // kTag_ESDescriptor; 333 int esdescriptorsize = 26 + csd0size; 334 CHECK(esdescriptorsize < 268435456); // 7 bits per byte, so max is 2^28-1 335 esds[1] = 0x80 | (esdescriptorsize >> 21); 336 esds[2] = 0x80 | ((esdescriptorsize >> 14) & 0x7f); 337 esds[3] = 0x80 | ((esdescriptorsize >> 7) & 0x7f); 338 esds[4] = (esdescriptorsize & 0x7f); 339 esds[5] = esds[6] = 0; // es id 340 esds[7] = 0; // flags 341 esds[8] = 4; // kTag_DecoderConfigDescriptor 342 int configdescriptorsize = 18 + csd0size; 343 esds[9] = 0x80 | (configdescriptorsize >> 21); 344 esds[10] = 0x80 | ((configdescriptorsize >> 14) & 0x7f); 345 esds[11] = 0x80 | ((configdescriptorsize >> 7) & 0x7f); 346 esds[12] = (configdescriptorsize & 0x7f); 347 esds[13] = 0x40; // objectTypeIndication 348 esds[14] = 0x15; // not sure what 14-25 mean, they are ignored by ESDS.cpp, 349 esds[15] = 0x00; // but the actual values here were taken from a real file. 350 esds[16] = 0x18; 351 esds[17] = 0x00; 352 esds[18] = 0x00; 353 esds[19] = 0x00; 354 esds[20] = 0xfa; 355 esds[21] = 0x00; 356 esds[22] = 0x00; 357 esds[23] = 0x00; 358 esds[24] = 0xfa; 359 esds[25] = 0x00; 360 esds[26] = 5; // kTag_DecoderSpecificInfo; 361 esds[27] = 0x80 | (csd0size >> 21); 362 esds[28] = 0x80 | ((csd0size >> 14) & 0x7f); 363 esds[29] = 0x80 | ((csd0size >> 7) & 0x7f); 364 esds[30] = (csd0size & 0x7f); 365 memcpy((void*)&esds[31], csd0->data(), csd0size); 366 // data following this is ignored, so don't bother appending it 367 368 } 369 370 void convertMessageToMetaData(const sp<AMessage> &msg, sp<MetaData> &meta) { 371 AString mime; 372 if (msg->findString("mime", &mime)) { 373 meta->setCString(kKeyMIMEType, mime.c_str()); 374 } else { 375 ALOGW("did not find mime type"); 376 } 377 378 int64_t durationUs; 379 if (msg->findInt64("durationUs", &durationUs)) { 380 meta->setInt64(kKeyDuration, durationUs); 381 } 382 383 int32_t isSync; 384 if (msg->findInt32("is-sync-frame", &isSync) && isSync != 0) { 385 meta->setInt32(kKeyIsSyncFrame, 1); 386 } 387 388 if (mime.startsWith("video/")) { 389 int32_t width; 390 int32_t height; 391 if (msg->findInt32("width", &width) && msg->findInt32("height", &height)) { 392 meta->setInt32(kKeyWidth, width); 393 meta->setInt32(kKeyHeight, height); 394 } else { 395 ALOGW("did not find width and/or height"); 396 } 397 398 int32_t sarWidth, sarHeight; 399 if (msg->findInt32("sar-width", &sarWidth) 400 && msg->findInt32("sar-height", &sarHeight)) { 401 meta->setInt32(kKeySARWidth, sarWidth); 402 meta->setInt32(kKeySARHeight, sarHeight); 403 } 404 } else if (mime.startsWith("audio/")) { 405 int32_t numChannels; 406 if (msg->findInt32("channel-count", &numChannels)) { 407 meta->setInt32(kKeyChannelCount, numChannels); 408 } 409 int32_t sampleRate; 410 if (msg->findInt32("sample-rate", &sampleRate)) { 411 meta->setInt32(kKeySampleRate, sampleRate); 412 } 413 int32_t channelMask; 414 if (msg->findInt32("channel-mask", &channelMask)) { 415 meta->setInt32(kKeyChannelMask, channelMask); 416 } 417 int32_t delay = 0; 418 if (msg->findInt32("encoder-delay", &delay)) { 419 meta->setInt32(kKeyEncoderDelay, delay); 420 } 421 int32_t padding = 0; 422 if (msg->findInt32("encoder-padding", &padding)) { 423 meta->setInt32(kKeyEncoderPadding, padding); 424 } 425 426 int32_t isADTS; 427 if (msg->findInt32("is-adts", &isADTS)) { 428 meta->setInt32(kKeyIsADTS, isADTS); 429 } 430 } 431 432 int32_t maxInputSize; 433 if (msg->findInt32("max-input-size", &maxInputSize)) { 434 meta->setInt32(kKeyMaxInputSize, maxInputSize); 435 } 436 437 // reassemble the csd data into its original form 438 sp<ABuffer> csd0; 439 if (msg->findBuffer("csd-0", &csd0)) { 440 if (mime.startsWith("video/")) { // do we need to be stricter than this? 441 sp<ABuffer> csd1; 442 if (msg->findBuffer("csd-1", &csd1)) { 443 char avcc[1024]; // that oughta be enough, right? 444 size_t outsize = reassembleAVCC(csd0, csd1, avcc); 445 meta->setData(kKeyAVCC, kKeyAVCC, avcc, outsize); 446 } 447 } else if (mime.startsWith("audio/")) { 448 int csd0size = csd0->size(); 449 char esds[csd0size + 31]; 450 reassembleESDS(csd0, esds); 451 meta->setData(kKeyESDS, kKeyESDS, esds, sizeof(esds)); 452 } 453 } 454 455 // XXX TODO add whatever other keys there are 456 457 #if 0 458 ALOGI("converted %s to:", msg->debugString(0).c_str()); 459 meta->dumpToLog(); 460 #endif 461 } 462 463 AString MakeUserAgent() { 464 AString ua; 465 ua.append("stagefright/1.2 (Linux;Android "); 466 467 #if (PROPERTY_VALUE_MAX < 8) 468 #error "PROPERTY_VALUE_MAX must be at least 8" 469 #endif 470 471 char value[PROPERTY_VALUE_MAX]; 472 property_get("ro.build.version.release", value, "Unknown"); 473 ua.append(value); 474 ua.append(")"); 475 476 return ua; 477 } 478 479 status_t sendMetaDataToHal(sp<MediaPlayerBase::AudioSink>& sink, 480 const sp<MetaData>& meta) 481 { 482 int32_t sampleRate = 0; 483 int32_t bitRate = 0; 484 int32_t channelMask = 0; 485 int32_t delaySamples = 0; 486 int32_t paddingSamples = 0; 487 488 AudioParameter param = AudioParameter(); 489 490 if (meta->findInt32(kKeySampleRate, &sampleRate)) { 491 param.addInt(String8(AUDIO_OFFLOAD_CODEC_SAMPLE_RATE), sampleRate); 492 } 493 if (meta->findInt32(kKeyChannelMask, &channelMask)) { 494 param.addInt(String8(AUDIO_OFFLOAD_CODEC_NUM_CHANNEL), channelMask); 495 } 496 if (meta->findInt32(kKeyBitRate, &bitRate)) { 497 param.addInt(String8(AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE), bitRate); 498 } 499 if (meta->findInt32(kKeyEncoderDelay, &delaySamples)) { 500 param.addInt(String8(AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES), delaySamples); 501 } 502 if (meta->findInt32(kKeyEncoderPadding, &paddingSamples)) { 503 param.addInt(String8(AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES), paddingSamples); 504 } 505 506 ALOGV("sendMetaDataToHal: bitRate %d, sampleRate %d, chanMask %d," 507 "delaySample %d, paddingSample %d", bitRate, sampleRate, 508 channelMask, delaySamples, paddingSamples); 509 510 sink->setParameters(param.toString()); 511 return OK; 512 } 513 514 struct mime_conv_t { 515 const char* mime; 516 audio_format_t format; 517 }; 518 519 static const struct mime_conv_t mimeLookup[] = { 520 { MEDIA_MIMETYPE_AUDIO_MPEG, AUDIO_FORMAT_MP3 }, 521 { MEDIA_MIMETYPE_AUDIO_RAW, AUDIO_FORMAT_PCM_16_BIT }, 522 { MEDIA_MIMETYPE_AUDIO_AMR_NB, AUDIO_FORMAT_AMR_NB }, 523 { MEDIA_MIMETYPE_AUDIO_AMR_WB, AUDIO_FORMAT_AMR_WB }, 524 { MEDIA_MIMETYPE_AUDIO_AAC, AUDIO_FORMAT_AAC }, 525 { MEDIA_MIMETYPE_AUDIO_VORBIS, AUDIO_FORMAT_VORBIS }, 526 { 0, AUDIO_FORMAT_INVALID } 527 }; 528 529 status_t mapMimeToAudioFormat( audio_format_t& format, const char* mime ) 530 { 531 const struct mime_conv_t* p = &mimeLookup[0]; 532 while (p->mime != NULL) { 533 if (0 == strcasecmp(mime, p->mime)) { 534 format = p->format; 535 return OK; 536 } 537 ++p; 538 } 539 540 return BAD_VALUE; 541 } 542 543 bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo, 544 bool isStreaming, audio_stream_type_t streamType) 545 { 546 const char *mime; 547 CHECK(meta->findCString(kKeyMIMEType, &mime)); 548 549 audio_offload_info_t info = AUDIO_INFO_INITIALIZER; 550 551 info.format = AUDIO_FORMAT_INVALID; 552 if (mapMimeToAudioFormat(info.format, mime) != OK) { 553 ALOGE(" Couldn't map mime type \"%s\" to a valid AudioSystem::audio_format !", mime); 554 return false; 555 } else { 556 ALOGV("Mime type \"%s\" mapped to audio_format %d", mime, info.format); 557 } 558 559 if (AUDIO_FORMAT_INVALID == info.format) { 560 // can't offload if we don't know what the source format is 561 ALOGE("mime type \"%s\" not a known audio format", mime); 562 return false; 563 } 564 565 // check whether it is ELD/LD content -> no offloading 566 // FIXME: this should depend on audio DSP capabilities. mapMimeToAudioFormat() should use the 567 // metadata to refine the AAC format and the audio HAL should only list supported profiles. 568 int32_t aacaot = -1; 569 if (meta->findInt32(kKeyAACAOT, &aacaot)) { 570 if (aacaot == 23 || aacaot == 39 ) { 571 ALOGV("track of type '%s' is ELD/LD content", mime); 572 return false; 573 } 574 } 575 576 int32_t srate = -1; 577 if (!meta->findInt32(kKeySampleRate, &srate)) { 578 ALOGV("track of type '%s' does not publish sample rate", mime); 579 } 580 info.sample_rate = srate; 581 582 int32_t cmask = 0; 583 if (!meta->findInt32(kKeyChannelMask, &cmask)) { 584 ALOGV("track of type '%s' does not publish channel mask", mime); 585 586 // Try a channel count instead 587 int32_t channelCount; 588 if (!meta->findInt32(kKeyChannelCount, &channelCount)) { 589 ALOGV("track of type '%s' does not publish channel count", mime); 590 } else { 591 cmask = audio_channel_out_mask_from_count(channelCount); 592 } 593 } 594 info.channel_mask = cmask; 595 596 int64_t duration = 0; 597 if (!meta->findInt64(kKeyDuration, &duration)) { 598 ALOGV("track of type '%s' does not publish duration", mime); 599 } 600 info.duration_us = duration; 601 602 int32_t brate = -1; 603 if (!meta->findInt32(kKeyBitRate, &brate)) { 604 ALOGV("track of type '%s' does not publish bitrate", mime); 605 } 606 info.bit_rate = brate; 607 608 609 info.stream_type = streamType; 610 info.has_video = hasVideo; 611 info.is_streaming = isStreaming; 612 613 // Check if offload is possible for given format, stream type, sample rate, 614 // bit rate, duration, video and streaming 615 return AudioSystem::isOffloadSupported(info); 616 } 617 618 } // namespace android 619 620