Monday, November 2, 2009

So I got thrown into this other project that I know almost nothing about, doing stuff with protocols that I know nothing about, and working with data formats that I know nothing about. Fortunately, dealing with the stuff I know nothing about either involves making library calls or porting existing code. Not that making library calls is simple matter when lacking the understanding of what the calls are supposed to do, which means the code winds up being blindly based on sample code.

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