Decode unsynchronized ID3v2 frames efficiently.

It makes a great difference when decoding huge unsynchronized ID3v2 frames.
This commit is contained in:
Tsuda Kageyu
2016-02-18 03:07:38 +09:00
parent dadfe79799
commit a9acca5d81
3 changed files with 32 additions and 4 deletions

View File

@ -74,11 +74,29 @@ ByteVector SynchData::fromUInt(unsigned int value)
ByteVector SynchData::decode(const ByteVector &data)
{
// We have this optimized method instead of using ByteVector::replace(),
// since it makes a great difference when decoding huge unsynchronized frames.
if(data.size() < 2)
return data;
ByteVector result = data;
ByteVector pattern(2, char(0));
pattern[0] = '\xFF';
pattern[1] = '\x00';
char *begin = result.data();
char *end = begin + result.size();
return result.replace(pattern, '\xFF');
char *dst = begin;
const char *src = begin;
do {
*dst++ = *src++;
if(*(src - 1) == '\xff' && *src == '\x00')
src++;
} while (src < end);
result.resize(static_cast<unsigned int>(dst - begin));
return result;
}