Merge commit '254e00253ce72ee7252c26aab3a9ec6fb72e0542' * commit '254e00253ce72ee7252c26aab3a9ec6fb72e0542': Add support for XING header
This commit is contained in:
@@ -173,6 +173,133 @@ static bool get_mp3_frame_size(
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool parse_xing_header(
|
||||
const sp<DataSource> &source, off_t first_frame_pos,
|
||||
int32_t *frame_number = NULL, int32_t *byte_number = NULL,
|
||||
char *table_of_contents = NULL, int32_t *quality_indicator = NULL,
|
||||
int64_t *duration = NULL) {
|
||||
|
||||
if (frame_number) {
|
||||
*frame_number = 0;
|
||||
}
|
||||
if (byte_number) {
|
||||
*byte_number = 0;
|
||||
}
|
||||
if (table_of_contents) {
|
||||
table_of_contents[0] = 0;
|
||||
}
|
||||
if (quality_indicator) {
|
||||
*quality_indicator = 0;
|
||||
}
|
||||
if (duration) {
|
||||
*duration = 0;
|
||||
}
|
||||
|
||||
uint8_t buffer[4];
|
||||
int offset = first_frame_pos;
|
||||
if (source->readAt(offset, &buffer, 4) < 4) { // get header
|
||||
return false;
|
||||
}
|
||||
offset += 4;
|
||||
|
||||
uint8_t id, layer, sr_index, mode;
|
||||
layer = (buffer[1] >> 1) & 3;
|
||||
id = (buffer[1] >> 3) & 3;
|
||||
sr_index = (buffer[2] >> 2) & 3;
|
||||
mode = (buffer[3] >> 6) & 3;
|
||||
if (layer == 0) {
|
||||
return false;
|
||||
}
|
||||
if (id == 1) {
|
||||
return false;
|
||||
}
|
||||
if (sr_index == 3) {
|
||||
return false;
|
||||
}
|
||||
// determine offset of XING header
|
||||
if(id&1) { // mpeg1
|
||||
if (mode != 3) offset += 32;
|
||||
else offset += 17;
|
||||
} else { // mpeg2
|
||||
if (mode != 3) offset += 17;
|
||||
else offset += 9;
|
||||
}
|
||||
|
||||
if (source->readAt(offset, &buffer, 4) < 4) { // XING header ID
|
||||
return false;
|
||||
}
|
||||
offset += 4;
|
||||
// Check XING ID
|
||||
if ((buffer[0] != 'X') || (buffer[1] != 'i')
|
||||
|| (buffer[2] != 'n') || (buffer[3] != 'g')) {
|
||||
if ((buffer[0] != 'I') || (buffer[1] != 'n')
|
||||
|| (buffer[2] != 'f') || (buffer[3] != 'o')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (source->readAt(offset, &buffer, 4) < 4) { // flags
|
||||
return false;
|
||||
}
|
||||
offset += 4;
|
||||
uint32_t flags = U32_AT(buffer);
|
||||
|
||||
if (flags & 0x0001) { // Frames field is present
|
||||
if (source->readAt(offset, buffer, 4) < 4) {
|
||||
return false;
|
||||
}
|
||||
if (frame_number) {
|
||||
*frame_number = U32_AT(buffer);
|
||||
}
|
||||
int32_t frame = U32_AT(buffer);
|
||||
// Samples per Frame: 1. index = MPEG Version ID, 2. index = Layer
|
||||
const int samplesPerFrames[2][3] =
|
||||
{
|
||||
{ 384, 1152, 576 }, // MPEG 2, 2.5: layer1, layer2, layer3
|
||||
{ 384, 1152, 1152 }, // MPEG 1: layer1, layer2, layer3
|
||||
};
|
||||
// sampling rates in hertz: 1. index = MPEG Version ID, 2. index = sampling rate index
|
||||
const int samplingRates[4][3] =
|
||||
{
|
||||
{ 11025, 12000, 8000, }, // MPEG 2.5
|
||||
{ 0, 0, 0, }, // reserved
|
||||
{ 22050, 24000, 16000, }, // MPEG 2
|
||||
{ 44100, 48000, 32000, } // MPEG 1
|
||||
};
|
||||
if (duration) {
|
||||
*duration = (int64_t)frame * samplesPerFrames[id&1][3-layer] * 1000000LL
|
||||
/ samplingRates[id][sr_index];
|
||||
}
|
||||
offset += 4;
|
||||
}
|
||||
if (flags & 0x0002) { // Bytes field is present
|
||||
if (byte_number) {
|
||||
if (source->readAt(offset, buffer, 4) < 4) {
|
||||
return false;
|
||||
}
|
||||
*byte_number = U32_AT(buffer);
|
||||
}
|
||||
offset += 4;
|
||||
}
|
||||
if (flags & 0x0004) { // TOC field is present
|
||||
if (table_of_contents) {
|
||||
if (source->readAt(offset + 1, table_of_contents, 99) < 99) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
offset += 100;
|
||||
}
|
||||
if (flags & 0x0008) { // Quality indicator field is present
|
||||
if (quality_indicator) {
|
||||
if (source->readAt(offset, buffer, 4) < 4) {
|
||||
return false;
|
||||
}
|
||||
*quality_indicator = U32_AT(buffer);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool Resync(
|
||||
const sp<DataSource> &source, uint32_t match_header,
|
||||
off_t *inout_pos, uint32_t *out_header) {
|
||||
@@ -314,7 +441,8 @@ class MP3Source : public MediaSource {
|
||||
public:
|
||||
MP3Source(
|
||||
const sp<MetaData> &meta, const sp<DataSource> &source,
|
||||
off_t first_frame_pos, uint32_t fixed_header);
|
||||
off_t first_frame_pos, uint32_t fixed_header,
|
||||
int32_t byte_number, const char *table_of_contents);
|
||||
|
||||
virtual status_t start(MetaData *params = NULL);
|
||||
virtual status_t stop();
|
||||
@@ -335,7 +463,9 @@ private:
|
||||
off_t mCurrentPos;
|
||||
int64_t mCurrentTimeUs;
|
||||
bool mStarted;
|
||||
|
||||
int32_t mByteNumber; // total number of bytes in this MP3
|
||||
// TOC entries in XING header. Skip the first one since it's always 0.
|
||||
char mTableOfContents[99];
|
||||
MediaBufferGroup *mGroup;
|
||||
|
||||
MP3Source(const MP3Source &);
|
||||
@@ -345,7 +475,8 @@ private:
|
||||
MP3Extractor::MP3Extractor(const sp<DataSource> &source)
|
||||
: mDataSource(source),
|
||||
mFirstFramePos(-1),
|
||||
mFixedHeader(0) {
|
||||
mFixedHeader(0),
|
||||
mByteNumber(0) {
|
||||
off_t pos = 0;
|
||||
uint32_t header;
|
||||
bool success = Resync(mDataSource, 0, &pos, &header);
|
||||
@@ -369,11 +500,19 @@ MP3Extractor::MP3Extractor(const sp<DataSource> &source)
|
||||
mMeta->setInt32(kKeyBitRate, bitrate * 1000);
|
||||
mMeta->setInt32(kKeyChannelCount, num_channels);
|
||||
|
||||
off_t fileSize;
|
||||
if (mDataSource->getSize(&fileSize) == OK) {
|
||||
mMeta->setInt64(
|
||||
kKeyDuration,
|
||||
8000LL * (fileSize - mFirstFramePos) / bitrate);
|
||||
int64_t duration;
|
||||
parse_xing_header(
|
||||
mDataSource, mFirstFramePos, NULL, &mByteNumber,
|
||||
mTableOfContents, NULL, &duration);
|
||||
if (duration > 0) {
|
||||
mMeta->setInt64(kKeyDuration, duration);
|
||||
} else {
|
||||
off_t fileSize;
|
||||
if (mDataSource->getSize(&fileSize) == OK) {
|
||||
mMeta->setInt64(
|
||||
kKeyDuration,
|
||||
8000LL * (fileSize - mFirstFramePos) / bitrate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -391,7 +530,8 @@ sp<MediaSource> MP3Extractor::getTrack(size_t index) {
|
||||
}
|
||||
|
||||
return new MP3Source(
|
||||
mMeta, mDataSource, mFirstFramePos, mFixedHeader);
|
||||
mMeta, mDataSource, mFirstFramePos, mFixedHeader,
|
||||
mByteNumber, mTableOfContents);
|
||||
}
|
||||
|
||||
sp<MetaData> MP3Extractor::getTrackMetaData(size_t index, uint32_t flags) {
|
||||
@@ -406,7 +546,8 @@ sp<MetaData> MP3Extractor::getTrackMetaData(size_t index, uint32_t flags) {
|
||||
|
||||
MP3Source::MP3Source(
|
||||
const sp<MetaData> &meta, const sp<DataSource> &source,
|
||||
off_t first_frame_pos, uint32_t fixed_header)
|
||||
off_t first_frame_pos, uint32_t fixed_header,
|
||||
int32_t byte_number, const char *table_of_contents)
|
||||
: mMeta(meta),
|
||||
mDataSource(source),
|
||||
mFirstFramePos(first_frame_pos),
|
||||
@@ -414,7 +555,9 @@ MP3Source::MP3Source(
|
||||
mCurrentPos(0),
|
||||
mCurrentTimeUs(0),
|
||||
mStarted(false),
|
||||
mByteNumber(byte_number),
|
||||
mGroup(NULL) {
|
||||
memcpy (mTableOfContents, table_of_contents, strlen(table_of_contents));
|
||||
}
|
||||
|
||||
MP3Source::~MP3Source() {
|
||||
@@ -469,7 +612,35 @@ status_t MP3Source::read(
|
||||
}
|
||||
|
||||
mCurrentTimeUs = seekTimeUs;
|
||||
mCurrentPos = mFirstFramePos + seekTimeUs * bitrate / 8000000;
|
||||
// interpolate in TOC to get file seek point in bytes
|
||||
int64_t duration;
|
||||
if ((mByteNumber > 0) && (mTableOfContents[0] > 0)
|
||||
&& mMeta->findInt64(kKeyDuration, &duration)) {
|
||||
float percent = (float)seekTimeUs * 100 / duration;
|
||||
float fx;
|
||||
if( percent <= 0.0f ) {
|
||||
fx = 0.0f;
|
||||
} else if( percent >= 100.0f ) {
|
||||
fx = 256.0f;
|
||||
} else {
|
||||
int a = (int)percent;
|
||||
float fa, fb;
|
||||
if ( a == 0 ) {
|
||||
fa = 0.0f;
|
||||
} else {
|
||||
fa = (float)mTableOfContents[a-1];
|
||||
}
|
||||
if ( a < 99 ) {
|
||||
fb = (float)mTableOfContents[a];
|
||||
} else {
|
||||
fb = 256.0f;
|
||||
}
|
||||
fx = fa + (fb-fa)*(percent-a);
|
||||
}
|
||||
mCurrentPos = mFirstFramePos + (int)((1.0f/256.0f)*fx*mByteNumber);
|
||||
} else {
|
||||
mCurrentPos = mFirstFramePos + seekTimeUs * bitrate / 8000000;
|
||||
}
|
||||
}
|
||||
|
||||
MediaBuffer *buffer;
|
||||
|
||||
@@ -42,6 +42,8 @@ private:
|
||||
off_t mFirstFramePos;
|
||||
sp<MetaData> mMeta;
|
||||
uint32_t mFixedHeader;
|
||||
int32_t mByteNumber; // total number of bytes in this MP3
|
||||
char mTableOfContents[99]; // TOC entries in XING header
|
||||
|
||||
MP3Extractor(const MP3Extractor &);
|
||||
MP3Extractor &operator=(const MP3Extractor &);
|
||||
|
||||
Reference in New Issue
Block a user