Example #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];
    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);

}
Example #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];
    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);

}