Help - Search - Members - Calendar
Full Version: mp2 gain -change scale factor
Hydrogenaudio Forums > Lossy Audio Compression > Other Lossy Codecs
alonsag
hey
how r u?

can anybody help me to understand what am i doing wrong in the code:
assume i parsed the header and buf contain only data

void CAudioGain::changeScaleFactor(UCHAR *buf){

int sb; // Subband
int ch; // Channel

unsigned int val=-1;
unsigned int c=4; //change value;

for (sb=0; sb < header.sblimit; sb++)
for (ch=0; ch < header.nch; ch++) {
unsigned int allocation_value=(NBAL[header.table][sb]);

unsigned int val=getBitFromBuf(allocation_value,buf) ;
allocation[ch][sb] =val;
}


for (sb=0; sb < header.sblimit; sb++)
for (ch=0; ch < header.nch; ch++)
if (allocation[ch][sb] != 0){
unsigned int val=getBitFromBuf(2,buf) ;
scfsi[ch][sb]=val;

}
for (sb=0; sb < header.sblimit; sb++){
for (ch=0; ch < header.nch; ch++)
if (allocation[ch][sb] != 0) {
if (scfsi[ch][sb] == 0) {

val=getBitFromBuf(6,buf);
setBitToBuf(6,buf,val+c);

val=getBitFromBuf(6,buf);
setBitToBuf(6,buf,val+c);

val=getBitFromBuf(6,buf);
setBitToBuf(6,buf,val+c);

}
if (scfsi[ch][sb] == 1 ) {

val=getBitFromBuf(6,buf);
setBitToBuf(6,buf,val+c);

val=getBitFromBuf(6,buf);
setBitToBuf(6,buf,val+c);

}
if ( scfsi[ch][sb] == 3) {

val=getBitFromBuf(6,buf);
setBitToBuf(6,buf,val+c);

val=getBitFromBuf(6,buf);
setBitToBuf(6,buf,val+c);

}
if (scfsi[ch][sb] == 2) {
val=getBitFromBuf(6,buf);
setBitToBuf(6,buf,val+c);

}
}
}
smack
After some formatting the code looks like this: (btw. it's a bad habit to omit the curly braces when they are "unnecessary")

CODE
void CAudioGain::changeScaleFactor(UCHAR *buf)
{
int sb; // Subband
int ch; // Channel
unsigned int val=-1;
unsigned int c=4; //change value;
for (sb=0; sb < header.sblimit; sb++)
{
 for (ch=0; ch < header.nch; ch++)
 {
  unsigned int allocation_value=(NBAL[header.table][sb]);
  unsigned int val=getBitFromBuf(allocation_value,buf);
  allocation[ch][sb] =val;
 }
}
for (sb=0; sb < header.sblimit; sb++)
{
 for (ch=0; ch < header.nch; ch++)
 {
  if (allocation[ch][sb] != 0)
  {
   unsigned int val=getBitFromBuf(2,buf);
   scfsi[ch][sb]=val;
  }
 }
}
for (sb=0; sb < header.sblimit; sb++)
{
 for (ch=0; ch < header.nch; ch++)
 {
  if (allocation[ch][sb] != 0)
  {
   if (scfsi[ch][sb] == 0)
   {
    val=getBitFromBuf(6,buf);
    setBitToBuf(6,buf,val+c);
    val=getBitFromBuf(6,buf);
    setBitToBuf(6,buf,val+c);
    val=getBitFromBuf(6,buf);
    setBitToBuf(6,buf,val+c);
   }
   if (scfsi[ch][sb] == 1 )
   {
    val=getBitFromBuf(6,buf);
    setBitToBuf(6,buf,val+c);
    val=getBitFromBuf(6,buf);
    setBitToBuf(6,buf,val+c);
   }
   if ( scfsi[ch][sb] == 3)
   {
    val=getBitFromBuf(6,buf);
    setBitToBuf(6,buf,val+c);
    val=getBitFromBuf(6,buf);
    setBitToBuf(6,buf,val+c);
   }
   if (scfsi[ch][sb] == 2)
   {
    val=getBitFromBuf(6,buf);
    setBitToBuf(6,buf,val+c);
   }
  }
 }
}
}


At first glance the code looks OK, very similar to the sources of well-known MPEG decoders. wink.gif

But what does the function setBitToBuf do? You must make sure that it overwrites the correct bits (i.e. the bits that have been read be the previous getBitFromBuf-call) and that it limits the value that is to be written (remember: valid range for 6bit-value is 0...63).

Does that help you?
alonsag
first thank you for the answer

I double checked the function setBitToBuf and getBitFromBuf

when the c value is 0 everything is ok meaning that the fanction is working good

but when the value is not 0 and I play the result I hear screechs in the sound
insted of volume change .

please help

alon
smack
Did you also check that the value is correctly limited in the function setBitToBuf - before it is converted to 6bit format?

something like this: if (val<0) val=0; else if (val>63) val=63;

btw. that val variable should be signed, otherwise the lower boundary check (val<0) will be useless. this is important if you ever want to use negative values for your c variable.
alonsag
I have made the changes but still nothing works


some other suggestions??


alon



smack
Just had another look and compared your code with the Layer II decoder of libmad. I think there is a bug in your code:

In the first loop where the bit allocation is decoded your code doesn't consider the joint-stereo boundary. Above the jsbound only one allocation value per scalefactor band is stored in the file which must be applied to both channels.

reference: look at function mad_layer_II in file layer12.c of libmad source code (download here)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.