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