uintptr_t aacenc_init(int sample_rate, int bit_rate, int chn_num, int mpeg_version, int aac_objtype, int band_width, int speed_level, int ms_enable, int lfe_enable, int tns_enable, int block_switch_enable, int psy_enable, int blockswitch_method, int quantize_method) { int i; int bits_average; int bits_res_maxsize; int real_band_width; fa_aacenc_ctx_t *f = (fa_aacenc_ctx_t *)malloc(sizeof(fa_aacenc_ctx_t)); chn_info_t chn_info_tmp[MAX_CHANNELS]; /* if (bit_rate > 256000 || bit_rate < 32000) return (uintptr_t)NULL; */ memset(f, 0, sizeof(fa_aacenc_ctx_t)); f->speed_level = speed_level; /*init rom*/ fa_aacenc_rom_init(); /*init configuration*/ f->cfg.sample_rate = sample_rate; f->cfg.bit_rate = bit_rate; f->cfg.chn_num = chn_num; f->cfg.mpeg_version = mpeg_version; f->cfg.aac_objtype = aac_objtype; f->cfg.ms_enable = ms_enable; f->cfg.lfe_enable = lfe_enable; f->cfg.tns_enable = tns_enable; f->cfg.sample_rate_index = get_samplerate_index(sample_rate); f->sample = (float *)malloc(sizeof(float)*chn_num*AAC_FRAME_LEN); memset(f->sample, 0, sizeof(float)*chn_num*AAC_FRAME_LEN); f->block_switch_en = block_switch_enable; f->psy_enable = psy_enable; f->band_width = get_bandwidth(chn_num, sample_rate, bit_rate); if (speed_level > 5) { if (f->band_width > 10000) f->band_width = 10000; } if (band_width >= 5000 && band_width <= 20000) { if (band_width < f->band_width) f->band_width = band_width; } printf("band width= %d kHz\n", f->band_width); memset(chn_info_tmp, 0, sizeof(chn_info_t)*MAX_CHANNELS); get_aac_chn_info(chn_info_tmp, chn_num, lfe_enable); bits_average = (bit_rate*1024)/(sample_rate*chn_num); bits_res_maxsize = get_aac_bitreservoir_maxsize(bits_average, sample_rate); f->h_bitstream = fa_bitstream_init((6144/8)*chn_num); switch (blockswitch_method) { case BLOCKSWITCH_PSY: f->blockswitch_method = BLOCKSWITCH_PSY; f->do_blockswitch = fa_blockswitch_psy; break; case BLOCKSWITCH_VAR: f->blockswitch_method = BLOCKSWITCH_VAR; f->do_blockswitch = fa_blockswitch_var; break; default: f->blockswitch_method = BLOCKSWITCH_VAR; f->do_blockswitch = fa_blockswitch_var; break; } switch (quantize_method) { case QUANTIZE_LOOP: f->quantize_method = QUANTIZE_LOOP; f->do_quantize = fa_quantize_loop; break; case QUANTIZE_FAST: f->quantize_method = QUANTIZE_FAST; f->do_quantize = fa_quantize_fast; break; default: f->quantize_method = QUANTIZE_LOOP; f->do_quantize = fa_quantize_loop; } /*init psy and mdct quant */ for (i = 0; i < chn_num; i++) { f->ctx[i].pe = 0.0; f->ctx[i].var_max_prev = 0.0; f->ctx[i].block_type = ONLY_LONG_BLOCK; f->ctx[i].psy_enable = psy_enable; f->ctx[i].window_shape = SINE_WINDOW; f->ctx[i].common_scalefac = 0; memset(f->ctx[i].scalefactor, 0, sizeof(int)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].scalefactor_win, 0, sizeof(int)*8*FA_SWB_NUM_MAX); f->ctx[i].num_window_groups = 1; f->ctx[i].window_group_length[0] = 1; f->ctx[i].window_group_length[1] = 0; f->ctx[i].window_group_length[2] = 0; f->ctx[i].window_group_length[3] = 0; f->ctx[i].window_group_length[4] = 0; f->ctx[i].window_group_length[5] = 0; f->ctx[i].window_group_length[6] = 0; f->ctx[i].window_group_length[7] = 0; memset(f->ctx[i].lastx, 0, sizeof(int)*8); memset(f->ctx[i].avgenergy, 0, sizeof(float)*8); f->ctx[i].used_bits= 0; f->ctx[i].bits_average = bits_average; f->ctx[i].bits_res_maxsize = bits_res_maxsize; f->ctx[i].res_buf = (unsigned char *)malloc(sizeof(unsigned char)*(bits_res_maxsize/8 + 1)); memset(f->ctx[i].res_buf, 0, sizeof(unsigned char)*(bits_res_maxsize/8 + 1)); f->ctx[i].bits_res_size = 0; f->ctx[i].last_common_scalefac = 0; f->ctx[i].h_aacpsy = fa_aacpsy_init(sample_rate); f->ctx[i].h_aac_analysis = fa_aacfilterbank_init(); f->ctx[i].h_tns = fa_tns_init(f->cfg.mpeg_version, f->cfg.aac_objtype, f->cfg.sample_rate_index); memcpy(&(f->ctx[i].chn_info), &(chn_info_tmp[i]), sizeof(chn_info_t)); f->ctx[i].chn_info.common_window = 0; if (f->ctx[i].chn_info.lfe == 1) real_band_width = 2000; else real_band_width = f->band_width; switch (sample_rate) { case 48000: f->ctx[i].cutoff_line_long = get_cutoff_line(48000, 1024, real_band_width); f->ctx[i].cutoff_line_short= get_cutoff_line(48000, 128 , real_band_width); f->ctx[i].cutoff_sfb_long = get_cutoff_sfb(FA_SWB_48k_LONG_NUM , fa_swb_48k_long_offset , f->ctx[i].cutoff_line_long); f->ctx[i].cutoff_sfb_short = get_cutoff_sfb(FA_SWB_48k_SHORT_NUM, fa_swb_48k_short_offset, f->ctx[i].cutoff_line_short); /*f->ctx[i].h_mdctq_long = fa_mdctquant_init(1024, FA_SWB_48k_LONG_NUM ,fa_swb_48k_long_offset, 1);*/ /*f->ctx[i].h_mdctq_short= fa_mdctquant_init(128 , FA_SWB_48k_SHORT_NUM,fa_swb_48k_short_offset, 8);*/ f->ctx[i].h_mdctq_long = fa_mdctquant_init(1024, f->ctx[i].cutoff_sfb_long , fa_swb_48k_long_offset, 1); f->ctx[i].h_mdctq_short= fa_mdctquant_init(128 , f->ctx[i].cutoff_sfb_short, fa_swb_48k_short_offset, 8); /*f->ctx[i].sfb_num_long = FA_SWB_48k_LONG_NUM;*/ /*f->ctx[i].sfb_num_short= FA_SWB_48k_SHORT_NUM;*/ f->ctx[i].sfb_num_long = f->ctx[i].cutoff_sfb_long; f->ctx[i].sfb_num_short= f->ctx[i].cutoff_sfb_short; break; case 44100: f->ctx[i].cutoff_line_long = get_cutoff_line(44100, 1024, real_band_width); f->ctx[i].cutoff_line_short= get_cutoff_line(44100, 128 , real_band_width); f->ctx[i].cutoff_sfb_long = get_cutoff_sfb(FA_SWB_44k_LONG_NUM , fa_swb_44k_long_offset , f->ctx[i].cutoff_line_long); f->ctx[i].cutoff_sfb_short = get_cutoff_sfb(FA_SWB_44k_SHORT_NUM, fa_swb_44k_short_offset, f->ctx[i].cutoff_line_short); /*f->ctx[i].h_mdctq_long = fa_mdctquant_init(1024, FA_SWB_44k_LONG_NUM ,fa_swb_44k_long_offset, 1);*/ /*f->ctx[i].h_mdctq_short= fa_mdctquant_init(128 , FA_SWB_44k_SHORT_NUM,fa_swb_44k_short_offset, 8);*/ f->ctx[i].h_mdctq_long = fa_mdctquant_init(1024, f->ctx[i].cutoff_sfb_long , fa_swb_44k_long_offset, 1); f->ctx[i].h_mdctq_short= fa_mdctquant_init(128 , f->ctx[i].cutoff_sfb_short, fa_swb_44k_short_offset, 8); f->ctx[i].sfb_num_long = f->ctx[i].cutoff_sfb_long; f->ctx[i].sfb_num_short= f->ctx[i].cutoff_sfb_short; break; case 32000: f->ctx[i].cutoff_line_long = get_cutoff_line(32000, 1024, real_band_width); f->ctx[i].cutoff_line_short= get_cutoff_line(32000, 128 , real_band_width); f->ctx[i].cutoff_sfb_long = get_cutoff_sfb(FA_SWB_32k_LONG_NUM , fa_swb_32k_long_offset , f->ctx[i].cutoff_line_long); f->ctx[i].cutoff_sfb_short = get_cutoff_sfb(FA_SWB_32k_SHORT_NUM, fa_swb_32k_short_offset, f->ctx[i].cutoff_line_short); /*f->ctx[i].h_mdctq_long = fa_mdctquant_init(1024, FA_SWB_32k_LONG_NUM ,fa_swb_32k_long_offset, 1);*/ /*f->ctx[i].h_mdctq_short= fa_mdctquant_init(128 , FA_SWB_32k_SHORT_NUM,fa_swb_32k_short_offset, 8);*/ f->ctx[i].h_mdctq_long = fa_mdctquant_init(1024, f->ctx[i].cutoff_sfb_long , fa_swb_32k_long_offset, 1); f->ctx[i].h_mdctq_short= fa_mdctquant_init(128 , f->ctx[i].cutoff_sfb_short, fa_swb_32k_short_offset, 8); /*f->ctx[i].sfb_num_long = FA_SWB_32k_LONG_NUM;*/ /*f->ctx[i].sfb_num_short= FA_SWB_32k_SHORT_NUM;*/ f->ctx[i].sfb_num_long = f->ctx[i].cutoff_sfb_long; f->ctx[i].sfb_num_short= f->ctx[i].cutoff_sfb_short; break; } memset(f->ctx[i].mdct_line, 0, sizeof(float)*2*AAC_FRAME_LEN); /*f->ctx[i].max_pred_sfb = get_max_pred_sfb(f->cfg.sample_rate_index);*/ f->ctx[i].quant_ok = 0; } /*f->bitres_maxsize = get_aac_bitreservoir_maxsize(f->cfg.bit_rate, f->cfg.sample_rate);*/ return (uintptr_t)f; }
uintptr_t aacenc_init(int sample_rate, int bit_rate, int chn_num, float qcof, int vbr_flag, int mpeg_version, int aac_objtype, float band_width, int speed_level, int ms_enable, int lfe_enable, int tns_enable, int block_switch_enable, int psy_enable, int psy_model, int blockswitch_method, int quantize_method, int time_resolution_first) { int i; int bits_adj; int bits_average; int bits_res_maxsize; int real_band_width; float bits_thr_cof; float adj; fa_aacenc_ctx_t *f = (fa_aacenc_ctx_t *)malloc(sizeof(fa_aacenc_ctx_t)); chn_info_t chn_info_tmp[MAX_CHANNELS]; /* if (bit_rate > 256000 || bit_rate < 32000) return (uintptr_t)NULL; */ memset(f, 0, sizeof(fa_aacenc_ctx_t)); f->speed_level = speed_level; /*init rom*/ fa_aacenc_rom_init(); /*init configuration*/ f->cfg.sample_rate = sample_rate; f->cfg.bit_rate = bit_rate; f->cfg.qcof = qcof; f->cfg.vbr_flag = vbr_flag; f->cfg.chn_num = chn_num; f->cfg.mpeg_version = mpeg_version; f->cfg.aac_objtype = aac_objtype; f->cfg.ms_enable = ms_enable; f->cfg.lfe_enable = lfe_enable; f->cfg.tns_enable = tns_enable; f->cfg.sample_rate_index = get_samplerate_index(sample_rate); f->sample = (float *)malloc(sizeof(float)*chn_num*AAC_FRAME_LEN); memset(f->sample, 0, sizeof(float)*chn_num*AAC_FRAME_LEN); f->block_switch_en = block_switch_enable; f->psy_enable = psy_enable; f->psy_model = psy_model; if (vbr_flag) { f->band_width = get_bandwidth1(chn_num, sample_rate, qcof, &(f->cfg.bit_rate)); bit_rate = f->cfg.bit_rate; /*printf("\nNOTE: final bitrate/chn = %d\n", f->cfg.bit_rate);*/ } else { f->band_width = get_bandwidth(chn_num, sample_rate, bit_rate, &(f->cfg.qcof)); /*printf("\nNOTE: final qcof = %f\n", f->cfg.qcof);*/ } bits_adj = 0; if (bit_rate >= 200000 && bit_rate < 260000) bits_adj = 9000; if (bit_rate >= 140000 && bit_rate < 200000) bits_adj = 4000; if (bit_rate >= 90000 && bit_rate < 110000) bits_adj = -2000; #if 0 bits_thr_cof = get_bit_thr_cof(chn_num, sample_rate, bit_rate); adj = get_adj_cof(chn_num, sample_rate, bit_rate); #else bits_thr_cof = get_bit_thr_cof(chn_num, sample_rate, bit_rate+bits_adj); adj = get_adj_cof(chn_num, sample_rate, bit_rate+bits_adj); #endif /*printf("bits thr cof=%f\n", bits_thr_cof);*/ if (speed_level > 5) { if (f->band_width > 10000.) f->band_width = 10000.; } /*if (band_width >= 5000 && band_width <= 20000) {*/ if (band_width >= 5000. && band_width <= BW_MAX) { if (band_width < f->band_width) f->band_width = band_width; } printf("band width= %f kHz\n", f->band_width); memset(chn_info_tmp, 0, sizeof(chn_info_t)*MAX_CHANNELS); get_aac_chn_info(chn_info_tmp, chn_num, lfe_enable); /*bits_average = (bit_rate*1024)/(sample_rate*chn_num);*/ bits_average = ((bit_rate+bits_adj)*1024)/(sample_rate*chn_num); bits_res_maxsize = get_aac_bitreservoir_maxsize(bits_average, sample_rate); f->h_bitstream = fa_bitstream_init((6144/8)*chn_num); switch (blockswitch_method) { case BLOCKSWITCH_PSY: f->blockswitch_method = BLOCKSWITCH_PSY; f->do_blockswitch = fa_blockswitch_psy; break; case BLOCKSWITCH_VAR: f->blockswitch_method = BLOCKSWITCH_VAR; f->do_blockswitch = fa_blockswitch_var; break; default: f->blockswitch_method = BLOCKSWITCH_VAR; f->do_blockswitch = fa_blockswitch_var; break; } switch (quantize_method) { case QUANTIZE_LOOP: f->quantize_method = QUANTIZE_LOOP; f->do_quantize = fa_quantize_loop; break; case QUANTIZE_FAST: f->quantize_method = QUANTIZE_FAST; f->do_quantize = fa_quantize_fast; break; case QUANTIZE_BEST: f->quantize_method = QUANTIZE_BEST; f->do_quantize = fa_quantize_best; /*f->do_quantize = fa_quantize_fast;*/ /*f->do_quantize = fa_quantize_loop;*/ break; default: f->quantize_method = QUANTIZE_BEST; f->do_quantize = fa_quantize_best; } /*init psy and mdct quant */ for (i = 0; i < chn_num; i++) { f->ctx[i].h_blockctrl = fa_blockswitch_init(2048); f->ctx[i].time_resolution_first = time_resolution_first; f->ctx[i].pe = 0.0; f->ctx[i].var_max_prev = 0.0; f->ctx[i].block_type = ONLY_LONG_BLOCK; f->ctx[i].psy_enable = psy_enable; f->ctx[i].window_shape = SINE_WINDOW; f->ctx[i].common_scalefac = 0; memset(f->ctx[i].scalefactor, 0, sizeof(int)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].scalefactor_win, 0, sizeof(int)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].maxscale_win,0, sizeof(int)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].xmin, 0, sizeof(float)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].miu,0, sizeof(float)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].miuhalf,0, sizeof(float)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].miu2, 0, sizeof(float)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].pdft, 0, sizeof(float)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].Px, 0, sizeof(float)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].Tm, 0, sizeof(float)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].Ti, 0, sizeof(float)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].Ti1,0, sizeof(float)*8*FA_SWB_NUM_MAX); memset(f->ctx[i].G ,0, sizeof(float)*8*FA_SWB_NUM_MAX); f->ctx[i].Pt_long = 0; f->ctx[i].Pt_short = 0; f->ctx[i].up = 0; f->ctx[i].step_down_db = 0.0; f->ctx[i].bit_thr_cof = bits_thr_cof; f->ctx[i].adj = adj; f->ctx[i].num_window_groups = 1; f->ctx[i].window_group_length[0] = 1; f->ctx[i].window_group_length[1] = 0; f->ctx[i].window_group_length[2] = 0; f->ctx[i].window_group_length[3] = 0; f->ctx[i].window_group_length[4] = 0; f->ctx[i].window_group_length[5] = 0; f->ctx[i].window_group_length[6] = 0; f->ctx[i].window_group_length[7] = 0; memset(f->ctx[i].lastx, 0, sizeof(int)*8); memset(f->ctx[i].avgenergy, 0, sizeof(float)*8); f->ctx[i].used_bits= 0; f->ctx[i].bits_average = bits_average; f->ctx[i].bits_res_maxsize = bits_res_maxsize; f->ctx[i].res_buf = (unsigned char *)malloc(sizeof(unsigned char)*(bits_res_maxsize/8 + 1)); memset(f->ctx[i].res_buf, 0, sizeof(unsigned char)*(bits_res_maxsize/8 + 1)); f->ctx[i].bits_res_size = 0; f->ctx[i].last_common_scalefac = 0; f->ctx[i].h_aacpsy = fa_aacpsy_init(sample_rate); f->ctx[i].h_aac_analysis = fa_aacfilterbank_init(); f->ctx[i].h_tns = fa_tns_init(f->cfg.mpeg_version, f->cfg.aac_objtype, f->cfg.sample_rate_index); f->ctx[i].tns_active = 0; memcpy(&(f->ctx[i].chn_info), &(chn_info_tmp[i]), sizeof(chn_info_t)); f->ctx[i].chn_info.common_window = 0; if (f->ctx[i].chn_info.lfe == 1) real_band_width = 2000; else real_band_width = (int)f->band_width; switch (sample_rate) { case 48000: f->ctx[i].cutoff_line_long = get_cutoff_line(48000, 1024, real_band_width); f->ctx[i].cutoff_line_short= get_cutoff_line(48000, 128 , real_band_width); f->ctx[i].cutoff_sfb_long = get_cutoff_sfb(FA_SWB_48k_LONG_NUM , fa_swb_48k_long_offset , f->ctx[i].cutoff_line_long); f->ctx[i].cutoff_sfb_short = get_cutoff_sfb(FA_SWB_48k_SHORT_NUM, fa_swb_48k_short_offset, f->ctx[i].cutoff_line_short); f->ctx[i].h_mdctq_long = fa_mdctquant_init(1024, f->ctx[i].cutoff_sfb_long , fa_swb_48k_long_offset, 1); f->ctx[i].h_mdctq_short= fa_mdctquant_init(128 , f->ctx[i].cutoff_sfb_short, fa_swb_48k_short_offset, 8); f->ctx[i].sfb_num_long = f->ctx[i].cutoff_sfb_long; f->ctx[i].sfb_num_short= f->ctx[i].cutoff_sfb_short; f->ctx[i].Pt_long = fa_protect_db_48k_long; f->ctx[i].Pt_short = fa_protect_db_48k_short; break; case 44100: f->ctx[i].cutoff_line_long = get_cutoff_line(44100, 1024, real_band_width); f->ctx[i].cutoff_line_short= get_cutoff_line(44100, 128 , real_band_width); f->ctx[i].cutoff_sfb_long = get_cutoff_sfb(FA_SWB_44k_LONG_NUM , fa_swb_44k_long_offset , f->ctx[i].cutoff_line_long); f->ctx[i].cutoff_sfb_short = get_cutoff_sfb(FA_SWB_44k_SHORT_NUM, fa_swb_44k_short_offset, f->ctx[i].cutoff_line_short); f->ctx[i].h_mdctq_long = fa_mdctquant_init(1024, f->ctx[i].cutoff_sfb_long , fa_swb_44k_long_offset, 1); f->ctx[i].h_mdctq_short= fa_mdctquant_init(128 , f->ctx[i].cutoff_sfb_short, fa_swb_44k_short_offset, 8); f->ctx[i].sfb_num_long = f->ctx[i].cutoff_sfb_long; f->ctx[i].sfb_num_short= f->ctx[i].cutoff_sfb_short; f->ctx[i].Pt_long = fa_protect_db_44k_long; f->ctx[i].Pt_short = fa_protect_db_44k_short; break; case 32000: f->ctx[i].cutoff_line_long = get_cutoff_line(32000, 1024, real_band_width); f->ctx[i].cutoff_line_short= get_cutoff_line(32000, 128 , real_band_width); f->ctx[i].cutoff_sfb_long = get_cutoff_sfb(FA_SWB_32k_LONG_NUM , fa_swb_32k_long_offset , f->ctx[i].cutoff_line_long); f->ctx[i].cutoff_sfb_short = get_cutoff_sfb(FA_SWB_32k_SHORT_NUM, fa_swb_32k_short_offset, f->ctx[i].cutoff_line_short); f->ctx[i].h_mdctq_long = fa_mdctquant_init(1024, f->ctx[i].cutoff_sfb_long , fa_swb_32k_long_offset, 1); f->ctx[i].h_mdctq_short= fa_mdctquant_init(128 , f->ctx[i].cutoff_sfb_short, fa_swb_32k_short_offset, 8); f->ctx[i].sfb_num_long = f->ctx[i].cutoff_sfb_long; f->ctx[i].sfb_num_short= f->ctx[i].cutoff_sfb_short; f->ctx[i].Pt_long = fa_protect_db_32k_long; f->ctx[i].Pt_short = fa_protect_db_32k_short; break; } memset(f->ctx[i].mdct_line, 0, sizeof(float)*2*AAC_FRAME_LEN); /*f->ctx[i].max_pred_sfb = get_max_pred_sfb(f->cfg.sample_rate_index);*/ f->ctx[i].quant_ok = 0; if (f->band_width < BW_MAX) { if (time_resolution_first) fa_quantqdf_para_init(&(f->ctx[i].qp), 0.9); else /*fa_quantqdf_para_init(&(f->ctx[i].qp), 0.95);*/ fa_quantqdf_para_init(&(f->ctx[i].qp), 0.85); } else { if (time_resolution_first) fa_quantqdf_para_init(&(f->ctx[i].qp), 0.9); else /*fa_quantqdf_para_init(&(f->ctx[i].qp), 1.0);*/ fa_quantqdf_para_init(&(f->ctx[i].qp), 0.9); } } /*f->bitres_maxsize = get_aac_bitreservoir_maxsize(f->cfg.bit_rate, f->cfg.sample_rate);*/ return (uintptr_t)f; }