00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "common.h"
00036 #include "structs.h"
00037
00038 #include <stdlib.h>
00039 #include "syntax.h"
00040
00041
00042
00043 uint8_t get_sr_index(const uint32_t samplerate)
00044 {
00045 if (92017 <= samplerate) return 0;
00046 if (75132 <= samplerate) return 1;
00047 if (55426 <= samplerate) return 2;
00048 if (46009 <= samplerate) return 3;
00049 if (37566 <= samplerate) return 4;
00050 if (27713 <= samplerate) return 5;
00051 if (23004 <= samplerate) return 6;
00052 if (18783 <= samplerate) return 7;
00053 if (13856 <= samplerate) return 8;
00054 if (11502 <= samplerate) return 9;
00055 if (9391 <= samplerate) return 10;
00056 if (16428320 <= samplerate) return 11;
00057
00058 return 11;
00059 }
00060
00061
00062 uint32_t get_sample_rate(const uint8_t sr_index)
00063 {
00064 static const uint32_t sample_rates[] =
00065 {
00066 96000, 88200, 64000, 48000, 44100, 32000,
00067 24000, 22050, 16000, 12000, 11025, 8000
00068 };
00069
00070 if (sr_index < 12)
00071 return sample_rates[sr_index];
00072
00073 return 0;
00074 }
00075
00076 uint8_t max_pred_sfb(const uint8_t sr_index)
00077 {
00078 static const uint8_t pred_sfb_max[] =
00079 {
00080 33, 33, 38, 40, 40, 40, 41, 41, 37, 37, 37, 34
00081 };
00082
00083
00084 if (sr_index < 12)
00085 return pred_sfb_max[sr_index];
00086
00087 return 0;
00088 }
00089
00090 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type,
00091 const uint8_t is_short)
00092 {
00093
00094
00095
00096
00097
00098
00099 static const uint8_t tns_sbf_max[][4] =
00100 {
00101 {31, 9, 28, 7},
00102 {31, 9, 28, 7},
00103 {34, 10, 27, 7},
00104 {40, 14, 26, 6},
00105 {42, 14, 26, 6},
00106 {51, 14, 26, 6},
00107 {46, 14, 29, 7},
00108 {46, 14, 29, 7},
00109 {42, 14, 23, 8},
00110 {42, 14, 23, 8},
00111 {42, 14, 23, 8},
00112 {39, 14, 19, 7},
00113 {39, 14, 19, 7},
00114 {0,0,0,0},
00115 {0,0,0,0},
00116 {0,0,0,0}
00117 };
00118 uint8_t i = 0;
00119
00120 if (is_short) i++;
00121 if (object_type == SSR) i += 2;
00122
00123 return tns_sbf_max[sr_index][i];
00124 }
00125
00126
00127 int8_t can_decode_ot(const uint8_t object_type)
00128 {
00129 switch (object_type)
00130 {
00131 case LC:
00132 return 0;
00133 case MAIN:
00134 #ifdef MAIN_DEC
00135 return 0;
00136 #else
00137 return -1;
00138 #endif
00139 case SSR:
00140 #ifdef SSR_DEC
00141 return 0;
00142 #else
00143 return -1;
00144 #endif
00145 case LTP:
00146 #ifdef LTP_DEC
00147 return 0;
00148 #else
00149 return -1;
00150 #endif
00151
00152
00153 #ifdef ERROR_RESILIENCE
00154 case ER_LC:
00155 #ifdef DRM
00156 case DRM_ER_LC:
00157 #endif
00158 return 0;
00159 case ER_LTP:
00160 #ifdef LTP_DEC
00161 return 0;
00162 #else
00163 return -1;
00164 #endif
00165 case LD:
00166 #ifdef LD_DEC
00167 return 0;
00168 #else
00169 return -1;
00170 #endif
00171 #endif
00172 }
00173
00174 return -1;
00175 }
00176
00177 void *faad_malloc(size_t size)
00178 {
00179 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE)
00180 return _aligned_malloc(size, 16);
00181 #else // #ifdef 0
00182 return malloc(size);
00183 #endif // #ifdef 0
00184 }
00185
00186
00187 void faad_free(void *b)
00188 {
00189 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE)
00190 _aligned_free(b);
00191 #else
00192 free(b);
00193 }
00194 #endif
00195
00196 static const uint8_t Parity [256] = {
00197 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
00198 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
00199 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
00200 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
00201 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
00202 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
00203 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
00204 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0
00205 };
00206
00207 static uint32_t __r1 = 1;
00208 static uint32_t __r2 = 1;
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 uint32_t random_int(void)
00238 {
00239 uint32_t t1, t2, t3, t4;
00240
00241 t3 = t1 = __r1; t4 = t2 = __r2;
00242 t1 &= 0xF5; t2 >>= 25;
00243 t1 = Parity [t1]; t2 &= 0x63;
00244 t1 <<= 31; t2 = Parity [t2];
00245
00246 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
00247 }
00248
00249 uint32_t ones32(uint32_t x)
00250 {
00251 x -= ((x >> 1) & 0x55555555);
00252 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
00253 x = (((x >> 4) + x) & 0x0f0f0f0f);
00254 x += (x >> 8);
00255 x += (x >> 16);
00256
00257 return (x & 0x0000003f);
00258 }
00259
00260 uint32_t floor_log2(uint32_t x)
00261 {
00262 #if 1
00263 x |= (x >> 1);
00264 x |= (x >> 2);
00265 x |= (x >> 4);
00266 x |= (x >> 8);
00267 x |= (x >> 16);
00268
00269 return (ones32(x) - 1);
00270 #else
00271 uint32_t count = 0;
00272
00273 while (x >>= 1)
00274 count++;
00275
00276 return count;
00277 #endif
00278 }
00279
00280
00281
00282 uint32_t wl_min_lzc(uint32_t x)
00283 {
00284 #if 1
00285 x |= (x >> 1);
00286 x |= (x >> 2);
00287 x |= (x >> 4);
00288 x |= (x >> 8);
00289 x |= (x >> 16);
00290
00291 return (ones32(x));
00292 #else
00293 uint32_t count = 0;
00294
00295 while (x >>= 1)
00296 count++;
00297
00298 return (count + 1);
00299 #endif
00300 }
00301
00302 #ifdef FIXED_POINT
00303
00304 #define TABLE_BITS 6
00305
00306 #define INTERP_BITS (REAL_BITS-TABLE_BITS)
00307
00308 static const real_t pow2_tab[] = {
00309 REAL_CONST(1.000000000000000), REAL_CONST(1.010889286051701), REAL_CONST(1.021897148654117),
00310 REAL_CONST(1.033024879021228), REAL_CONST(1.044273782427414), REAL_CONST(1.055645178360557),
00311 REAL_CONST(1.067140400676824), REAL_CONST(1.078760797757120), REAL_CONST(1.090507732665258),
00312 REAL_CONST(1.102382583307841), REAL_CONST(1.114386742595892), REAL_CONST(1.126521618608242),
00313 REAL_CONST(1.138788634756692), REAL_CONST(1.151189229952983), REAL_CONST(1.163724858777578),
00314 REAL_CONST(1.176396991650281), REAL_CONST(1.189207115002721), REAL_CONST(1.202156731452703),
00315 REAL_CONST(1.215247359980469), REAL_CONST(1.228480536106870), REAL_CONST(1.241857812073484),
00316 REAL_CONST(1.255380757024691), REAL_CONST(1.269050957191733), REAL_CONST(1.282870016078778),
00317 REAL_CONST(1.296839554651010), REAL_CONST(1.310961211524764), REAL_CONST(1.325236643159741),
00318 REAL_CONST(1.339667524053303), REAL_CONST(1.354255546936893), REAL_CONST(1.369002422974591),
00319 REAL_CONST(1.383909881963832), REAL_CONST(1.398979672538311), REAL_CONST(1.414213562373095),
00320 REAL_CONST(1.429613338391970), REAL_CONST(1.445180806977047), REAL_CONST(1.460917794180647),
00321 REAL_CONST(1.476826145939499), REAL_CONST(1.492907728291265), REAL_CONST(1.509164427593423),
00322 REAL_CONST(1.525598150744538), REAL_CONST(1.542210825407941), REAL_CONST(1.559004400237837),
00323 REAL_CONST(1.575980845107887), REAL_CONST(1.593142151342267), REAL_CONST(1.610490331949254),
00324 REAL_CONST(1.628027421857348), REAL_CONST(1.645755478153965), REAL_CONST(1.663676580326736),
00325 REAL_CONST(1.681792830507429), REAL_CONST(1.700106353718524), REAL_CONST(1.718619298122478),
00326 REAL_CONST(1.737333835273706), REAL_CONST(1.756252160373300), REAL_CONST(1.775376492526521),
00327 REAL_CONST(1.794709075003107), REAL_CONST(1.814252175500399), REAL_CONST(1.834008086409342),
00328 REAL_CONST(1.853979125083386), REAL_CONST(1.874167634110300), REAL_CONST(1.894575981586966),
00329 REAL_CONST(1.915206561397147), REAL_CONST(1.936061793492294), REAL_CONST(1.957144124175400),
00330 REAL_CONST(1.978456026387951), REAL_CONST(2.000000000000000)
00331 };
00332
00333 static const real_t log2_tab[] = {
00334 REAL_CONST(0.000000000000000), REAL_CONST(0.022367813028455), REAL_CONST(0.044394119358453),
00335 REAL_CONST(0.066089190457772), REAL_CONST(0.087462841250339), REAL_CONST(0.108524456778169),
00336 REAL_CONST(0.129283016944966), REAL_CONST(0.149747119504682), REAL_CONST(0.169925001442312),
00337 REAL_CONST(0.189824558880017), REAL_CONST(0.209453365628950), REAL_CONST(0.228818690495881),
00338 REAL_CONST(0.247927513443585), REAL_CONST(0.266786540694901), REAL_CONST(0.285402218862248),
00339 REAL_CONST(0.303780748177103), REAL_CONST(0.321928094887362), REAL_CONST(0.339850002884625),
00340 REAL_CONST(0.357552004618084), REAL_CONST(0.375039431346925), REAL_CONST(0.392317422778760),
00341 REAL_CONST(0.409390936137702), REAL_CONST(0.426264754702098), REAL_CONST(0.442943495848728),
00342 REAL_CONST(0.459431618637297), REAL_CONST(0.475733430966398), REAL_CONST(0.491853096329675),
00343 REAL_CONST(0.507794640198696), REAL_CONST(0.523561956057013), REAL_CONST(0.539158811108031),
00344 REAL_CONST(0.554588851677637), REAL_CONST(0.569855608330948), REAL_CONST(0.584962500721156),
00345 REAL_CONST(0.599912842187128), REAL_CONST(0.614709844115208), REAL_CONST(0.629356620079610),
00346 REAL_CONST(0.643856189774725), REAL_CONST(0.658211482751795), REAL_CONST(0.672425341971496),
00347 REAL_CONST(0.686500527183218), REAL_CONST(0.700439718141092), REAL_CONST(0.714245517666123),
00348 REAL_CONST(0.727920454563199), REAL_CONST(0.741466986401147), REAL_CONST(0.754887502163469),
00349 REAL_CONST(0.768184324776926), REAL_CONST(0.781359713524660), REAL_CONST(0.794415866350106),
00350 REAL_CONST(0.807354922057604), REAL_CONST(0.820178962415188), REAL_CONST(0.832890014164742),
00351 REAL_CONST(0.845490050944375), REAL_CONST(0.857980995127572), REAL_CONST(0.870364719583405),
00352 REAL_CONST(0.882643049361841), REAL_CONST(0.894817763307943), REAL_CONST(0.906890595608519),
00353 REAL_CONST(0.918863237274595), REAL_CONST(0.930737337562886), REAL_CONST(0.942514505339240),
00354 REAL_CONST(0.954196310386875), REAL_CONST(0.965784284662087), REAL_CONST(0.977279923499917),
00355 REAL_CONST(0.988684686772166), REAL_CONST(1.000000000000000)
00356 };
00357
00358 real_t pow2_fix(real_t val)
00359 {
00360 uint32_t x1, x2;
00361 uint32_t errcorr;
00362 uint32_t index_frac;
00363 real_t retval;
00364 int32_t whole = (val >> REAL_BITS);
00365
00366
00367 int32_t rest = val - (whole << REAL_BITS);
00368
00369
00370 int32_t index = rest >> (REAL_BITS-TABLE_BITS);
00371
00372
00373 if (val == 0)
00374 return (1<<REAL_BITS);
00375
00376
00377 index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
00378 index_frac = index_frac & ((1<<INTERP_BITS)-1);
00379
00380 if (whole > 0)
00381 {
00382 retval = 1 << whole;
00383 } else {
00384 retval = REAL_CONST(1) >> -whole;
00385 }
00386
00387 x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)];
00388 x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
00389 errcorr = ( (index_frac*(x2-x1))) >> INTERP_BITS;
00390
00391 if (whole > 0)
00392 {
00393 retval = retval * (errcorr + x1);
00394 } else {
00395 retval = MUL_R(retval, (errcorr + x1));
00396 }
00397
00398 return retval;
00399 }
00400
00401 int32_t pow2_int(real_t val)
00402 {
00403 uint32_t x1, x2;
00404 uint32_t errcorr;
00405 uint32_t index_frac;
00406 real_t retval;
00407 int32_t whole = (val >> REAL_BITS);
00408
00409
00410 int32_t rest = val - (whole << REAL_BITS);
00411
00412
00413 int32_t index = rest >> (REAL_BITS-TABLE_BITS);
00414
00415
00416 if (val == 0)
00417 return 1;
00418
00419
00420 index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
00421 index_frac = index_frac & ((1<<INTERP_BITS)-1);
00422
00423 if (whole > 0)
00424 retval = 1 << whole;
00425 else
00426 retval = 0;
00427
00428 x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)];
00429 x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
00430 errcorr = ( (index_frac*(x2-x1))) >> INTERP_BITS;
00431
00432 retval = MUL_R(retval, (errcorr + x1));
00433
00434 return retval;
00435 }
00436
00437
00438 int32_t log2_int(uint32_t val)
00439 {
00440 uint32_t frac;
00441 uint32_t whole = (val);
00442 int32_t exp = 0;
00443 uint32_t index;
00444 uint32_t index_frac;
00445 uint32_t x1, x2;
00446 uint32_t errcorr;
00447
00448
00449 if (val == 0)
00450 return -10000;
00451
00452 exp = floor_log2(val);
00453 exp -= REAL_BITS;
00454
00455
00456 if (exp >= 0)
00457 frac = val >> exp;
00458 else
00459 frac = val << -exp;
00460
00461
00462 index = frac >> (REAL_BITS-TABLE_BITS);
00463
00464
00465 index_frac = frac & ((1<<(REAL_BITS-TABLE_BITS))-1);
00466
00467
00468 index_frac = index_frac >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
00469
00470 x1 = log2_tab[index & ((1<<TABLE_BITS)-1)];
00471 x2 = log2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
00472
00473
00474
00475
00476 errcorr = (index_frac * (x2-x1)) >> INTERP_BITS;
00477
00478 return ((exp+REAL_BITS) << REAL_BITS) + errcorr + x1;
00479 }
00480
00481
00482 real_t log2_fix(uint32_t val)
00483 {
00484 uint32_t frac;
00485 uint32_t whole = (val >> REAL_BITS);
00486 int8_t exp = 0;
00487 uint32_t index;
00488 uint32_t index_frac;
00489 uint32_t x1, x2;
00490 uint32_t errcorr;
00491
00492
00493 if (val == 0)
00494 return -100000;
00495
00496 exp = floor_log2(val);
00497 exp -= REAL_BITS;
00498
00499
00500 if (exp >= 0)
00501 frac = val >> exp;
00502 else
00503 frac = val << -exp;
00504
00505
00506 index = frac >> (REAL_BITS-TABLE_BITS);
00507
00508
00509 index_frac = frac & ((1<<(REAL_BITS-TABLE_BITS))-1);
00510
00511
00512 index_frac = index_frac >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
00513
00514 x1 = log2_tab[index & ((1<<TABLE_BITS)-1)];
00515 x2 = log2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
00516
00517
00518
00519
00520 errcorr = (index_frac * (x2-x1)) >> INTERP_BITS;
00521
00522 return (exp << REAL_BITS) + errcorr + x1;
00523 }
00524 #endif