/* Copyright (c) 2007-2008 CSIRO Copyright (c) 2007-2009 Xiph.Org Foundation Written by Jean-Marc Valin */ /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the Xiph.org Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include "modes.h" #include "cwrs.h" #include "arch.h" #include "os_support.h" #include "entcode.h" #include "rate.h" static const unsigned char LOG2_FRAC_TABLE[24]={ 0, 8,13, 16,19,21,23, 24,26,27,28,29,30,31,32, 32,33,34,34,35,36,36,37,37 }; #ifndef STATIC_MODES /*Determines if V(N,K) fits in a 32-bit unsigned integer. N and K are themselves limited to 15 bits.*/ static int fits_in32(int _n, int _k) { static const celt_int16 maxN[15] = { 32767, 32767, 32767, 1476, 283, 109, 60, 40, 29, 24, 20, 18, 16, 14, 13}; static const celt_int16 maxK[15] = { 32767, 32767, 32767, 32767, 1172, 238, 95, 53, 36, 27, 22, 18, 16, 15, 13}; if (_n>=14) { if (_k>=14) return 0; else return _n <= maxN[_k]; } else { return _k <= maxK[_n]; } } void compute_pulse_cache(CELTMode *m, int LM) { int i; int curr=0; int nbEntries=0; int entryN[100], entryK[100], entryI[100]; const celt_int16 *eBands = m->eBands; PulseCache *cache = &m->cache; celt_int16 *cindex; unsigned char *bits; cindex = celt_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2)); cache->index = cindex; /* Scan for all unique band sizes */ for (i=0;i<=LM+1;i++) { int j; for (j=0;jnbEBands;j++) { int k; int N = (eBands[j+1]-eBands[j])<>1; cindex[i*m->nbEBands+j] = -1; /* Find other bands that have the same size */ for (k=0;k<=i;k++) { int n; for (n=0;nnbEBands && (k!=i || n>1) { cindex[i*m->nbEBands+j] = cindex[k*m->nbEBands+n]; break; } } } if (cache->index[i*m->nbEBands+j] == -1 && N!=0) { int K; entryN[nbEntries] = N; K = 0; while (fits_in32(N,get_pulses(K+1)) && KnbEBands+j] = curr; entryI[nbEntries] = curr; curr += K+1; nbEntries++; } } } bits = celt_alloc(sizeof(unsigned char)*curr); cache->bits = bits; cache->size = curr; /* Compute the cache for all unique sizes */ for (i=0;i1; logM = LM<>1; psum = 0; done = 0; for (j=end;j-->start;) { int tmp = bits1[j] + (mid*bits2[j]>>ALLOC_STEPS); if (tmp >= thresh[j] || done) { done = 1; /* Don't allocate more than we can actually use */ psum += IMIN(tmp, 64*C<= alloc_floor) psum += alloc_floor; } } if (psum > total) hi = mid; else lo = mid; } psum = 0; /*printf ("interp bisection gave %d\n", lo);*/ done = 0; for (j=end;j-->start;) { int tmp = bits1[j] + (lo*bits2[j]>>ALLOC_STEPS); if (tmp < thresh[j] && !done) { if (tmp >= alloc_floor) tmp = alloc_floor; else tmp = 0; } else done = 1; /* Don't allocate more than we can actually use */ tmp = IMIN(tmp, 64*C<eBands[codedBands]-m->eBands[start]); left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0); band_width = m->eBands[codedBands]-m->eBands[j]; band_bits = bits[j] + percoeff*band_width + rem; /*Only code a skip decision if we're above the threshold for this band. Otherwise it is force-skipped. This ensures that we have enough bits to code the skip flag.*/ if (band_bits >= IMAX(thresh[j], alloc_floor+(1< ((j>4) { ec_enc_bit_logp((ec_enc *)ec, 1, 1); break; } ec_enc_bit_logp((ec_enc *)ec, 0, 1); } else if (ec_dec_bit_logp((ec_dec *)ec, 1)) { break; } /*We used a bit to skip this band.*/ psum += 1< 0) intensity_rsv = LOG2_FRAC_TABLE[j-start]; psum += intensity_rsv; if (band_bits >= alloc_floor) { /*If we have enough for a fine energy bit per channel, use it.*/ psum += alloc_floor; bits[j] = alloc_floor; } else { /*Otherwise this band gets nothing at all.*/ bits[j] = 0; } } celt_assert(codedBands > start); /* Code the intensity and dual stereo parameters. */ if (intensity_rsv > 0) { if (encode) { *intensity = IMIN(*intensity, codedBands); ec_enc_uint((ec_enc *)ec, *intensity-start, codedBands+1-start); } else *intensity = start+ec_dec_uint((ec_dec *)ec, codedBands+1-start); } else *intensity = 0; if (*intensity <= start) { total += dual_stereo_rsv; dual_stereo_rsv = 0; } if (dual_stereo_rsv > 0) { if (encode) ec_enc_bit_logp((ec_enc *)ec, *dual_stereo, 1); else *dual_stereo = ec_dec_bit_logp((ec_dec *)ec, 1); } else *dual_stereo = 0; /* Allocate the remaining bits */ left = total-psum; percoeff = left/(m->eBands[codedBands]-m->eBands[start]); left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; for (j=start;jeBands[j+1]-m->eBands[j]); for (j=start;jeBands[j+1]-m->eBands[j]); bits[j] += tmp; left -= tmp; } /*for (j=0;j= 0); N0 = m->eBands[j+1]-m->eBands[j]; N=N0<1) { NClogN = N*C*(m->logN[j] + logM); /* Compensate for the extra DoF in stereo */ den=(C*N+ ((C==2 && N>2) ? 1 : 0)); /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET compared to their "fair share" of total/N */ offset = (NClogN>>1)-N*C*FINE_OFFSET; /* N=2 is the only point that doesn't match the curve */ if (N==2) offset += N*C<>2; /* Changing the offset for allocating the second and third fine energy bit */ if (bits[j] + offset < den*2<>2; else if (bits[j] + offset < den*3<>3; /* Divide with rounding */ ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1))) / (den< (bits[j]>>BITRES)) ebits[j] = bits[j] >> stereo >> BITRES; /* More than that is useless because that's about as far as PVQ can go */ ebits[j] = IMIN(ebits[j], MAX_FINE_BITS); /* If we rounded down or capped this band, make it a candidate for the final fine energy pass */ fine_priority[j] = ebits[j]*(den<= bits[j]+offset; } else { /* For N=1, all bits go to fine energy except for a single sign bit */ ebits[j] = IMIN(IMAX(0,(bits[j] >> stereo >> BITRES)-1),MAX_FINE_BITS); fine_priority[j] = (ebits[j]+1)*C<= (bits[j]-balance); /* N=1 bands can't take advantage of the re-balancing in quant_all_bands() because they don't have shape, only fine energy. Instead, do the re-balancing here.*/ balance = IMAX(0,bits[j] - ((ebits[j]+1)*C<= 0); celt_assert(ebits[j] >= 0); } /* The skipped bands use all their bits for fine energy. */ for (;j> stereo >> BITRES; celt_assert(C*ebits[j]<nbEBands; skip_start = start; /* Reserve a bit to signal the end of manually skipped bands. */ skip_rsv = total >= 1<total) intensity_rsv = 0; else { total -= intensity_rsv; dual_stereo_rsv = total>=1<eBands[j+1]-m->eBands[j])<>4); /* Tilt of the allocation curve */ trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(m->nbEBands-j-1) <<(LM+BITRES)>>6; /* Giving less resolution to single-coefficient bands because they get more benefit from having one coarse value per coefficient*/ if ((m->eBands[j+1]-m->eBands[j])<nbAllocVectors - 2; do { int done = 0; int psum = 0; int mid = (lo+hi) >> 1; for (j=end;j-->start;) { int N = m->eBands[j+1]-m->eBands[j]; bits1[j] = C*N*m->allocVectors[mid*len+j]<>2; if (bits1[j] > 0) bits1[j] = IMAX(0, bits1[j] + trim_offset[j]); bits1[j] += offsets[j]; if (bits1[j] >= thresh[j] || done) { done = 1; /* Don't allocate more than we can actually use */ psum += IMIN(bits1[j], 64*C<= C< total) hi = mid - 1; else lo = mid + 1; /*printf ("lo = %d, hi = %d\n", lo, hi);*/ } while (lo <= hi); hi = lo--; /*printf ("interp between %d and %d\n", lo, hi);*/ for (j=start;jeBands[j+1]-m->eBands[j]; bits1[j] = C*N*m->allocVectors[lo*len+j]<>2; bits2[j] = C*N*m->allocVectors[hi*len+j]<>2; if (bits1[j] > 0) bits1[j] = IMAX(0, bits1[j] + trim_offset[j]); if (bits2[j] > 0) bits2[j] = IMAX(0, bits2[j] + trim_offset[j]); if (lo > 0) bits1[j] += offsets[j]; bits2[j] += offsets[j]; if (offsets[j]>0) skip_start = j; bits2[j] -= bits1[j]; } codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, total, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv, pulses, ebits, fine_priority, C, LM, ec, encode, prev); RESTORE_STACK; return codedBands; }