[vorbis-dev] read codebook function

Adriano Almeida adriano at continuum.com.br
Thu Feb 19 06:22:42 PST 2004


Anexed is my function for reading codebooks and the Vq Table. My problem is:
I read a codeBook number of 41, so i called this function above 41 times.
The firtst time, the sync patterns (BCV) is alligned correctly, so it reads correctly. the sencond time it is executed,  the patter is not there anymore and there are only 8 BCV in my file. 

function fReadBits1 is the same as the ogg's read bit function.

orry for the long function and to bother
Adriano

<p>
-------------- next part --------------
int cOgg::readCodeBook(tOggCodebook *cb)
{
dword i, curEntry, num;
byte curLength;

// read sync pattern
this->fRead((byte*) cb->sync, 3);
cb->sync[3] = '\0';

// print info
printf("Codebook sync pattern: %s\n", cb->sync);

// check if pattern is valid
if( strncmp(cb->sync, "BCV", 3) )
   return 0;

// read codebook dimensions and entries, flag ordered 
this->fReadBits1((int*) &cb->dimensions, 16);
this->fReadBits1((int*) &cb->codeEntries, 24);
this->fReadBits1((int*) &cb->flagOrdered, 1);

// assign memory
cb->length = new byte[cb->codeEntries];
cb->unused = new int[cb->codeEntries];
if( (cb->length==NULL) || (cb->unused==NULL) )
   return 0;
memset(cb->length, 0, sizeof(byte)*cb->codeEntries);
memset(cb->unused, 0, sizeof(int)*cb->codeEntries);

// read codebook according to flag
if( cb->flagOrdered )
   {
   // get initial values
   curEntry = 0;
   this->fReadBits1((int*) &curLength, 5);

   while( curEntry<cb->codeEntries )
      {
      num = this->iLog(cb->codeEntries-curEntry);

      // check if entry is valid
      if( i>curEntry )
         return 0;

      // assign code book values
      for( i=curEntry; (i<curEntry+num) ; i++)
         {
         // assign value
         cb->length[i] = curLength;
         }

      // make new values
      curEntry  += num;
      curLength += 1;
      }
   }
else
   {
   // read if there is unused entries
   this->fReadBits1((int*) &cb->sparse, 1);

   // for each entries
   for(i=0; i<cb->codeEntries; i++)
      {
      // check if this entry is unused
      if( cb->sparse )
         {
         this->fReadBits1((int*) &cb->unused[i], 1);
         if( cb->unused[i] )
            continue;
         }

      // get length and adjust
      this->fReadBits1((int*) &cb->length[i], 5);
      cb->length[i] += 1;
      }
   }

// read vector lookup table
this->fReadBits1((int*) &cb->mapType, 4);

switch( cb->mapType ) {
case 0:
   break;
case 1:
case 2:
   // read minimum, delta value and value bits and flag sequence
   this->fReadBits1((int*) &cb->minValue, 32);
   this->fReadBits1((int*) &cb->deltaValue, 32);
   this->fReadBits1((int*) &cb->quantWidth, 4);
   this->fReadBits1((int*) &cb->flagSeq, 1);
   cb->quantWidth++;

   // now process acording to type // AQUI AQUI - fazer ou achar a tabela abaixo
   if( cb->mapType )
      {
      cb->quantNum = cb->codeEntries^(1/cb->dimensions);
      }
   else
      {
      cb->quantNum = cb->dimensions*cb->codeEntries;
      }

   // create multiplicands array and read them
   cb->quantValues = new byte[cb->quantNum];
   if( cb->quantValues==NULL )
      return 0;

   // read all quad values
   for(i=0; i<cb->quantNum; i++)
      this->fReadBits1((int*) &cb->quantValues[i], 5);
   break;
default :
   return 0;
   }

return 1;


More information about the Vorbis-dev mailing list