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