rework seeking calculations

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28025 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
David McPaul
2008-10-13 10:43:06 +00:00
parent fe0570240c
commit aef9c5249e
2 changed files with 124 additions and 116 deletions
@@ -46,16 +46,16 @@
struct wavdata struct wavdata
{ {
uint64 position; int64 position;
uint64 datasize; int64 datasize;
int32 bitsperframe; uint32 bitrate;
int32 fps; uint32 fps;
void *buffer; void *buffer;
int32 buffersize; uint32 buffersize;
int64 framecount; uint64 framecount;
bigtime_t duration; bigtime_t duration;
bool raw; bool raw;
@@ -63,6 +63,30 @@ struct wavdata
media_format format; media_format format;
}; };
static bigtime_t FrameToTime(uint64 frame, uint32 fps) {
return frame * 1000000LL / fps;
}
static uint64 TimeToFrame(bigtime_t time, uint32 fps) {
return (time * fps) / 1000000LL;
}
static int64 TimeToPosition(bigtime_t time, uint32 bitrate) {
return (time * bitrate) / 8000000LL;
}
static bigtime_t PositionToTime(int64 position, uint32 bitrate) {
return (position * 8000000LL) / bitrate;
}
static int64 FrameToPosition(uint64 frame, uint32 bitrate, uint32 fps) {
return TimeToPosition(FrameToTime(frame,fps),bitrate);
}
static uint64 PositionToFrame(int64 position, uint32 bitrate, uint32 fps) {
return TimeToFrame(PositionToTime(position,bitrate),fps);
}
WavReader::WavReader() WavReader::WavReader()
{ {
TRACE("WavReader::WavReader\n"); TRACE("WavReader::WavReader\n");
@@ -204,19 +228,23 @@ WavReader::Sniff(int32 *streamCount)
} }
fChannelCount = UINT16(format.channels); fChannelCount = UINT16(format.channels);
fFrameRate = UINT32(format.samples_per_sec); fSampleRate = UINT32(format.samples_per_sec);
fBlockAlign = UINT16(format.block_align);
fBitsPerSample = UINT16(format.bits_per_sample); fBitsPerSample = UINT16(format.bits_per_sample);
if (fBitsPerSample == 0) { if (fBitsPerSample == 0) {
printf("WavReader::Sniff: Error, bits_per_sample = 0 assuming 8\n"); printf("WavReader::Sniff: Error, bits_per_sample = 0 calculating\n");
fBitsPerSample = 8; fBitsPerSample = fBlockAlign * 8 / fChannelCount;
} }
fFrameRate = fSampleRate * fChannelCount;
fAvgBytesPerSec = format.avg_bytes_per_sec; fAvgBytesPerSec = format.avg_bytes_per_sec;
fFrameCount = foundFact ? UINT32(fact.sample_length) : 0;
// fact.sample_length is really no of samples for all channels
fFrameCount = foundFact ? UINT32(fact.sample_length) / fChannelCount : 0;
fFormatCode = UINT16(format.format_tag); fFormatCode = UINT16(format.format_tag);
if (fFormatCode == 0xfffe && foundFmtExt) if (fFormatCode == 0xfffe && foundFmtExt)
fFormatCode = (format_ext.guid[1] << 8) | format_ext.guid[0]; fFormatCode = (format_ext.guid[1] << 8) | format_ext.guid[0];
fBlockAlign = UINT16(format.block_align);
int min_align = (fFormatCode == 0x0001) ? (fBitsPerSample * fChannelCount + 7) / 8 : 1; int min_align = (fFormatCode == 0x0001) ? (fBitsPerSample * fChannelCount + 7) / 8 : 1;
if (fBlockAlign < min_align) if (fBlockAlign < min_align)
fBlockAlign = min_align; fBlockAlign = min_align;
@@ -224,6 +252,7 @@ WavReader::Sniff(int32 *streamCount)
TRACE(" fDataStart %Ld\n", fDataStart); TRACE(" fDataStart %Ld\n", fDataStart);
TRACE(" fDataSize %Ld\n", fDataSize); TRACE(" fDataSize %Ld\n", fDataSize);
TRACE(" fChannelCount %ld\n", fChannelCount); TRACE(" fChannelCount %ld\n", fChannelCount);
TRACE(" fSampleRate %ld\n", fSampleRate);
TRACE(" fFrameRate %ld\n", fFrameRate); TRACE(" fFrameRate %ld\n", fFrameRate);
TRACE(" fFrameCount %Ld\n", fFrameCount); TRACE(" fFrameCount %Ld\n", fFrameCount);
TRACE(" fBitsPerSample %d\n", fBitsPerSample); TRACE(" fBitsPerSample %d\n", fBitsPerSample);
@@ -231,7 +260,7 @@ WavReader::Sniff(int32 *streamCount)
TRACE(" min_align %d\n", min_align); TRACE(" min_align %d\n", min_align);
TRACE(" fFormatCode 0x%04x\n", fFormatCode); TRACE(" fFormatCode 0x%04x\n", fFormatCode);
// XXX fact.sample_length contains duration of encodec files? // XXX fact.sample_length contains duration of encoded files?
*streamCount = 1; *streamCount = 1;
return B_OK; return B_OK;
@@ -262,29 +291,25 @@ WavReader::AllocateCookie(int32 streamNumber, void **cookie)
data->position = 0; data->position = 0;
data->datasize = fDataSize; data->datasize = fDataSize;
data->bitsperframe = fChannelCount * fBitsPerSample; data->fps = fSampleRate;
data->fps = fFrameRate;
data->buffersize = (BUFFER_SIZE / fBlockAlign) * fBlockAlign; data->buffersize = (BUFFER_SIZE / fBlockAlign) * fBlockAlign;
data->buffer = malloc(data->buffersize); data->buffer = malloc(data->buffersize);
data->framecount = fFrameCount ? fFrameCount : (8 * fDataSize) / data->bitsperframe; data->framecount = fFrameCount ? fFrameCount : (8 * fDataSize) / (fChannelCount * fBitsPerSample);
if (fFormatCode == 0x0055) {
// mp3 in wav file
if (fAvgBytesPerSec) {
data->duration = (data->datasize * 1000000LL) / fAvgBytesPerSec;
} else {
data->duration = (data->framecount * 1000000LL) / data->fps / fBitsPerSample * fChannelCount;
}
} else {
data->duration = (data->framecount * 1000000LL) / data->fps;
}
data->raw = fFormatCode == 0x0001; data->raw = fFormatCode == 0x0001;
TRACE(" bitsperframe %ld\n", data->bitsperframe); if (!fAvgBytesPerSec) {
TRACE(" fps %ld\n", data->fps); fAvgBytesPerSec = fSampleRate * fBlockAlign;
TRACE(" buffersize %ld\n", data->buffersize); }
data->duration = (data->datasize * 1000000LL) / fAvgBytesPerSec;
data->bitrate = fAvgBytesPerSec * 8;
TRACE(" raw %s\n", data->raw ? "true" : "false");
TRACE(" framecount %Ld\n", data->framecount); TRACE(" framecount %Ld\n", data->framecount);
TRACE(" duration %Ld\n", data->duration); TRACE(" duration %Ld\n", data->duration);
TRACE(" raw %d\n", data->raw); TRACE(" bitrate %ld\n", data->bitrate);
TRACE(" fps %ld\n", data->fps);
TRACE(" buffersize %ld\n", data->buffersize);
BMediaFormats formats; BMediaFormats formats;
if (fFormatCode == 0x0001) { if (fFormatCode == 0x0001) {
@@ -293,7 +318,8 @@ WavReader::AllocateCookie(int32 streamNumber, void **cookie)
description.family = B_BEOS_FORMAT_FAMILY; description.family = B_BEOS_FORMAT_FAMILY;
description.u.beos.format = B_BEOS_FORMAT_RAW_AUDIO; description.u.beos.format = B_BEOS_FORMAT_RAW_AUDIO;
formats.GetFormatFor(description, &data->format); formats.GetFormatFor(description, &data->format);
data->format.u.raw_audio.frame_rate = fFrameRate; // Really SampleRate
data->format.u.raw_audio.frame_rate = fSampleRate;
data->format.u.raw_audio.channel_count = fChannelCount; data->format.u.raw_audio.channel_count = fChannelCount;
switch (fBitsPerSample) { switch (fBitsPerSample) {
case 8: case 8:
@@ -321,7 +347,8 @@ WavReader::AllocateCookie(int32 streamNumber, void **cookie)
description.family = B_WAV_FORMAT_FAMILY; description.family = B_WAV_FORMAT_FAMILY;
description.u.wav.codec = fFormatCode; description.u.wav.codec = fFormatCode;
formats.GetFormatFor(description, &data->format); formats.GetFormatFor(description, &data->format);
data->format.u.encoded_audio.output.frame_rate = fFrameRate; // Really SampleRate
data->format.u.encoded_audio.output.frame_rate = fSampleRate;
data->format.u.encoded_audio.output.channel_count = fChannelCount; data->format.u.encoded_audio.output.channel_count = fChannelCount;
} }
@@ -358,6 +385,42 @@ WavReader::GetStreamInfo(void *cookie, int64 *frameCount, bigtime_t *duration,
return B_OK; return B_OK;
} }
status_t
WavReader::CalculateNewPosition(void *cookie,
uint32 flags,
int64 *frame, bigtime_t *time, int64 *position)
{
wavdata *data = reinterpret_cast<wavdata *>(cookie);
if (flags & B_MEDIA_SEEK_TO_FRAME) {
TRACE(" to frame %Ld",*frame);
*position = FrameToPosition(*frame, data->bitrate, data->fps);
} else if (flags & B_MEDIA_SEEK_TO_TIME) {
TRACE(" to time %Ld", *time);
*position = TimeToPosition(*time, data->bitrate);
} else {
printf("WavReader::CalculateNewPosition invalid flag passed %ld\n", flags);
return B_ERROR;
}
*position = (*position / fBlockAlign) * fBlockAlign; // round down to a block start
TRACE(", position %Ld ", *position);
*frame = PositionToFrame(*position, data->bitrate, data->fps);
*time = FrameToTime(*frame,data->fps);
TRACE("newtime %Ld ", *time);
TRACE("newframe %Ld\n", *frame);
if (*position < 0 || *position > data->datasize) {
printf("WavReader::CalculateNewPosition invalid position %Ld\n", *position);
return B_ERROR;
}
return B_OK;
}
status_t status_t
WavReader::Seek(void *cookie, WavReader::Seek(void *cookie,
@@ -366,42 +429,18 @@ WavReader::Seek(void *cookie,
{ {
// Seek to the given position // Seek to the given position
wavdata *data = reinterpret_cast<wavdata *>(cookie); wavdata *data = reinterpret_cast<wavdata *>(cookie);
uint64 pos; status_t status;
int64 pos;
if (flags & B_MEDIA_SEEK_TO_FRAME) {
if (data->raw)
pos = (*frame * data->bitsperframe) / 8;
else
pos = (*frame * fDataSize) / data->framecount;
pos = (pos / fBlockAlign) * fBlockAlign; // round down to a block start
TRACE("WavReader::Seek to frame %Ld, pos %Ld\n", *frame, pos);
} else if (flags & B_MEDIA_SEEK_TO_TIME) {
if (data->raw)
pos = (*time * data->fps * data->bitsperframe) / (1000000LL * 8);
else
pos = (*time * fDataSize) / data->duration;
pos = (pos / fBlockAlign) * fBlockAlign; // round down to a block start
TRACE("WavReader::Seek to time %Ld, pos %Ld\n", *time, pos);
} else {
return B_ERROR;
}
if (data->raw)
*frame = (8 * pos) / data->bitsperframe;
else
*frame = (pos * data->framecount) / fDataSize;
*time = (*frame * 1000000LL) / data->fps;
TRACE("WavReader::Seek newtime %Ld\n", *time);
TRACE("WavReader::Seek newframe %Ld\n", *frame);
if (pos < 0 || pos > data->datasize) { TRACE("WavReader::Seek");
TRACE("WavReader::Seek invalid position %Ld\n", pos); status = CalculateNewPosition(cookie, flags, frame, time, &pos);
return B_ERROR;
if (status == B_OK) {
// set the new position so next GetNextChunk will read from new seek pos
data->position = pos;
} }
data->position = pos; return status;
return B_OK;
} }
status_t status_t
@@ -409,42 +448,10 @@ WavReader::FindKeyFrame(void* cookie, uint32 flags,
int64* frame, bigtime_t* time) int64* frame, bigtime_t* time)
{ {
// Find a seek position without actually seeking // Find a seek position without actually seeking
wavdata *data = reinterpret_cast<wavdata *>(cookie); int64 pos;
uint64 pos; TRACE("WavReader::FindKeyFrame");
if (flags & B_MEDIA_SEEK_TO_FRAME) {
if (data->raw)
pos = (*frame * data->bitsperframe) / 8;
else
pos = (*frame * fDataSize) / data->framecount;
pos = (pos / fBlockAlign) * fBlockAlign; // round down to a block start
TRACE("WavReader::Seek to frame %Ld, pos %Ld\n", *frame, pos);
} else if (flags & B_MEDIA_SEEK_TO_TIME) {
if (data->raw)
pos = (*time * data->fps * data->bitsperframe) / (1000000LL * 8);
else
pos = (*time * fDataSize) / data->duration;
pos = (pos / fBlockAlign) * fBlockAlign; // round down to a block start
TRACE("WavReader::Seek to time %Ld, pos %Ld\n", *time, pos);
} else {
return B_ERROR;
}
if (data->raw)
*frame = (8 * pos) / data->bitsperframe;
else
*frame = (pos * data->framecount) / fDataSize;
*time = (*frame * 1000000LL) / data->fps;
TRACE("WavReader::Seek newtime %Ld\n", *time);
TRACE("WavReader::Seek newframe %Ld\n", *frame);
if (pos < 0 || pos > data->datasize) { return CalculateNewPosition(cookie, flags, frame, time, &pos);
TRACE("WavReader::Seek invalid position %Ld\n", pos);
return B_ERROR;
}
return B_OK;
} }
status_t status_t
@@ -456,28 +463,25 @@ WavReader::GetNextChunk(void *cookie,
// XXX it might be much better to not return any start_time information for encoded formats here, // XXX it might be much better to not return any start_time information for encoded formats here,
// XXX and instead use the last time returned from seek and count forward after decoding. // XXX and instead use the last time returned from seek and count forward after decoding.
if (data->raw) mediaHeader->start_time = PositionToTime(data->position,data->bitrate);
mediaHeader->start_time = (((8 * data->position) / data->bitsperframe) * 1000000LL) / data->fps;
else
mediaHeader->start_time = (data->position * data->duration) / fDataSize;
mediaHeader->file_pos = fDataStart + data->position; mediaHeader->file_pos = fDataStart + data->position;
/* TRACE("(%s) position = %9Ld ", data->raw ? "raw" : "encoded", data->position);
printf("position = %9Ld\n", data->position); TRACE("frame = %9Ld ", PositionToFrame(data->position,data->bitrate,data->fps));
printf("fDataSize = %9Ld\n", fDataSize); TRACE("fDataSize = %9Ld ", fDataSize);
printf("duration = %9Ld\n", data->duration); TRACE("start_time = %9Ld\n", mediaHeader->start_time);
printf("start_time = %9Ld\n", mediaHeader->start_time);
*/
int64 maxreadsize = data->datasize - data->position; int64 maxreadsize = data->datasize - data->position;
int32 readsize = data->buffersize; int32 readsize = data->buffersize;
if (maxreadsize < readsize) if (maxreadsize < readsize)
readsize = maxreadsize; readsize = maxreadsize;
if (readsize == 0) if (readsize == 0) {
printf("WavReader::GetNextChunk: LAST BUFFER ERROR at time %9Ld\n",mediaHeader->start_time);
return B_LAST_BUFFER_ERROR; return B_LAST_BUFFER_ERROR;
}
if (readsize != Source()->ReadAt(fDataStart + data->position, data->buffer, readsize)) { if (readsize != Source()->ReadAt(fDataStart + data->position, data->buffer, readsize)) {
TRACE("WavReader::GetNextChunk: unexpected read error\n"); printf("WavReader::GetNextChunk: unexpected read error at position %9Ld\n",fDataStart + data->position);
return B_ERROR; return B_ERROR;
} }
@@ -55,7 +55,10 @@ public:
status_t GetNextChunk(void *cookie, status_t GetNextChunk(void *cookie,
const void **chunkBuffer, size_t *chunkSize, const void **chunkBuffer, size_t *chunkSize,
media_header *mediaHeader); media_header *mediaHeader);
status_t CalculateNewPosition(void* cookie, uint32 flags,
int64 *frame, bigtime_t *time, int64 *position);
BPositionIO *Source() { return fSource; } BPositionIO *Source() { return fSource; }
private: private:
@@ -67,10 +70,11 @@ private:
int64 fFrameCount; int64 fFrameCount;
int32 fChannelCount; int32 fChannelCount;
int32 fFrameRate; int32 fFrameRate;
int fBitsPerSample; int32 fSampleRate;
uint16 fBitsPerSample;
uint16 fFormatCode; uint16 fFormatCode;
uint16 fBlockAlign; uint16 fBlockAlign;
uint16 fAvgBytesPerSec; uint32 fAvgBytesPerSec;
}; };