1bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi/* 2bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * Copyright (C) 2011 The Android Open Source Project 3bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * 4bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License"); 5bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * you may not use this file except in compliance with the License. 6bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * You may obtain a copy of the License at 7bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * 8bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * http://www.apache.org/licenses/LICENSE-2.0 9bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * 10bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software 11bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS, 12bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * See the License for the specific language governing permissions and 14bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi * limitations under the License. 15bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi */ 16bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 17bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#include "sllog.h" 18bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#include <utils/Log.h> 19bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 20bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#include "android/include/AacAdtsExtractor.h" 21bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#include "include/avc_utils.h" 22bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 23bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 24bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivinamespace android { 25bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 26bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ADTS_HEADER_LENGTH 7 27bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi// ADTS header size is 7, but frame size information ends on byte 6 (when counting from byte 1) 28bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ADTS_HEADER_SIZE_UP_TO_FRAMESIZE 6 29bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 30bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi//////////////////////////////////////////////////////////////////////////////// 31bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 32bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi// Returns the sample rate based on the sampling frequency index 33bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatic uint32_t get_sample_rate(const uint8_t sf_index) 34bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi{ 35bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi static const uint32_t sample_rates[] = 36bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi { 37bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 96000, 88200, 64000, 48000, 44100, 32000, 38bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 24000, 22050, 16000, 12000, 11025, 8000 39bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi }; 40bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 41bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if (sf_index < sizeof(sample_rates) / sizeof(sample_rates[0])) { 42bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return sample_rates[sf_index]; 43bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 44bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 45bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return 0; 46bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 47bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 48bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatic size_t getFrameSize(const sp<DataSource> &source, off64_t offset) { 49bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi size_t frameSize = 0; 50bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 51bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi uint8_t syncHeader[ADTS_HEADER_SIZE_UP_TO_FRAMESIZE]; 52bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi const uint8_t *syncword = syncHeader; 53bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi const uint8_t *header = syncHeader + 3; 54bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 55677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten ssize_t readSize = source->readAt(offset, &syncHeader, ADTS_HEADER_SIZE_UP_TO_FRAMESIZE); 56677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten if (readSize == 0) { 57677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten // EOS is normal, not an error 58677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten SL_LOGV("AacAdtsExtractor::getFrameSize EOS"); 59677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten return 0; 60677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten } 61677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten if (readSize != ADTS_HEADER_SIZE_UP_TO_FRAMESIZE) { 62677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten SL_LOGE("AacAdtsExtractor:: getFrameSize() returns %d (syncword and header read error)", 63677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten (int) readSize); 64bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return 0; 65bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 66bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 67bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if ((syncword[0] != 0xff) || ((syncword[1] & 0xf6) != 0xf0)) { 68bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi SL_LOGE("AacAdtsExtractor:: getFrameSize() returns 0 (syncword pb)"); 69bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return 0; 70bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 71bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 72bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi const uint8_t protectionAbsent = syncword[1] & 0x1; 73bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 74bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi frameSize = (header[0] & 0x3) << 11 | header[1] << 3 | header[2] >> 5; 75bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi // the frame size read already contains the size of the ADTS header, so no need to add it here 76145156a92c1d379e494cb69920d49b87f9c0e300Glenn Kasten 77145156a92c1d379e494cb69920d49b87f9c0e300Glenn Kasten // protectionAbsent is 0 if there is CRC 78145156a92c1d379e494cb69920d49b87f9c0e300Glenn Kasten static const size_t kAdtsHeaderLengthNoCrc = 7; 79145156a92c1d379e494cb69920d49b87f9c0e300Glenn Kasten static const size_t kAdtsHeaderLengthWithCrc = 9; 80145156a92c1d379e494cb69920d49b87f9c0e300Glenn Kasten size_t headSize = protectionAbsent ? kAdtsHeaderLengthNoCrc : kAdtsHeaderLengthWithCrc; 81145156a92c1d379e494cb69920d49b87f9c0e300Glenn Kasten if (headSize > frameSize) { 8201f8573bc2a850536b02855d483dfe130c050a2fAshok Bhat SL_LOGE("AacAdtsExtractor:: getFrameSize() returns 0 (frameSize %zu < headSize %zu)", 83145156a92c1d379e494cb69920d49b87f9c0e300Glenn Kasten frameSize, headSize); 84145156a92c1d379e494cb69920d49b87f9c0e300Glenn Kasten return 0; 85145156a92c1d379e494cb69920d49b87f9c0e300Glenn Kasten } 86bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 87bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi //SL_LOGV("AacAdtsExtractor:: getFrameSize() returns %u", frameSize); 88bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 89bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return frameSize; 90bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 91bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 92bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 93bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsExtractor::AacAdtsExtractor(const sp<DataSource> &source) 94bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi : mDataSource(source), 95bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mInitCheck(NO_INIT), 96bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mFrameDurationUs(0) { 97bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 98bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi // difference with framework's AAC Extractor: we have already validated the data 99bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi // upon enqueueing, so no need to sniff the data: 100bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi // String8 mimeType; 101bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi // float confidence; 102bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi // if (!SniffAAC(mDataSource, &mimeType, &confidence, NULL)) { 103bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi // return; 104bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi // } 105bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 106bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi uint8_t profile, sf_index, channel, header[2]; 1076e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten ssize_t readSize = mDataSource->readAt(2, &header, 2); 1086e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten if (readSize != 2) { 1096e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten SL_LOGE("Unable to determine sample rate"); 110bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return; 111bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 112bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 113bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi profile = (header[0] >> 6) & 0x3; 114bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi sf_index = (header[0] >> 2) & 0xf; 115bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi uint32_t sr = get_sample_rate(sf_index); 116bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 117bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if (sr == 0) { 1186e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten SL_LOGE("Invalid sample rate"); 119bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return; 120bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 121bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi channel = (header[0] & 0x1) << 2 | (header[1] >> 6); 122bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 123bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi SL_LOGV("AacAdtsExtractor has found sr=%d channel=%d", sr, channel); 124bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 1256e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten // Never fails 126bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mMeta = MakeAACCodecSpecificData(profile, sf_index, channel); 127bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 1284efb2aa0d563b86b3c95d418d6d61d97f51c0bbbGlenn Kasten // Round up and get the duration of each frame 1294efb2aa0d563b86b3c95d418d6d61d97f51c0bbbGlenn Kasten mFrameDurationUs = (1024 * 1000000ll + (sr - 1)) / sr; 130bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 1314efb2aa0d563b86b3c95d418d6d61d97f51c0bbbGlenn Kasten off64_t streamSize; 132bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if (mDataSource->getSize(&streamSize) == OK) { 1334efb2aa0d563b86b3c95d418d6d61d97f51c0bbbGlenn Kasten off64_t offset = 0, numFrames = 0; 134bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi while (offset < streamSize) { 1354efb2aa0d563b86b3c95d418d6d61d97f51c0bbbGlenn Kasten size_t frameSize; 136bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if ((frameSize = getFrameSize(mDataSource, offset)) == 0) { 1376e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten // Usually frameSize == 0 due to EOS is benign (and getFrameSize() doesn't SL_LOGE), 1386e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten // but in this case we were told the total size of the data source and so an EOS 1396e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten // should not happen. 140a6585530d8cf92658042c001b6a27fc397c62b9fGlenn Kasten SL_LOGE("AacAdtsExtractor() failed querying framesize at offset=%lld", (long long) offset); 141bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return; 142bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 143bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 144bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi offset += frameSize; 1456e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten if (offset > streamSize) { 1466e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten SL_LOGE("AacAdtsExtractor() frame of size %zu at offset=%lld is beyond EOF %lld", 147a6585530d8cf92658042c001b6a27fc397c62b9fGlenn Kasten frameSize, (long long) offset, (long long) streamSize); 1486e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten return; 1496e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten } 150bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi numFrames ++; 151bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 152bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 1534efb2aa0d563b86b3c95d418d6d61d97f51c0bbbGlenn Kasten // Compute total duration 1544efb2aa0d563b86b3c95d418d6d61d97f51c0bbbGlenn Kasten int64_t duration = numFrames * mFrameDurationUs; 155bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mMeta->setInt64(kKeyDuration, duration); 156bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 157bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 1586e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten // Any earlier "return" would leave mInitCheck as NO_INIT, causing later methods to fail quickly 159bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mInitCheck = OK; 160bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 161bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 162bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 163bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 164bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsExtractor::~AacAdtsExtractor() { 165bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 166bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 167bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 168bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MetaData> AacAdtsExtractor::getMetaData() { 169bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi sp<MetaData> meta = new MetaData; 170bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 171bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if (mInitCheck != OK) { 172bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return meta; 173bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 174bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 175bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC_ADTS); 176bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 177bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return meta; 178bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 179bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 180bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 181bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisize_t AacAdtsExtractor::countTracks() { 182bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return mInitCheck == OK ? 1 : 0; 183bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 184bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 185bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 186bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MediaSource> AacAdtsExtractor::getTrack(size_t index) { 187bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if (mInitCheck != OK || index != 0) { 188bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return NULL; 189bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 190bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 191bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return new AacAdtsSource(mDataSource, mMeta, mFrameDurationUs); 192bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 193bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 194bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 195bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MetaData> AacAdtsExtractor::getTrackMetaData(size_t index, uint32_t flags) { 196bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if (mInitCheck != OK || index != 0) { 197bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return NULL; 198bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 199bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 200bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return mMeta; 201bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 202bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 203bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 204bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi//////////////////////////////////////////////////////////////////////////////// 205bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 206bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi// 8192 = 2^13, 13bit AAC frame size (in bytes) 207bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Triviconst size_t AacAdtsSource::kMaxFrameSize = 8192; 208bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 209bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsSource::AacAdtsSource( 210bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi const sp<DataSource> &source, const sp<MetaData> &meta, 211bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi int64_t frame_duration_us) 212bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi : mDataSource(source), 213bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mMeta(meta), 214bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mOffset(0), 215bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mCurrentTimeUs(0), 216bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mStarted(false), 217bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mGroup(NULL), 218bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mFrameDurationUs(frame_duration_us) { 219bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 220bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 221bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 222bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel TriviAacAdtsSource::~AacAdtsSource() { 223bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if (mStarted) { 224bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi stop(); 225bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 226bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 227bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 228bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 229bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatus_t AacAdtsSource::start(MetaData *params) { 230bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi CHECK(!mStarted); 231bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 232bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mOffset = 0; 233bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mCurrentTimeUs = 0; 234bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mGroup = new MediaBufferGroup; 235bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mGroup->add_buffer(new MediaBuffer(kMaxFrameSize)); 236bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mStarted = true; 237bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 238bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return OK; 239bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 240bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 241bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 242bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatus_t AacAdtsSource::stop() { 243bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi CHECK(mStarted); 244bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 245bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi delete mGroup; 246bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mGroup = NULL; 247bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 248bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mStarted = false; 249bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return OK; 250bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 251bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 252bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 253bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivisp<MetaData> AacAdtsSource::getFormat() { 254bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return mMeta; 255bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 256bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 257bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 258bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatus_t AacAdtsSource::read( 259bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi MediaBuffer **out, const ReadOptions *options) { 260bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi *out = NULL; 261bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 262bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi int64_t seekTimeUs; 263bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi ReadOptions::SeekMode mode; 264bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if (options && options->getSeekTo(&seekTimeUs, &mode)) { 265bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi // difference with framework's AAC Extractor: no seeking 266bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi SL_LOGE("Can't seek in AAC ADTS buffer queue"); 267bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 268bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 269bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi size_t frameSize, frameSizeWithoutHeader; 270bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi SL_LOGV("AacAdtsSource::read() offset=%lld", mOffset); 271bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if ((frameSize = getFrameSize(mDataSource, mOffset)) == 0) { 2726e0a69b1185405c11a88e3d7c8a8278ac93ccd54Glenn Kasten // EOS is normal, not an error 273677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten SL_LOGV("AacAdtsSource::read() returns EOS"); 274bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return ERROR_END_OF_STREAM; 275bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 276bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 277bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi MediaBuffer *buffer; 278bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi status_t err = mGroup->acquire_buffer(&buffer); 279bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if (err != OK) { 280bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return err; 281bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 282bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 283bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi frameSizeWithoutHeader = frameSize - ADTS_HEADER_LENGTH; 284677c76347d9aaca4cf3746b3dbfc8a741281066bGlenn Kasten ssize_t readSize = mDataSource->readAt(mOffset + ADTS_HEADER_LENGTH, buffer->data(), 285bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi frameSizeWithoutHeader); 286bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi //SL_LOGV("AacAdtsSource::read() readAt returned %u bytes", readSize); 287bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi if (readSize != (ssize_t)frameSizeWithoutHeader) { 288bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi SL_LOGW("AacAdtsSource::read() readSize != frameSizeWithoutHeader"); 289bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi buffer->release(); 290bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi buffer = NULL; 291bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return ERROR_IO; 292bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi } 293bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 294bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi buffer->set_range(0, frameSizeWithoutHeader); 295bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi buffer->meta_data()->setInt64(kKeyTime, mCurrentTimeUs); 296bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); 297bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 298bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mOffset += frameSize; 299bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi mCurrentTimeUs += mFrameDurationUs; 300bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 301bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi *out = buffer; 302bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi return OK; 303bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} 304bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 305bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi} // namespace android 306