| OLD | NEW |
| 1 /* Copyright (c) 2007-2008 CSIRO | 1 /* Copyright (c) 2007-2008 CSIRO |
| 2 Copyright (c) 2007-2009 Xiph.Org Foundation | 2 Copyright (c) 2007-2009 Xiph.Org Foundation |
| 3 Written by Jean-Marc Valin */ | 3 Written by Jean-Marc Valin */ |
| 4 /* | 4 /* |
| 5 Redistribution and use in source and binary forms, with or without | 5 Redistribution and use in source and binary forms, with or without |
| 6 modification, are permitted provided that the following conditions | 6 modification, are permitted provided that the following conditions |
| 7 are met: | 7 are met: |
| 8 | 8 |
| 9 - Redistributions of source code must retain the above copyright | 9 - Redistributions of source code must retain the above copyright |
| 10 notice, this list of conditions and the following disclaimer. | 10 notice, this list of conditions and the following disclaimer. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 int j; | 77 int j; |
| 78 int curr=0; | 78 int curr=0; |
| 79 int nbEntries=0; | 79 int nbEntries=0; |
| 80 int entryN[100], entryK[100], entryI[100]; | 80 int entryN[100], entryK[100], entryI[100]; |
| 81 const opus_int16 *eBands = m->eBands; | 81 const opus_int16 *eBands = m->eBands; |
| 82 PulseCache *cache = &m->cache; | 82 PulseCache *cache = &m->cache; |
| 83 opus_int16 *cindex; | 83 opus_int16 *cindex; |
| 84 unsigned char *bits; | 84 unsigned char *bits; |
| 85 unsigned char *cap; | 85 unsigned char *cap; |
| 86 | 86 |
| 87 cindex = opus_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2)); | 87 cindex = (opus_int16 *)opus_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2))
; |
| 88 cache->index = cindex; | 88 cache->index = cindex; |
| 89 | 89 |
| 90 /* Scan for all unique band sizes */ | 90 /* Scan for all unique band sizes */ |
| 91 for (i=0;i<=LM+1;i++) | 91 for (i=0;i<=LM+1;i++) |
| 92 { | 92 { |
| 93 for (j=0;j<m->nbEBands;j++) | 93 for (j=0;j<m->nbEBands;j++) |
| 94 { | 94 { |
| 95 int k; | 95 int k; |
| 96 int N = (eBands[j+1]-eBands[j])<<i>>1; | 96 int N = (eBands[j+1]-eBands[j])<<i>>1; |
| 97 cindex[i*m->nbEBands+j] = -1; | 97 cindex[i*m->nbEBands+j] = -1; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 117 K++; | 117 K++; |
| 118 entryK[nbEntries] = K; | 118 entryK[nbEntries] = K; |
| 119 cindex[i*m->nbEBands+j] = curr; | 119 cindex[i*m->nbEBands+j] = curr; |
| 120 entryI[nbEntries] = curr; | 120 entryI[nbEntries] = curr; |
| 121 | 121 |
| 122 curr += K+1; | 122 curr += K+1; |
| 123 nbEntries++; | 123 nbEntries++; |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 } | 126 } |
| 127 bits = opus_alloc(sizeof(unsigned char)*curr); | 127 bits = (unsigned char *)opus_alloc(sizeof(unsigned char)*curr); |
| 128 cache->bits = bits; | 128 cache->bits = bits; |
| 129 cache->size = curr; | 129 cache->size = curr; |
| 130 /* Compute the cache for all unique sizes */ | 130 /* Compute the cache for all unique sizes */ |
| 131 for (i=0;i<nbEntries;i++) | 131 for (i=0;i<nbEntries;i++) |
| 132 { | 132 { |
| 133 unsigned char *ptr = bits+entryI[i]; | 133 unsigned char *ptr = bits+entryI[i]; |
| 134 opus_int16 tmp[MAX_PULSES+1]; | 134 opus_int16 tmp[MAX_PULSES+1]; |
| 135 get_required_bits(tmp, entryN[i], get_pulses(entryK[i]), BITRES); | 135 get_required_bits(tmp, entryN[i], get_pulses(entryK[i]), BITRES); |
| 136 for (j=1;j<=entryK[i];j++) | 136 for (j=1;j<=entryK[i];j++) |
| 137 ptr[j] = tmp[get_pulses(j)]-1; | 137 ptr[j] = tmp[get_pulses(j)]-1; |
| 138 ptr[0] = entryK[i]; | 138 ptr[0] = entryK[i]; |
| 139 } | 139 } |
| 140 | 140 |
| 141 /* Compute the maximum rate for each band at which we'll reliably use as | 141 /* Compute the maximum rate for each band at which we'll reliably use as |
| 142 many bits as we ask for. */ | 142 many bits as we ask for. */ |
| 143 cache->caps = cap = opus_alloc(sizeof(cache->caps[0])*(LM+1)*2*m->nbEBands); | 143 cache->caps = cap = (unsigned char *)opus_alloc(sizeof(cache->caps[0])*(LM+1)
*2*m->nbEBands); |
| 144 for (i=0;i<=LM;i++) | 144 for (i=0;i<=LM;i++) |
| 145 { | 145 { |
| 146 for (C=1;C<=2;C++) | 146 for (C=1;C<=2;C++) |
| 147 { | 147 { |
| 148 for (j=0;j<m->nbEBands;j++) | 148 for (j=0;j<m->nbEBands;j++) |
| 149 { | 149 { |
| 150 int N0; | 150 int N0; |
| 151 int max_bits; | 151 int max_bits; |
| 152 N0 = m->eBands[j+1]-m->eBands[j]; | 152 N0 = m->eBands[j+1]-m->eBands[j]; |
| 153 /* N=1 bands only have a sign bit and fine bits. */ | 153 /* N=1 bands only have a sign bit and fine bits. */ |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 { | 252 { |
| 253 opus_int32 psum; | 253 opus_int32 psum; |
| 254 int lo, hi; | 254 int lo, hi; |
| 255 int i, j; | 255 int i, j; |
| 256 int logM; | 256 int logM; |
| 257 int stereo; | 257 int stereo; |
| 258 int codedBands=-1; | 258 int codedBands=-1; |
| 259 int alloc_floor; | 259 int alloc_floor; |
| 260 opus_int32 left, percoeff; | 260 opus_int32 left, percoeff; |
| 261 int done; | 261 int done; |
| 262 int balance; | 262 opus_int32 balance; |
| 263 SAVE_STACK; | 263 SAVE_STACK; |
| 264 | 264 |
| 265 alloc_floor = C<<BITRES; | 265 alloc_floor = C<<BITRES; |
| 266 stereo = C>1; | 266 stereo = C>1; |
| 267 | 267 |
| 268 logM = LM<<BITRES; | 268 logM = LM<<BITRES; |
| 269 lo = 0; | 269 lo = 0; |
| 270 hi = 1<<ALLOC_STEPS; | 270 hi = 1<<ALLOC_STEPS; |
| 271 for (i=0;i<ALLOC_STEPS;i++) | 271 for (i=0;i<ALLOC_STEPS;i++) |
| 272 { | 272 { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 if (encode) | 346 if (encode) |
| 347 { | 347 { |
| 348 /*This if() block is the only part of the allocation function that | 348 /*This if() block is the only part of the allocation function that |
| 349 is not a mandatory part of the bitstream: any bands we choose to | 349 is not a mandatory part of the bitstream: any bands we choose to |
| 350 skip here must be explicitly signaled.*/ | 350 skip here must be explicitly signaled.*/ |
| 351 /*Choose a threshold with some hysteresis to keep bands from | 351 /*Choose a threshold with some hysteresis to keep bands from |
| 352 fluctuating in and out.*/ | 352 fluctuating in and out.*/ |
| 353 #ifdef FUZZING | 353 #ifdef FUZZING |
| 354 if ((rand()&0x1) == 0) | 354 if ((rand()&0x1) == 0) |
| 355 #else | 355 #else |
| 356 if (band_bits > ((j<prev?7:9)*band_width<<LM<<BITRES)>>4) | 356 if (codedBands<=start+2 || band_bits > ((j<prev?7:9)*band_width<<LM<
<BITRES)>>4) |
| 357 #endif | 357 #endif |
| 358 { | 358 { |
| 359 ec_enc_bit_logp(ec, 1, 1); | 359 ec_enc_bit_logp(ec, 1, 1); |
| 360 break; | 360 break; |
| 361 } | 361 } |
| 362 ec_enc_bit_logp(ec, 0, 1); | 362 ec_enc_bit_logp(ec, 0, 1); |
| 363 } else if (ec_dec_bit_logp(ec, 1)) { | 363 } else if (ec_dec_bit_logp(ec, 1)) { |
| 364 break; | 364 break; |
| 365 } | 365 } |
| 366 /*We used a bit to skip this band.*/ | 366 /*We used a bit to skip this band.*/ |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 left -= tmp; | 425 left -= tmp; |
| 426 } | 426 } |
| 427 /*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/ | 427 /*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/ |
| 428 | 428 |
| 429 balance = 0; | 429 balance = 0; |
| 430 for (j=start;j<codedBands;j++) | 430 for (j=start;j<codedBands;j++) |
| 431 { | 431 { |
| 432 int N0, N, den; | 432 int N0, N, den; |
| 433 int offset; | 433 int offset; |
| 434 int NClogN; | 434 int NClogN; |
| 435 int excess; | 435 opus_int32 excess, bit; |
| 436 | 436 |
| 437 celt_assert(bits[j] >= 0); | 437 celt_assert(bits[j] >= 0); |
| 438 N0 = m->eBands[j+1]-m->eBands[j]; | 438 N0 = m->eBands[j+1]-m->eBands[j]; |
| 439 N=N0<<LM; | 439 N=N0<<LM; |
| 440 bits[j] += balance; | 440 bit = (opus_int32)bits[j]+balance; |
| 441 | 441 |
| 442 if (N>1) | 442 if (N>1) |
| 443 { | 443 { |
| 444 excess = IMAX(bits[j]-cap[j],0); | 444 excess = MAX32(bit-cap[j],0); |
| 445 bits[j] -= excess; | 445 bits[j] = bit-excess; |
| 446 | 446 |
| 447 /* Compensate for the extra DoF in stereo */ | 447 /* Compensate for the extra DoF in stereo */ |
| 448 den=(C*N+ ((C==2 && N>2 && !*dual_stereo && j<*intensity) ? 1 : 0)); | 448 den=(C*N+ ((C==2 && N>2 && !*dual_stereo && j<*intensity) ? 1 : 0)); |
| 449 | 449 |
| 450 NClogN = den*(m->logN[j] + logM); | 450 NClogN = den*(m->logN[j] + logM); |
| 451 | 451 |
| 452 /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET | 452 /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET |
| 453 compared to their "fair share" of total/N */ | 453 compared to their "fair share" of total/N */ |
| 454 offset = (NClogN>>1)-den*FINE_OFFSET; | 454 offset = (NClogN>>1)-den*FINE_OFFSET; |
| 455 | 455 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 476 | 476 |
| 477 /* If we rounded down or capped this band, make it a candidate for the | 477 /* If we rounded down or capped this band, make it a candidate for the |
| 478 final fine energy pass */ | 478 final fine energy pass */ |
| 479 fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset; | 479 fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset; |
| 480 | 480 |
| 481 /* Remove the allocated fine bits; the rest are assigned to PVQ */ | 481 /* Remove the allocated fine bits; the rest are assigned to PVQ */ |
| 482 bits[j] -= C*ebits[j]<<BITRES; | 482 bits[j] -= C*ebits[j]<<BITRES; |
| 483 | 483 |
| 484 } else { | 484 } else { |
| 485 /* For N=1, all bits go to fine energy except for a single sign bit */ | 485 /* For N=1, all bits go to fine energy except for a single sign bit */ |
| 486 excess = IMAX(0,bits[j]-(C<<BITRES)); | 486 excess = MAX32(0,bit-(C<<BITRES)); |
| 487 bits[j] -= excess; | 487 bits[j] = bit-excess; |
| 488 ebits[j] = 0; | 488 ebits[j] = 0; |
| 489 fine_priority[j] = 1; | 489 fine_priority[j] = 1; |
| 490 } | 490 } |
| 491 | 491 |
| 492 /* Fine energy can't take advantage of the re-balancing in | 492 /* Fine energy can't take advantage of the re-balancing in |
| 493 quant_all_bands(). | 493 quant_all_bands(). |
| 494 Instead, do the re-balancing here.*/ | 494 Instead, do the re-balancing here.*/ |
| 495 if(excess > 0) | 495 if(excess > 0) |
| 496 { | 496 { |
| 497 int extra_fine; | 497 int extra_fine; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 bits1[j] = bits1j; | 629 bits1[j] = bits1j; |
| 630 bits2[j] = bits2j; | 630 bits2[j] = bits2j; |
| 631 } | 631 } |
| 632 codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thre
sh, cap, | 632 codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thre
sh, cap, |
| 633 total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_s
tereo_rsv, | 633 total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_s
tereo_rsv, |
| 634 pulses, ebits, fine_priority, C, LM, ec, encode, prev); | 634 pulses, ebits, fine_priority, C, LM, ec, encode, prev); |
| 635 RESTORE_STACK; | 635 RESTORE_STACK; |
| 636 return codedBands; | 636 return codedBands; |
| 637 } | 637 } |
| 638 | 638 |
| OLD | NEW |