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