void ampegdecoder::hybrid(grsistruct &si, float (*hout)[32], float (*prev)[18], float *xr) { int nbands=32>>ratereduce; int lim=(si.blocktype!=2)?(32>>ratereduce):si.mixedblock?(sfbands[hdrlsf][hdrfreq][3]/18):0; int sb,ss; for (sb=1;sb<lim;sb++) for (ss=0;ss<8;ss++) { float bu = xr[(sb-1)*18+17-ss]; float bd = xr[sb*18+ss]; xr[(sb-1)*18+17-ss] = (bu * csatab[ss][0]) - (bd * csatab[ss][1]); xr[sb*18+ss] = (bd * csatab[ss][0]) + (bu * csatab[ss][1]); } if (l3equalon) { int i; int shlim=lim*6; for (i=0; i<shlim; i++) { xr[3*i+0]*=l3equals[i]; xr[3*i+1]*=l3equals[i]; xr[3*i+2]*=l3equals[i]; } int nbnd=nbands*18; for (i=shlim*3; i<nbnd; i++) xr[i]*=l3equall[i]; } int mixlim=si.mixedblock?(sfbands[hdrlsf][hdrfreq][3]/18):0; for (sb=0; sb<nbands; sb++) { float rawout[18]; imdct(rawout, xr+sb*18, prev[sb], (sb<mixlim)?0:si.blocktype); if (sb&1) for (ss=0; ss<18; ss+=2) { hout[ss][sb]=rawout[ss]; hout[ss+1][sb]=-rawout[ss+1]; } else for (ss=0; ss<18; ss++) hout[ss][sb]=rawout[ss]; } }
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 __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; }
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; }
void ifilter_bank(fb_info *fb, uint8_t window_sequence, uint8_t window_shape, uint8_t window_shape_prev, real_t *freq_in, real_t *time_out, uint8_t object_type, uint16_t frame_len) { int32_t i; real_t *transf_buf=fb->tmp_buf; real_t *window_long; real_t *window_long_prev; real_t *window_short; real_t *window_short_prev; uint32_t nlong = frame_len; uint32_t nshort = frame_len/8; uint32_t trans = nshort/2; uint32_t nflat_ls = (nlong-nshort)/2; #ifdef LD_DEC if (object_type == LD){ window_long = fb->ld_window[window_shape]; window_long_prev = fb->ld_window[window_shape_prev]; } else { #endif window_long = fb->long_window[window_shape]; window_long_prev = fb->long_window[window_shape_prev]; window_short = fb->short_window[window_shape]; window_short_prev = fb->short_window[window_shape_prev]; #ifdef LD_DEC } #endif switch (window_sequence){ case ONLY_LONG_SEQUENCE: imdct(fb, freq_in, transf_buf, 2*nlong); for (i = 0; i <nlong; i+=4){ time_out[i ] = time_out[nlong+i ] + MUL_R_C(transf_buf[i ],window_long_prev[i ]); time_out[nlong+i ] = MUL_R_C(transf_buf[nlong+i ],window_long[nlong-1-i]); time_out[i+1] = time_out[nlong+i+1] + MUL_R_C(transf_buf[i+1],window_long_prev[i+1]); time_out[nlong+i+1] = MUL_R_C(transf_buf[nlong+i+1],window_long[nlong-1-i-1]); time_out[i+2] = time_out[nlong+i+2] + MUL_R_C(transf_buf[i+2],window_long_prev[i+2]); time_out[nlong+i+2] = MUL_R_C(transf_buf[nlong+i+2],window_long[nlong-1-i-2]); time_out[i+3] = time_out[nlong+i+3] + MUL_R_C(transf_buf[i+3],window_long_prev[i+3]); time_out[nlong+i+3] = MUL_R_C(transf_buf[nlong+i+3],window_long[nlong-1-i-3]); } /*for (i = nlong-1; i >= 0; i--){ time_out[i] = time_out[nlong+i] + MUL_R_C(transf_buf[i],window_long_prev[i]); time_out[nlong+i] = MUL_R_C(transf_buf[nlong+i],window_long[nlong-1-i]); }*/ break; case LONG_START_SEQUENCE: imdct(fb, freq_in, transf_buf, 2*nlong); for (i = 0; i <nlong; i+=4){ time_out[i ] = time_out[nlong+i ] + MUL_R_C(transf_buf[i ],window_long_prev[i ]); time_out[i+1] = time_out[nlong+i+1] + MUL_R_C(transf_buf[i+1],window_long_prev[i+1]); time_out[i+2] = time_out[nlong+i+2] + MUL_R_C(transf_buf[i+2],window_long_prev[i+2]); time_out[i+3] = time_out[nlong+i+3] + MUL_R_C(transf_buf[i+3],window_long_prev[i+3]); } //for (i = 0; i < nlong; i++) // time_out[i] = time_out[nlong+i] + MUL_R_C(transf_buf[i],window_long_prev[i]); for (i = 0; i < nflat_ls; i++) time_out[nlong+i] = transf_buf[nlong+i]; for (i = 0; i < nshort; i++) time_out[nlong+nflat_ls+i] = MUL_R_C(transf_buf[nlong+nflat_ls+i],window_short[nshort-i-1]); for (i = 0; i < nflat_ls; i++) time_out[nlong+nflat_ls+nshort+i] = 0; break; case EIGHT_SHORT_SEQUENCE: imdct(fb, freq_in+0*nshort, transf_buf+2*nshort*0, 2*nshort); imdct(fb, freq_in+1*nshort, transf_buf+2*nshort*1, 2*nshort); imdct(fb, freq_in+2*nshort, transf_buf+2*nshort*2, 2*nshort); imdct(fb, freq_in+3*nshort, transf_buf+2*nshort*3, 2*nshort); imdct(fb, freq_in+4*nshort, transf_buf+2*nshort*4, 2*nshort); imdct(fb, freq_in+5*nshort, transf_buf+2*nshort*5, 2*nshort); imdct(fb, freq_in+6*nshort, transf_buf+2*nshort*6, 2*nshort); imdct(fb, freq_in+7*nshort, transf_buf+2*nshort*7, 2*nshort); for (i = 0; i < nflat_ls; i++) time_out[i] = time_out[nlong+i]; for(i = nshort-1; i >= 0; i--){ time_out[nflat_ls+ i] = time_out[nlong+nflat_ls+ i] + MUL_R_C(transf_buf[nshort*0+i],window_short_prev[i]); time_out[nflat_ls+1*nshort+i] = time_out[nlong+nflat_ls+nshort*1+i] + MUL_R_C(transf_buf[nshort*1+i],window_short[nshort-1-i]) + MUL_R_C(transf_buf[nshort*2+i],window_short[i]); time_out[nflat_ls+2*nshort+i] = time_out[nlong+nflat_ls+nshort*2+i] + MUL_R_C(transf_buf[nshort*3+i],window_short[nshort-1-i]) + MUL_R_C(transf_buf[nshort*4+i],window_short[i]); time_out[nflat_ls+3*nshort+i] = time_out[nlong+nflat_ls+nshort*3+i] + MUL_R_C(transf_buf[nshort*5+i],window_short[nshort-1-i]) + MUL_R_C(transf_buf[nshort*6+i],window_short[i]); if (i < trans) time_out[nflat_ls+4*nshort+i] = time_out[nlong+nflat_ls+nshort*4+i] + MUL_R_C(transf_buf[nshort*7+i],window_short[nshort-1-i]) + MUL_R_C(transf_buf[nshort*8+i],window_short[i]); else time_out[nflat_ls+4*nshort+i] = MUL_R_C(transf_buf[nshort*7+i],window_short[nshort-1-i]) + MUL_R_C(transf_buf[nshort*8+i],window_short[i]); time_out[nflat_ls+5*nshort+i] = MUL_R_C(transf_buf[nshort*9+i],window_short[nshort-1-i]) + MUL_R_C(transf_buf[nshort*10+i],window_short[i]); time_out[nflat_ls+6*nshort+i] = MUL_R_C(transf_buf[nshort*11+i],window_short[nshort-1-i]) + MUL_R_C(transf_buf[nshort*12+i],window_short[i]); time_out[nflat_ls+7*nshort+i] = MUL_R_C(transf_buf[nshort*13+i],window_short[nshort-1-i]) + MUL_R_C(transf_buf[nshort*14+i],window_short[i]); time_out[nflat_ls+8*nshort+i] = MUL_R_C(transf_buf[nshort*15+i],window_short[nshort-1-i]); } for (i = 0; i < nflat_ls; i++) time_out[nlong+nflat_ls+nshort+i] = 0; break; case LONG_STOP_SEQUENCE: imdct(fb, freq_in, transf_buf, 2*nlong); for (i = 0; i < nflat_ls; i++) time_out[i] = time_out[nlong+i]; for (i = 0; i < nshort; i++) time_out[nflat_ls+i] = time_out[nlong+nflat_ls+i] + MUL_R_C(transf_buf[nflat_ls+i],window_short_prev[i]); for (i = 0; i < nflat_ls; i++) time_out[nflat_ls+nshort+i] = time_out[nlong+nflat_ls+nshort+i] + transf_buf[nflat_ls+nshort+i]; for (i = 0; i < nlong; i++) time_out[nlong+i] = MUL_R_C(transf_buf[nlong+i],window_long[nlong-1-i]); break; } }
/* this function decodes one layer3 audio frame, except for the header decoding * which is done in main() [audio.c]. returns 0 if everything is ok. */ int layer3_frame(struct AUDIO_HEADER *header,int cnt) { static struct SIDE_INFO info; int gr,ch,sb,i; int mean_frame_size,bitrate,fs,hsize,ssize; /* we need these later, hsize is the size of header+side_info */ if (header->ID) if (header->mode==3) { nch=1; hsize=21; } else { nch=2; hsize=36; } else if (header->mode==3) { nch=1; hsize=13; } else { nch=2; hsize=21; } /* crc increases hsize by 2 */ if (header->protection_bit==0) hsize+=2; /* read layer3 specific side_info */ getinfo(header,&info); /* MPEG2 only has one granule */ bitrate=t_bitrate[header->ID][3-header->layer][header->bitrate_index]; fs=t_sampling_frequency[header->ID][header->sampling_frequency]; if (header->ID) mean_frame_size=144000*bitrate/fs; else mean_frame_size=72000*bitrate/fs; /* check if mdb is too big for the first few frames. this means that * a part of the stream could be missing. We must still fill the buffer * * don't forget to (re)initialise bclean_bytes to 0, and f_bdirty to FALSE!!! */ if (f_bdirty) if (info.main_data_begin > bclean_bytes) { fillbfr(mean_frame_size + header->padding_bit - hsize); bclean_bytes+=mean_frame_size + header->padding_bit - hsize; /* warn(" frame %d discarded, incomplete main_data\n",cnt); */ return 0; } else { /* re-initialise */ f_bdirty=FALSE; bclean_bytes=0; } /* now update the data 'pointer' (counting in bits) according to * the main_data_begin information */ data = 8 * ((append - info.main_data_begin) & (BUFFER_SIZE-1)); /* read into the buffer all bytes up to the start of next header */ fillbfr(mean_frame_size + header->padding_bit - hsize); /* these two should go away */ t_l=&t_b8_l[header->ID][header->sampling_frequency][0]; t_s=&t_b8_s[header->ID][header->sampling_frequency][0]; /* debug/dump stuff */ if (A_DUMP_BINARY) dump((int *)info.part2_3_length); /* decode the scalefactors and huffman data * this part needs to be enhanced for error robustness */ for (gr=0;gr < ((header->ID) ? 2 : 1);gr++) { for (ch=0;ch<nch;ch++) { ssize=decode_scalefactors(&info,header,gr,ch); decode_huffman_data(&info,gr,ch,ssize); } /* requantization, stereo processing, reordering(shortbl) */ if (A_DOWNMIX && nch==2) requantize_downmix(gr,&info,header); else if (header->mode!=1 || (header->mode==1 && header->mode_extension==0)) for (ch=0;ch<nch;ch++) requantize_mono(gr,ch,&info,header); else requantize_ms(gr,&info,header); /* just which window? */ /* this is a very temporary, very ugly hack. */ if (A_DOWNMIX) nch=1; for (ch=0; ch < (A_DOWNMIX ? 1:nch) ;ch++) { int win_type; /* same as in the standard, long=0, start=1 ,.... */ int window_switching_flag = info.window_switching_flag[gr][ch]; int block_type = info.block_type[gr][ch]; int mixed_block_flag = info.mixed_block_flag[gr][ch]; /* antialiasing butterflies */ if (!(window_switching_flag && block_type==2)) alias_reduction(ch); if (window_switching_flag && block_type==2 && mixed_block_flag) win_type=0; else if (!window_switching_flag) win_type=0; else win_type=block_type; /* imdct ... */ for (sb=0;sb<2;sb++) imdct(win_type,sb,ch); if (window_switching_flag && block_type==2 && mixed_block_flag) win_type=2; /* no_of_imdcts tells us how many subbands from the top are all zero * it is set by the requantize functions in misc2.c */ for (sb=2;sb<no_of_imdcts[ch];sb++) imdct(win_type,sb,ch); for (;sb<32;sb++) for (i=0;i<18;i++) { res[sb][i]=s[ch][sb][i]; s[ch][sb][i]=0.0f; } /* polyphase filterbank */ /* if (nch == 2) this was a bug, tomislav */ for (i=0;i<18;i++) poly(ch,i); } printout(); /* this is part2 of a particularily ugly hack. this should vanish as soon as nch isn't a global variable anymore */ if (A_DOWNMIX && header->mode!=3) nch=2; } /* for (gr... */ /* return status: 0 for ok, errors will be added */ return 0; }