uint8_t rvlc_decode_scale_factors(ic_stream *ics, bitfile *ld) { uint8_t result; uint8_t intensity_used = 0; uint8_t *rvlc_sf_buffer = NULL; uint8_t *rvlc_esc_buffer = NULL; bitfile ld_rvlc_sf, ld_rvlc_esc; // bitfile ld_rvlc_sf_rev, ld_rvlc_esc_rev; if (ics->length_of_rvlc_sf > 0) { /* We read length_of_rvlc_sf bits here to put it in a seperate bitfile. */ rvlc_sf_buffer = faad_getbitbuffer(ld, ics->length_of_rvlc_sf DEBUGVAR(1,156,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_sf")); faad_initbits(&ld_rvlc_sf, (void*)rvlc_sf_buffer, bit2byte(ics->length_of_rvlc_sf)); // faad_initbits_rev(&ld_rvlc_sf_rev, (void*)rvlc_sf_buffer, // ics->length_of_rvlc_sf); } if (ics->sf_escapes_present) { /* We read length_of_rvlc_escapes bits here to put it in a seperate bitfile. */ rvlc_esc_buffer = faad_getbitbuffer(ld, ics->length_of_rvlc_escapes DEBUGVAR(1,157,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_escapes")); faad_initbits(&ld_rvlc_esc, (void*)rvlc_esc_buffer, bit2byte(ics->length_of_rvlc_escapes)); // faad_initbits_rev(&ld_rvlc_esc_rev, (void*)rvlc_esc_buffer, // ics->length_of_rvlc_escapes); } /* decode the rvlc scale factors and escapes */ result = rvlc_decode_sf_forward(ics, &ld_rvlc_sf, &ld_rvlc_esc, &intensity_used); // result = rvlc_decode_sf_reverse(ics, &ld_rvlc_sf_rev, // &ld_rvlc_esc_rev, intensity_used); if (rvlc_esc_buffer) faad_free(rvlc_esc_buffer); if (rvlc_sf_buffer) faad_free(rvlc_sf_buffer); if (ics->length_of_rvlc_sf > 0) faad_endbits(&ld_rvlc_sf); if (ics->sf_escapes_present) faad_endbits(&ld_rvlc_esc); return result; }
/* reversed bit reading routines, used for RVLC and HCR */ void faad_initbits_rev(bitfile *ld, void *buffer, uint32_t bits_in_buffer) { uint32_t tmp; int32_t index; ld->buffer_size = bit2byte(bits_in_buffer); index = (bits_in_buffer+31)/32 - 1; ld->start = (uint32_t*)buffer + index - 2; tmp = getdword((uint32_t*)buffer + index); ld->bufa = tmp; tmp = getdword((uint32_t*)buffer + index - 1); ld->bufb = tmp; ld->tail = (uint32_t*)buffer + index; ld->bits_left = bits_in_buffer % 32; if (ld->bits_left == 0) ld->bits_left = 32; ld->bytes_left = ld->buffer_size; ld->error = 0; }
static int CountBitstream(faacEncHandle hEncoder, CoderInfo *coderInfo, ChannelInfo *channelInfo, BitStream *bitStream, int numChannel) { int channel; int bits = 0; int bitsLeftAfterFill, numFillBits; if(hEncoder->config.outputFormat == 1){ bits += WriteADTSHeader(hEncoder, bitStream, 0); }else{ bits = 0; // compilier will remove it, byt anyone will see that current size of bitstream is 0 } /* sur: faad2 complains about scalefactor error if we are writing FAAC String */ #ifndef DRM if (hEncoder->frameNum == 4) bits += WriteFAACStr(bitStream, hEncoder->config.name, 0); #endif for (channel = 0; channel < numChannel; channel++) { if (channelInfo[channel].present) { /* Write out a single_channel_element */ if (!channelInfo[channel].cpe) { if (channelInfo[channel].lfe) { /* Write out lfe */ bits += WriteLFE(&coderInfo[channel], &channelInfo[channel], bitStream, hEncoder->config.aacObjectType, 0); } else { /* Write out sce */ bits += WriteSCE(&coderInfo[channel], &channelInfo[channel], bitStream, hEncoder->config.aacObjectType, 0); } } else { if (channelInfo[channel].ch_is_left) { /* Write out cpe */ bits += WriteCPE(&coderInfo[channel], &coderInfo[channelInfo[channel].paired_ch], &channelInfo[channel], bitStream, hEncoder->config.aacObjectType, 0); } } } } /* Compute how many fill bits are needed to avoid overflowing bit reservoir */ /* Save room for ID_END terminator */ if (bits < (8 - LEN_SE_ID) ) { numFillBits = 8 - LEN_SE_ID - bits; } else { numFillBits = 0; } /* Write AAC fill_elements, smallest fill element is 7 bits. */ /* Function may leave up to 6 bits left after fill, so tell it to fill a few extra */ numFillBits += 6; bitsLeftAfterFill = WriteAACFillBits(bitStream, numFillBits, 0); bits += (numFillBits - bitsLeftAfterFill); /* Write ID_END terminator */ bits += LEN_SE_ID; /* Now byte align the bitstream */ bits += ByteAlign(bitStream, 0, bits); hEncoder->usedBytes = bit2byte(bits); return bits; }
int fa_write_bitstream(fa_aacenc_ctx_t *f) { int i; int total_bits; int chn_num; int chn; int write_bits; aacenc_ctx_t *s, *sl, *sr; total_bits = 56; i = 0; chn = 1; chn_num = f->cfg.chn_num; while (i < chn_num) { s = &(f->ctx[i]); if (s->chn_info.cpe == 1) { chn = 2; /*total_bits += s->used_bits;*/ total_bits += fa_bits_count(f->h_bitstream, &f->cfg, &(f->ctx[i]), &(f->ctx[i+1])); } else if (s->chn_info.sce == 1) { chn = 1; /*total_bits += s->used_bits;*/ total_bits += fa_bits_count(f->h_bitstream, &f->cfg, &(f->ctx[i]), NULL); } else { chn = 1; total_bits += s->used_bits; } i += chn; } total_bits += LEN_SE_ID; total_bits += write_bits_for_bytealign(f->h_bitstream, s, total_bits, 0); f->used_bytes = bit2byte(total_bits); write_bits = 0; write_bits += write_adtsheader(f->h_bitstream, &f->cfg, f->used_bytes, 1); i = 0; chn = 1; while (i < chn_num) { s = &(f->ctx[i]); if (s->chn_info.cpe == 1) { chn = 2; sl = s; sr = &(f->ctx[i+1]); write_bits += fa_write_bitstream_onechn(f->h_bitstream, &f->cfg, sl, sr); } else if (s->chn_info.sce == 1) { chn = 1; write_bits += fa_write_bitstream_onechn(f->h_bitstream, &f->cfg, s, NULL); } else { chn = 1; write_bits += fa_write_bitstream_onechn(f->h_bitstream, &f->cfg, s, NULL); } i += chn; } write_bits += fa_bitstream_putbits(f->h_bitstream, ID_END, LEN_SE_ID); write_bits += write_bits_for_bytealign(f->h_bitstream, s, write_bits, 1); return total_bits; }
/* * Decode task call for CELP */ static int celp_decode (codec_data_t *ptr, frame_timestamp_t *pts, int from_rtp, int *sync_frame, uint8_t *buffer, uint32_t buflen, void *userdata) { int usedNumBit; celp_codec_t *celp = (celp_codec_t *)ptr; uint32_t freq_ts; freq_ts = pts->audio_freq_timestamp; if (pts->audio_freq != celp->m_freq) { freq_ts = convert_timescale(freq_ts, pts->audio_freq, celp->m_freq); } if (celp->m_record_sync_time) { celp->m_current_frame = 0; celp->m_record_sync_time = 0; celp->m_current_time = pts->msec_timestamp; celp->m_last_rtp_ts = freq_ts; celp->m_current_freq_time = freq_ts; } else { if (celp->m_last_rtp_ts == pts->audio_freq_timestamp) { celp->m_current_frame++; celp->m_current_time = celp->m_last_rtp_ts; celp->m_current_time += celp->m_samples_per_frame * celp->m_current_frame * TO_U64(1000) / celp->m_freq; celp->m_current_freq_time += celp->m_samples_per_frame; } else { celp->m_last_rtp_ts = celp->m_current_freq_time = freq_ts; celp->m_current_time = pts->msec_timestamp; celp->m_current_frame = 0; } // Note - here m_current_time should pretty much always be >= rtpts. // If we're not, we most likely want to stop and resync. We don't // need to keep decoding - just decode this frame and indicate we // need a resync... That should handle fast forwards... We need // someway to handle reverses - perhaps if we're more than .5 seconds // later... } if (celp->m_celp_inited == 0) { /* * If not initialized, do so. */ // celp->m_celp_inited = 1; } //printf("buflen:%d\n",buflen); //if ( ((celp->m_last-buflen)/celp->m_last) < 0.2) return (0); if ( buflen<5) return (-1); BsBitBuffer local; local.data= (unsigned char *)buffer; local.numBit=buflen*8; local.size=buflen*8; DecLpcFrame(&local,celp->m_sampleBuf,&usedNumBit); //AudioWriteData(celp->audiFile,celp->m_sampleBuf,celp->m_output_frame_size); int chan,sample; uint8_t *now = celp->m_vft->audio_get_buffer(celp->m_ifptr, celp->m_current_freq_time, celp->m_current_time); if (now != NULL) { uint16_t *buf = (uint16_t *)now; for(chan=0;chan<celp->m_chans;chan++){ for(sample=0;sample < celp->m_output_frame_size; sample++){ buf[sample +(chan*celp->m_output_frame_size)]= (uint16_t)celp->m_sampleBuf[chan][sample]; } } } #if DUMP_OUTPUT_TO_FILE fwrite(buff, celp->m_output_frame_size * 4, 1, celp->m_outfile); #endif celp->m_vft->audio_filled_buffer(celp->m_ifptr); return bit2byte(usedNumBit); }