cannam@154: /* Copyright (c) 2007-2008 CSIRO cannam@154: Copyright (c) 2007-2009 Xiph.Org Foundation cannam@154: Written by Jean-Marc Valin */ cannam@154: /* cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: cannam@154: - Redistributions of source code must retain the above copyright cannam@154: notice, this list of conditions and the following disclaimer. cannam@154: cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@154: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT cannam@154: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR cannam@154: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER cannam@154: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@154: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@154: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR cannam@154: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF cannam@154: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING cannam@154: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS cannam@154: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@154: */ cannam@154: cannam@154: #ifdef HAVE_CONFIG_H cannam@154: #include "config.h" cannam@154: #endif cannam@154: cannam@154: #include cannam@154: #include "modes.h" cannam@154: #include "cwrs.h" cannam@154: #include "arch.h" cannam@154: #include "os_support.h" cannam@154: cannam@154: #include "entcode.h" cannam@154: #include "rate.h" cannam@154: cannam@154: static const unsigned char LOG2_FRAC_TABLE[24]={ cannam@154: 0, cannam@154: 8,13, cannam@154: 16,19,21,23, cannam@154: 24,26,27,28,29,30,31,32, cannam@154: 32,33,34,34,35,36,36,37,37 cannam@154: }; cannam@154: cannam@154: #ifdef CUSTOM_MODES cannam@154: cannam@154: /*Determines if V(N,K) fits in a 32-bit unsigned integer. cannam@154: N and K are themselves limited to 15 bits.*/ cannam@154: static int fits_in32(int _n, int _k) cannam@154: { cannam@154: static const opus_int16 maxN[15] = { cannam@154: 32767, 32767, 32767, 1476, 283, 109, 60, 40, cannam@154: 29, 24, 20, 18, 16, 14, 13}; cannam@154: static const opus_int16 maxK[15] = { cannam@154: 32767, 32767, 32767, 32767, 1172, 238, 95, 53, cannam@154: 36, 27, 22, 18, 16, 15, 13}; cannam@154: if (_n>=14) cannam@154: { cannam@154: if (_k>=14) cannam@154: return 0; cannam@154: else cannam@154: return _n <= maxN[_k]; cannam@154: } else { cannam@154: return _k <= maxK[_n]; cannam@154: } cannam@154: } cannam@154: cannam@154: void compute_pulse_cache(CELTMode *m, int LM) cannam@154: { cannam@154: int C; cannam@154: int i; cannam@154: int j; cannam@154: int curr=0; cannam@154: int nbEntries=0; cannam@154: int entryN[100], entryK[100], entryI[100]; cannam@154: const opus_int16 *eBands = m->eBands; cannam@154: PulseCache *cache = &m->cache; cannam@154: opus_int16 *cindex; cannam@154: unsigned char *bits; cannam@154: unsigned char *cap; cannam@154: cannam@154: cindex = (opus_int16 *)opus_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2)); cannam@154: cache->index = cindex; cannam@154: cannam@154: /* Scan for all unique band sizes */ cannam@154: for (i=0;i<=LM+1;i++) cannam@154: { cannam@154: for (j=0;jnbEBands;j++) cannam@154: { cannam@154: int k; cannam@154: int N = (eBands[j+1]-eBands[j])<>1; cannam@154: cindex[i*m->nbEBands+j] = -1; cannam@154: /* Find other bands that have the same size */ cannam@154: for (k=0;k<=i;k++) cannam@154: { cannam@154: int n; cannam@154: for (n=0;nnbEBands && (k!=i || n>1) cannam@154: { cannam@154: cindex[i*m->nbEBands+j] = cindex[k*m->nbEBands+n]; cannam@154: break; cannam@154: } cannam@154: } cannam@154: } cannam@154: if (cache->index[i*m->nbEBands+j] == -1 && N!=0) cannam@154: { cannam@154: int K; cannam@154: entryN[nbEntries] = N; cannam@154: K = 0; cannam@154: while (fits_in32(N,get_pulses(K+1)) && KnbEBands+j] = curr; cannam@154: entryI[nbEntries] = curr; cannam@154: cannam@154: curr += K+1; cannam@154: nbEntries++; cannam@154: } cannam@154: } cannam@154: } cannam@154: bits = (unsigned char *)opus_alloc(sizeof(unsigned char)*curr); cannam@154: cache->bits = bits; cannam@154: cache->size = curr; cannam@154: /* Compute the cache for all unique sizes */ cannam@154: for (i=0;icaps = cap = (unsigned char *)opus_alloc(sizeof(cache->caps[0])*(LM+1)*2*m->nbEBands); cannam@154: for (i=0;i<=LM;i++) cannam@154: { cannam@154: for (C=1;C<=2;C++) cannam@154: { cannam@154: for (j=0;jnbEBands;j++) cannam@154: { cannam@154: int N0; cannam@154: int max_bits; cannam@154: N0 = m->eBands[j+1]-m->eBands[j]; cannam@154: /* N=1 bands only have a sign bit and fine bits. */ cannam@154: if (N0<1 are even, including custom modes.*/ cannam@154: if (N0 > 2) cannam@154: { cannam@154: N0>>=1; cannam@154: LM0--; cannam@154: } cannam@154: /* N0=1 bands can't be split down to N<2. */ cannam@154: else if (N0 <= 1) cannam@154: { cannam@154: LM0=IMIN(i,1); cannam@154: N0<<=LM0; cannam@154: } cannam@154: /* Compute the cost for the lowest-level PVQ of a fully split cannam@154: band. */ cannam@154: pcache = bits + cindex[(LM0+1)*m->nbEBands+j]; cannam@154: max_bits = pcache[pcache[0]]+1; cannam@154: /* Add in the cost of coding regular splits. */ cannam@154: N = N0; cannam@154: for(k=0;klogN[j]+((LM0+k)<>1)-QTHETA_OFFSET; cannam@154: /* The number of qtheta bits we'll allocate if the remainder cannam@154: is to be max_bits. cannam@154: The average measured cost for theta is 0.89701 times qb, cannam@154: approximated here as 459/512. */ cannam@154: num=459*(opus_int32)((2*N-1)*offset+max_bits); cannam@154: den=((opus_int32)(2*N-1)<<9)-459; cannam@154: qb = IMIN((num+(den>>1))/den, 57); cannam@154: celt_assert(qb >= 0); cannam@154: max_bits += qb; cannam@154: N <<= 1; cannam@154: } cannam@154: /* Add in the cost of a stereo split, if necessary. */ cannam@154: if (C==2) cannam@154: { cannam@154: max_bits <<= 1; cannam@154: offset = ((m->logN[j]+(i<>1)-(N==2?QTHETA_OFFSET_TWOPHASE:QTHETA_OFFSET); cannam@154: ndof = 2*N-1-(N==2); cannam@154: /* The average measured cost for theta with the step PDF is cannam@154: 0.95164 times qb, approximated here as 487/512. */ cannam@154: num = (N==2?512:487)*(opus_int32)(max_bits+ndof*offset); cannam@154: den = ((opus_int32)ndof<<9)-(N==2?512:487); cannam@154: qb = IMIN((num+(den>>1))/den, (N==2?64:61)); cannam@154: celt_assert(qb >= 0); cannam@154: max_bits += qb; cannam@154: } cannam@154: /* Add the fine bits we'll use. */ cannam@154: /* Compensate for the extra DoF in stereo */ cannam@154: ndof = C*N + ((C==2 && N>2) ? 1 : 0); cannam@154: /* Offset the number of fine bits by log2(N)/2 + FINE_OFFSET cannam@154: compared to their "fair share" of total/N */ cannam@154: offset = ((m->logN[j] + (i<>1)-FINE_OFFSET; cannam@154: /* N=2 is the only point that doesn't match the curve */ cannam@154: if (N==2) cannam@154: offset += 1<>2; cannam@154: /* The number of fine bits we'll allocate if the remainder is cannam@154: to be max_bits. */ cannam@154: num = max_bits+ndof*offset; cannam@154: den = (ndof-1)<>1))/den, MAX_FINE_BITS); cannam@154: celt_assert(qb >= 0); cannam@154: max_bits += C*qb<eBands[j+1]-m->eBands[j])<= 0); cannam@154: celt_assert(max_bits < 256); cannam@154: *cap++ = (unsigned char)max_bits; cannam@154: } cannam@154: } cannam@154: } cannam@154: } cannam@154: cannam@154: #endif /* CUSTOM_MODES */ cannam@154: cannam@154: #define ALLOC_STEPS 6 cannam@154: cannam@154: static OPUS_INLINE int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start, cannam@154: const int *bits1, const int *bits2, const int *thresh, const int *cap, opus_int32 total, opus_int32 *_balance, cannam@154: int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits, cannam@154: int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev, int signalBandwidth) cannam@154: { cannam@154: opus_int32 psum; cannam@154: int lo, hi; cannam@154: int i, j; cannam@154: int logM; cannam@154: int stereo; cannam@154: int codedBands=-1; cannam@154: int alloc_floor; cannam@154: opus_int32 left, percoeff; cannam@154: int done; cannam@154: opus_int32 balance; cannam@154: SAVE_STACK; cannam@154: cannam@154: alloc_floor = C<1; cannam@154: cannam@154: logM = LM<>1; cannam@154: psum = 0; cannam@154: done = 0; cannam@154: for (j=end;j-->start;) cannam@154: { cannam@154: int tmp = bits1[j] + (mid*(opus_int32)bits2[j]>>ALLOC_STEPS); cannam@154: if (tmp >= thresh[j] || done) cannam@154: { cannam@154: done = 1; cannam@154: /* Don't allocate more than we can actually use */ cannam@154: psum += IMIN(tmp, cap[j]); cannam@154: } else { cannam@154: if (tmp >= alloc_floor) cannam@154: psum += alloc_floor; cannam@154: } cannam@154: } cannam@154: if (psum > total) cannam@154: hi = mid; cannam@154: else cannam@154: lo = mid; cannam@154: } cannam@154: psum = 0; cannam@154: /*printf ("interp bisection gave %d\n", lo);*/ cannam@154: done = 0; cannam@154: for (j=end;j-->start;) cannam@154: { cannam@154: int tmp = bits1[j] + ((opus_int32)lo*bits2[j]>>ALLOC_STEPS); cannam@154: if (tmp < thresh[j] && !done) cannam@154: { cannam@154: if (tmp >= alloc_floor) cannam@154: tmp = alloc_floor; cannam@154: else cannam@154: tmp = 0; cannam@154: } else cannam@154: done = 1; cannam@154: /* Don't allocate more than we can actually use */ cannam@154: tmp = IMIN(tmp, cap[j]); cannam@154: bits[j] = tmp; cannam@154: psum += tmp; cannam@154: } cannam@154: cannam@154: /* Decide which bands to skip, working backwards from the end. */ cannam@154: for (codedBands=end;;codedBands--) cannam@154: { cannam@154: int band_width; cannam@154: int band_bits; cannam@154: int rem; cannam@154: j = codedBands-1; cannam@154: /* Never skip the first band, nor a band that has been boosted by cannam@154: dynalloc. cannam@154: In the first case, we'd be coding a bit to signal we're going to waste cannam@154: all the other bits. cannam@154: In the second case, we'd be coding a bit to redistribute all the bits cannam@154: we just signaled should be cocentrated in this band. */ cannam@154: if (j<=skip_start) cannam@154: { cannam@154: /* Give the bit we reserved to end skipping back. */ cannam@154: total += skip_rsv; cannam@154: break; cannam@154: } cannam@154: /*Figure out how many left-over bits we would be adding to this band. cannam@154: This can include bits we've stolen back from higher, skipped bands.*/ cannam@154: left = total-psum; cannam@154: percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]); cannam@154: left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; cannam@154: rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0); cannam@154: band_width = m->eBands[codedBands]-m->eBands[j]; cannam@154: band_bits = (int)(bits[j] + percoeff*band_width + rem); cannam@154: /*Only code a skip decision if we're above the threshold for this band. cannam@154: Otherwise it is force-skipped. cannam@154: This ensures that we have enough bits to code the skip flag.*/ cannam@154: if (band_bits >= IMAX(thresh[j], alloc_floor+(1< 17) cannam@154: depth_threshold = j (depth_threshold*band_width<>4 && j<=signalBandwidth)) cannam@154: #endif cannam@154: { cannam@154: ec_enc_bit_logp(ec, 1, 1); cannam@154: break; cannam@154: } cannam@154: ec_enc_bit_logp(ec, 0, 1); cannam@154: } else if (ec_dec_bit_logp(ec, 1)) { cannam@154: break; cannam@154: } cannam@154: /*We used a bit to skip this band.*/ cannam@154: psum += 1< 0) cannam@154: intensity_rsv = LOG2_FRAC_TABLE[j-start]; cannam@154: psum += intensity_rsv; cannam@154: if (band_bits >= alloc_floor) cannam@154: { cannam@154: /*If we have enough for a fine energy bit per channel, use it.*/ cannam@154: psum += alloc_floor; cannam@154: bits[j] = alloc_floor; cannam@154: } else { cannam@154: /*Otherwise this band gets nothing at all.*/ cannam@154: bits[j] = 0; cannam@154: } cannam@154: } cannam@154: cannam@154: celt_assert(codedBands > start); cannam@154: /* Code the intensity and dual stereo parameters. */ cannam@154: if (intensity_rsv > 0) cannam@154: { cannam@154: if (encode) cannam@154: { cannam@154: *intensity = IMIN(*intensity, codedBands); cannam@154: ec_enc_uint(ec, *intensity-start, codedBands+1-start); cannam@154: } cannam@154: else cannam@154: *intensity = start+ec_dec_uint(ec, codedBands+1-start); cannam@154: } cannam@154: else cannam@154: *intensity = 0; cannam@154: if (*intensity <= start) cannam@154: { cannam@154: total += dual_stereo_rsv; cannam@154: dual_stereo_rsv = 0; cannam@154: } cannam@154: if (dual_stereo_rsv > 0) cannam@154: { cannam@154: if (encode) cannam@154: ec_enc_bit_logp(ec, *dual_stereo, 1); cannam@154: else cannam@154: *dual_stereo = ec_dec_bit_logp(ec, 1); cannam@154: } cannam@154: else cannam@154: *dual_stereo = 0; cannam@154: cannam@154: /* Allocate the remaining bits */ cannam@154: left = total-psum; cannam@154: percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]); cannam@154: left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; cannam@154: for (j=start;jeBands[j+1]-m->eBands[j])); cannam@154: for (j=start;jeBands[j+1]-m->eBands[j]); cannam@154: bits[j] += tmp; cannam@154: left -= tmp; cannam@154: } cannam@154: /*for (j=0;j= 0); cannam@154: N0 = m->eBands[j+1]-m->eBands[j]; cannam@154: N=N0<1) cannam@154: { cannam@154: excess = MAX32(bit-cap[j],0); cannam@154: bits[j] = bit-excess; cannam@154: cannam@154: /* Compensate for the extra DoF in stereo */ cannam@154: den=(C*N+ ((C==2 && N>2 && !*dual_stereo && j<*intensity) ? 1 : 0)); cannam@154: cannam@154: NClogN = den*(m->logN[j] + logM); cannam@154: cannam@154: /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET cannam@154: compared to their "fair share" of total/N */ cannam@154: offset = (NClogN>>1)-den*FINE_OFFSET; cannam@154: cannam@154: /* N=2 is the only point that doesn't match the curve */ cannam@154: if (N==2) cannam@154: offset += den<>2; cannam@154: cannam@154: /* Changing the offset for allocating the second and third cannam@154: fine energy bit */ cannam@154: if (bits[j] + offset < den*2<>2; cannam@154: else if (bits[j] + offset < den*3<>3; cannam@154: cannam@154: /* Divide with rounding */ cannam@154: ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1)))); cannam@154: ebits[j] = celt_udiv(ebits[j], den)>>BITRES; cannam@154: cannam@154: /* Make sure not to bust */ cannam@154: if (C*ebits[j] > (bits[j]>>BITRES)) cannam@154: ebits[j] = bits[j] >> stereo >> BITRES; cannam@154: cannam@154: /* More than that is useless because that's about as far as PVQ can go */ cannam@154: ebits[j] = IMIN(ebits[j], MAX_FINE_BITS); cannam@154: cannam@154: /* If we rounded down or capped this band, make it a candidate for the cannam@154: final fine energy pass */ cannam@154: fine_priority[j] = ebits[j]*(den<= bits[j]+offset; cannam@154: cannam@154: /* Remove the allocated fine bits; the rest are assigned to PVQ */ cannam@154: bits[j] -= C*ebits[j]< 0) cannam@154: { cannam@154: int extra_fine; cannam@154: int extra_bits; cannam@154: extra_fine = IMIN(excess>>(stereo+BITRES),MAX_FINE_BITS-ebits[j]); cannam@154: ebits[j] += extra_fine; cannam@154: extra_bits = extra_fine*C<= excess-balance; cannam@154: excess -= extra_bits; cannam@154: } cannam@154: balance = excess; cannam@154: cannam@154: celt_assert(bits[j] >= 0); cannam@154: celt_assert(ebits[j] >= 0); cannam@154: } cannam@154: /* Save any remaining bits over the cap for the rebalancing in cannam@154: quant_all_bands(). */ cannam@154: *_balance = balance; cannam@154: cannam@154: /* The skipped bands use all their bits for fine energy. */ cannam@154: for (;j> stereo >> BITRES; cannam@154: celt_assert(C*ebits[j]<nbEBands; cannam@154: skip_start = start; cannam@154: /* Reserve a bit to signal the end of manually skipped bands. */ cannam@154: skip_rsv = total >= 1<total) cannam@154: intensity_rsv = 0; cannam@154: else cannam@154: { cannam@154: total -= intensity_rsv; cannam@154: dual_stereo_rsv = total>=1<eBands[j+1]-m->eBands[j])<>4); cannam@154: /* Tilt of the allocation curve */ cannam@154: trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(end-j-1) cannam@154: *(1<<(LM+BITRES))>>6; cannam@154: /* Giving less resolution to single-coefficient bands because they get cannam@154: more benefit from having one coarse value per coefficient*/ cannam@154: if ((m->eBands[j+1]-m->eBands[j])<nbAllocVectors - 1; cannam@154: do cannam@154: { cannam@154: int done = 0; cannam@154: int psum = 0; cannam@154: int mid = (lo+hi) >> 1; cannam@154: for (j=end;j-->start;) cannam@154: { cannam@154: int bitsj; cannam@154: int N = m->eBands[j+1]-m->eBands[j]; cannam@154: bitsj = C*N*m->allocVectors[mid*len+j]<>2; cannam@154: if (bitsj > 0) cannam@154: bitsj = IMAX(0, bitsj + trim_offset[j]); cannam@154: bitsj += offsets[j]; cannam@154: if (bitsj >= thresh[j] || done) cannam@154: { cannam@154: done = 1; cannam@154: /* Don't allocate more than we can actually use */ cannam@154: psum += IMIN(bitsj, cap[j]); cannam@154: } else { cannam@154: if (bitsj >= C< total) cannam@154: hi = mid - 1; cannam@154: else cannam@154: lo = mid + 1; cannam@154: /*printf ("lo = %d, hi = %d\n", lo, hi);*/ cannam@154: } cannam@154: while (lo <= hi); cannam@154: hi = lo--; cannam@154: /*printf ("interp between %d and %d\n", lo, hi);*/ cannam@154: for (j=start;jeBands[j+1]-m->eBands[j]; cannam@154: bits1j = C*N*m->allocVectors[lo*len+j]<>2; cannam@154: bits2j = hi>=m->nbAllocVectors ? cannam@154: cap[j] : C*N*m->allocVectors[hi*len+j]<>2; cannam@154: if (bits1j > 0) cannam@154: bits1j = IMAX(0, bits1j + trim_offset[j]); cannam@154: if (bits2j > 0) cannam@154: bits2j = IMAX(0, bits2j + trim_offset[j]); cannam@154: if (lo > 0) cannam@154: bits1j += offsets[j]; cannam@154: bits2j += offsets[j]; cannam@154: if (offsets[j]>0) cannam@154: skip_start = j; cannam@154: bits2j = IMAX(0,bits2j-bits1j); cannam@154: bits1[j] = bits1j; cannam@154: bits2[j] = bits2j; cannam@154: } cannam@154: codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, cap, cannam@154: total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv, cannam@154: pulses, ebits, fine_priority, C, LM, ec, encode, prev, signalBandwidth); cannam@154: RESTORE_STACK; cannam@154: return codedBands; cannam@154: } cannam@154: