Home | History | Annotate | Download | only in source
      1 /*
      2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 /*
     12  * structs.h
     13  *
     14  * This header file contains all the structs used in the ISAC codec
     15  *
     16  */
     17 
     18 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_SOURCE_STRUCTS_H_
     19 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_SOURCE_STRUCTS_H_
     20 
     21 #include "webrtc/modules/audio_coding/codecs/isac/bandwidth_info.h"
     22 #include "webrtc/modules/audio_coding/codecs/isac/main/include/isac.h"
     23 #include "webrtc/modules/audio_coding/codecs/isac/main/source/settings.h"
     24 #include "webrtc/typedefs.h"
     25 
     26 typedef struct Bitstreamstruct {
     27 
     28   uint8_t   stream[STREAM_SIZE_MAX];
     29   uint32_t  W_upper;
     30   uint32_t  streamval;
     31   uint32_t  stream_index;
     32 
     33 } Bitstr;
     34 
     35 typedef struct {
     36 
     37   double    DataBufferLo[WINLEN];
     38   double    DataBufferHi[WINLEN];
     39 
     40   double    CorrBufLo[ORDERLO+1];
     41   double    CorrBufHi[ORDERHI+1];
     42 
     43   float    PreStateLoF[ORDERLO+1];
     44   float    PreStateLoG[ORDERLO+1];
     45   float    PreStateHiF[ORDERHI+1];
     46   float    PreStateHiG[ORDERHI+1];
     47   float    PostStateLoF[ORDERLO+1];
     48   float    PostStateLoG[ORDERLO+1];
     49   float    PostStateHiF[ORDERHI+1];
     50   float    PostStateHiG[ORDERHI+1];
     51 
     52   double    OldEnergy;
     53 
     54 } MaskFiltstr;
     55 
     56 
     57 typedef struct {
     58 
     59   //state vectors for each of the two analysis filters
     60   double    INSTAT1[2*(QORDER-1)];
     61   double    INSTAT2[2*(QORDER-1)];
     62   double    INSTATLA1[2*(QORDER-1)];
     63   double    INSTATLA2[2*(QORDER-1)];
     64   double    INLABUF1[QLOOKAHEAD];
     65   double    INLABUF2[QLOOKAHEAD];
     66 
     67   float    INSTAT1_float[2*(QORDER-1)];
     68   float    INSTAT2_float[2*(QORDER-1)];
     69   float    INSTATLA1_float[2*(QORDER-1)];
     70   float    INSTATLA2_float[2*(QORDER-1)];
     71   float    INLABUF1_float[QLOOKAHEAD];
     72   float    INLABUF2_float[QLOOKAHEAD];
     73 
     74   /* High pass filter */
     75   double    HPstates[HPORDER];
     76   float    HPstates_float[HPORDER];
     77 
     78 } PreFiltBankstr;
     79 
     80 
     81 typedef struct {
     82 
     83   //state vectors for each of the two analysis filters
     84   double    STATE_0_LOWER[2*POSTQORDER];
     85   double    STATE_0_UPPER[2*POSTQORDER];
     86 
     87   /* High pass filter */
     88   double    HPstates1[HPORDER];
     89   double    HPstates2[HPORDER];
     90 
     91   float    STATE_0_LOWER_float[2*POSTQORDER];
     92   float    STATE_0_UPPER_float[2*POSTQORDER];
     93 
     94   float    HPstates1_float[HPORDER];
     95   float    HPstates2_float[HPORDER];
     96 
     97 } PostFiltBankstr;
     98 
     99 typedef struct {
    100 
    101   //data buffer for pitch filter
    102   double    ubuf[PITCH_BUFFSIZE];
    103 
    104   //low pass state vector
    105   double    ystate[PITCH_DAMPORDER];
    106 
    107   //old lag and gain
    108   double    oldlagp[1];
    109   double    oldgainp[1];
    110 
    111 } PitchFiltstr;
    112 
    113 typedef struct {
    114 
    115   //data buffer
    116   double    buffer[PITCH_WLPCBUFLEN];
    117 
    118   //state vectors
    119   double    istate[PITCH_WLPCORDER];
    120   double    weostate[PITCH_WLPCORDER];
    121   double    whostate[PITCH_WLPCORDER];
    122 
    123   //LPC window   -> should be a global array because constant
    124   double    window[PITCH_WLPCWINLEN];
    125 
    126 } WeightFiltstr;
    127 
    128 typedef struct {
    129 
    130   //for inital estimator
    131   double         dec_buffer[PITCH_CORR_LEN2 + PITCH_CORR_STEP2 +
    132                             PITCH_MAX_LAG/2 - PITCH_FRAME_LEN/2+2];
    133   double        decimator_state[2*ALLPASSSECTIONS+1];
    134   double        hp_state[2];
    135 
    136   double        whitened_buf[QLOOKAHEAD];
    137 
    138   double        inbuf[QLOOKAHEAD];
    139 
    140   PitchFiltstr  PFstr_wght;
    141   PitchFiltstr  PFstr;
    142   WeightFiltstr Wghtstr;
    143 
    144 } PitchAnalysisStruct;
    145 
    146 
    147 
    148 /* Have instance of struct together with other iSAC structs */
    149 typedef struct {
    150 
    151   /* Previous frame length (in ms)                                    */
    152   int32_t    prev_frame_length;
    153 
    154   /* Previous RTP timestamp from received
    155      packet (in samples relative beginning)                           */
    156   int32_t    prev_rec_rtp_number;
    157 
    158   /* Send timestamp for previous packet (in ms using timeGetTime())   */
    159   uint32_t    prev_rec_send_ts;
    160 
    161   /* Arrival time for previous packet (in ms using timeGetTime())     */
    162   uint32_t    prev_rec_arr_ts;
    163 
    164   /* rate of previous packet, derived from RTP timestamps (in bits/s) */
    165   float   prev_rec_rtp_rate;
    166 
    167   /* Time sinse the last update of the BN estimate (in ms)            */
    168   uint32_t    last_update_ts;
    169 
    170   /* Time sinse the last reduction (in ms)                            */
    171   uint32_t    last_reduction_ts;
    172 
    173   /* How many times the estimate was update in the beginning          */
    174   int32_t    count_tot_updates_rec;
    175 
    176   /* The estimated bottle neck rate from there to here (in bits/s)    */
    177   int32_t  rec_bw;
    178   float   rec_bw_inv;
    179   float   rec_bw_avg;
    180   float   rec_bw_avg_Q;
    181 
    182   /* The estimated mean absolute jitter value,
    183      as seen on this side (in ms)                                     */
    184   float   rec_jitter;
    185   float   rec_jitter_short_term;
    186   float   rec_jitter_short_term_abs;
    187   float   rec_max_delay;
    188   float   rec_max_delay_avg_Q;
    189 
    190   /* (assumed) bitrate for headers (bps)                              */
    191   float   rec_header_rate;
    192 
    193   /* The estimated bottle neck rate from here to there (in bits/s)    */
    194   float    send_bw_avg;
    195 
    196   /* The estimated mean absolute jitter value, as seen on
    197      the other siee (in ms)                                           */
    198   float   send_max_delay_avg;
    199 
    200   // number of packets received since last update
    201   int num_pkts_rec;
    202 
    203   int num_consec_rec_pkts_over_30k;
    204 
    205   // flag for marking that a high speed network has been
    206   // detected downstream
    207   int hsn_detect_rec;
    208 
    209   int num_consec_snt_pkts_over_30k;
    210 
    211   // flag for marking that a high speed network has
    212   // been detected upstream
    213   int hsn_detect_snd;
    214 
    215   uint32_t start_wait_period;
    216 
    217   int in_wait_period;
    218 
    219   int change_to_WB;
    220 
    221   uint32_t                 senderTimestamp;
    222   uint32_t                 receiverTimestamp;
    223   //enum IsacSamplingRate incomingStreamSampFreq;
    224   uint16_t                 numConsecLatePkts;
    225   float                        consecLatency;
    226   int16_t                  inWaitLatePkts;
    227 
    228   IsacBandwidthInfo external_bw_info;
    229 } BwEstimatorstr;
    230 
    231 
    232 typedef struct {
    233 
    234   /* boolean, flags if previous packet exceeded B.N. */
    235   int    PrevExceed;
    236   /* ms */
    237   int    ExceedAgo;
    238   /* packets left to send in current burst */
    239   int    BurstCounter;
    240   /* packets */
    241   int    InitCounter;
    242   /* ms remaining in buffer when next packet will be sent */
    243   double StillBuffered;
    244 
    245 } RateModel;
    246 
    247 
    248 typedef struct {
    249 
    250   unsigned int SpaceAlloced;
    251   unsigned int MaxPermAlloced;
    252   double Tmp0[MAXFFTSIZE];
    253   double Tmp1[MAXFFTSIZE];
    254   double Tmp2[MAXFFTSIZE];
    255   double Tmp3[MAXFFTSIZE];
    256   int Perm[MAXFFTSIZE];
    257   int factor [NFACTOR];
    258 
    259 } FFTstr;
    260 
    261 
    262 /* The following strutc is used to store data from encoding, to make it
    263    fast and easy to construct a new bitstream with a different Bandwidth
    264    estimate. All values (except framelength and minBytes) is double size to
    265    handle 60 ms of data.
    266 */
    267 typedef struct {
    268 
    269   /* Used to keep track of if it is first or second part of 60 msec packet */
    270   int         startIdx;
    271 
    272   /* Frame length in samples */
    273   int16_t framelength;
    274 
    275   /* Pitch Gain */
    276   int         pitchGain_index[2];
    277 
    278   /* Pitch Lag */
    279   double      meanGain[2];
    280   int         pitchIndex[PITCH_SUBFRAMES*2];
    281 
    282   /* LPC */
    283   int         LPCindex_s[108*2]; /* KLT_ORDER_SHAPE = 108 */
    284   int         LPCindex_g[12*2];  /* KLT_ORDER_GAIN = 12 */
    285   double      LPCcoeffs_lo[(ORDERLO+1)*SUBFRAMES*2];
    286   double      LPCcoeffs_hi[(ORDERHI+1)*SUBFRAMES*2];
    287 
    288   /* Encode Spec */
    289   int16_t fre[FRAMESAMPLES];
    290   int16_t fim[FRAMESAMPLES];
    291   int16_t AvgPitchGain[2];
    292 
    293   /* Used in adaptive mode only */
    294   int         minBytes;
    295 
    296 } IsacSaveEncoderData;
    297 
    298 
    299 typedef struct {
    300 
    301   int         indexLPCShape[UB_LPC_ORDER * UB16_LPC_VEC_PER_FRAME];
    302   double      lpcGain[SUBFRAMES<<1];
    303   int         lpcGainIndex[SUBFRAMES<<1];
    304 
    305   Bitstr      bitStreamObj;
    306 
    307   int16_t realFFT[FRAMESAMPLES_HALF];
    308   int16_t imagFFT[FRAMESAMPLES_HALF];
    309 } ISACUBSaveEncDataStruct;
    310 
    311 
    312 
    313 typedef struct {
    314 
    315   Bitstr              bitstr_obj;
    316   MaskFiltstr         maskfiltstr_obj;
    317   PreFiltBankstr      prefiltbankstr_obj;
    318   PitchFiltstr        pitchfiltstr_obj;
    319   PitchAnalysisStruct pitchanalysisstr_obj;
    320   FFTstr              fftstr_obj;
    321   IsacSaveEncoderData SaveEnc_obj;
    322 
    323   int                 buffer_index;
    324   int16_t         current_framesamples;
    325 
    326   float               data_buffer_float[FRAMESAMPLES_30ms];
    327 
    328   int                 frame_nb;
    329   double              bottleneck;
    330   int16_t         new_framelength;
    331   double              s2nr;
    332 
    333   /* Maximum allowed number of bits for a 30 msec packet */
    334   int16_t         payloadLimitBytes30;
    335   /* Maximum allowed number of bits for a 30 msec packet */
    336   int16_t         payloadLimitBytes60;
    337   /* Maximum allowed number of bits for both 30 and 60 msec packet */
    338   int16_t         maxPayloadBytes;
    339   /* Maximum allowed rate in bytes per 30 msec packet */
    340   int16_t         maxRateInBytes;
    341 
    342   /*---
    343     If set to 1 iSAC will not addapt the frame-size, if used in
    344     channel-adaptive mode. The initial value will be used for all rates.
    345     ---*/
    346   int16_t         enforceFrameSize;
    347 
    348   /*-----
    349     This records the BWE index the encoder injected into the bit-stream.
    350     It will be used in RCU. The same BWE index of main payload will be in
    351     the redundant payload. We can not retrive it from BWE because it is
    352     a recursive procedure (WebRtcIsac_GetDownlinkBwJitIndexImpl) and has to be
    353     called only once per each encode.
    354     -----*/
    355   int16_t         lastBWIdx;
    356 } ISACLBEncStruct;
    357 
    358 typedef struct {
    359 
    360   Bitstr                  bitstr_obj;
    361   MaskFiltstr             maskfiltstr_obj;
    362   PreFiltBankstr          prefiltbankstr_obj;
    363   FFTstr                  fftstr_obj;
    364   ISACUBSaveEncDataStruct SaveEnc_obj;
    365 
    366   int                     buffer_index;
    367   float                   data_buffer_float[MAX_FRAMESAMPLES +
    368                                             LB_TOTAL_DELAY_SAMPLES];
    369   double                  bottleneck;
    370   /* Maximum allowed number of bits for a 30 msec packet */
    371   //int16_t        payloadLimitBytes30;
    372   /* Maximum allowed number of bits for both 30 and 60 msec packet */
    373   //int16_t        maxPayloadBytes;
    374   int16_t             maxPayloadSizeBytes;
    375 
    376   double                  lastLPCVec[UB_LPC_ORDER];
    377   int16_t             numBytesUsed;
    378   int16_t             lastJitterInfo;
    379 } ISACUBEncStruct;
    380 
    381 
    382 
    383 typedef struct {
    384 
    385   Bitstr          bitstr_obj;
    386   MaskFiltstr     maskfiltstr_obj;
    387   PostFiltBankstr postfiltbankstr_obj;
    388   PitchFiltstr    pitchfiltstr_obj;
    389   FFTstr          fftstr_obj;
    390 
    391 } ISACLBDecStruct;
    392 
    393 typedef struct {
    394 
    395   Bitstr          bitstr_obj;
    396   MaskFiltstr     maskfiltstr_obj;
    397   PostFiltBankstr postfiltbankstr_obj;
    398   FFTstr          fftstr_obj;
    399 
    400 } ISACUBDecStruct;
    401 
    402 
    403 
    404 typedef struct {
    405 
    406   ISACLBEncStruct ISACencLB_obj;
    407   ISACLBDecStruct ISACdecLB_obj;
    408 } ISACLBStruct;
    409 
    410 
    411 typedef struct {
    412 
    413   ISACUBEncStruct ISACencUB_obj;
    414   ISACUBDecStruct ISACdecUB_obj;
    415 } ISACUBStruct;
    416 
    417 /*
    418   This struct is used to take a snapshot of the entropy coder and LPC gains
    419   right before encoding LPC gains. This allows us to go back to that state
    420   if we like to limit the payload size.
    421 */
    422 typedef struct {
    423   /* 6 lower-band & 6 upper-band */
    424   double       loFiltGain[SUBFRAMES];
    425   double       hiFiltGain[SUBFRAMES];
    426   /* Upper boundary of interval W */
    427   uint32_t W_upper;
    428   uint32_t streamval;
    429   /* Index to the current position in bytestream */
    430   uint32_t stream_index;
    431   uint8_t  stream[3];
    432 } transcode_obj;
    433 
    434 typedef struct {
    435   // TODO(kwiberg): The size of these tables could be reduced by storing floats
    436   // instead of doubles, and by making use of the identity cos(x) =
    437   // sin(x+pi/2). They could also be made global constants that we fill in at
    438   // compile time.
    439   double costab1[FRAMESAMPLES_HALF];
    440   double sintab1[FRAMESAMPLES_HALF];
    441   double costab2[FRAMESAMPLES_QUARTER];
    442   double sintab2[FRAMESAMPLES_QUARTER];
    443 } TransformTables;
    444 
    445 typedef struct {
    446   // lower-band codec instance
    447   ISACLBStruct              instLB;
    448   // upper-band codec instance
    449   ISACUBStruct              instUB;
    450 
    451   // Bandwidth Estimator and model for the rate.
    452   BwEstimatorstr            bwestimator_obj;
    453   RateModel                 rate_data_obj;
    454   double                    MaxDelay;
    455 
    456   /* 0 = adaptive; 1 = instantaneous */
    457   int16_t               codingMode;
    458 
    459   // overall bottleneck of the codec
    460   int32_t               bottleneck;
    461 
    462   // QMF Filter state
    463   int32_t               analysisFBState1[FB_STATE_SIZE_WORD32];
    464   int32_t               analysisFBState2[FB_STATE_SIZE_WORD32];
    465   int32_t               synthesisFBState1[FB_STATE_SIZE_WORD32];
    466   int32_t               synthesisFBState2[FB_STATE_SIZE_WORD32];
    467 
    468   // Error Code
    469   int16_t               errorCode;
    470 
    471   // bandwidth of the encoded audio 8, 12 or 16 kHz
    472   enum ISACBandwidth        bandwidthKHz;
    473   // Sampling rate of audio, encoder and decode,  8 or 16 kHz
    474   enum IsacSamplingRate encoderSamplingRateKHz;
    475   enum IsacSamplingRate decoderSamplingRateKHz;
    476   // Flag to keep track of initializations, lower & upper-band
    477   // encoder and decoder.
    478   int16_t               initFlag;
    479 
    480   // Flag to to indicate signal bandwidth switch
    481   int16_t               resetFlag_8kHz;
    482 
    483   // Maximum allowed rate, measured in Bytes per 30 ms.
    484   int16_t               maxRateBytesPer30Ms;
    485   // Maximum allowed payload-size, measured in Bytes.
    486   int16_t               maxPayloadSizeBytes;
    487   /* The expected sampling rate of the input signal. Valid values are 16000
    488    * and 32000. This is not the operation sampling rate of the codec. */
    489   uint16_t in_sample_rate_hz;
    490 
    491   // Trig tables for WebRtcIsac_Time2Spec and WebRtcIsac_Spec2time.
    492   TransformTables transform_tables;
    493 } ISACMainStruct;
    494 
    495 #endif /* WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_SOURCE_STRUCTS_H_ */
    496