62 lines
1.2 KiB
C++
62 lines
1.2 KiB
C++
#include "audio/packed_opus_reader.h"
|
|
|
|
namespace borla {
|
|
namespace {
|
|
|
|
uint16_t ReadLe16(const uint8_t* data)
|
|
{
|
|
return static_cast<uint16_t>(
|
|
static_cast<uint16_t>(data[0]) |
|
|
(static_cast<uint16_t>(data[1]) << 8));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
PackedOpusReader::PackedOpusReader(
|
|
const uint8_t* data,
|
|
size_t size)
|
|
: data_(data),
|
|
size_(size)
|
|
{
|
|
if (data_ == nullptr || size_ < 2) {
|
|
return;
|
|
}
|
|
frame_count_ = ReadLe16(data_);
|
|
offset_ = 2;
|
|
valid_ = frame_count_ > 0;
|
|
}
|
|
|
|
bool PackedOpusReader::Next(
|
|
const uint8_t** frame,
|
|
size_t* frame_size)
|
|
{
|
|
if (!valid_ || frame == nullptr || frame_size == nullptr ||
|
|
frames_read_ >= frame_count_) {
|
|
return false;
|
|
}
|
|
if (offset_ + 2 > size_) {
|
|
valid_ = false;
|
|
return false;
|
|
}
|
|
|
|
const uint16_t size = ReadLe16(data_ + offset_);
|
|
offset_ += 2;
|
|
if (size == 0 || offset_ + size > size_) {
|
|
valid_ = false;
|
|
return false;
|
|
}
|
|
|
|
*frame = data_ + offset_;
|
|
*frame_size = size;
|
|
offset_ += size;
|
|
frames_read_++;
|
|
return true;
|
|
}
|
|
|
|
bool PackedOpusReader::complete() const
|
|
{
|
|
return valid_ && frames_read_ == frame_count_ && offset_ == size_;
|
|
}
|
|
|
|
} // namespace borla
|