ots1@cs.waikato.ac.nz
2003-Jun-13 16:36 UTC
[vorbis-dev] Trouble decoding number of codebooks
I'm having trouble decoding the number of codebooks out of an ogg file, and was wondering if anyone here could lend a hand? I read in the sync pattern, and I think the codebook dimensions properly, but when I try and read in the number of codebook entries, I get a number larger than the number of bytes in the file, which I have to assume is wrong :) Anyway this is how I read in the numbers : void readCodeBooks(ifstream *file, int codebookCount) { //read in the 24 bit sync pattern unsigned char *c = readBytes(file, 3); //read the codebook dimensions unsigned char *dimensions = readBytes(file,2); int codebookDimensions = (int)convertArray2(dimensions, 2); //read codebook entries unsigned char *entries = readBytes(file,3); long codebookEntries = convertArray2(entries,3); } long convertArray2(unsigned char *array, int size) { //reverse the array unsigned char revArray[size]; for(int i = size - 1; i >= 0; i--) revArray[(size-1) - i] = array[i]; //return a pointer to a long long *result = (long *)revArray; return *result; } The results that I get from the code are : Sync Pattern (starting byte #269) : 42 43 56 (outputed as integers) Codebook dimensions (starting byte #272) : 256 Codebook entries (starting byte #274) : 4194304 Cheers for the help, Oliver Sneyd ots1@cs.waikato.ac.nz <p><p><p>--- >8 ---- List archives: http://www.xiph.org/archives/ Ogg project homepage: http://www.xiph.org/ogg/ To unsubscribe from this list, send a message to 'vorbis-dev-request@xiph.org' containing only the word 'unsubscribe' in the body. No subject is needed. Unsubscribe messages sent to the list will be ignored/filtered.
On Saturday, June 14, 2003, at 12:36 am, <ots1@cs.waikato.ac.nz> wrote:> I read in the sync pattern, and I think the codebook dimensions > properly, > but when I try and read in the number of codebook entries, I get a > number > larger than the number of bytes in the file, which I have to assume is > wrong :)The code you've written isn't at all portable, but assuming you're trying to convert from vorbis little-endian to native integers on a big-endian host, your problem is convertArray2(). Assuming 'readBytes()' reads in a byte at a time, you start with entries = byte0,byte1,byte2,unknown,.... convertArray2 swaps this to byte2,byte1,byte0,unknown, and then casts to a long. The actual value is 0,byte2,byte1,byte0, so your result is multiplied by 256 + a random value. The dimensions read is wrong too. Hope that helps, -r --- >8 ---- List archives: http://www.xiph.org/archives/ Ogg project homepage: http://www.xiph.org/ogg/ To unsubscribe from this list, send a message to 'vorbis-dev-request@xiph.org' containing only the word 'unsubscribe' in the body. No subject is needed. Unsubscribe messages sent to the list will be ignored/filtered.
ots1@cs.waikato.ac.nz
2003-Jun-13 17:48 UTC
[vorbis-dev] Trouble decoding number of codebooks
I can see why that would completely mangle the results that i'm getting, but I can't see why my code is doing this. When I display whats going on in the for loop, it seems to work on the right number of bytes. If Print out each iteration I get array[(size - i) - 1] : 64 revArray[(size-1) - 1] : 0 array[(size - i) - 1] : 0 revArray[(size-1) - 1] : 0 array[(size - i) - 1] : 0 revArray[(size-1) - 1] : 64 o I can see that in the file i'm working on, there are going to be 64 codebooks. Is the problem i'm having to do with the casting of the long ? readBytes does read in a single byte at a time. Cheers, Oliver Sneyd (ots1@cs.waikato.ac.nz) <p>--- >8 ---- List archives: http://www.xiph.org/archives/ Ogg project homepage: http://www.xiph.org/ogg/ To unsubscribe from this list, send a message to 'vorbis-dev-request@xiph.org' containing only the word 'unsubscribe' in the body. No subject is needed. Unsubscribe messages sent to the list will be ignored/filtered.