uint_32 ac3_decode_data(uint_8 *data_start, uint_32 length, uint_32 *start, short *audio_buf_ptr) { uint_8 *data_initial = data_start; uint_8 *data_end = data_start + length; uint_32 i, j = *start*2; int ret; // error_flag = buffer_size = 0; // syncinfo.syncword = 0xffff; // printf("In ac3_decode_data\n"); while ((ret = decode_buffer_syncframe(&syncinfo, &data_start, data_end)) > 0) { if (error_flag) { #ifdef _WIN32 SetDlgItemText(hDlg, IDC_INFO, "A.E.!"); #else printf("A.E.!\n"); #endif ZeroMemory(s16_samples, sizeof(sint_16) * 256 * 2 * 6); error_flag = 0; continue; } parse_bsi(&bsi); for (i=0; i<6; i++) { ZeroMemory(samples, sizeof(double) * 256 * (bsi.nfchans + bsi.lfeon)); parse_audblk(&bsi, &audblk); exponent_unpack(&bsi, &audblk); if (error_flag) goto error; bit_allocate(syncinfo.fscod, &bsi, &audblk); coeff_unpack(&bsi, &audblk, samples); if (error_flag) goto error; if (bsi.acmod == 0x2) rematrix(&audblk, samples); imdct(&bsi, &audblk, samples); downmix(&audblk, &bsi, samples, &s16_samples[i * 512]); sanity_check(&bsi, &audblk); if (error_flag) goto error; } memcpy(&audio_buf_ptr[(*start)/2], s16_samples, 6144); *start += 6144; error: ; } // if (ret == -1) // return(-1); sampling_rate=syncinfo.sampling_rate; AC3_byterate = syncinfo.bit_rate/8*1000; /* if (Decision_Flag || (!SRC_Flag && Normalization_Flag)) for (i=(j>>1); i<(start>>1); i++) if (Sound_Max < abs(ptrAC3Dec_Buffer[i])) { Sound_Max = abs(ptrAC3Dec_Buffer[i]); if (Decision_Flag && Sound_Max > Norm_Ratio) { sprintf(szBuffer, "%.2f", 327.68 * Norm_Ratio / Sound_Max); SetDlgItemText(hDlg, IDC_INFO, szBuffer); } } */ // return (data_end - data_initial); return (data_start - data_initial); }
int AC3_SampleConvert(sint_16 *pPcmData, uint_32 *pnPcmDataLen, uint_8* pAc3Buf, uint_32 nAc3DataLen) { int i; bitstream_t *bs; long SampleRate = 0; /* bitstream sample-rate */ uint_32 cbErrors = 0; /* bitstream error count returned by decode_sanity_check() */ uint_32 cbMantErrors, cbExpErrors; /* error counters for mantissa & exponent unpacking errors */ bsi_t bsi_blank = {0}; static audblk_t audblk_blank = {0}; bs = bitstream_open(pAc3Buf, pAc3Buf+nAc3DataLen); imdct_init(); /* initialize decoder and counter variables */ (bsi) = bsi_blank; (audblk) = audblk_blank; decode_sanity_check_init(); bsi_blank = bsi; audblk_blank = audblk; cbErrors = 0; audblk = audblk_blank; /* clear out audioblock */ if(!(decode_resync(bs))) { return 0; } bsi = bsi_blank; /* v0.04 wipe bsi clear, not really necessary */ parse_syncinfo(&syncinfo,bs); parse_bsi(&bsi,bs); switch (syncinfo.fscod) { case 2: SampleRate = 32000; break; case 1: SampleRate = 44100; break; case 0: SampleRate = 48000; break; default: return 0; } /* reset bitstream error counters */ cbErrors = cbExpErrors = cbMantErrors = 0; memset(pPcmData, 0, BUFFER_SIZE1*2); *pnPcmDataLen = 0; for(i=0; i < 6; i++) { long buf_offset; parse_audblk(&bsi,&audblk,bs,i); // CPU time 10% cbExpErrors = exponent_unpack(&bsi,&audblk); if(cbExpErrors > 0) { return 0; cbExpErrors =0; } bit_allocate(syncinfo.fscod,&bsi,&audblk); // CPU TIME 1.2% if ( bsi.nfchans > 6 ) { bsi.nfchans = 0; return 0;//(LPBYTE) out_buf; } cbMantErrors = mantissa_unpack(&bsi,&audblk,bs); // CPU TIME 62.0% if( cbMantErrors > 0) { return 0; cbMantErrors = 0; } uncouple(&bsi,&audblk,&stream_coeffs); // CPU TIME 1.7% if(bsi.acmod == 0x2) rematrix(&audblk,&stream_coeffs); // CPU TIME 0.1% imdct(&bsi,&audblk,&stream_coeffs,&stream_samples); // CPU TIME 11.2% buf_offset = i * 512; downmix( &stream_samples, pPcmData+buf_offset, &bsi, 0 ); } /* endfor ( i = 0 ... ) */ *pnPcmDataLen = 6*512*2; cbErrors = decode_sanity_check(); if(cbErrors > 0) { return 0; } parse_auxdata(&syncinfo,bs); // CPU TIME 2.0% if(!crc_validate()) { return 0; } return 1; }