Exemplo n.º 1
0
int mpeg3audio_read_pcm_header(mpeg3audio_t *audio)
{
	unsigned int code;

	code = mpeg3bits_getbits(audio->astream, 16);
	while(!mpeg3bits_eof(audio->astream) && code != MPEG3_PCM_START_CODE)
	{
		code <<= 8;
		code &= 0xffff;
		code |= mpeg3bits_getbits(audio->astream, 8);
	}

	audio->avg_framesize = audio->framesize = 0x7db;
	audio->channels = 2;

	return mpeg3bits_eof(audio->astream);
}
Exemplo n.º 2
0
int mpeg3audio_dolayer2(mpeg3audio_t *audio)
{
	int i, j, result = 0;
	int channels = audio->channels;
	float fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
	unsigned int bit_alloc[64];
	int scale[192];
	int single = audio->single;

 	if(audio->error_protection)
		mpeg3bits_getbits(audio->astream, 16);

	mpeg3audio_II_select_table(audio);

  	audio->jsbound = (audio->mode == MPG_MD_JOINT_STEREO) ?
     	(audio->mode_ext << 2) + 4 : audio->II_sblimit;

  	if(channels == 1 || single == 3)
    	single = 0;

  	result |= mpeg3audio_II_step_one(audio, bit_alloc, scale);

	for(i = 0; i < SCALE_BLOCK && !result; i++)
	{
    	result |= mpeg3audio_II_step_two(audio, bit_alloc, fraction, scale, i >> 2);

    	for(j = 0; j < 3; j++) 
    	{
    		if(single >= 0)
    		{
/* Monaural */
        		mpeg3audio_synth_mono(audio, fraction[single][j], audio->pcm_sample, &(audio->pcm_point));
    		}
    		else 
			{
/* Stereo */
        		int p1 = audio->pcm_point;
        		mpeg3audio_synth_stereo(audio, fraction[0][j], 0, audio->pcm_sample, &p1);
        		mpeg3audio_synth_stereo(audio, fraction[1][j], 1, audio->pcm_sample, &(audio->pcm_point));
    		}

    		if(audio->pcm_point / audio->channels >= audio->pcm_allocated - MPEG3AUDIO_PADDING * audio->channels)
			{
/* Need more room */
				mpeg3audio_replace_buffer(audio, audio->pcm_allocated + MPEG3AUDIO_PADDING * audio->channels);
			}
    	}
	}


  	return result;
}
int mpeg3audio_III_get_scale_factors_1(mpeg3audio_t *audio,
		int *scf, 
		struct gr_info_s *gr_info, 
		int ch, 
		int gr)
{
	static unsigned char slen[2][16] = 
		{{0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
		 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}};
	int numbits;
	int num0 = slen[0][gr_info->scalefac_compress];
	int num1 = slen[1][gr_info->scalefac_compress];

    if (gr_info->block_type == 2) 
	{
		int i = 18;
		numbits = (num0 + num1) * 18;

		if (gr_info->mixed_block_flag) 
		{
			for(i = 8; i; i--)
				*scf++ = mpeg3bits_getbits(audio->astream, num0);
			i = 9;
/* num0 * 17 + num1 * 18 */
			numbits -= num0; 
		}

		for( ; i; i--)
			*scf++ = mpeg3bits_getbits(audio->astream, num0);
		for(i = 18; i; i--)
			*scf++ = mpeg3bits_getbits(audio->astream, num1);
/* short[13][0..2] = 0 */
		*scf++ = 0; 
		*scf++ = 0; 
		*scf++ = 0; 
    }
    else 
	{
    	int i;
    	int scfsi = gr_info->scfsi;

    	if(scfsi < 0)
		{ 
/* scfsi < 0 => granule == 0 */
			for(i = 11; i; i--)
			{
				*scf++ = mpeg3bits_getbits(audio->astream, num0);
			}
			for(i = 10; i; i--)
				*scf++ = mpeg3bits_getbits(audio->astream, num1);
			numbits = (num0 + num1) * 10 + num0;
			*scf++ = 0;
    	}
    	else 
		{
    		numbits = 0;
    		if(!(scfsi & 0x8)) 
			{
        		for(i = 0; i < 6; i++)
				{
        			*scf++ = mpeg3bits_getbits(audio->astream, num0);
				}
        		numbits += num0 * 6;
    		}
    		else 
			{
        		scf += 6; 
    		}

    		if(!(scfsi & 0x4)) 
			{
        		for(i = 0; i < 5; i++)
        		  *scf++ = mpeg3bits_getbits(audio->astream, num0);
        		numbits += num0 * 5;
    		}
    		else 
			{
				scf += 5;
    		}

    		if(!(scfsi & 0x2)) 
			{
        	    for(i = 0; i < 5; i++)
        			*scf++ = mpeg3bits_getbits(audio->astream, num1);
        	    numbits += num1 * 5;
    		}
    		else 
			{
        	    scf += 5; 
    		}

    		if(!(scfsi & 0x1)) 
			{
        	    for(i = 0; i < 5; i++)
        			*scf++ = mpeg3bits_getbits(audio->astream, num1);
        	    numbits += num1 * 5;
    		}
    		else 
			{
        	    scf += 5;
    		}
    		*scf++ = 0;  /* no l[21] in original sources */
    	}
    }
    return numbits;
}
Exemplo n.º 4
0
/* Fetch an unpacked, left justified, and properly biased/dithered mantissa value */
static unsigned short mpeg3audio_ac3_mantissa_get(mpeg3audio_t *audio, 
	unsigned short bap, 
	unsigned short dithflag)
{
	unsigned short mantissa;
	unsigned int group_code;
	mpeg3_ac3_mantissa_t *mantissa_struct = &(audio->ac3_mantissa);

/* If the bap is 0-5 then we have special cases to take care of */
	switch(bap)
	{
		case 0:
			if(dithflag)
				mantissa = mpeg3audio_ac3_dither_gen(audio);
			else
				mantissa = 0;
			break;

		case 1:
			if(mantissa_struct->m_1_pointer > 2)
			{
				group_code = mpeg3bits_getbits(audio->astream, 5);

				if(group_code > 26)
				{
/* FIXME do proper block error handling */
//					fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 1 %d\n", group_code);
					return 0;
				}

				mantissa_struct->m_1[0] = group_code / 9; 
				mantissa_struct->m_1[1] = (group_code % 9) / 3; 
				mantissa_struct->m_1[2] = (group_code % 9) % 3; 
				mantissa_struct->m_1_pointer = 0;
			}
			mantissa = mantissa_struct->m_1[mantissa_struct->m_1_pointer++];
			mantissa = mpeg3_q_1[mantissa];
			break;

		case 2:
			if(mantissa_struct->m_2_pointer > 2)
			{
				group_code = mpeg3bits_getbits(audio->astream, 7);

				if(group_code > 124)
				{
//					fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 2 %d\n", group_code);
					return 0;
				}

				mantissa_struct->m_2[0] = group_code / 25;
				mantissa_struct->m_2[1] = (group_code % 25) / 5;
				mantissa_struct->m_2[2] = (group_code % 25) % 5; 
				mantissa_struct->m_2_pointer = 0;
			}
			mantissa = mantissa_struct->m_2[mantissa_struct->m_2_pointer++];
			mantissa = mpeg3_q_2[mantissa];
			break;

		case 3:
			mantissa = mpeg3bits_getbits(audio->astream, 3);

			if(mantissa > 6)
			{
//				fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 3 %d\n", group_code);
				return 0;
			}

			mantissa = mpeg3_q_3[mantissa];
			break;

		case 4:
			if(mantissa_struct->m_4_pointer > 1)
			{
				group_code = mpeg3bits_getbits(audio->astream, 7);

				if(group_code > 120)
				{
//					fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 4 %d\n", group_code);
					return 0;
				}

				mantissa_struct->m_4[0] = group_code / 11;
				mantissa_struct->m_4[1] = group_code % 11;
				mantissa_struct->m_4_pointer = 0;
			}
			mantissa = mantissa_struct->m_4[mantissa_struct->m_4_pointer++];
			mantissa = mpeg3_q_4[mantissa];
			break;

		case 5:
			mantissa = mpeg3bits_getbits(audio->astream, 4);

			if(mantissa > 14)
			{
/* FIXME do proper block error handling */
//				fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 5 %d\n", group_code);
				return 0;
			}

			mantissa = mpeg3_q_5[mantissa];
			break;

		default:
			mantissa = mpeg3bits_getbits(audio->astream, mpeg3_qnttztab[bap]);
			mantissa <<= 16 - mpeg3_qnttztab[bap];
	}
	return mantissa;
}
Exemplo n.º 5
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.º 6
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.º 7
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);
}
Exemplo n.º 8
0
int mpeg3audio_read_ac3_header(mpeg3audio_t *audio)
{
	unsigned int code, crc;
	unsigned int i;
	mpeg3_ac3bsi_t *bsi = &(audio->ac3_bsi);

/* Get the sync code */
	code = mpeg3bits_getbits(audio->astream, 16);
	while(!mpeg3bits_eof(audio->astream) && code != MPEG3_AC3_START_CODE)
	{
		code <<= 8;
		code &= 0xffff;
		code |= mpeg3bits_getbits(audio->astream, 8);
	}

	if(mpeg3bits_eof(audio->astream)) return 1;

/* Get crc1 - we don't actually use this data though */
/* The crc can be the same as the sync code or come after a sync code repeated twice */
	crc = mpeg3bits_getbits(audio->astream, 16);

/* Got the sync code.  Read the entire frame into a buffer if possible. */
	if(audio->avg_framesize > 0)
	{
		if(mpeg3bits_read_buffer(audio->astream, audio->ac3_buffer, audio->framesize - 4))
			return 1;
		mpeg3bits_use_ptr(audio->astream, audio->ac3_buffer);	
	}

/* Get the sampling rate code */
	audio->sampling_frequency_code  = mpeg3bits_getbits(audio->astream, 2);

/* Get the frame size code */
	audio->ac3_framesize_code = mpeg3bits_getbits(audio->astream, 6);

	audio->bitrate = framesize_codes[audio->ac3_framesize_code].bit_rate;
	audio->avg_framesize = audio->framesize = 2 * framesize_codes[audio->ac3_framesize_code].frm_size[audio->sampling_frequency_code];

/* Check the AC-3 version number */
	bsi->bsid = mpeg3bits_getbits(audio->astream, 5);

/* Get the audio service provided by the steram */
	bsi->bsmod = mpeg3bits_getbits(audio->astream, 3);

/* Get the audio coding mode (ie how many channels)*/
	bsi->acmod = mpeg3bits_getbits(audio->astream, 3);
	
/* Predecode the number of full bandwidth channels as we use this
 * number a lot */
	bsi->nfchans = mpeg3_ac3_acmodes[bsi->acmod];
	audio->channels = bsi->nfchans;

/* If it is in use, get the centre channel mix level */
	if((bsi->acmod & 0x1) && (bsi->acmod != 0x1))
		bsi->cmixlev = mpeg3bits_getbits(audio->astream, 2);

/* If it is in use, get the surround channel mix level */
	if(bsi->acmod & 0x4)
		bsi->surmixlev = mpeg3bits_getbits(audio->astream, 2);

/* Get the dolby surround mode if in 2/0 mode */
	if(bsi->acmod == 0x2)
		bsi->dsurmod= mpeg3bits_getbits(audio->astream, 2);

/* Is the low frequency effects channel on? */
	bsi->lfeon = mpeg3bits_getbits(audio->astream, 1);

/* Get the dialogue normalization level */
	bsi->dialnorm = mpeg3bits_getbits(audio->astream, 5);

/* Does compression gain exist? */
	bsi->compre = mpeg3bits_getbits(audio->astream, 1);
	if (bsi->compre)
	{
/* Get compression gain */
		bsi->compr = mpeg3bits_getbits(audio->astream, 8);
	}

/* Does language code exist? */
	bsi->langcode = mpeg3bits_getbits(audio->astream, 1);
	if (bsi->langcode)
	{
/* Get langauge code */
		bsi->langcod = mpeg3bits_getbits(audio->astream, 8);
	}

/* Does audio production info exist? */
	bsi->audprodie = mpeg3bits_getbits(audio->astream, 1);
	if (bsi->audprodie)
	{
/* Get mix level */
		bsi->mixlevel = mpeg3bits_getbits(audio->astream, 5);

/* Get room type */
		bsi->roomtyp = mpeg3bits_getbits(audio->astream, 2);
	}

/* If we're in dual mono mode then get some extra info */
	if (bsi->acmod == 0)
	{
/* Get the dialogue normalization level two */
		bsi->dialnorm2 = mpeg3bits_getbits(audio->astream, 5);

/* Does compression gain two exist? */
		bsi->compr2e = mpeg3bits_getbits(audio->astream, 1);
		if (bsi->compr2e)
		{
/* Get compression gain two */
			bsi->compr2 = mpeg3bits_getbits(audio->astream, 8);
		}

/* Does language code two exist? */
		bsi->langcod2e = mpeg3bits_getbits(audio->astream, 1);
		if (bsi->langcod2e)
		{
/* Get langauge code two */
			bsi->langcod2 = mpeg3bits_getbits(audio->astream, 8);
		}

/* Does audio production info two exist? */
		bsi->audprodi2e = mpeg3bits_getbits(audio->astream, 1);
		if (bsi->audprodi2e)
		{
/* Get mix level two */
			bsi->mixlevel2 = mpeg3bits_getbits(audio->astream, 5);

/* Get room type two */
			bsi->roomtyp2 = mpeg3bits_getbits(audio->astream, 2);
		}
	}

/* Get the copyright bit */
	bsi->copyrightb = mpeg3bits_getbits(audio->astream, 1);

/* Get the original bit */
	bsi->origbs = mpeg3bits_getbits(audio->astream, 1);
	
/* Does timecode one exist? */
	bsi->timecod1e = mpeg3bits_getbits(audio->astream, 1);

	if(bsi->timecod1e)
		bsi->timecod1 = mpeg3bits_getbits(audio->astream, 14);

/* Does timecode two exist? */
	bsi->timecod2e = mpeg3bits_getbits(audio->astream, 1);

	if(bsi->timecod2e)
		bsi->timecod2 = mpeg3bits_getbits(audio->astream, 14);

/* Does addition info exist? */
	bsi->addbsie = mpeg3bits_getbits(audio->astream, 1);

	if(bsi->addbsie)
	{
/* Get how much info is there */
		bsi->addbsil = mpeg3bits_getbits(audio->astream, 6);

/* Get the additional info */
		for(i = 0; i < (bsi->addbsil + 1); i++)
			bsi->addbsi[i] = mpeg3bits_getbits(audio->astream, 8);
	}

	if(mpeg3bits_eof(audio->astream))
	{
		mpeg3bits_use_demuxer(audio->astream);
		return 1;
	}
	return 0;
//	return mpeg3bits_error(audio->astream);
}