示例#1
0
void fa_aacenc_encode(uintptr_t handle, unsigned char *buf_in, int inlen, unsigned char *buf_out, int *outlen)
{
    int i,j;
    int chn_num;
    short *sample_in;
    float *sample_buf;
    float sample_psy_buf[2*AAC_FRAME_LEN];
    int ms_enable;
    int tns_enable;
    int block_switch_en;
    int psy_enable;
    int psy_model;
    int speed_level;
    fa_aacenc_ctx_t *f = (fa_aacenc_ctx_t *)handle;
    aacenc_ctx_t *s;
    int block_type;

    float qcof;

    speed_level = f->speed_level;

    tns_enable  = f->cfg.tns_enable;
    ms_enable   = f->cfg.ms_enable;
    chn_num     = f->cfg.chn_num;
    qcof        = f->cfg.qcof;
    /*assert(inlen == chn_num*AAC_FRAME_LEN*2);*/

    /*update sample buffer, ith sample, jth chn*/
    sample_in = (short *)buf_in;
    for (i = 0; i < AAC_FRAME_LEN; i++) 
        for (j = 0; j < chn_num; j++) 
            f->sample[i+j*AAC_FRAME_LEN] = (float)(0.99 * sample_in[i*chn_num+j]);

    block_switch_en = f->block_switch_en;
    psy_enable      = f->psy_enable;
    psy_model       = f->psy_model;

    /*block switch and use filterbank to generate mdctline*/
    block_type = ONLY_LONG_BLOCK;

    /*should use cpe or sce to decide in the future*/
    for (i = 0; i < chn_num; i++) {
        s = &(f->ctx[i]);

        /*block switch */
        if (s->time_resolution_first) {
            s->block_type = ONLY_SHORT_BLOCK;
        } else {
            if (block_switch_en) {
#if 0 
                f->do_blockswitch(s);
#else 
                fa_blockswitch_robust(s, f->sample+i*AAC_FRAME_LEN);
#endif 
            } else {
                s->block_type = ONLY_LONG_BLOCK;
                /*s->block_type = ONLY_SHORT_BLOCK;*/
            }
        }
    }

    fa_blocksync(f);

    for (i = 0; i < chn_num; i++) {
        s = &(f->ctx[i]);

        /*memset(s->xmin, 0, sizeof(float)*8*FA_SWB_NUM_MAX);*/

        /*get the input sample*/
        sample_buf = f->sample+i*AAC_FRAME_LEN;

        /*analysis*/
        fa_aacfilterbank_analysis(s->h_aac_analysis, s->block_type, &(s->window_shape),
                                  sample_buf, s->mdct_line);

        /*cutoff the frequence according to the bitrate*/
        if (s->block_type == ONLY_SHORT_BLOCK) {
            int k;
            for (k = 0; k < 8; k++)
                zero_cutoff(s->mdct_line+k*128, 128, s->cutoff_line_short);
        } else
            zero_cutoff(s->mdct_line, 1024, s->cutoff_line_long);

        /* 
           calculate xmin and pe
           --use current sample_buf calculate pe to decide which block used in the next frame
        */
        if (psy_enable) {
            fa_aacfilterbank_get_xbuf(s->h_aac_analysis, sample_psy_buf);
            if (psy_model == PSYCH1) {
                fa_aacpsy_calculate_xmin_usepsych1(s->h_aacpsy, s->mdct_line, s->block_type, s->xmin);
            } else {
                fa_aacpsy_calculate_pe(s->h_aacpsy, sample_psy_buf, s->block_type, &s->pe, &s->tns_active);
                /*fa_aacpsy_calculate_pe_hp(s->h_aacpsy, sample_psy_buf, s->block_type, &s->pe, &s->tns_active);*/
                fa_aacpsy_calculate_xmin(s->h_aacpsy, s->mdct_line, s->block_type, s->xmin, qcof);
                /*printf("=====>tns_active=%d\n", s->tns_active);*/
            }
            /*if (speed_level == 2 || speed_level == 3)*/
                /*fa_calculate_scalefactor_win(s, s->xmin);*/
        } else {
            /*if (speed_level < 5) {*/
                fa_fastquant_calculate_sfb_avgenergy(s);
                fa_fastquant_calculate_xmin(s, s->xmin, qcof);
                /*fa_calculate_scalefactor_win(s, s->xmin);*/   // if use QUANTIZE_FAST , uncommented
            /*}*/
        }
    }

    fa_tnssync(f);

    for (i = 0; i < chn_num; i++) {
        s = &(f->ctx[i]);

        /*if (tns_enable && (!s->chn_info.lfe))*/
        /*if (tns_enable && (!s->chn_info.lfe) && (1 == s->tns_active))*/
        if (tns_enable && (!s->chn_info.lfe) &&
        /*if (tns_enable && (!s->chn_info.lfe) && (1 == s->tns_active) &&*/
            /*((s->block_type == ONLY_SHORT_BLOCK) || (s->block_type == LONG_START_BLOCK) || (s->block_type == LONG_STOP_BLOCK)))*/
           ((s->block_type == ONLY_SHORT_BLOCK))) // || (s->block_type == ONLY_LONG_BLOCK)) )
            fa_tns_encode_frame(s);

        /*if is short block , recorder will arrange the mdctline to sfb-grouped*/
        mdctline_reorder(s, s->xmin);

        /*reset the quantize status*/
        s->quant_ok = 0;
    }

    /*mid/side encoding*/
    if (ms_enable)
        fa_aacmsenc(f);

    /*quantize*/
    f->do_quantize(f);

    /* offset the difference of common_scalefac and scalefactors by SF_OFFSET  */
    scalefactor_recalculate(f, chn_num);

    /*format bitstream*/
    fa_write_bitstream(f);

    *outlen = fa_bitstream_getbufval(f->h_bitstream, buf_out);

    fa_bitstream_reset(f->h_bitstream);

}
示例#2
0
void fa_aacenc_encode(uintptr_t handle, unsigned char *buf_in, int inlen, unsigned char *buf_out, int *outlen)
{
    int i,j;
    int chn_num;
    short *sample_in;
    float *sample_buf;
    float sample_psy_buf[2*AAC_FRAME_LEN];
    float xmin[8][FA_SWB_NUM_MAX];
    int ms_enable;
    int tns_enable;
    int block_switch_en;
    int psy_enable;
    int speed_level;
    fa_aacenc_ctx_t *f = (fa_aacenc_ctx_t *)handle;
    aacenc_ctx_t *s;

    speed_level = f->speed_level;

    tns_enable  = f->cfg.tns_enable;
    ms_enable   = f->cfg.ms_enable;
    chn_num     = f->cfg.chn_num;
    /*assert(inlen == chn_num*AAC_FRAME_LEN*2);*/

    memset(xmin, 0, sizeof(float)*8*FA_SWB_NUM_MAX);
    /*update sample buffer, ith sample, jth chn*/
    sample_in = (short *)buf_in;
    for (i = 0; i < AAC_FRAME_LEN; i++) 
        for (j = 0; j < chn_num; j++) 
            f->sample[i+j*AAC_FRAME_LEN] = (float)(sample_in[i*chn_num+j]);

    block_switch_en = f->block_switch_en;
    psy_enable      = f->psy_enable;

    /*block switch and use filterbank to generate mdctline*/
    for (i = 0; i < chn_num; i++) {
        s = &(f->ctx[i]);

        /*get the input sample*/
        sample_buf = f->sample+i*AAC_FRAME_LEN;

        /*block switch */
        if (block_switch_en) {
            f->do_blockswitch(s);
#if 0 
            if (s->block_type == 2)
                printf("i=%d, block_type=%d, pe=%f, bits_alloc=%d\n", i+1, s->block_type, s->pe, s->bits_alloc);
#endif
        } else {
            s->block_type = ONLY_LONG_BLOCK;
        }

        /*analysis*/
        fa_aacfilterbank_analysis(s->h_aac_analysis, s->block_type, &(s->window_shape),
                                  sample_buf, s->mdct_line);

        /*cutoff the frequence according to the bitrate*/
        if (s->block_type == ONLY_SHORT_BLOCK) {
            int k;
            for (k = 0; k < 8; k++)
                zero_cutoff(s->mdct_line+k*128, 128, s->cutoff_line_short);
        } else
            zero_cutoff(s->mdct_line, 1024, s->cutoff_line_long);


        /* 
           calculate xmin and pe
           --use current sample_buf calculate pe to decide which block used in the next frame
        */
        if (psy_enable) {
#if  0 
            fa_aacpsy_calculate_pe(s->h_aacpsy, sample_buf, s->block_type, &s->pe);
#else
            fa_aacfilterbank_get_xbuf(s->h_aac_analysis, sample_psy_buf);
            fa_aacpsy_calculate_pe(s->h_aacpsy, sample_psy_buf, s->block_type, &s->pe);
#endif
            fa_aacpsy_calculate_xmin(s->h_aacpsy, s->mdct_line, s->block_type, xmin);
            fa_calculate_scalefactor_win(s, xmin);
        } else {
            if (speed_level < 4) {
                fa_fastquant_calculate_sfb_avgenergy(s);
                fa_fastquant_calculate_xmin(s, xmin);
                fa_calculate_scalefactor_win(s, xmin);
            }
        }

        if (tns_enable && (!s->chn_info.lfe))
            fa_tns_encode_frame(s);

        /*if is short block , recorder will arrange the mdctline to sfb-grouped*/
        mdctline_reorder(s, xmin);

        /*reset the quantize status*/
        s->quant_ok = 0;
    }

    /*mid/side encoding*/
    if (ms_enable)
        fa_aacmsenc(f);

    /*quantize*/
    f->do_quantize(f);

    /* offset the difference of common_scalefac and scalefactors by SF_OFFSET  */
    scalefactor_recalculate(f, chn_num);

    /*format bitstream*/
    fa_write_bitstream(f);

#if 0   //for inverse decode debug sign
    for (i = 0; i < 1024; i++) {
        if (s->mdct_line[i] >= 0)
            s->mdct_line_sign[i] = 1;
        else
            s->mdct_line_sign[i] = -1;
    }
#endif

    *outlen = fa_bitstream_getbufval(f->h_bitstream, buf_out);

    fa_bitstream_reset(f->h_bitstream);

}
示例#3
0
文件: main.c 项目: jackyxinli/falab
int main(int argc, char *argv[])
{
    int ret;
    int frame_index = 0;

	FILE  * destfile;
	FILE  * sourcefile;
	fa_wavfmt_t fmt;

	int i;
    int is_last = 0;
    int read_len = 0;
    int write_total_size= 0;

    uintptr_t h_aac_analysis, h_aac_synthesis;
    uintptr_t h_aacpsy;
    int block_type;
    float pe;

	short wavsamples_in[FRAME_SIZE_MAX];
	short wavsamples_out[FRAME_SIZE_MAX];
	float buf_in[FRAME_SIZE_MAX];
    float buf_out[FRAME_SIZE_MAX];
    float mdct_line[FRAME_SIZE_MAX];

    int block_switch_en = 1;

    ret = fa_parseopt(argc, argv);
    if(ret) return -1;

    if ((destfile = fopen(opt_outputfile, "w+b")) == NULL) {
		printf("output file can not be opened\n");
		return 0; 
	}                         

	if ((sourcefile = fopen(opt_inputfile, "rb")) == NULL) {
		printf("input file can not be opened;\n");
		return 0; 
    }

    fmt = fa_wavfmt_readheader(sourcefile);
    fseek(sourcefile,46,0);
    fseek(destfile, 0, SEEK_SET);
    fa_wavfmt_writeheader(fmt, destfile);

    h_aacpsy = fa_aacpsy_init(48000);
    h_aac_analysis = fa_aacfilterbank_init(block_switch_en);
    h_aac_synthesis = fa_aacfilterbank_init(block_switch_en);


    while(1)
    {
        if(is_last)
            break;

        memset(wavsamples_in, 0, 2*opt_framelen);
        read_len = fread(wavsamples_in, 2, opt_framelen, sourcefile);
        if(read_len < opt_framelen)
            is_last = 1;
       
        for(i = 0 ; i < read_len; i++) {
            buf_in[i] = (float)wavsamples_in[i]/32768;
            buf_out[i] = 0;
        }
        
        if(block_switch_en) {
            block_type = fa_get_aacblocktype(h_aac_analysis);
            pe = fa_aacpsy_calculate_pe(h_aacpsy, wavsamples_in, block_type);
            fa_aacblocktype_switch(h_aac_analysis, h_aacpsy, pe);
        }

        fa_aacfilterbank_analysis(h_aac_analysis, buf_in, mdct_line);

        if(block_switch_en) {
            block_type = fa_get_aacblocktype(h_aac_analysis);
            fa_set_aacblocktype(h_aac_synthesis, block_type);
        }
        fa_aacfilterbank_synthesis(h_aac_synthesis, mdct_line, buf_out);

        for(i = 0 ; i < opt_framelen; i++) {
            float temp;
            temp = buf_out[i] * 32768;

            if (temp >= 32767)
                temp = 32767;
            if (temp < -32768)
                temp = -32768;

            wavsamples_out[i] = temp;
        }

        fwrite(wavsamples_out, 2, opt_framelen, destfile);

        write_total_size += 2 * opt_framelen;

        frame_index++;
        fprintf(stderr,"\rthe frame = [%d]", frame_index);
    }

    fmt.data_size=write_total_size/fmt.block_align;
    fseek(destfile, 0, SEEK_SET);
    fa_wavfmt_writeheader(fmt,destfile);

    fclose(sourcefile);
    fclose(destfile);

    /*fa_aacfilterbank_uninit();*/
    printf("\n");

    return 0;
}