int __stdcall ac3_decode_frame(ac3_state *state) { uint_32 i; int j; int error_flag=0; // debug(DAC3,"(decode_frame) begin frame %d\n",state->frame_count); //find a syncframe and parse parse_syncinfo(&state->syncinfo,&error_flag,state); if(error_flag) { // debug(DAC3," -- fail after parse_syncinfo\n"); goto error; } state->frame_count++; state->sampling_rate = state->syncinfo.sampling_rate; parse_bsi(&state->bsi,state); stats_print_banner(&state->syncinfo,&state->bsi,state); for(i=0; i < 6; i++) { //Initialize freq/time sample storage memset(state->samples,0,sizeof(float) * 256 * (state->bsi.nfchans + state->bsi.lfeon)); // Extract most of the audblk info from the bitstream // (minus the mantissas parse_audblk(&state->bsi,&state->audblk,state); // Take the differential exponent data and turn it into // absolute exponents exponent_unpack(&state->bsi,&state->audblk,&error_flag,state); if(error_flag) { // debug(DAC3," -- fail after exponent_unpack\n"); goto error; } // Figure out how many bits per mantissa bit_allocate(state->syncinfo.fscod,&state->bsi,&state->audblk,state); // Extract the mantissas from the stream and // generate floating point frequency coefficients coeff_unpack(&state->bsi,&state->audblk,state->samples,&error_flag,state); if(error_flag) { // debug(DAC3," -- fail after coeff_unpack\n"); goto error; } if(state->bsi.acmod == 0x2) { rematrix(&state->audblk,state->samples); } // Convert the frequency samples into time samples imdct(&state->bsi,&state->audblk,state->samples); // Downmix into the requested number of channels // and convert floating point to sint_16 for(j=0;j<256;j++) { state->left[i*256+j]=(sint_16)(state->samples[0][j]*32767.0f); state->center[i*256+j]=(sint_16)(state->samples[1][j]*32767.0f); state->right[i*256+j]=(sint_16)(state->samples[2][j]*32767.0f); state->sleft[i*256+j]=(sint_16)(state->samples[3][j]*32767.0f); state->sright[i*256+j]=(sint_16)(state->samples[4][j]*32767.0f); state->subwoofer[i*256+j]=(sint_16)(state->samples[5][j]*32767.0f); } sanity_check(&state->syncinfo,&state->bsi,&state->audblk,&error_flag,state); if(error_flag) { // debug(DAC3," -- sanity check fail\n"); goto error; } } parse_auxdata(&state->syncinfo,state); return 0; error: // debug(DAC3," -- FRAME ZEROED\n"); return -1; }
uint_32 decode_buffer_syncframe(syncinfo_t *syncinfo, uint_8 **start, uint_8 *end) { uint_8 *cur = *start; uint_16 syncword = syncinfo->syncword; uint_32 ret = 0; // find an ac3 sync frame resync: while(syncword != 0x0b77) { if(cur >= end) { // ret = -1; goto done; } syncword = (syncword << 8) + *cur++; } // need the next 3 bytes to decide how big the frame is while(buffer_size < 3) { if(cur >= end) goto done; buffer[buffer_size++] = *cur++; } parse_syncinfo(syncinfo, buffer); if (syncinfo->frame_size==0) // CRITICAL CONDITION goto done; while (buffer_size < (syncinfo->frame_size<<1) - 2) { if(cur >= end) goto done; buffer[buffer_size++] = *cur++; } // check the crc over the entire frame if (crc_process_frame(buffer, (syncinfo->frame_size<<1) - 2)) { #ifdef _WIN32 Debug(8, "Audio error\n"); SetDlgItemText(hDlg, IDC_INFO, "A.E.!"); #else fprintf(stderr, "\nAudio error, skipping bad input frame\n"); #endif *start = cur; // Skip bad input frame syncword = 0xffff; buffer_size = 0; goto resync; } // if we got to this point, we found a valid ac3 frame to decode bitstream_init(buffer); // get rid of the syncinfo struct as we already parsed it bitstream_get(24); ret = 1; *start = cur; done: // reset the syncword for next time syncword = 0xffff; buffer_size = 0; //done: syncinfo->syncword = syncword; return ret; }
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; }