long MPEG::File::nextFrameOffset(long position)
{
- char frameSyncBytes[2] = {};
+ ByteVector frameSyncBytes(2, '\0');
while(true) {
seek(position);
for(unsigned int i = 0; i < buffer.size(); ++i) {
frameSyncBytes[0] = frameSyncBytes[1];
frameSyncBytes[1] = buffer[i];
- if(firstSyncByte(frameSyncBytes[0]) && secondSynchByte(frameSyncBytes[1])) {
+ if(isFrameSync(frameSyncBytes)) {
Header header(this, position + i - 1, true);
if(header.isValid())
return position + i - 1;
long MPEG::File::previousFrameOffset(long position)
{
- char frameSyncBytes[2] = {};
+ ByteVector frameSyncBytes(2, '\0');
while(position > 0) {
const long size = std::min<long>(position, bufferSize());
for(int i = buffer.size() - 1; i >= 0; i--) {
frameSyncBytes[1] = frameSyncBytes[0];
frameSyncBytes[0] = buffer[i];
- if(firstSyncByte(frameSyncBytes[0]) && secondSynchByte(frameSyncBytes[1])) {
+ if(isFrameSync(frameSyncBytes)) {
Header header(this, position + i, true);
if(header.isValid())
return position + i + header.frameLength();
// Look for an ID3v2 tag until reaching the first valid MPEG frame.
- char frameSyncBytes[2] = {};
- char tagHeaderBytes[4] = {};
+ ByteVector frameSyncBytes(2, '\0');
+ ByteVector tagHeaderBytes(3, '\0');
long position = 0;
while(true) {
for(unsigned int i = 0; i < buffer.size(); ++i) {
frameSyncBytes[0] = frameSyncBytes[1];
frameSyncBytes[1] = buffer[i];
- if(firstSyncByte(frameSyncBytes[0]) && secondSynchByte(frameSyncBytes[1])) {
+ if(isFrameSync(frameSyncBytes)) {
Header header(this, position + i - 1, true);
if(header.isValid())
return -1;
tagHeaderBytes[0] = tagHeaderBytes[1];
tagHeaderBytes[1] = tagHeaderBytes[2];
tagHeaderBytes[2] = buffer[i];
- if(headerID == tagHeaderBytes)
+ if(tagHeaderBytes == headerID)
return position + i - 2;
}
* MPEG frames can be recognized by the bit pattern 11111111 111, so the
* first byte is easy to check for, however checking to see if the second byte
* starts with \e 111 is a bit more tricky, hence these functions.
+ *
+ * \note This does not check the length of the vector, since this is an
+ * internal utility function.
*/
- inline bool firstSyncByte(unsigned char byte)
+ inline bool isFrameSync(const ByteVector &bytes)
{
- return (byte == 0xFF);
- }
-
- inline bool secondSynchByte(unsigned char byte)
- {
- // 0xFF is possible in theory, but it's very unlikely be a header.
+ // 0xFF in the second byte is possible in theory, but it's very unlikely.
- return (byte != 0xFF && ((byte & 0xE0) == 0xE0));
+ const unsigned char b1 = bytes[0];
+ const unsigned char b2 = bytes[1];
+ return (b1 == 0xFF && b2 != 0xFF && (b2 & 0xE0) == 0xE0);
}
}