Adds a WAVExtractor for 16-bit signed PCM audio wave files.
This commit is contained in:
@ -34,6 +34,7 @@ extern const char *MEDIA_MIMETYPE_AUDIO_AAC;
|
||||
extern const char *MEDIA_MIMETYPE_AUDIO_RAW;
|
||||
|
||||
extern const char *MEDIA_MIMETYPE_CONTAINER_MPEG4;
|
||||
extern const char *MEDIA_MIMETYPE_CONTAINER_WAV;
|
||||
|
||||
} // namespace android
|
||||
|
||||
|
@ -16,6 +16,7 @@ ifeq ($(BUILD_WITH_FULL_STAGEFRIGHT),true)
|
||||
|
||||
LOCAL_SRC_FILES += \
|
||||
AMRExtractor.cpp \
|
||||
AudioPlayer.cpp \
|
||||
CachingDataSource.cpp \
|
||||
CameraSource.cpp \
|
||||
DataSource.cpp \
|
||||
@ -23,17 +24,17 @@ LOCAL_SRC_FILES += \
|
||||
HTTPDataSource.cpp \
|
||||
HTTPStream.cpp \
|
||||
JPEGSource.cpp \
|
||||
MediaExtractor.cpp \
|
||||
MP3Extractor.cpp \
|
||||
MPEG4Extractor.cpp \
|
||||
MPEG4Writer.cpp \
|
||||
MediaExtractor.cpp \
|
||||
MediaPlayerImpl.cpp \
|
||||
MmapSource.cpp \
|
||||
SampleTable.cpp \
|
||||
ShoutcastSource.cpp \
|
||||
TimeSource.cpp \
|
||||
TimedEventQueue.cpp \
|
||||
AudioPlayer.cpp \
|
||||
WAVExtractor.cpp \
|
||||
string.cpp
|
||||
|
||||
endif
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include "include/AMRExtractor.h"
|
||||
#include "include/MP3Extractor.h"
|
||||
#include "include/MPEG4Extractor.h"
|
||||
#include "include/WAVExtractor.h"
|
||||
|
||||
#include <media/stagefright/DataSource.h>
|
||||
#include <media/stagefright/MediaErrors.h>
|
||||
@ -87,6 +88,7 @@ void DataSource::RegisterDefaultSniffers() {
|
||||
RegisterSniffer(SniffMP3);
|
||||
RegisterSniffer(SniffMPEG4);
|
||||
RegisterSniffer(SniffAMR);
|
||||
RegisterSniffer(SniffWAV);
|
||||
}
|
||||
|
||||
} // namespace android
|
||||
|
@ -1001,7 +1001,8 @@ bool SniffMPEG4(
|
||||
}
|
||||
|
||||
if (!memcmp(header, "ftyp3gp", 7) || !memcmp(header, "ftypmp42", 8)
|
||||
|| !memcmp(header, "ftypisom", 8) || !memcmp(header, "ftypM4V ", 8)) {
|
||||
|| !memcmp(header, "ftypisom", 8) || !memcmp(header, "ftypM4V ", 8)
|
||||
|| !memcmp(header, "ftypM4A ", 8)) {
|
||||
*mimeType = MEDIA_MIMETYPE_CONTAINER_MPEG4;
|
||||
*confidence = 0.1;
|
||||
|
||||
|
@ -32,5 +32,6 @@ const char *MEDIA_MIMETYPE_AUDIO_AAC = "audio/mp4a-latm";
|
||||
const char *MEDIA_MIMETYPE_AUDIO_RAW = "audio/raw";
|
||||
|
||||
const char *MEDIA_MIMETYPE_CONTAINER_MPEG4 = "video/mpeg4";
|
||||
const char *MEDIA_MIMETYPE_CONTAINER_WAV = "audio/wav";
|
||||
|
||||
} // namespace android
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include "include/AMRExtractor.h"
|
||||
#include "include/MP3Extractor.h"
|
||||
#include "include/MPEG4Extractor.h"
|
||||
#include "include/WAVExtractor.h"
|
||||
|
||||
#include <media/stagefright/CachingDataSource.h>
|
||||
#include <media/stagefright/DataSource.h>
|
||||
@ -57,6 +58,8 @@ sp<MediaExtractor> MediaExtractor::Create(
|
||||
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)
|
||||
|| !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) {
|
||||
return new AMRExtractor(source);
|
||||
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV)) {
|
||||
return new WAVExtractor(source);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
317
media/libstagefright/WAVExtractor.cpp
Normal file
317
media/libstagefright/WAVExtractor.cpp
Normal file
@ -0,0 +1,317 @@
|
||||
/*
|
||||
* Copyright (C) 2009 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//#define LOG_NDEBUG 0
|
||||
#define LOG_TAG "WAVExtractor"
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include "include/WAVExtractor.h"
|
||||
|
||||
#include <media/stagefright/DataSource.h>
|
||||
#include <media/stagefright/MediaBufferGroup.h>
|
||||
#include <media/stagefright/MediaDebug.h>
|
||||
#include <media/stagefright/MediaDefs.h>
|
||||
#include <media/stagefright/MediaErrors.h>
|
||||
#include <media/stagefright/MediaSource.h>
|
||||
#include <media/stagefright/MetaData.h>
|
||||
#include <utils/String8.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
static uint16_t WAVE_FORMAT_PCM = 1;
|
||||
|
||||
static uint32_t U32_LE_AT(const uint8_t *ptr) {
|
||||
return ptr[3] << 24 | ptr[2] << 16 | ptr[1] << 8 | ptr[0];
|
||||
}
|
||||
|
||||
static uint16_t U16_LE_AT(const uint8_t *ptr) {
|
||||
return ptr[1] << 8 | ptr[0];
|
||||
}
|
||||
|
||||
struct WAVSource : public MediaSource {
|
||||
WAVSource(
|
||||
const sp<DataSource> &dataSource,
|
||||
int32_t sampleRate, int32_t numChannels,
|
||||
off_t offset, size_t size);
|
||||
|
||||
virtual status_t start(MetaData *params = NULL);
|
||||
virtual status_t stop();
|
||||
virtual sp<MetaData> getFormat();
|
||||
|
||||
virtual status_t read(
|
||||
MediaBuffer **buffer, const ReadOptions *options = NULL);
|
||||
|
||||
protected:
|
||||
virtual ~WAVSource();
|
||||
|
||||
private:
|
||||
static const size_t kMaxFrameSize;
|
||||
|
||||
sp<DataSource> mDataSource;
|
||||
int32_t mSampleRate;
|
||||
int32_t mNumChannels;
|
||||
off_t mOffset;
|
||||
size_t mSize;
|
||||
bool mStarted;
|
||||
MediaBufferGroup *mGroup;
|
||||
off_t mCurrentPos;
|
||||
|
||||
WAVSource(const WAVSource &);
|
||||
WAVSource &operator=(const WAVSource &);
|
||||
};
|
||||
|
||||
WAVExtractor::WAVExtractor(const sp<DataSource> &source)
|
||||
: mDataSource(source),
|
||||
mValidFormat(false) {
|
||||
mInitCheck = init();
|
||||
}
|
||||
|
||||
WAVExtractor::~WAVExtractor() {
|
||||
}
|
||||
|
||||
size_t WAVExtractor::countTracks() {
|
||||
return mInitCheck == OK ? 1 : 0;
|
||||
}
|
||||
|
||||
sp<MediaSource> WAVExtractor::getTrack(size_t index) {
|
||||
if (mInitCheck != OK || index > 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return new WAVSource(
|
||||
mDataSource, mSampleRate, mNumChannels, mDataOffset, mDataSize);
|
||||
}
|
||||
|
||||
sp<MetaData> WAVExtractor::getTrackMetaData(
|
||||
size_t index, uint32_t flags) {
|
||||
if (mInitCheck != OK || index > 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sp<MetaData> meta = new MetaData;
|
||||
meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
|
||||
meta->setInt32(kKeyChannelCount, mNumChannels);
|
||||
meta->setInt32(kKeySampleRate, mSampleRate);
|
||||
|
||||
int64_t durationUs =
|
||||
1000000LL * (mDataSize / (mNumChannels * 2)) / mSampleRate;
|
||||
|
||||
meta->setInt64(kKeyDuration, durationUs);
|
||||
|
||||
return meta;
|
||||
}
|
||||
|
||||
status_t WAVExtractor::init() {
|
||||
uint8_t header[12];
|
||||
if (mDataSource->readAt(
|
||||
0, header, sizeof(header)) < (ssize_t)sizeof(header)) {
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
if (memcmp(header, "RIFF", 4) || memcmp(&header[8], "WAVE", 4)) {
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
size_t totalSize = U32_LE_AT(&header[4]);
|
||||
|
||||
off_t offset = 12;
|
||||
size_t remainingSize = totalSize;
|
||||
while (remainingSize >= 8) {
|
||||
uint8_t chunkHeader[8];
|
||||
if (mDataSource->readAt(offset, chunkHeader, 8) < 8) {
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
remainingSize -= 8;
|
||||
offset += 8;
|
||||
|
||||
uint32_t chunkSize = U32_LE_AT(&chunkHeader[4]);
|
||||
|
||||
if (chunkSize > remainingSize) {
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
if (!memcmp(chunkHeader, "fmt ", 4)) {
|
||||
if (chunkSize < 16) {
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
uint8_t formatSpec[16];
|
||||
if (mDataSource->readAt(offset, formatSpec, 16) < 16) {
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
uint16_t format = U16_LE_AT(formatSpec);
|
||||
if (format != WAVE_FORMAT_PCM) {
|
||||
return ERROR_UNSUPPORTED;
|
||||
}
|
||||
|
||||
mNumChannels = U16_LE_AT(&formatSpec[2]);
|
||||
if (mNumChannels != 1 && mNumChannels != 2) {
|
||||
return ERROR_UNSUPPORTED;
|
||||
}
|
||||
|
||||
mSampleRate = U32_LE_AT(&formatSpec[4]);
|
||||
|
||||
if (U16_LE_AT(&formatSpec[14]) != 16) {
|
||||
return ERROR_UNSUPPORTED;
|
||||
}
|
||||
|
||||
mValidFormat = true;
|
||||
} else if (!memcmp(chunkHeader, "data", 4)) {
|
||||
if (mValidFormat) {
|
||||
mDataOffset = offset;
|
||||
mDataSize = chunkSize;
|
||||
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
|
||||
offset += chunkSize;
|
||||
}
|
||||
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
const size_t WAVSource::kMaxFrameSize = 32768;
|
||||
|
||||
WAVSource::WAVSource(
|
||||
const sp<DataSource> &dataSource,
|
||||
int32_t sampleRate, int32_t numChannels,
|
||||
off_t offset, size_t size)
|
||||
: mDataSource(dataSource),
|
||||
mSampleRate(sampleRate),
|
||||
mNumChannels(numChannels),
|
||||
mOffset(offset),
|
||||
mSize(size),
|
||||
mStarted(false),
|
||||
mGroup(NULL) {
|
||||
}
|
||||
|
||||
WAVSource::~WAVSource() {
|
||||
if (mStarted) {
|
||||
stop();
|
||||
}
|
||||
}
|
||||
|
||||
status_t WAVSource::start(MetaData *params) {
|
||||
LOGV("WAVSource::start");
|
||||
|
||||
CHECK(!mStarted);
|
||||
|
||||
mGroup = new MediaBufferGroup;
|
||||
mGroup->add_buffer(new MediaBuffer(kMaxFrameSize));
|
||||
|
||||
mCurrentPos = mOffset;
|
||||
|
||||
mStarted = true;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
status_t WAVSource::stop() {
|
||||
LOGV("WAVSource::stop");
|
||||
|
||||
CHECK(mStarted);
|
||||
|
||||
delete mGroup;
|
||||
mGroup = NULL;
|
||||
|
||||
mStarted = false;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
sp<MetaData> WAVSource::getFormat() {
|
||||
LOGV("WAVSource::getFormat");
|
||||
|
||||
sp<MetaData> meta = new MetaData;
|
||||
meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
|
||||
meta->setInt32(kKeyChannelCount, mNumChannels);
|
||||
meta->setInt32(kKeySampleRate, mSampleRate);
|
||||
|
||||
int64_t durationUs =
|
||||
1000000LL * (mSize / (mNumChannels * 2)) / mSampleRate;
|
||||
|
||||
meta->setInt64(kKeyDuration, durationUs);
|
||||
|
||||
return meta;
|
||||
}
|
||||
|
||||
status_t WAVSource::read(
|
||||
MediaBuffer **out, const ReadOptions *options) {
|
||||
*out = NULL;
|
||||
|
||||
int64_t seekTimeUs;
|
||||
if (options != NULL && options->getSeekTo(&seekTimeUs)) {
|
||||
int64_t pos = (seekTimeUs * mSampleRate) / 1000000 * mNumChannels * 2;
|
||||
if (pos > mSize) {
|
||||
pos = mSize;
|
||||
}
|
||||
mCurrentPos = pos + mOffset;
|
||||
}
|
||||
|
||||
MediaBuffer *buffer;
|
||||
status_t err = mGroup->acquire_buffer(&buffer);
|
||||
if (err != OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
ssize_t n = mDataSource->readAt(
|
||||
mCurrentPos, buffer->data(), kMaxFrameSize);
|
||||
|
||||
if (n <= 0) {
|
||||
buffer->release();
|
||||
buffer = NULL;
|
||||
|
||||
return ERROR_END_OF_STREAM;
|
||||
}
|
||||
|
||||
mCurrentPos += n;
|
||||
|
||||
buffer->set_range(0, n);
|
||||
buffer->meta_data()->setInt64(
|
||||
kKeyTime,
|
||||
1000000LL * (mCurrentPos - mOffset)
|
||||
/ (mNumChannels * 2) / mSampleRate);
|
||||
|
||||
|
||||
*out = buffer;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool SniffWAV(
|
||||
const sp<DataSource> &source, String8 *mimeType, float *confidence) {
|
||||
char header[12];
|
||||
if (source->readAt(0, header, sizeof(header)) < (ssize_t)sizeof(header)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (memcmp(header, "RIFF", 4) || memcmp(&header[8], "WAVE", 4)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*mimeType = MEDIA_MIMETYPE_CONTAINER_WAV;
|
||||
*confidence = 0.3f;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace android
|
||||
|
61
media/libstagefright/include/WAVExtractor.h
Normal file
61
media/libstagefright/include/WAVExtractor.h
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (C) 2009 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef WAV_EXTRACTOR_H_
|
||||
|
||||
#define WAV_EXTRACTOR_H_
|
||||
|
||||
#include <media/stagefright/MediaExtractor.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
class DataSource;
|
||||
class String8;
|
||||
|
||||
class WAVExtractor : public MediaExtractor {
|
||||
public:
|
||||
// Extractor assumes ownership of "source".
|
||||
WAVExtractor(const sp<DataSource> &source);
|
||||
|
||||
virtual size_t countTracks();
|
||||
virtual sp<MediaSource> getTrack(size_t index);
|
||||
virtual sp<MetaData> getTrackMetaData(size_t index, uint32_t flags);
|
||||
|
||||
protected:
|
||||
virtual ~WAVExtractor();
|
||||
|
||||
private:
|
||||
sp<DataSource> mDataSource;
|
||||
status_t mInitCheck;
|
||||
bool mValidFormat;
|
||||
uint16_t mNumChannels;
|
||||
uint32_t mSampleRate;
|
||||
off_t mDataOffset;
|
||||
size_t mDataSize;
|
||||
|
||||
status_t init();
|
||||
|
||||
WAVExtractor(const WAVExtractor &);
|
||||
WAVExtractor &operator=(const WAVExtractor &);
|
||||
};
|
||||
|
||||
bool SniffWAV(
|
||||
const sp<DataSource> &source, String8 *mimeType, float *confidence);
|
||||
|
||||
} // namespace android
|
||||
|
||||
#endif // WAV_EXTRACTOR_H_
|
||||
|
Reference in New Issue
Block a user