Home | History | Annotate | Download | only in source
      1 /*
      2  *  Copyright (c) 2011 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_FIX_SOURCE_STRUCTS_H_
     19 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_FIX_SOURCE_STRUCTS_H_
     20 
     21 
     22 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
     23 #include "webrtc/modules/audio_coding/codecs/isac/fix/source/settings.h"
     24 #include "webrtc/typedefs.h"
     25 
     26 /* Bitstream struct for decoder */
     27 typedef struct Bitstreamstruct_dec {
     28 
     29   uint16_t  stream[STREAM_MAXW16_60MS];  /* Array bytestream to decode */
     30   uint32_t  W_upper;          /* Upper boundary of interval W */
     31   uint32_t  streamval;
     32   uint16_t  stream_index;     /* Index to the current position in bytestream */
     33   int16_t   full;             /* 0 - first byte in memory filled, second empty*/
     34   /* 1 - both bytes are empty (we just filled the previous memory */
     35 
     36   int stream_size;  /* The size of stream. */
     37 } Bitstr_dec;
     38 
     39 /* Bitstream struct for encoder */
     40 typedef struct Bitstreamstruct_enc {
     41 
     42   uint16_t  stream[STREAM_MAXW16_60MS];   /* Vector for adding encoded bytestream */
     43   uint32_t  W_upper;          /* Upper boundary of interval W */
     44   uint32_t  streamval;
     45   uint16_t  stream_index;     /* Index to the current position in bytestream */
     46   int16_t   full;             /* 0 - first byte in memory filled, second empty*/
     47   /* 1 - both bytes are empty (we just filled the previous memory */
     48 
     49 } Bitstr_enc;
     50 
     51 
     52 typedef struct {
     53 
     54   int16_t DataBufferLoQ0[WINLEN];
     55   int16_t DataBufferHiQ0[WINLEN];
     56 
     57   int32_t CorrBufLoQQ[ORDERLO+1];
     58   int32_t CorrBufHiQQ[ORDERHI+1];
     59 
     60   int16_t CorrBufLoQdom[ORDERLO+1];
     61   int16_t CorrBufHiQdom[ORDERHI+1];
     62 
     63   int32_t PreStateLoGQ15[ORDERLO+1];
     64   int32_t PreStateHiGQ15[ORDERHI+1];
     65 
     66   uint32_t OldEnergy;
     67 
     68 } MaskFiltstr_enc;
     69 
     70 
     71 
     72 typedef struct {
     73 
     74   int16_t PostStateLoGQ0[ORDERLO+1];
     75   int16_t PostStateHiGQ0[ORDERHI+1];
     76 
     77   uint32_t OldEnergy;
     78 
     79 } MaskFiltstr_dec;
     80 
     81 
     82 
     83 
     84 
     85 
     86 
     87 
     88 typedef struct {
     89 
     90   //state vectors for each of the two analysis filters
     91 
     92   int32_t INSTAT1_fix[2*(QORDER-1)];
     93   int32_t INSTAT2_fix[2*(QORDER-1)];
     94   int16_t INLABUF1_fix[QLOOKAHEAD];
     95   int16_t INLABUF2_fix[QLOOKAHEAD];
     96 
     97   /* High pass filter */
     98   int32_t HPstates_fix[HPORDER];
     99 
    100 } PreFiltBankstr;
    101 
    102 
    103 typedef struct {
    104 
    105   //state vectors for each of the two analysis filters
    106   int32_t STATE_0_LOWER_fix[2*POSTQORDER];
    107   int32_t STATE_0_UPPER_fix[2*POSTQORDER];
    108 
    109   /* High pass filter */
    110 
    111   int32_t HPstates1_fix[HPORDER];
    112   int32_t HPstates2_fix[HPORDER];
    113 
    114 } PostFiltBankstr;
    115 
    116 typedef struct {
    117 
    118 
    119   /* data buffer for pitch filter */
    120   int16_t ubufQQ[PITCH_BUFFSIZE];
    121 
    122   /* low pass state vector */
    123   int16_t ystateQQ[PITCH_DAMPORDER];
    124 
    125   /* old lag and gain */
    126   int16_t oldlagQ7;
    127   int16_t oldgainQ12;
    128 
    129 } PitchFiltstr;
    130 
    131 
    132 
    133 typedef struct {
    134 
    135   //for inital estimator
    136   int16_t   dec_buffer16[PITCH_CORR_LEN2+PITCH_CORR_STEP2+PITCH_MAX_LAG/2-PITCH_FRAME_LEN/2+2];
    137   int32_t   decimator_state32[2*ALLPASSSECTIONS+1];
    138   int16_t   inbuf[QLOOKAHEAD];
    139 
    140   PitchFiltstr  PFstr_wght;
    141   PitchFiltstr  PFstr;
    142 
    143 
    144 } PitchAnalysisStruct;
    145 
    146 
    147 typedef struct {
    148   /* Parameters used in PLC to avoid re-computation       */
    149 
    150   /* --- residual signals --- */
    151   int16_t prevPitchInvIn[FRAMESAMPLES/2];
    152   int16_t prevPitchInvOut[PITCH_MAX_LAG + 10];            // [FRAMESAMPLES/2]; save 90
    153   int32_t prevHP[PITCH_MAX_LAG + 10];                     // [FRAMESAMPLES/2]; save 90
    154 
    155 
    156   int16_t decayCoeffPriodic; /* how much to supress a sample */
    157   int16_t decayCoeffNoise;
    158   int16_t used;       /* if PLC is used */
    159 
    160 
    161   int16_t *lastPitchLP;                                  // [FRAMESAMPLES/2]; saved 240;
    162 
    163 
    164   /* --- LPC side info --- */
    165   int16_t lofilt_coefQ15[ ORDERLO ];
    166   int16_t hifilt_coefQ15[ ORDERHI ];
    167   int32_t gain_lo_hiQ17[2];
    168 
    169   /* --- LTP side info --- */
    170   int16_t AvgPitchGain_Q12;
    171   int16_t lastPitchGain_Q12;
    172   int16_t lastPitchLag_Q7;
    173 
    174   /* --- Add-overlap in recovery packet --- */
    175   int16_t overlapLP[ RECOVERY_OVERLAP ];                 // [FRAMESAMPLES/2]; saved 160
    176 
    177   int16_t pitchCycles;
    178   int16_t A;
    179   int16_t B;
    180   int16_t pitchIndex;
    181   int16_t stretchLag;
    182   int16_t *prevPitchLP;                                  // [ FRAMESAMPLES/2 ]; saved 240
    183   int16_t seed;
    184 
    185   int16_t std;
    186 } PLCstr;
    187 
    188 
    189 
    190 /* Have instance of struct together with other iSAC structs */
    191 typedef struct {
    192 
    193   int16_t   prevFrameSizeMs;      /* Previous frame size (in ms) */
    194   uint16_t  prevRtpNumber;      /* Previous RTP timestamp from received packet */
    195   /* (in samples relative beginning)  */
    196   uint32_t  prevSendTime;   /* Send time for previous packet, from RTP header */
    197   uint32_t  prevArrivalTime;      /* Arrival time for previous packet (in ms using timeGetTime()) */
    198   uint16_t  prevRtpRate;          /* rate of previous packet, derived from RTP timestamps (in bits/s) */
    199   uint32_t  lastUpdate;           /* Time since the last update of the Bottle Neck estimate (in samples) */
    200   uint32_t  lastReduction;        /* Time sinse the last reduction (in samples) */
    201   int32_t   countUpdates;         /* How many times the estimate was update in the beginning */
    202 
    203   /* The estimated bottle neck rate from there to here (in bits/s)                */
    204   uint32_t  recBw;
    205   uint32_t  recBwInv;
    206   uint32_t  recBwAvg;
    207   uint32_t  recBwAvgQ;
    208 
    209   uint32_t  minBwInv;
    210   uint32_t  maxBwInv;
    211 
    212   /* The estimated mean absolute jitter value, as seen on this side (in ms)       */
    213   int32_t   recJitter;
    214   int32_t   recJitterShortTerm;
    215   int32_t   recJitterShortTermAbs;
    216   int32_t   recMaxDelay;
    217   int32_t   recMaxDelayAvgQ;
    218 
    219 
    220   int16_t   recHeaderRate;         /* (assumed) bitrate for headers (bps) */
    221 
    222   uint32_t  sendBwAvg;           /* The estimated bottle neck rate from here to there (in bits/s) */
    223   int32_t   sendMaxDelayAvg;    /* The estimated mean absolute jitter value, as seen on the other siee (in ms)  */
    224 
    225 
    226   int16_t   countRecPkts;          /* number of packets received since last update */
    227   int16_t   highSpeedRec;        /* flag for marking that a high speed network has been detected downstream */
    228 
    229   /* number of consecutive pkts sent during which the bwe estimate has
    230      remained at a value greater than the downstream threshold for determining highspeed network */
    231   int16_t   countHighSpeedRec;
    232 
    233   /* flag indicating bwe should not adjust down immediately for very late pckts */
    234   int16_t   inWaitPeriod;
    235 
    236   /* variable holding the time of the start of a window of time when
    237      bwe should not adjust down immediately for very late pckts */
    238   uint32_t  startWaitPeriod;
    239 
    240   /* number of consecutive pkts sent during which the bwe estimate has
    241      remained at a value greater than the upstream threshold for determining highspeed network */
    242   int16_t   countHighSpeedSent;
    243 
    244   /* flag indicated the desired number of packets over threshold rate have been sent and
    245      bwe will assume the connection is over broadband network */
    246   int16_t   highSpeedSend;
    247 
    248 
    249 
    250 
    251 } BwEstimatorstr;
    252 
    253 
    254 typedef struct {
    255 
    256   /* boolean, flags if previous packet exceeded B.N. */
    257   int16_t    PrevExceed;
    258   /* ms */
    259   int16_t    ExceedAgo;
    260   /* packets left to send in current burst */
    261   int16_t    BurstCounter;
    262   /* packets */
    263   int16_t    InitCounter;
    264   /* ms remaining in buffer when next packet will be sent */
    265   int16_t    StillBuffered;
    266 
    267 } RateModel;
    268 
    269 /* The following strutc is used to store data from encoding, to make it
    270    fast and easy to construct a new bitstream with a different Bandwidth
    271    estimate. All values (except framelength and minBytes) is double size to
    272    handle 60 ms of data.
    273 */
    274 typedef struct {
    275 
    276   /* Used to keep track of if it is first or second part of 60 msec packet */
    277   int     startIdx;
    278 
    279   /* Frame length in samples */
    280   int16_t         framelength;
    281 
    282   /* Pitch Gain */
    283   int16_t   pitchGain_index[2];
    284 
    285   /* Pitch Lag */
    286   int32_t   meanGain[2];
    287   int16_t   pitchIndex[PITCH_SUBFRAMES*2];
    288 
    289   /* LPC */
    290   int32_t         LPCcoeffs_g[12*2]; /* KLT_ORDER_GAIN = 12 */
    291   int16_t   LPCindex_s[108*2]; /* KLT_ORDER_SHAPE = 108 */
    292   int16_t   LPCindex_g[12*2];  /* KLT_ORDER_GAIN = 12 */
    293 
    294   /* Encode Spec */
    295   int16_t   fre[FRAMESAMPLES];
    296   int16_t   fim[FRAMESAMPLES];
    297   int16_t   AvgPitchGain[2];
    298 
    299   /* Used in adaptive mode only */
    300   int     minBytes;
    301 
    302 } ISAC_SaveEncData_t;
    303 
    304 typedef struct {
    305 
    306   Bitstr_enc          bitstr_obj;
    307   MaskFiltstr_enc     maskfiltstr_obj;
    308   PreFiltBankstr      prefiltbankstr_obj;
    309   PitchFiltstr        pitchfiltstr_obj;
    310   PitchAnalysisStruct pitchanalysisstr_obj;
    311   RateModel           rate_data_obj;
    312 
    313   int16_t         buffer_index;
    314   int16_t         current_framesamples;
    315 
    316   int16_t      data_buffer_fix[FRAMESAMPLES]; // the size was MAX_FRAMESAMPLES
    317 
    318   int16_t         frame_nb;
    319   int16_t         BottleNeck;
    320   int16_t         MaxDelay;
    321   int16_t         new_framelength;
    322   int16_t         s2nr;
    323   uint16_t        MaxBits;
    324 
    325   int16_t         bitstr_seed;
    326 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
    327   PostFiltBankstr     interpolatorstr_obj;
    328 #endif
    329 
    330   ISAC_SaveEncData_t *SaveEnc_ptr;
    331   int16_t         payloadLimitBytes30; /* Maximum allowed number of bits for a 30 msec packet */
    332   int16_t         payloadLimitBytes60; /* Maximum allowed number of bits for a 30 msec packet */
    333   int16_t         maxPayloadBytes;     /* Maximum allowed number of bits for both 30 and 60 msec packet */
    334   int16_t         maxRateInBytes;      /* Maximum allowed rate in bytes per 30 msec packet */
    335   int16_t         enforceFrameSize;    /* If set iSAC will never change packet size */
    336 
    337 } ISACFIX_EncInst_t;
    338 
    339 
    340 typedef struct {
    341 
    342   Bitstr_dec          bitstr_obj;
    343   MaskFiltstr_dec     maskfiltstr_obj;
    344   PostFiltBankstr     postfiltbankstr_obj;
    345   PitchFiltstr        pitchfiltstr_obj;
    346   PLCstr              plcstr_obj;               /* TS; for packet loss concealment */
    347 
    348 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
    349   PreFiltBankstr      decimatorstr_obj;
    350 #endif
    351 
    352 } ISACFIX_DecInst_t;
    353 
    354 
    355 
    356 typedef struct {
    357 
    358   ISACFIX_EncInst_t ISACenc_obj;
    359   ISACFIX_DecInst_t ISACdec_obj;
    360   BwEstimatorstr     bwestimator_obj;
    361   int16_t         CodingMode;       /* 0 = adaptive; 1 = instantaneous */
    362   int16_t   errorcode;
    363   int16_t   initflag;  /* 0 = nothing initiated; 1 = encoder or decoder */
    364   /* not initiated; 2 = all initiated */
    365 } ISACFIX_SubStruct;
    366 
    367 
    368 typedef struct {
    369   int32_t   lpcGains[12];     /* 6 lower-band & 6 upper-band we may need to double it for 60*/
    370   /* */
    371   uint32_t  W_upper;          /* Upper boundary of interval W */
    372   uint32_t  streamval;
    373   uint16_t  stream_index;     /* Index to the current position in bytestream */
    374   int16_t   full;             /* 0 - first byte in memory filled, second empty*/
    375   /* 1 - both bytes are empty (we just filled the previous memory */
    376   uint16_t  beforeLastWord;
    377   uint16_t  lastWord;
    378 } transcode_obj;
    379 
    380 
    381 //Bitstr_enc myBitStr;
    382 
    383 #endif  /* WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_FIX_SOURCE_STRUCTS_H_ */
    384