nec_abs_exc_generation.c
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:8k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /*
  2. This software module was originally developed by
  3. Toshiyuki Nomura (NEC Corporation)
  4. and edited by
  5. Naoya Tanaka (Matsushita Communication Ind. Co., Ltd.)
  6. in the course of development of the
  7. MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7, 14496-1,2 and 3.
  8. This software module is an implementation of a part of one or more
  9. MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4 Audio
  10. standard. ISO/IEC  gives users of the MPEG-2 NBC/MPEG-4 Audio standards
  11. free license to this software module or modifications thereof for use in
  12. hardware or software products claiming conformance to the MPEG-2 NBC/
  13. MPEG-4 Audio  standards. Those intending to use this software module in
  14. hardware or software products are advised that this use may infringe
  15. existing patents. The original developer of this software module and
  16. his/her company, the subsequent editors and their companies, and ISO/IEC
  17. have no liability for use of this software module or modifications
  18. thereof in an implementation. Copyright is not released for non
  19. MPEG-2 NBC/MPEG-4 Audio conforming products. The original developer
  20. retains full right to use the code for his/her  own purpose, assign or
  21. donate the code to a third party and to inhibit third party from using
  22. the code for non MPEG-2 NBC/MPEG-4 Audio conforming products.
  23. This copyright notice must be included in all copies or derivative works.
  24. Copyright (c)1996.
  25. */
  26. /*
  27.  * MPEG-4 Audio Verification Model (LPC-ABS Core)
  28.  *
  29.  * Excitation Generation Subroutines
  30.  *
  31.  * Ver1.0 96.12.16 T.Nomura(NEC)
  32.  * Ver2.0 97.03.17 T.Nomura(NEC)
  33.  */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <math.h>
  37. #include "buffersHandle.h"       /* handler, defines, enums */
  38. #include "bitstream.h"
  39. #include "nec_abs_proto.h"
  40. #include "nec_abs_const.h"
  41. #include "nec_exc_proto.h"
  42. void nec_abs_excitation_generation(
  43. float LpcCoef[], /* input */
  44. unsigned long  shape_indices[], /* input */
  45. unsigned long  gain_indices[], /* input */
  46. unsigned long  rms_index, /* input */
  47. unsigned long  signal_mode, /* input */
  48. float decoded_excitation[], /* output */
  49. float *adapt_gain, /* output */
  50. long  *acb_delay, /* output */
  51. long  lpc_order, /* configuration input */
  52. long  sbfrm_size,  /* configuration input */
  53. long  n_subframes, /* configuration input */
  54. long  frame_bit_allocation[], /* configuration input */
  55. long  num_shape_cbks, /* configuration input */
  56. long  num_gain_cbks, /* configuration input */
  57. long  d_enhstages, /* configuration input */
  58. float bws_mp_exc[],
  59. long postfilter,
  60.         long SampleRateMode
  61. )
  62. {
  63.    static float mem_past_exc[NEC_PITCH_MAX_FRQ16 + NEC_PITCH_IFTAP16+1];
  64.    static float qxnorm[NEC_MAX_NSF];
  65.    static long  flag_mem = 0;
  66.    static long c_subframe, vu_flag;
  67.    static long pitch_max, pitch_iftap;
  68.    long i;
  69.    float *CoreExcitation, *PreStageExcitation;
  70.    float *acbexc, *mpexc;
  71.    float g_ac, g_ec, g_pc;
  72.    long lag_idx, mp_pos_idx, mp_sgn_idx, ga_idx;
  73.    long integer_lag;
  74.    long rmsbit, lagbit, posbit, sgnbit, gainbit;
  75.    long c_enh, idx_ctr, *num_pulse, *pre_indices;
  76.    if (flag_mem == 0){
  77.     if(fs8kHz==SampleRateMode) {
  78.       pitch_max = NEC_PITCH_MAX;
  79.       pitch_iftap = NEC_PITCH_IFTAP;
  80.     }else {
  81.       pitch_max = NEC_PITCH_MAX_FRQ16;
  82.       pitch_iftap = NEC_PITCH_IFTAP16;
  83.     }
  84.       for ( i = 0; i < pitch_max+pitch_iftap+1; i++ ) {
  85.  mem_past_exc[i] = 0.0;
  86.       }
  87.       c_subframe = 0;
  88.       flag_mem = 1;
  89.    }
  90.    c_subframe = c_subframe % n_subframes;
  91.    rmsbit =frame_bit_allocation[1];
  92.    lagbit =frame_bit_allocation[2+c_subframe*(num_shape_cbks+num_gain_cbks)+0];
  93.    posbit =frame_bit_allocation[2+c_subframe*(num_shape_cbks+num_gain_cbks)+1];
  94.    sgnbit =frame_bit_allocation[2+c_subframe*(num_shape_cbks+num_gain_cbks)+2];
  95.    gainbit=frame_bit_allocation[2+c_subframe*(num_shape_cbks+num_gain_cbks)+3];
  96.    /* Frame Operation */
  97.    if(c_subframe==0) {
  98.       vu_flag = (long)signal_mode;
  99.       if ( vu_flag == 0 ) {
  100.  nec_dec_rms(qxnorm, n_subframes,
  101.      (float)NEC_RMS_MAX_U, (float)NEC_MU_LAW_U,
  102.      rmsbit, (long)rms_index);
  103.       } else {
  104.  nec_dec_rms(qxnorm, n_subframes,
  105.      (float)NEC_RMS_MAX_V, (float)NEC_MU_LAW_V,
  106.      rmsbit, (long)rms_index);
  107.       }
  108.    }
  109.    qxnorm[c_subframe] = qxnorm[c_subframe] * (float)sqrt((float)sbfrm_size);
  110.    /*------ Memory Allocation ----------*/
  111.    if((acbexc = (float *)calloc (sbfrm_size, sizeof(float)))==NULL) {
  112.       printf("n Memory allocation error in nec_abs_exc_generation n");
  113.       exit(1);
  114.    }
  115.    if((mpexc = (float *)calloc (sbfrm_size, sizeof(float)))==NULL) {
  116.       printf("n Memory allocation error in nec_abs_exc_generation n");
  117.       exit(1);
  118.    }
  119.    /* decode INDICES */
  120.    lag_idx = (long)shape_indices[c_subframe*num_shape_cbks+0];
  121.    mp_pos_idx = (long)shape_indices[c_subframe*num_shape_cbks+1];
  122.    mp_sgn_idx = (long)shape_indices[c_subframe*num_shape_cbks+2];
  123.    ga_idx = (long)gain_indices[c_subframe*num_gain_cbks+0];
  124.    /* Adaptive Code Book Decode */
  125.    nec_dec_acb(acbexc,lag_idx,sbfrm_size,lagbit,mem_past_exc,
  126.        &integer_lag, SampleRateMode);
  127.    /* Multi-Pulse Excitation Decode */
  128.    nec_dec_mp(vu_flag,&g_ac, &g_ec, qxnorm[c_subframe],
  129.       LpcCoef,
  130.       integer_lag, mp_pos_idx, mp_sgn_idx,
  131.       mpexc, acbexc, lpc_order, sbfrm_size,
  132.       sgnbit, gainbit, ga_idx);
  133.    /* Enhanced Multi-Pulse Excitation Decoding */
  134.    if((CoreExcitation = (float *)calloc (sbfrm_size, sizeof(float)))==NULL) {
  135.       printf("n Memory allocation error in nec_abs_exc_analysis n");
  136.       exit(1);
  137.    }
  138.    if((PreStageExcitation=(float *)calloc (sbfrm_size, sizeof(float)))==NULL) {
  139.       printf("n Memory allocation error in nec_abs_exc_analysis n");
  140.       exit(1);
  141.    }
  142.    if((num_pulse = (long *)calloc (d_enhstages+1, sizeof(float)))==NULL) {
  143.       printf("n Memory allocation error in nec_mk_target n");
  144.       exit(1);
  145.    }
  146.    if((pre_indices = (long *)calloc (d_enhstages, sizeof(float)))==NULL) {
  147.       printf("n Memory allocation error in nec_mk_target n");
  148.       exit(1);
  149.    }
  150.    for(i = 0; i < sbfrm_size; i++){
  151.       CoreExcitation[i] = g_ac * acbexc[i] + g_ec * mpexc[i];
  152.       bws_mp_exc[i] = g_ec * mpexc[i];
  153.    }
  154.    if ( postfilter ) {
  155.       nec_pitch_enhancement(CoreExcitation,PreStageExcitation, mem_past_exc,
  156.     vu_flag, lag_idx, sbfrm_size, SampleRateMode );
  157.    } else {
  158.       for(i = 0; i < sbfrm_size; i++){
  159.  PreStageExcitation[i] = CoreExcitation[i];
  160.       }
  161.    }
  162.    for(i = 0; i < pitch_max + pitch_iftap+1 - sbfrm_size; i++){
  163.       mem_past_exc[i] = mem_past_exc[i + sbfrm_size];
  164.    }
  165.    for(i = 0; i < sbfrm_size; i++){
  166.       mem_past_exc[pitch_max + pitch_iftap+1 - sbfrm_size + i]
  167.  = CoreExcitation[i];
  168.    }
  169.    num_pulse[0] = sgnbit;
  170.    for ( c_enh = 0; c_enh < d_enhstages; c_enh++ ) {
  171.       idx_ctr = (c_enh+1)*n_subframes + c_subframe;
  172.       num_pulse[c_enh+1] = frame_bit_allocation[2+idx_ctr*(num_shape_cbks+num_gain_cbks)+2];
  173.       gainbit = frame_bit_allocation[2+idx_ctr*(num_shape_cbks+num_gain_cbks)+3];
  174.       pre_indices[c_enh] = mp_pos_idx;
  175.       mp_pos_idx = (long)shape_indices[idx_ctr*num_shape_cbks+1];
  176.       mp_sgn_idx = (long)shape_indices[idx_ctr*num_shape_cbks+2];
  177.       ga_idx = (long)gain_indices[idx_ctr*num_gain_cbks+0];
  178.       nec_enh_mp_dec(vu_flag,&g_pc, &g_ec, qxnorm[c_subframe],
  179.      LpcCoef,
  180.      integer_lag, mp_pos_idx, mp_sgn_idx,
  181.      mpexc, lpc_order, sbfrm_size,
  182.      num_pulse, pre_indices, c_enh+1, gainbit, ga_idx);
  183.       for(i = 0; i < sbfrm_size; i++){
  184.  PreStageExcitation[i] = g_pc*PreStageExcitation[i] + g_ec*mpexc[i];
  185.  bws_mp_exc[i] += g_ec * mpexc[i];
  186.       }
  187.    }
  188.    for(i = 0; i < sbfrm_size; i++){
  189.       decoded_excitation[i] = PreStageExcitation[i];
  190.    }
  191.    *adapt_gain = g_ac;
  192.    *acb_delay = 0;
  193.    c_subframe++;
  194.    FREE( CoreExcitation );
  195.    FREE( PreStageExcitation );
  196.    FREE( num_pulse );
  197.    FREE( pre_indices );
  198.    FREE( acbexc );
  199.    FREE( mpexc );
  200. }