Exemplo n.º 1
0
int mpeg3audio_ac3_coeff_unpack(mpeg3audio_t *audio, 
	mpeg3_ac3bsi_t *bsi, 
	mpeg3_ac3audblk_t *audblk,
	mpeg3ac3_stream_samples_t samples)
{
	int i, j;
	int done_cpl = 0;
	short mantissa;

	mpeg3audio_ac3_mantissa_reset(&(audio->ac3_mantissa));

	for(i = 0; i < bsi->nfchans && !mpeg3bits_error(audio->astream); i++)
	{
		for(j = 0; j < audblk->endmant[i] && !mpeg3bits_error(audio->astream); j++)
		{
			mantissa = mpeg3audio_ac3_mantissa_get(audio, audblk->fbw_bap[i][j], audblk->dithflag[i]);
			samples[i][j] = mpeg3audio_ac3_tofloat(audblk->fbw_exp[i][j], mantissa);
		}

		if(audblk->cplinu && audblk->chincpl[i] && !(done_cpl) && !mpeg3bits_error(audio->astream))
		{
/* ncplmant is equal to 12 * ncplsubnd */
/* Don't dither coupling channel until channel separation so that
 * interchannel noise is uncorrelated */
			for(j = audblk->cplstrtmant; 
				j < audblk->cplendmant && !mpeg3bits_error(audio->astream); 
				j++)
			{
				audblk->cplmant[j] = mpeg3audio_ac3_mantissa_get(audio, audblk->cpl_bap[j], 0);
			}
			done_cpl = 1;
		}
	}

/* Uncouple the channel */
	if(audblk->cplinu)
	{
		if(audblk->chincpl[i])
			mpeg3audio_ac3_uncouple_channel(audio, samples[i], bsi, audblk, i);
	}

	if(bsi->lfeon && !mpeg3bits_error(audio->astream))
	{
/* There are always 7 mantissas for lfe, no dither for lfe */
		for(j = 0; j < 7 && !mpeg3bits_error(audio->astream); j++)
		{
			mantissa = mpeg3audio_ac3_mantissa_get(audio, audblk->lfe_bap[j], 0);
			samples[5][j] = mpeg3audio_ac3_tofloat(audblk->lfe_exp[j], mantissa);
//printf("%f ", samples[5][j]);
		}
//printf("\n");
	}

	return mpeg3bits_error(audio->astream);
}
Exemplo n.º 2
0
int mpeg3audio_II_step_one(mpeg3audio_t *audio, unsigned int *bit_alloc, int *scale)
{
    int stereo = audio->channels - 1;
    int sblimit = audio->II_sblimit;
    int jsbound = audio->jsbound;
    int sblimit2 = audio->II_sblimit << stereo;
    struct al_table *alloc1 = audio->alloc;
    int i, result = 0;
    unsigned int *scfsi_buf = audio->layer2_scfsi_buf;
    unsigned int *scfsi, *bita;
    int sc, step;

    bita = bit_alloc;
    if(stereo)
    {
/* Stereo */
    	for(i = jsbound;i ; i--, alloc1 += (1 << step))
    	{
        	*bita++ = (char)mpeg3bits_getbits(audio->astream, step = alloc1->bits);
        	*bita++ = (char)mpeg3bits_getbits(audio->astream, step);
    	}
    	for(i = sblimit-jsbound; i; i--, alloc1 += (1 << step))
    	{
        	bita[0] = (char)mpeg3bits_getbits(audio->astream, step = alloc1->bits);
        	bita[1] = bita[0];
        	bita += 2;
    	}
    	bita = bit_alloc;
    	scfsi = scfsi_buf;
    	for(i = sblimit2; i; i--)
        	if(*bita++) *scfsi++ = (char)mpeg3bits_getbits(audio->astream, 2);
    }
    else 
    {
/* mono */
      	for(i = sblimit; i; i--, alloc1 += (1 << step))
        *bita++ = (char)mpeg3bits_getbits(audio->astream, step = alloc1->bits);
      	bita = bit_alloc;
      	scfsi = scfsi_buf;
      	for(i = sblimit; i; i--) if (*bita++) *scfsi++ = (char)mpeg3bits_getbits(audio->astream, 2);
    }

    bita = bit_alloc;
    scfsi = scfsi_buf;
    for(i = sblimit2; i; i--) 
	{
      	if(*bita++)
        	switch(*scfsi++) 
        	{
        	  case 0: 
                	*scale++ = mpeg3bits_getbits(audio->astream, 6);
                	*scale++ = mpeg3bits_getbits(audio->astream, 6);
                	*scale++ = mpeg3bits_getbits(audio->astream, 6);
                	break;
        	  case 1 : 
                	*scale++ = sc = mpeg3bits_getbits(audio->astream, 6);
                	*scale++ = sc;
                	*scale++ = mpeg3bits_getbits(audio->astream, 6);
                	break;
        	  case 2: 
                	*scale++ = sc = mpeg3bits_getbits(audio->astream, 6);
                	*scale++ = sc;
                	*scale++ = sc;
                	break;
        	  default:              /* case 3 */
                	*scale++ = mpeg3bits_getbits(audio->astream, 6);
                	*scale++ = sc = mpeg3bits_getbits(audio->astream, 6);
                	*scale++ = sc;
                	break;
        	}
	}
	return result | mpeg3bits_error(audio->astream);
}
Exemplo n.º 3
0
int mpeg3audio_II_step_two(mpeg3audio_t *audio, unsigned int *bit_alloc, float fraction[2][4][SBLIMIT], int *scale, int x1)
{
    int i, j, k, ba, result = 0;
    int channels = audio->channels;
    int sblimit = audio->II_sblimit;
    int jsbound = audio->jsbound;
    struct al_table *alloc2, *alloc1 = audio->alloc;
    unsigned int *bita = bit_alloc;
    int d1, step, test;

    for(i = 0; i < jsbound; i++, alloc1 += (1 << step))
    {
    	step = alloc1->bits;
    	for(j = 0; j < channels; j++)
    	{
        	if(ba = *bita++)
        	{
        		k = (alloc2 = alloc1 + ba)->bits;
        		if((d1 = alloc2->d) < 0) 
        		{
            		float cm = mpeg3_muls[k][scale[x1]];

            		fraction[j][0][i] = ((float)((int)mpeg3bits_getbits(audio->astream, k) + d1)) * cm;
            		fraction[j][1][i] = ((float)((int)mpeg3bits_getbits(audio->astream, k) + d1)) * cm;
            		fraction[j][2][i] = ((float)((int)mpeg3bits_getbits(audio->astream, k) + d1)) * cm;
        		}
        		else 
        		{
            		static int *table[] = 
						{0, 0, 0, mpeg3_grp_3tab, 0, mpeg3_grp_5tab, 0, 0, 0, mpeg3_grp_9tab};
            		unsigned int idx, *tab, m = scale[x1];
					
            		idx = (unsigned int)mpeg3bits_getbits(audio->astream, k);
            		tab = (unsigned int*)(table[d1] + idx + idx + idx);
            		fraction[j][0][i] = mpeg3_muls[*tab++][m];
            		fraction[j][1][i] = mpeg3_muls[*tab++][m];
            		fraction[j][2][i] = mpeg3_muls[*tab][m];  
        		}
        		scale += 3;
        	}
          	else
        		fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
    	}
    }

    for(i = jsbound; i < sblimit; i++, alloc1 += (1 << step))
    {
    	step = alloc1->bits;
/* channel 1 and channel 2 bitalloc are the same */
    	bita++;		
    	if((ba = *bita++))
    	{
        	k=(alloc2 = alloc1+ba)->bits;
        	if((d1 = alloc2->d) < 0)
        	{
        		float cm;
				
        		cm = mpeg3_muls[k][scale[x1 + 3]];
        		fraction[1][0][i] = (fraction[0][0][i] = (float)((int)mpeg3bits_getbits(audio->astream, k) + d1)) * cm;
        		fraction[1][1][i] = (fraction[0][1][i] = (float)((int)mpeg3bits_getbits(audio->astream, k) + d1)) * cm;
        		fraction[1][2][i] = (fraction[0][2][i] = (float)((int)mpeg3bits_getbits(audio->astream, k) + d1)) * cm;
        		cm = mpeg3_muls[k][scale[x1]];
        		fraction[0][0][i] *= cm; 
				fraction[0][1][i] *= cm; 
				fraction[0][2][i] *= cm;
        	}
        	else
        	{
        	  static int *table[] = {0, 0, 0, mpeg3_grp_3tab, 0, mpeg3_grp_5tab, 0, 0, 0, mpeg3_grp_9tab};
        	  unsigned int idx, *tab, m1, m2;
			  
        	  m1 = scale[x1]; 
			  m2 = scale[x1+3];
        	  idx = (unsigned int)mpeg3bits_getbits(audio->astream, k);
        	  tab = (unsigned int*)(table[d1] + idx + idx + idx);
        	  fraction[0][0][i] = mpeg3_muls[*tab][m1]; 
			  fraction[1][0][i] = mpeg3_muls[*tab++][m2];
        	  fraction[0][1][i] = mpeg3_muls[*tab][m1]; 
			  fraction[1][1][i] = mpeg3_muls[*tab++][m2];
        	  fraction[0][2][i] = mpeg3_muls[*tab][m1]; 
			  fraction[1][2][i] = mpeg3_muls[*tab][m2];
        	}
        	scale += 6;
      	}
    	else 
		{
        	fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
        	fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
    	}
/* 
   should we use individual scalefac for channel 2 or
   is the current way the right one , where we just copy channel 1 to
   channel 2 ?? 
   The current 'strange' thing is, that we throw away the scalefac
   values for the second channel ...!!
-> changed .. now we use the scalefac values of channel one !! 
*/
    }

    if(sblimit > SBLIMIT) sblimit = SBLIMIT;

    for(i = sblimit; i < SBLIMIT; i++)
      	for(j = 0; j < channels; j++)
        	fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;

	return result | mpeg3bits_error(audio->astream);
}
Exemplo n.º 4
0
int mpeg3audio_read_ac3_audblk(mpeg3audio_t *audio)
{
	int i, j;
	mpeg3_ac3bsi_t *bsi = &(audio->ac3_bsi);
	mpeg3_ac3audblk_t *audblk = &(audio->ac3_audblk);

	for(i = 0; i < bsi->nfchans; i++)
	{
/* Is this channel an interleaved 256 + 256 block ? */
		audblk->blksw[i] = mpeg3bits_getbits(audio->astream, 1);
	}

	for(i = 0; i < bsi->nfchans; i++)
	{
/* Should we dither this channel? */
		audblk->dithflag[i] = mpeg3bits_getbits(audio->astream, 1);
	}

/* Does dynamic range control exist? */
	audblk->dynrnge = mpeg3bits_getbits(audio->astream, 1);
	if(audblk->dynrnge)
	{
/* Get dynamic range info */
		audblk->dynrng = mpeg3bits_getbits(audio->astream, 8);
	}

/* If we're in dual mono mode then get the second channel DR info */
	if(bsi->acmod == 0)
	{
/* Does dynamic range control two exist? */
		audblk->dynrng2e = mpeg3bits_getbits(audio->astream, 1);
		if (audblk->dynrng2e)
		{
/* Get dynamic range info */
			audblk->dynrng2 = mpeg3bits_getbits(audio->astream, 8);
		}
	}

/* Does coupling strategy exist? */
	audblk->cplstre = mpeg3bits_getbits(audio->astream, 1);
	if(audblk->cplstre)
	{
/* Is coupling turned on? */
		audblk->cplinu = mpeg3bits_getbits(audio->astream, 1);
		if(audblk->cplinu)
		{
			for(i = 0; i < bsi->nfchans; i++)
				audblk->chincpl[i] = mpeg3bits_getbits(audio->astream, 1);

			if(bsi->acmod == 0x2)
				audblk->phsflginu = mpeg3bits_getbits(audio->astream, 1);

 			audblk->cplbegf = mpeg3bits_getbits(audio->astream, 4);
 			audblk->cplendf = mpeg3bits_getbits(audio->astream, 4);
			audblk->ncplsubnd = (audblk->cplendf + 2) - audblk->cplbegf + 1;

/* Calculate the start and end bins of the coupling channel */
			audblk->cplstrtmant = (audblk->cplbegf * 12) + 37 ; 
			audblk->cplendmant =  ((audblk->cplendf + 3) * 12) + 37;

/* The number of combined subbands is ncplsubnd minus each combined band */
			audblk->ncplbnd = audblk->ncplsubnd; 

			for(i = 1; i < audblk->ncplsubnd; i++)
			{
				audblk->cplbndstrc[i] = mpeg3bits_getbits(audio->astream, 1);
				audblk->ncplbnd -= audblk->cplbndstrc[i];
			}
		}
	}

	if(audblk->cplinu)
	{
/* Loop through all the channels and get their coupling co-ords */	
		for(i = 0; i < bsi->nfchans; i++)
		{
			if(!audblk->chincpl[i])
				continue;

/* Is there new coupling co-ordinate info? */
			audblk->cplcoe[i] = mpeg3bits_getbits(audio->astream, 1);

			if(audblk->cplcoe[i])
			{
				audblk->mstrcplco[i] = mpeg3bits_getbits(audio->astream, 2); 
				for(j = 0; j < audblk->ncplbnd; j++)
				{
					audblk->cplcoexp[i][j] = mpeg3bits_getbits(audio->astream, 4); 
					audblk->cplcomant[i][j] = mpeg3bits_getbits(audio->astream, 4); 
				}
			}
		}

/* If we're in dual mono mode, there's going to be some phase info */
		if((bsi->acmod == 0x2) && audblk->phsflginu && 
			(audblk->cplcoe[0] || audblk->cplcoe[1]))
		{
			for(j = 0; j < audblk->ncplbnd; j++)
			{
				audblk->phsflg[j] = mpeg3bits_getbits(audio->astream, 1);
			}
		}
	}

/* If we're in dual mono mode, there may be a rematrix strategy */
	if(bsi->acmod == 0x2)
	{
		audblk->rematstr = mpeg3bits_getbits(audio->astream, 1);
		if(audblk->rematstr)
		{
			if (audblk->cplinu == 0) 
			{ 
				for(i = 0; i < 4; i++) 
					audblk->rematflg[i] = mpeg3bits_getbits(audio->astream, 1);
			}
			if((audblk->cplbegf > 2) && audblk->cplinu) 
			{
				for(i = 0; i < 4; i++) 
					audblk->rematflg[i] = mpeg3bits_getbits(audio->astream, 1);
			}
			if((audblk->cplbegf <= 2) && audblk->cplinu) 
			{ 
				for(i = 0; i < 3; i++) 
					audblk->rematflg[i] = mpeg3bits_getbits(audio->astream, 1);
			} 
			if((audblk->cplbegf == 0) && audblk->cplinu) 
				for(i = 0; i < 2; i++) 
					audblk->rematflg[i] = mpeg3bits_getbits(audio->astream, 1);

		}
	}

	if (audblk->cplinu)
	{
/* Get the coupling channel exponent strategy */
		audblk->cplexpstr = mpeg3bits_getbits(audio->astream, 2);

		if(audblk->cplexpstr == 0) 
			audblk->ncplgrps = 0;
		else
			audblk->ncplgrps = (audblk->cplendmant - audblk->cplstrtmant) / 
				(3 << (audblk->cplexpstr - 1));

	}

	for(i = 0; i < bsi->nfchans; i++)
	{
		audblk->chexpstr[i] = mpeg3bits_getbits(audio->astream, 2);
	}

/* Get the exponent strategy for lfe channel */
	if(bsi->lfeon) 
		audblk->lfeexpstr = mpeg3bits_getbits(audio->astream, 1);

/* Determine the bandwidths of all the fbw channels */
	for(i = 0; i < bsi->nfchans; i++)
	{ 
		unsigned short grp_size;

		if(audblk->chexpstr[i] != MPEG3_EXP_REUSE) 
		{ 
			if (audblk->cplinu && audblk->chincpl[i]) 
			{
				audblk->endmant[i] = audblk->cplstrtmant;
			}
			else
			{
				audblk->chbwcod[i] = mpeg3bits_getbits(audio->astream, 6); 
				audblk->endmant[i] = ((audblk->chbwcod[i] + 12) * 3) + 37;
			}

/* Calculate the number of exponent groups to fetch */
			grp_size =  3 * (1 << (audblk->chexpstr[i] - 1));
			audblk->nchgrps[i] = (audblk->endmant[i] - 1 + (grp_size - 3)) / grp_size;
		}
	}

/* Get the coupling exponents if they exist */
	if(audblk->cplinu && (audblk->cplexpstr != MPEG3_EXP_REUSE))
	{
		audblk->cplabsexp = mpeg3bits_getbits(audio->astream, 4);
		for(i = 0; i< audblk->ncplgrps; i++)
			audblk->cplexps[i] = mpeg3bits_getbits(audio->astream, 7);
	}

/* Get the fwb channel exponents */
	for(i = 0; i < bsi->nfchans; i++)
	{
		if(audblk->chexpstr[i] != MPEG3_EXP_REUSE)
		{
			audblk->exps[i][0] = mpeg3bits_getbits(audio->astream, 4);
			for(j = 1; j <= audblk->nchgrps[i]; j++)
				audblk->exps[i][j] = mpeg3bits_getbits(audio->astream, 7);
			audblk->gainrng[i] = mpeg3bits_getbits(audio->astream, 2);
		}
	}

/* Get the lfe channel exponents */
	if(bsi->lfeon && (audblk->lfeexpstr != MPEG3_EXP_REUSE))
	{
		audblk->lfeexps[0] = mpeg3bits_getbits(audio->astream, 4);
		audblk->lfeexps[1] = mpeg3bits_getbits(audio->astream, 7);
		audblk->lfeexps[2] = mpeg3bits_getbits(audio->astream, 7);
	}

/* Get the parametric bit allocation parameters */
	audblk->baie = mpeg3bits_getbits(audio->astream, 1);

	if(audblk->baie)
	{
		audblk->sdcycod = mpeg3bits_getbits(audio->astream, 2);
		audblk->fdcycod = mpeg3bits_getbits(audio->astream, 2);
		audblk->sgaincod = mpeg3bits_getbits(audio->astream, 2);
		audblk->dbpbcod = mpeg3bits_getbits(audio->astream, 2);
		audblk->floorcod = mpeg3bits_getbits(audio->astream, 3);
	}


/* Get the SNR off set info if it exists */
	audblk->snroffste = mpeg3bits_getbits(audio->astream, 1);

	if(audblk->snroffste)
	{
		audblk->csnroffst = mpeg3bits_getbits(audio->astream, 6);

		if(audblk->cplinu)
		{
			audblk->cplfsnroffst = mpeg3bits_getbits(audio->astream, 4);
			audblk->cplfgaincod = mpeg3bits_getbits(audio->astream, 3);
		}

		for(i = 0; i < bsi->nfchans; i++)
		{
			audblk->fsnroffst[i] = mpeg3bits_getbits(audio->astream, 4);
			audblk->fgaincod[i] = mpeg3bits_getbits(audio->astream, 3);
		}
		if(bsi->lfeon)
		{

			audblk->lfefsnroffst = mpeg3bits_getbits(audio->astream, 4);
			audblk->lfefgaincod = mpeg3bits_getbits(audio->astream, 3);
		}
	}

/* Get coupling leakage info if it exists */
	if(audblk->cplinu)
	{
		audblk->cplleake = mpeg3bits_getbits(audio->astream, 1);	
		
		if(audblk->cplleake)
		{
			audblk->cplfleak = mpeg3bits_getbits(audio->astream, 3);
			audblk->cplsleak = mpeg3bits_getbits(audio->astream, 3);
		}
	}
	
/* Get the delta bit alloaction info */
	audblk->deltbaie = mpeg3bits_getbits(audio->astream, 1);	

	if(audblk->deltbaie)
	{
		if(audblk->cplinu)
			audblk->cpldeltbae = mpeg3bits_getbits(audio->astream, 2);

		for(i = 0; i < bsi->nfchans; i++)
			audblk->deltbae[i] = mpeg3bits_getbits(audio->astream, 2);

		if (audblk->cplinu && (audblk->cpldeltbae == DELTA_BIT_NEW))
		{
			audblk->cpldeltnseg = mpeg3bits_getbits(audio->astream, 3);
			for(i = 0; i < audblk->cpldeltnseg + 1; i++)
			{
				audblk->cpldeltoffst[i] = mpeg3bits_getbits(audio->astream, 5);
				audblk->cpldeltlen[i] = mpeg3bits_getbits(audio->astream, 4);
				audblk->cpldeltba[i] = mpeg3bits_getbits(audio->astream, 3);
			}
		}

		for(i = 0; i < bsi->nfchans; i++)
		{
			if (audblk->deltbae[i] == DELTA_BIT_NEW)
			{
				audblk->deltnseg[i] = mpeg3bits_getbits(audio->astream, 3);
				for(j = 0; j < audblk->deltnseg[i] + 1; j++)
				{
					audblk->deltoffst[i][j] = mpeg3bits_getbits(audio->astream, 5);
					audblk->deltlen[i][j] = mpeg3bits_getbits(audio->astream, 4);
					audblk->deltba[i][j] = mpeg3bits_getbits(audio->astream, 3);
				}
			}
		}
	}

/* Check to see if there's any dummy info to get */
	if((audblk->skiple = mpeg3bits_getbits(audio->astream, 1)))
	{
		unsigned int skip_data;
		audblk->skipl = mpeg3bits_getbits(audio->astream, 9);
		for(i = 0; i < audblk->skipl ; i++)
		{
			skip_data = mpeg3bits_getbits(audio->astream, 8);
		}
	}

	return mpeg3bits_error(audio->astream);
}