While porting code that does stuff that I don't understand, and I came across this function:
int GetNextStartCode(Byte_t* pData, size_t bufSize, int startIndex)
{
uint32_t dataLen = bufSize;
uint32_t codeMask = 0x0ffffff;
uint32_t codeWindow = codeMask;//24 bit window. looking for
uint32_t startCode = 0x000001;
for (int i=startIndex; i<dataLen; i++)
{
codeWindow = (codeWindow<<8 | pData[i]) & codeMask;
if (codeWindow == startCode)
{
assert(i>2);
return i-3;
}
}
return -1;
}
After simple-mindedly rewriting it in Java, I realized what it was actually doing, and figured it would be clearer to write it like this:
private int getNextStartCode(byte[] data, int start) {
for (int i = start; i < data.length-2; i++) {
if (data[i] == 0 && data[i+1] == 0 && data[i+2] == 1)
return i;
}
return -1;
}
After looking even more at the code, I realized that the GetNextStartCode was really only being used once with startIndex = 0, and that the subsequent calls were ultimately not used, with bufSize being used in the end.
No comments:
Post a Comment