ESQueue.cpp revision 4d23645c8d3d93c91967a5494473b4a8b5d10d9c
1/* 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 "ESQueue" 19#include <media/stagefright/foundation/ADebug.h> 20 21#include "ESQueue.h" 22 23#include <media/stagefright/foundation/hexdump.h> 24#include <media/stagefright/foundation/ABitReader.h> 25#include <media/stagefright/foundation/ABuffer.h> 26#include <media/stagefright/foundation/AMessage.h> 27#include <media/stagefright/MediaErrors.h> 28#include <media/stagefright/MediaDefs.h> 29#include <media/stagefright/MetaData.h> 30#include <media/stagefright/Utils.h> 31 32#include "include/avc_utils.h" 33 34#include <inttypes.h> 35#include <netinet/in.h> 36 37namespace android { 38 39ElementaryStreamQueue::ElementaryStreamQueue(Mode mode, uint32_t flags) 40 : mMode(mode), 41 mFlags(flags) { 42} 43 44sp<MetaData> ElementaryStreamQueue::getFormat() { 45 return mFormat; 46} 47 48void ElementaryStreamQueue::clear(bool clearFormat) { 49 if (mBuffer != NULL) { 50 mBuffer->setRange(0, 0); 51 } 52 53 mRangeInfos.clear(); 54 55 if (clearFormat) { 56 mFormat.clear(); 57 } 58} 59 60// Parse AC3 header assuming the current ptr is start position of syncframe, 61// update metadata only applicable, and return the payload size 62static unsigned parseAC3SyncFrame( 63 const uint8_t *ptr, size_t size, sp<MetaData> *metaData) { 64 static const unsigned channelCountTable[] = {2, 1, 2, 3, 3, 4, 4, 5}; 65 static const unsigned samplingRateTable[] = {48000, 44100, 32000}; 66 static const unsigned rates[] = {32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 67 320, 384, 448, 512, 576, 640}; 68 69 static const unsigned frameSizeTable[19][3] = { 70 { 64, 69, 96 }, 71 { 80, 87, 120 }, 72 { 96, 104, 144 }, 73 { 112, 121, 168 }, 74 { 128, 139, 192 }, 75 { 160, 174, 240 }, 76 { 192, 208, 288 }, 77 { 224, 243, 336 }, 78 { 256, 278, 384 }, 79 { 320, 348, 480 }, 80 { 384, 417, 576 }, 81 { 448, 487, 672 }, 82 { 512, 557, 768 }, 83 { 640, 696, 960 }, 84 { 768, 835, 1152 }, 85 { 896, 975, 1344 }, 86 { 1024, 1114, 1536 }, 87 { 1152, 1253, 1728 }, 88 { 1280, 1393, 1920 }, 89 }; 90 91 ABitReader bits(ptr, size); 92 unsigned syncStartPos = 0; // in bytes 93 if (bits.numBitsLeft() < 16) { 94 return 0; 95 } 96 if (bits.getBits(16) != 0x0B77) { 97 return 0; 98 } 99 100 if (bits.numBitsLeft() < 16 + 2 + 6 + 5 + 3 + 3) { 101 ALOGV("Not enough bits left for further parsing"); 102 return 0; 103 } 104 bits.skipBits(16); // crc1 105 106 unsigned fscod = bits.getBits(2); 107 if (fscod == 3) { 108 ALOGW("Incorrect fscod in AC3 header"); 109 return 0; 110 } 111 112 unsigned frmsizecod = bits.getBits(6); 113 if (frmsizecod > 37) { 114 ALOGW("Incorrect frmsizecod in AC3 header"); 115 return 0; 116 } 117 118 unsigned bsid = bits.getBits(5); 119 if (bsid > 8) { 120 ALOGW("Incorrect bsid in AC3 header. Possibly E-AC-3?"); 121 return 0; 122 } 123 124 unsigned bsmod = bits.getBits(3); 125 unsigned acmod = bits.getBits(3); 126 unsigned cmixlev = 0; 127 unsigned surmixlev = 0; 128 unsigned dsurmod = 0; 129 130 if ((acmod & 1) > 0 && acmod != 1) { 131 if (bits.numBitsLeft() < 2) { 132 return 0; 133 } 134 cmixlev = bits.getBits(2); 135 } 136 if ((acmod & 4) > 0) { 137 if (bits.numBitsLeft() < 2) { 138 return 0; 139 } 140 surmixlev = bits.getBits(2); 141 } 142 if (acmod == 2) { 143 if (bits.numBitsLeft() < 2) { 144 return 0; 145 } 146 dsurmod = bits.getBits(2); 147 } 148 149 if (bits.numBitsLeft() < 1) { 150 return 0; 151 } 152 unsigned lfeon = bits.getBits(1); 153 154 unsigned samplingRate = samplingRateTable[fscod]; 155 unsigned payloadSize = frameSizeTable[frmsizecod >> 1][fscod]; 156 if (fscod == 1) { 157 payloadSize += frmsizecod & 1; 158 } 159 payloadSize <<= 1; // convert from 16-bit words to bytes 160 161 unsigned channelCount = channelCountTable[acmod] + lfeon; 162 163 if (metaData != NULL) { 164 (*metaData)->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC3); 165 (*metaData)->setInt32(kKeyChannelCount, channelCount); 166 (*metaData)->setInt32(kKeySampleRate, samplingRate); 167 } 168 169 return payloadSize; 170} 171 172static bool IsSeeminglyValidAC3Header(const uint8_t *ptr, size_t size) { 173 return parseAC3SyncFrame(ptr, size, NULL) > 0; 174} 175 176static bool IsSeeminglyValidADTSHeader( 177 const uint8_t *ptr, size_t size, size_t *frameLength) { 178 if (size < 7) { 179 // Not enough data to verify header. 180 return false; 181 } 182 183 if (ptr[0] != 0xff || (ptr[1] >> 4) != 0x0f) { 184 return false; 185 } 186 187 unsigned layer = (ptr[1] >> 1) & 3; 188 189 if (layer != 0) { 190 return false; 191 } 192 193 unsigned ID = (ptr[1] >> 3) & 1; 194 unsigned profile_ObjectType = ptr[2] >> 6; 195 196 if (ID == 1 && profile_ObjectType == 3) { 197 // MPEG-2 profile 3 is reserved. 198 return false; 199 } 200 201 size_t frameLengthInHeader = 202 ((ptr[3] & 3) << 11) + (ptr[4] << 3) + ((ptr[5] >> 5) & 7); 203 if (frameLengthInHeader > size) { 204 return false; 205 } 206 207 *frameLength = frameLengthInHeader; 208 return true; 209} 210 211static bool IsSeeminglyValidMPEGAudioHeader(const uint8_t *ptr, size_t size) { 212 if (size < 3) { 213 // Not enough data to verify header. 214 return false; 215 } 216 217 if (ptr[0] != 0xff || (ptr[1] >> 5) != 0x07) { 218 return false; 219 } 220 221 unsigned ID = (ptr[1] >> 3) & 3; 222 223 if (ID == 1) { 224 return false; // reserved 225 } 226 227 unsigned layer = (ptr[1] >> 1) & 3; 228 229 if (layer == 0) { 230 return false; // reserved 231 } 232 233 unsigned bitrateIndex = (ptr[2] >> 4); 234 235 if (bitrateIndex == 0x0f) { 236 return false; // reserved 237 } 238 239 unsigned samplingRateIndex = (ptr[2] >> 2) & 3; 240 241 if (samplingRateIndex == 3) { 242 return false; // reserved 243 } 244 245 return true; 246} 247 248status_t ElementaryStreamQueue::appendData( 249 const void *data, size_t size, int64_t timeUs) { 250 if (mBuffer == NULL || mBuffer->size() == 0) { 251 switch (mMode) { 252 case H264: 253 case MPEG_VIDEO: 254 { 255#if 0 256 if (size < 4 || memcmp("\x00\x00\x00\x01", data, 4)) { 257 return ERROR_MALFORMED; 258 } 259#else 260 uint8_t *ptr = (uint8_t *)data; 261 262 ssize_t startOffset = -1; 263 for (size_t i = 0; i + 3 < size; ++i) { 264 if (!memcmp("\x00\x00\x00\x01", &ptr[i], 4)) { 265 startOffset = i; 266 break; 267 } 268 } 269 270 if (startOffset < 0) { 271 return ERROR_MALFORMED; 272 } 273 274 if (startOffset > 0) { 275 ALOGI("found something resembling an H.264/MPEG syncword " 276 "at offset %zd", 277 startOffset); 278 } 279 280 data = &ptr[startOffset]; 281 size -= startOffset; 282#endif 283 break; 284 } 285 286 case MPEG4_VIDEO: 287 { 288#if 0 289 if (size < 3 || memcmp("\x00\x00\x01", data, 3)) { 290 return ERROR_MALFORMED; 291 } 292#else 293 uint8_t *ptr = (uint8_t *)data; 294 295 ssize_t startOffset = -1; 296 for (size_t i = 0; i + 2 < size; ++i) { 297 if (!memcmp("\x00\x00\x01", &ptr[i], 3)) { 298 startOffset = i; 299 break; 300 } 301 } 302 303 if (startOffset < 0) { 304 return ERROR_MALFORMED; 305 } 306 307 if (startOffset > 0) { 308 ALOGI("found something resembling an H.264/MPEG syncword " 309 "at offset %zd", 310 startOffset); 311 } 312 313 data = &ptr[startOffset]; 314 size -= startOffset; 315#endif 316 break; 317 } 318 319 case AAC: 320 { 321 uint8_t *ptr = (uint8_t *)data; 322 323#if 0 324 if (size < 2 || ptr[0] != 0xff || (ptr[1] >> 4) != 0x0f) { 325 return ERROR_MALFORMED; 326 } 327#else 328 ssize_t startOffset = -1; 329 size_t frameLength; 330 for (size_t i = 0; i < size; ++i) { 331 if (IsSeeminglyValidADTSHeader( 332 &ptr[i], size - i, &frameLength)) { 333 startOffset = i; 334 break; 335 } 336 } 337 338 if (startOffset < 0) { 339 return ERROR_MALFORMED; 340 } 341 342 if (startOffset > 0) { 343 ALOGI("found something resembling an AAC syncword at " 344 "offset %zd", 345 startOffset); 346 } 347 348 if (frameLength != size - startOffset) { 349 ALOGW("got ADTS AAC frame length %zd instead of %zd", 350 frameLength, size - startOffset); 351 } 352 353 data = &ptr[startOffset]; 354 size = frameLength; 355#endif 356 break; 357 } 358 359 case AC3: 360 { 361 uint8_t *ptr = (uint8_t *)data; 362 363 ssize_t startOffset = -1; 364 for (size_t i = 0; i < size; ++i) { 365 if (IsSeeminglyValidAC3Header(&ptr[i], size - i)) { 366 startOffset = i; 367 break; 368 } 369 } 370 371 if (startOffset < 0) { 372 return ERROR_MALFORMED; 373 } 374 375 if (startOffset > 0) { 376 ALOGI("found something resembling an AC3 syncword at " 377 "offset %zd", 378 startOffset); 379 } 380 381 data = &ptr[startOffset]; 382 size -= startOffset; 383 break; 384 } 385 386 case MPEG_AUDIO: 387 { 388 uint8_t *ptr = (uint8_t *)data; 389 390 ssize_t startOffset = -1; 391 for (size_t i = 0; i < size; ++i) { 392 if (IsSeeminglyValidMPEGAudioHeader(&ptr[i], size - i)) { 393 startOffset = i; 394 break; 395 } 396 } 397 398 if (startOffset < 0) { 399 return ERROR_MALFORMED; 400 } 401 402 if (startOffset > 0) { 403 ALOGI("found something resembling an MPEG audio " 404 "syncword at offset %zd", 405 startOffset); 406 } 407 408 data = &ptr[startOffset]; 409 size -= startOffset; 410 break; 411 } 412 413 case PCM_AUDIO: 414 { 415 break; 416 } 417 418 default: 419 TRESPASS(); 420 break; 421 } 422 } 423 424 size_t neededSize = (mBuffer == NULL ? 0 : mBuffer->size()) + size; 425 if (mBuffer == NULL || neededSize > mBuffer->capacity()) { 426 neededSize = (neededSize + 65535) & ~65535; 427 428 ALOGV("resizing buffer to size %zu", neededSize); 429 430 sp<ABuffer> buffer = new ABuffer(neededSize); 431 if (mBuffer != NULL) { 432 memcpy(buffer->data(), mBuffer->data(), mBuffer->size()); 433 buffer->setRange(0, mBuffer->size()); 434 } else { 435 buffer->setRange(0, 0); 436 } 437 438 mBuffer = buffer; 439 } 440 441 memcpy(mBuffer->data() + mBuffer->size(), data, size); 442 mBuffer->setRange(0, mBuffer->size() + size); 443 444 RangeInfo info; 445 info.mLength = size; 446 info.mTimestampUs = timeUs; 447 mRangeInfos.push_back(info); 448 449#if 0 450 if (mMode == AAC) { 451 ALOGI("size = %zu, timeUs = %.2f secs", size, timeUs / 1E6); 452 hexdump(data, size); 453 } 454#endif 455 456 return OK; 457} 458 459sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnit() { 460 if ((mFlags & kFlag_AlignedData) && mMode == H264) { 461 if (mRangeInfos.empty()) { 462 return NULL; 463 } 464 465 RangeInfo info = *mRangeInfos.begin(); 466 mRangeInfos.erase(mRangeInfos.begin()); 467 468 sp<ABuffer> accessUnit = new ABuffer(info.mLength); 469 memcpy(accessUnit->data(), mBuffer->data(), info.mLength); 470 accessUnit->meta()->setInt64("timeUs", info.mTimestampUs); 471 472 memmove(mBuffer->data(), 473 mBuffer->data() + info.mLength, 474 mBuffer->size() - info.mLength); 475 476 mBuffer->setRange(0, mBuffer->size() - info.mLength); 477 478 if (mFormat == NULL) { 479 mFormat = MakeAVCCodecSpecificData(accessUnit); 480 } 481 482 return accessUnit; 483 } 484 485 switch (mMode) { 486 case H264: 487 return dequeueAccessUnitH264(); 488 case AAC: 489 return dequeueAccessUnitAAC(); 490 case AC3: 491 return dequeueAccessUnitAC3(); 492 case MPEG_VIDEO: 493 return dequeueAccessUnitMPEGVideo(); 494 case MPEG4_VIDEO: 495 return dequeueAccessUnitMPEG4Video(); 496 case PCM_AUDIO: 497 return dequeueAccessUnitPCMAudio(); 498 default: 499 CHECK_EQ((unsigned)mMode, (unsigned)MPEG_AUDIO); 500 return dequeueAccessUnitMPEGAudio(); 501 } 502} 503 504sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitAC3() { 505 unsigned syncStartPos = 0; // in bytes 506 unsigned payloadSize = 0; 507 sp<MetaData> format = new MetaData; 508 while (true) { 509 if (syncStartPos + 2 >= mBuffer->size()) { 510 return NULL; 511 } 512 513 payloadSize = parseAC3SyncFrame( 514 mBuffer->data() + syncStartPos, 515 mBuffer->size() - syncStartPos, 516 &format); 517 if (payloadSize > 0) { 518 break; 519 } 520 ++syncStartPos; 521 } 522 523 if (mBuffer->size() < syncStartPos + payloadSize) { 524 ALOGV("Not enough buffer size for AC3"); 525 return NULL; 526 } 527 528 if (mFormat == NULL) { 529 mFormat = format; 530 } 531 532 sp<ABuffer> accessUnit = new ABuffer(syncStartPos + payloadSize); 533 memcpy(accessUnit->data(), mBuffer->data(), syncStartPos + payloadSize); 534 535 int64_t timeUs = fetchTimestamp(syncStartPos + payloadSize); 536 CHECK_GE(timeUs, 0ll); 537 accessUnit->meta()->setInt64("timeUs", timeUs); 538 539 memmove( 540 mBuffer->data(), 541 mBuffer->data() + syncStartPos + payloadSize, 542 mBuffer->size() - syncStartPos - payloadSize); 543 544 mBuffer->setRange(0, mBuffer->size() - syncStartPos - payloadSize); 545 546 return accessUnit; 547} 548 549sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitPCMAudio() { 550 if (mBuffer->size() < 4) { 551 return NULL; 552 } 553 554 ABitReader bits(mBuffer->data(), 4); 555 CHECK_EQ(bits.getBits(8), 0xa0); 556 unsigned numAUs = bits.getBits(8); 557 bits.skipBits(8); 558 unsigned quantization_word_length = bits.getBits(2); 559 unsigned audio_sampling_frequency = bits.getBits(3); 560 unsigned num_channels = bits.getBits(3); 561 562 CHECK_EQ(audio_sampling_frequency, 2); // 48kHz 563 CHECK_EQ(num_channels, 1u); // stereo! 564 565 if (mFormat == NULL) { 566 mFormat = new MetaData; 567 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); 568 mFormat->setInt32(kKeyChannelCount, 2); 569 mFormat->setInt32(kKeySampleRate, 48000); 570 } 571 572 static const size_t kFramesPerAU = 80; 573 size_t frameSize = 2 /* numChannels */ * sizeof(int16_t); 574 575 size_t payloadSize = numAUs * frameSize * kFramesPerAU; 576 577 if (mBuffer->size() < 4 + payloadSize) { 578 return NULL; 579 } 580 581 sp<ABuffer> accessUnit = new ABuffer(payloadSize); 582 memcpy(accessUnit->data(), mBuffer->data() + 4, payloadSize); 583 584 int64_t timeUs = fetchTimestamp(payloadSize + 4); 585 CHECK_GE(timeUs, 0ll); 586 accessUnit->meta()->setInt64("timeUs", timeUs); 587 588 int16_t *ptr = (int16_t *)accessUnit->data(); 589 for (size_t i = 0; i < payloadSize / sizeof(int16_t); ++i) { 590 ptr[i] = ntohs(ptr[i]); 591 } 592 593 memmove( 594 mBuffer->data(), 595 mBuffer->data() + 4 + payloadSize, 596 mBuffer->size() - 4 - payloadSize); 597 598 mBuffer->setRange(0, mBuffer->size() - 4 - payloadSize); 599 600 return accessUnit; 601} 602 603sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitAAC() { 604 if (mBuffer->size() == 0) { 605 return NULL; 606 } 607 608 CHECK(!mRangeInfos.empty()); 609 610 const RangeInfo &info = *mRangeInfos.begin(); 611 if (mBuffer->size() < info.mLength) { 612 return NULL; 613 } 614 615 CHECK_GE(info.mTimestampUs, 0ll); 616 617 // The idea here is consume all AAC frames starting at offsets before 618 // info.mLength so we can assign a meaningful timestamp without 619 // having to interpolate. 620 // The final AAC frame may well extend into the next RangeInfo but 621 // that's ok. 622 // TODO: the logic commented above is skipped because codec cannot take 623 // arbitrary sized input buffers; 624 size_t offset = 0; 625 while (offset < info.mLength) { 626 if (offset + 7 > mBuffer->size()) { 627 return NULL; 628 } 629 630 ABitReader bits(mBuffer->data() + offset, mBuffer->size() - offset); 631 632 // adts_fixed_header 633 634 CHECK_EQ(bits.getBits(12), 0xfffu); 635 bits.skipBits(3); // ID, layer 636 bool protection_absent = bits.getBits(1) != 0; 637 638 if (mFormat == NULL) { 639 unsigned profile = bits.getBits(2); 640 CHECK_NE(profile, 3u); 641 unsigned sampling_freq_index = bits.getBits(4); 642 bits.getBits(1); // private_bit 643 unsigned channel_configuration = bits.getBits(3); 644 CHECK_NE(channel_configuration, 0u); 645 bits.skipBits(2); // original_copy, home 646 647 mFormat = MakeAACCodecSpecificData( 648 profile, sampling_freq_index, channel_configuration); 649 650 mFormat->setInt32(kKeyIsADTS, true); 651 652 int32_t sampleRate; 653 int32_t numChannels; 654 CHECK(mFormat->findInt32(kKeySampleRate, &sampleRate)); 655 CHECK(mFormat->findInt32(kKeyChannelCount, &numChannels)); 656 657 ALOGI("found AAC codec config (%d Hz, %d channels)", 658 sampleRate, numChannels); 659 } else { 660 // profile_ObjectType, sampling_frequency_index, private_bits, 661 // channel_configuration, original_copy, home 662 bits.skipBits(12); 663 } 664 665 // adts_variable_header 666 667 // copyright_identification_bit, copyright_identification_start 668 bits.skipBits(2); 669 670 unsigned aac_frame_length = bits.getBits(13); 671 672 bits.skipBits(11); // adts_buffer_fullness 673 674 unsigned number_of_raw_data_blocks_in_frame = bits.getBits(2); 675 676 if (number_of_raw_data_blocks_in_frame != 0) { 677 // To be implemented. 678 TRESPASS(); 679 } 680 681 if (offset + aac_frame_length > mBuffer->size()) { 682 return NULL; 683 } 684 685 size_t headerSize = protection_absent ? 7 : 9; 686 687 offset += aac_frame_length; 688 // TODO: move back to concatenation when codec can support arbitrary input buffers. 689 // For now only queue a single buffer 690 break; 691 } 692 693 int64_t timeUs = fetchTimestampAAC(offset); 694 695 sp<ABuffer> accessUnit = new ABuffer(offset); 696 memcpy(accessUnit->data(), mBuffer->data(), offset); 697 698 memmove(mBuffer->data(), mBuffer->data() + offset, 699 mBuffer->size() - offset); 700 mBuffer->setRange(0, mBuffer->size() - offset); 701 702 accessUnit->meta()->setInt64("timeUs", timeUs); 703 704 return accessUnit; 705} 706 707int64_t ElementaryStreamQueue::fetchTimestamp(size_t size) { 708 int64_t timeUs = -1; 709 bool first = true; 710 711 while (size > 0) { 712 CHECK(!mRangeInfos.empty()); 713 714 RangeInfo *info = &*mRangeInfos.begin(); 715 716 if (first) { 717 timeUs = info->mTimestampUs; 718 first = false; 719 } 720 721 if (info->mLength > size) { 722 info->mLength -= size; 723 size = 0; 724 } else { 725 size -= info->mLength; 726 727 mRangeInfos.erase(mRangeInfos.begin()); 728 info = NULL; 729 } 730 731 } 732 733 if (timeUs == 0ll) { 734 ALOGV("Returning 0 timestamp"); 735 } 736 737 return timeUs; 738} 739 740// TODO: avoid interpolating timestamps once codec supports arbitrary sized input buffers 741int64_t ElementaryStreamQueue::fetchTimestampAAC(size_t size) { 742 int64_t timeUs = -1; 743 bool first = true; 744 745 size_t samplesize = size; 746 while (size > 0) { 747 CHECK(!mRangeInfos.empty()); 748 749 RangeInfo *info = &*mRangeInfos.begin(); 750 751 if (first) { 752 timeUs = info->mTimestampUs; 753 first = false; 754 } 755 756 if (info->mLength > size) { 757 int32_t sampleRate; 758 CHECK(mFormat->findInt32(kKeySampleRate, &sampleRate)); 759 info->mLength -= size; 760 size_t numSamples = 1024 * size / samplesize; 761 info->mTimestampUs += numSamples * 1000000ll / sampleRate; 762 size = 0; 763 } else { 764 size -= info->mLength; 765 766 mRangeInfos.erase(mRangeInfos.begin()); 767 info = NULL; 768 } 769 770 } 771 772 if (timeUs == 0ll) { 773 ALOGV("Returning 0 timestamp"); 774 } 775 776 return timeUs; 777} 778 779struct NALPosition { 780 size_t nalOffset; 781 size_t nalSize; 782}; 783 784sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitH264() { 785 const uint8_t *data = mBuffer->data(); 786 787 size_t size = mBuffer->size(); 788 Vector<NALPosition> nals; 789 790 size_t totalSize = 0; 791 792 status_t err; 793 const uint8_t *nalStart; 794 size_t nalSize; 795 bool foundSlice = false; 796 while ((err = getNextNALUnit(&data, &size, &nalStart, &nalSize)) == OK) { 797 if (nalSize == 0) continue; 798 799 unsigned nalType = nalStart[0] & 0x1f; 800 bool flush = false; 801 802 if (nalType == 1 || nalType == 5) { 803 if (foundSlice) { 804 ABitReader br(nalStart + 1, nalSize); 805 unsigned first_mb_in_slice = parseUE(&br); 806 807 if (first_mb_in_slice == 0) { 808 // This slice starts a new frame. 809 810 flush = true; 811 } 812 } 813 814 foundSlice = true; 815 } else if ((nalType == 9 || nalType == 7) && foundSlice) { 816 // Access unit delimiter and SPS will be associated with the 817 // next frame. 818 819 flush = true; 820 } 821 822 if (flush) { 823 // The access unit will contain all nal units up to, but excluding 824 // the current one, separated by 0x00 0x00 0x00 0x01 startcodes. 825 826 size_t auSize = 4 * nals.size() + totalSize; 827 sp<ABuffer> accessUnit = new ABuffer(auSize); 828 829#if !LOG_NDEBUG 830 AString out; 831#endif 832 833 size_t dstOffset = 0; 834 for (size_t i = 0; i < nals.size(); ++i) { 835 const NALPosition &pos = nals.itemAt(i); 836 837 unsigned nalType = mBuffer->data()[pos.nalOffset] & 0x1f; 838 839 if (nalType == 6) { 840 sp<ABuffer> sei = new ABuffer(pos.nalSize); 841 memcpy(sei->data(), mBuffer->data() + pos.nalOffset, pos.nalSize); 842 accessUnit->meta()->setBuffer("sei", sei); 843 } 844 845#if !LOG_NDEBUG 846 char tmp[128]; 847 sprintf(tmp, "0x%02x", nalType); 848 if (i > 0) { 849 out.append(", "); 850 } 851 out.append(tmp); 852#endif 853 854 memcpy(accessUnit->data() + dstOffset, "\x00\x00\x00\x01", 4); 855 856 memcpy(accessUnit->data() + dstOffset + 4, 857 mBuffer->data() + pos.nalOffset, 858 pos.nalSize); 859 860 dstOffset += pos.nalSize + 4; 861 } 862 863#if !LOG_NDEBUG 864 ALOGV("accessUnit contains nal types %s", out.c_str()); 865#endif 866 867 const NALPosition &pos = nals.itemAt(nals.size() - 1); 868 size_t nextScan = pos.nalOffset + pos.nalSize; 869 870 memmove(mBuffer->data(), 871 mBuffer->data() + nextScan, 872 mBuffer->size() - nextScan); 873 874 mBuffer->setRange(0, mBuffer->size() - nextScan); 875 876 int64_t timeUs = fetchTimestamp(nextScan); 877 CHECK_GE(timeUs, 0ll); 878 879 accessUnit->meta()->setInt64("timeUs", timeUs); 880 881 if (mFormat == NULL) { 882 mFormat = MakeAVCCodecSpecificData(accessUnit); 883 } 884 885 return accessUnit; 886 } 887 888 NALPosition pos; 889 pos.nalOffset = nalStart - mBuffer->data(); 890 pos.nalSize = nalSize; 891 892 nals.push(pos); 893 894 totalSize += nalSize; 895 } 896 CHECK_EQ(err, (status_t)-EAGAIN); 897 898 return NULL; 899} 900 901sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEGAudio() { 902 const uint8_t *data = mBuffer->data(); 903 size_t size = mBuffer->size(); 904 905 if (size < 4) { 906 return NULL; 907 } 908 909 uint32_t header = U32_AT(data); 910 911 size_t frameSize; 912 int samplingRate, numChannels, bitrate, numSamples; 913 CHECK(GetMPEGAudioFrameSize( 914 header, &frameSize, &samplingRate, &numChannels, 915 &bitrate, &numSamples)); 916 917 if (size < frameSize) { 918 return NULL; 919 } 920 921 unsigned layer = 4 - ((header >> 17) & 3); 922 923 sp<ABuffer> accessUnit = new ABuffer(frameSize); 924 memcpy(accessUnit->data(), data, frameSize); 925 926 memmove(mBuffer->data(), 927 mBuffer->data() + frameSize, 928 mBuffer->size() - frameSize); 929 930 mBuffer->setRange(0, mBuffer->size() - frameSize); 931 932 int64_t timeUs = fetchTimestamp(frameSize); 933 CHECK_GE(timeUs, 0ll); 934 935 accessUnit->meta()->setInt64("timeUs", timeUs); 936 937 if (mFormat == NULL) { 938 mFormat = new MetaData; 939 940 switch (layer) { 941 case 1: 942 mFormat->setCString( 943 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I); 944 break; 945 case 2: 946 mFormat->setCString( 947 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II); 948 break; 949 case 3: 950 mFormat->setCString( 951 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG); 952 break; 953 default: 954 TRESPASS(); 955 } 956 957 mFormat->setInt32(kKeySampleRate, samplingRate); 958 mFormat->setInt32(kKeyChannelCount, numChannels); 959 } 960 961 return accessUnit; 962} 963 964static void EncodeSize14(uint8_t **_ptr, size_t size) { 965 CHECK_LE(size, 0x3fff); 966 967 uint8_t *ptr = *_ptr; 968 969 *ptr++ = 0x80 | (size >> 7); 970 *ptr++ = size & 0x7f; 971 972 *_ptr = ptr; 973} 974 975static sp<ABuffer> MakeMPEGVideoESDS(const sp<ABuffer> &csd) { 976 sp<ABuffer> esds = new ABuffer(csd->size() + 25); 977 978 uint8_t *ptr = esds->data(); 979 *ptr++ = 0x03; 980 EncodeSize14(&ptr, 22 + csd->size()); 981 982 *ptr++ = 0x00; // ES_ID 983 *ptr++ = 0x00; 984 985 *ptr++ = 0x00; // streamDependenceFlag, URL_Flag, OCRstreamFlag 986 987 *ptr++ = 0x04; 988 EncodeSize14(&ptr, 16 + csd->size()); 989 990 *ptr++ = 0x40; // Audio ISO/IEC 14496-3 991 992 for (size_t i = 0; i < 12; ++i) { 993 *ptr++ = 0x00; 994 } 995 996 *ptr++ = 0x05; 997 EncodeSize14(&ptr, csd->size()); 998 999 memcpy(ptr, csd->data(), csd->size()); 1000 1001 return esds; 1002} 1003 1004sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEGVideo() { 1005 const uint8_t *data = mBuffer->data(); 1006 size_t size = mBuffer->size(); 1007 1008 bool sawPictureStart = false; 1009 int pprevStartCode = -1; 1010 int prevStartCode = -1; 1011 int currentStartCode = -1; 1012 1013 size_t offset = 0; 1014 while (offset + 3 < size) { 1015 if (memcmp(&data[offset], "\x00\x00\x01", 3)) { 1016 ++offset; 1017 continue; 1018 } 1019 1020 pprevStartCode = prevStartCode; 1021 prevStartCode = currentStartCode; 1022 currentStartCode = data[offset + 3]; 1023 1024 if (currentStartCode == 0xb3 && mFormat == NULL) { 1025 memmove(mBuffer->data(), mBuffer->data() + offset, size - offset); 1026 size -= offset; 1027 (void)fetchTimestamp(offset); 1028 offset = 0; 1029 mBuffer->setRange(0, size); 1030 } 1031 1032 if ((prevStartCode == 0xb3 && currentStartCode != 0xb5) 1033 || (pprevStartCode == 0xb3 && prevStartCode == 0xb5)) { 1034 // seqHeader without/with extension 1035 1036 if (mFormat == NULL) { 1037 CHECK_GE(size, 7u); 1038 1039 unsigned width = 1040 (data[4] << 4) | data[5] >> 4; 1041 1042 unsigned height = 1043 ((data[5] & 0x0f) << 8) | data[6]; 1044 1045 mFormat = new MetaData; 1046 mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2); 1047 mFormat->setInt32(kKeyWidth, width); 1048 mFormat->setInt32(kKeyHeight, height); 1049 1050 ALOGI("found MPEG2 video codec config (%d x %d)", width, height); 1051 1052 sp<ABuffer> csd = new ABuffer(offset); 1053 memcpy(csd->data(), data, offset); 1054 1055 memmove(mBuffer->data(), 1056 mBuffer->data() + offset, 1057 mBuffer->size() - offset); 1058 1059 mBuffer->setRange(0, mBuffer->size() - offset); 1060 size -= offset; 1061 (void)fetchTimestamp(offset); 1062 offset = 0; 1063 1064 // hexdump(csd->data(), csd->size()); 1065 1066 sp<ABuffer> esds = MakeMPEGVideoESDS(csd); 1067 mFormat->setData( 1068 kKeyESDS, kTypeESDS, esds->data(), esds->size()); 1069 1070 return NULL; 1071 } 1072 } 1073 1074 if (mFormat != NULL && currentStartCode == 0x00) { 1075 // Picture start 1076 1077 if (!sawPictureStart) { 1078 sawPictureStart = true; 1079 } else { 1080 sp<ABuffer> accessUnit = new ABuffer(offset); 1081 memcpy(accessUnit->data(), data, offset); 1082 1083 memmove(mBuffer->data(), 1084 mBuffer->data() + offset, 1085 mBuffer->size() - offset); 1086 1087 mBuffer->setRange(0, mBuffer->size() - offset); 1088 1089 int64_t timeUs = fetchTimestamp(offset); 1090 CHECK_GE(timeUs, 0ll); 1091 1092 offset = 0; 1093 1094 accessUnit->meta()->setInt64("timeUs", timeUs); 1095 1096 ALOGV("returning MPEG video access unit at time %" PRId64 " us", 1097 timeUs); 1098 1099 // hexdump(accessUnit->data(), accessUnit->size()); 1100 1101 return accessUnit; 1102 } 1103 } 1104 1105 ++offset; 1106 } 1107 1108 return NULL; 1109} 1110 1111static ssize_t getNextChunkSize( 1112 const uint8_t *data, size_t size) { 1113 static const char kStartCode[] = "\x00\x00\x01"; 1114 1115 if (size < 3) { 1116 return -EAGAIN; 1117 } 1118 1119 if (memcmp(kStartCode, data, 3)) { 1120 TRESPASS(); 1121 } 1122 1123 size_t offset = 3; 1124 while (offset + 2 < size) { 1125 if (!memcmp(&data[offset], kStartCode, 3)) { 1126 return offset; 1127 } 1128 1129 ++offset; 1130 } 1131 1132 return -EAGAIN; 1133} 1134 1135sp<ABuffer> ElementaryStreamQueue::dequeueAccessUnitMPEG4Video() { 1136 uint8_t *data = mBuffer->data(); 1137 size_t size = mBuffer->size(); 1138 1139 enum { 1140 SKIP_TO_VISUAL_OBJECT_SEQ_START, 1141 EXPECT_VISUAL_OBJECT_START, 1142 EXPECT_VO_START, 1143 EXPECT_VOL_START, 1144 WAIT_FOR_VOP_START, 1145 SKIP_TO_VOP_START, 1146 1147 } state; 1148 1149 if (mFormat == NULL) { 1150 state = SKIP_TO_VISUAL_OBJECT_SEQ_START; 1151 } else { 1152 state = SKIP_TO_VOP_START; 1153 } 1154 1155 int32_t width = -1, height = -1; 1156 1157 size_t offset = 0; 1158 ssize_t chunkSize; 1159 while ((chunkSize = getNextChunkSize( 1160 &data[offset], size - offset)) > 0) { 1161 bool discard = false; 1162 1163 unsigned chunkType = data[offset + 3]; 1164 1165 switch (state) { 1166 case SKIP_TO_VISUAL_OBJECT_SEQ_START: 1167 { 1168 if (chunkType == 0xb0) { 1169 // Discard anything before this marker. 1170 1171 state = EXPECT_VISUAL_OBJECT_START; 1172 } else { 1173 discard = true; 1174 } 1175 break; 1176 } 1177 1178 case EXPECT_VISUAL_OBJECT_START: 1179 { 1180 CHECK_EQ(chunkType, 0xb5); 1181 state = EXPECT_VO_START; 1182 break; 1183 } 1184 1185 case EXPECT_VO_START: 1186 { 1187 CHECK_LE(chunkType, 0x1f); 1188 state = EXPECT_VOL_START; 1189 break; 1190 } 1191 1192 case EXPECT_VOL_START: 1193 { 1194 CHECK((chunkType & 0xf0) == 0x20); 1195 1196 CHECK(ExtractDimensionsFromVOLHeader( 1197 &data[offset], chunkSize, 1198 &width, &height)); 1199 1200 state = WAIT_FOR_VOP_START; 1201 break; 1202 } 1203 1204 case WAIT_FOR_VOP_START: 1205 { 1206 if (chunkType == 0xb3 || chunkType == 0xb6) { 1207 // group of VOP or VOP start. 1208 1209 mFormat = new MetaData; 1210 mFormat->setCString( 1211 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); 1212 1213 mFormat->setInt32(kKeyWidth, width); 1214 mFormat->setInt32(kKeyHeight, height); 1215 1216 ALOGI("found MPEG4 video codec config (%d x %d)", 1217 width, height); 1218 1219 sp<ABuffer> csd = new ABuffer(offset); 1220 memcpy(csd->data(), data, offset); 1221 1222 // hexdump(csd->data(), csd->size()); 1223 1224 sp<ABuffer> esds = MakeMPEGVideoESDS(csd); 1225 mFormat->setData( 1226 kKeyESDS, kTypeESDS, 1227 esds->data(), esds->size()); 1228 1229 discard = true; 1230 state = SKIP_TO_VOP_START; 1231 } 1232 1233 break; 1234 } 1235 1236 case SKIP_TO_VOP_START: 1237 { 1238 if (chunkType == 0xb6) { 1239 offset += chunkSize; 1240 1241 sp<ABuffer> accessUnit = new ABuffer(offset); 1242 memcpy(accessUnit->data(), data, offset); 1243 1244 memmove(data, &data[offset], size - offset); 1245 size -= offset; 1246 mBuffer->setRange(0, size); 1247 1248 int64_t timeUs = fetchTimestamp(offset); 1249 CHECK_GE(timeUs, 0ll); 1250 1251 offset = 0; 1252 1253 accessUnit->meta()->setInt64("timeUs", timeUs); 1254 1255 ALOGV("returning MPEG4 video access unit at time %" PRId64 " us", 1256 timeUs); 1257 1258 // hexdump(accessUnit->data(), accessUnit->size()); 1259 1260 return accessUnit; 1261 } else if (chunkType != 0xb3) { 1262 offset += chunkSize; 1263 discard = true; 1264 } 1265 1266 break; 1267 } 1268 1269 default: 1270 TRESPASS(); 1271 } 1272 1273 if (discard) { 1274 (void)fetchTimestamp(offset); 1275 memmove(data, &data[offset], size - offset); 1276 size -= offset; 1277 offset = 0; 1278 mBuffer->setRange(0, size); 1279 } else { 1280 offset += chunkSize; 1281 } 1282 } 1283 1284 return NULL; 1285} 1286 1287} // namespace android 1288