c64_fft_t *c64_fft32_alloc(int length, int x, int y) { c64_fft_t *state; int i, c; for (c = 0; c < NBCACHE; c++) { if (cache32[c] == NULL) break; if (cache32[c]->nfft == length) return cache32[c]; } state = (c64_fft_t *)celt_alloc(sizeof(c64_fft_t)); state->shift = log(length)/log(2) - ceil(log(length)/log(4)-1); state->nfft = length; state->twiddle = celt_alloc(length*2*sizeof(opus_int32)); state->itwiddle = celt_alloc(length*2*sizeof(opus_int32)); // Generate the inverse twiddle first because it does not need scaling gen_twiddle32(state->itwiddle, length, 2147483647.000000000); for (i = 0; i < length; i++) { state->twiddle[2*i+0] = state->itwiddle[2*i+0] >> 1; state->twiddle[2*i+1] = state->itwiddle[2*i+1] >> 1; } if (c < NBCACHE) cache32[c++] = state; if (c < NBCACHE) cache32[c] = NULL; return state; }
static void compute_pbands(CELTMode *mode, int res) { int i; celt_int16_t *pBands; pBands=celt_alloc(sizeof(celt_int16_t)*(PBANDS+2)); mode->nbPBands = PBANDS; for (i=0;i<PBANDS+1;i++) { pBands[i] = (pitch_freq[i]+res/2)/res; if (pBands[i] < mode->eBands[i]) pBands[i] = mode->eBands[i]; } pBands[PBANDS+1] = mode->eBands[mode->nbEBands+1]; for (i=1;i<mode->nbPBands+1;i++) { int j; for (j=0;j<mode->nbEBands;j++) if (mode->eBands[j] <= pBands[i] && mode->eBands[j+1] > pBands[i]) break; /*printf ("%d %d\n", i, j);*/ if (mode->eBands[j] != pBands[i]) { if (pBands[i]-mode->eBands[j] < mode->eBands[j+1]-pBands[i] && mode->eBands[j] != pBands[i-1]) pBands[i] = mode->eBands[j]; else pBands[i] = mode->eBands[j+1]; } } /*for (i=0;i<mode->nbPBands+2;i++) printf("%d ", pBands[i]); printf ("\n");*/ mode->pBands = pBands; mode->pitchEnd = pBands[PBANDS]; }
void clt_mdct_init(mdct_lookup *l,int N) { int i; int N2; l->n = N; N2 = N>>1; l->kfft = cpx32_fft_alloc(N>>2); #ifndef ENABLE_TI_DSPLIB55 if (l->kfft==NULL) return; #endif l->trig = (kiss_twiddle_scalar*)celt_alloc(N2*sizeof(kiss_twiddle_scalar)); if (l->trig==NULL) return; /* We have enough points that sine isn't necessary */ #if defined(FIXED_POINT) #if defined(DOUBLE_PRECISION) & !defined(MIXED_PRECISION) for (i=0;i<N2;i++) l->trig[i] = SAMP_MAX*cos(2*M_PI*(i+1./8.)/N); #else for (i=0;i<N2;i++) l->trig[i] = TRIG_UPSCALE*celt_cos_norm(DIV32(ADD32(SHL32(EXTEND32(i),17),16386),N)); #endif #else for (i=0;i<N2;i++) l->trig[i] = cos(2*M_PI*(i+1./8.)/N); #endif }
CELTEncoder *celt_encoder_create(const CELTMode *mode) { int N, C; CELTEncoder *st; if (check_mode(mode) != CELT_OK) return NULL; N = mode->mdctSize; C = mode->nbChannels; st = celt_alloc(sizeof(CELTEncoder)); if (st==NULL) return NULL; st->marker = ENCODERPARTIAL; st->mode = mode; st->frame_size = N; st->block_size = N; st->overlap = mode->overlap; st->VBR_rate = 0; st->pitch_enabled = 1; st->pitch_permitted = 1; st->pitch_available = 1; st->force_intra = 0; st->delayedIntra = 1; st->tonal_average = QCONST16(1.,8); st->fold_decision = 1; st->in_mem = celt_alloc(st->overlap*C*sizeof(celt_sig_t)); st->out_mem = celt_alloc((MAX_PERIOD+st->overlap)*C*sizeof(celt_sig_t)); st->oldBandE = (celt_word16_t*)celt_alloc(C*mode->nbEBands*sizeof(celt_word16_t)); st->preemph_memE = (celt_word16_t*)celt_alloc(C*sizeof(celt_word16_t)); st->preemph_memD = (celt_sig_t*)celt_alloc(C*sizeof(celt_sig_t)); #ifdef EXP_PSY st->psy_mem = celt_alloc(MAX_PERIOD*sizeof(celt_word16_t)); psydecay_init(&st->psy, MAX_PERIOD/2, st->mode->Fs); #endif if ((st->in_mem!=NULL) && (st->out_mem!=NULL) && (st->oldBandE!=NULL) #ifdef EXP_PSY && (st->psy_mem!=NULL) #endif && (st->preemph_memE!=NULL) && (st->preemph_memD!=NULL)) { st->marker = ENCODERVALID; return st; } /* If the setup fails for some reason deallocate it. */ celt_encoder_destroy(st); return NULL; }
c64_fft_t *c64_fft16_alloc(int length, int x, int y) { c64_fft_t *state; opus_int16 *w, *iw; int i, c; for (c = 0; c < NBCACHE; c++) { if (cache16[c] == NULL) break; if (cache16[c]->nfft == length) return cache16[c]; } state = (c64_fft_t *)celt_alloc(sizeof(c64_fft_t)); state->shift = log(length)/log(2) - ceil(log(length)/log(4)-1); state->nfft = length; state->twiddle = celt_alloc(length*2*sizeof(opus_int16)); state->itwiddle = celt_alloc(length*2*sizeof(opus_int16)); gen_twiddle16((opus_int16 *)state->twiddle, length, 32767.0); w = (opus_int16 *)state->twiddle; iw = (opus_int16 *)state->itwiddle; for (i = 0; i < length; i++) { iw[2*i+0] = w[2*i+0]; iw[2*i+1] = - w[2*i+1]; } if (c < NBCACHE) cache16[c++] = state; if (c < NBCACHE) cache16[c] = NULL; return state; }
static celt_int16_t *compute_ebands(celt_int32_t Fs, int frame_size, int *nbEBands) { celt_int16_t *eBands; int i, res, min_width, lin, low, high, nBark; res = (Fs+frame_size)/(2*frame_size); min_width = MIN_BINS*res; /*printf ("min_width = %d\n", min_width);*/ /* Find the number of critical bands supported by our sampling rate */ for (nBark=1;nBark<BARK_BANDS;nBark++) if (bark_freq[nBark+1]*2 >= Fs) break; /* Find where the linear part ends (i.e. where the spacing is more than min_width */ for (lin=0;lin<nBark;lin++) if (bark_freq[lin+1]-bark_freq[lin] >= min_width) break; /*printf ("lin = %d (%d Hz)\n", lin, bark_freq[lin]);*/ low = ((bark_freq[lin]/res)+(MIN_BINS-1))/MIN_BINS; high = nBark-lin; *nbEBands = low+high; eBands = celt_alloc(sizeof(celt_int16_t)*(*nbEBands+2)); /* Linear spacing (min_width) */ for (i=0;i<low;i++) eBands[i] = MIN_BINS*i; /* Spacing follows critical bands */ for (i=0;i<high;i++) eBands[i+low] = (bark_freq[lin+i]+res/2)/res; /* Enforce the minimum spacing at the boundary */ for (i=0;i<*nbEBands;i++) if (eBands[i] < MIN_BINS*i) eBands[i] = MIN_BINS*i; eBands[*nbEBands] = (bark_freq[nBark]+res/2)/res; eBands[*nbEBands+1] = frame_size; if (eBands[*nbEBands] > eBands[*nbEBands+1]) eBands[*nbEBands] = eBands[*nbEBands+1]; /* FIXME: Remove last band if too small */ /*for (i=0;i<*nbEBands+2;i++) printf("%d ", eBands[i]); printf ("\n"); exit(1);*/ return eBands; }
static void compute_allocation_table(CELTMode *mode, int res) { int i, j, nBark; celt_int16_t *allocVectors; const int C = CHANNELS(mode); /* Find the number of critical bands supported by our sampling rate */ for (nBark=1;nBark<BARK_BANDS;nBark++) if (bark_freq[nBark+1]*2 >= mode->Fs) break; mode->nbAllocVectors = BITALLOC_SIZE; allocVectors = celt_alloc(sizeof(celt_int16_t)*(BITALLOC_SIZE*mode->nbEBands)); if (allocVectors==NULL) return; /* Compute per-codec-band allocation from per-critical-band matrix */ for (i=0;i<BITALLOC_SIZE;i++) { celt_int32_t current = 0; int eband = 0; for (j=0;j<nBark;j++) { int edge, low; celt_int32_t alloc; edge = mode->eBands[eband+1]*res; alloc = band_allocation[i*BARK_BANDS+j]; alloc = alloc*C*mode->mdctSize; if (edge < bark_freq[j+1]) { int num, den; num = alloc * (edge-bark_freq[j]); den = bark_freq[j+1]-bark_freq[j]; low = (num+den/2)/den; allocVectors[i*mode->nbEBands+eband] = (current+low+128)/256; current=0; eband++; current += alloc-low; } else { current += alloc; } } allocVectors[i*mode->nbEBands+eband] = (current+128)/256; } mode->allocVectors = allocVectors; }
/* Psychoacoustic spreading function. The idea here is compute a first order recursive filter. The filter coefficient is frequency dependent and chosen such that we have a -10dB/Bark slope on the right side and a -25dB/Bark slope on the left side. */ void psydecay_init(struct PsyDecay *decay, int len, celt_int32 Fs) { int i; celt_word16 *decayR = (celt_word16*)celt_alloc(sizeof(celt_word16)*len); decay->decayR = decayR; if (decayR==NULL) return; for (i=0;i<len;i++) { float f; float deriv; /* Real frequency (in Hz) */ f = Fs*i*(1/(2.f*len)); /* This is the derivative of the Vorbis freq->Bark function (see above) */ deriv = (8.288e-8 * f)/(3.4225e-16 *f*f*f*f + 1) + .009694/(5.476e-7 *f*f + 1) + 1e-4; /* Back to FFT bin units */ deriv *= Fs*(1/(2.f*len)); /* decay corresponding to -10dB/Bark */ decayR[i] = Q15ONE*pow(.1f, deriv); /* decay corresponding to -25dB/Bark */ /*decay->decayL[i] = Q15ONE*pow(0.0031623f, deriv);*/ /*printf ("%f %f\n", decayL[i], decayR[i]);*/ } }
CELTMode *celt051_mode_create(celt_int32_t Fs, int channels, int frame_size, int *error) { int i; #ifdef STDIN_TUNING scanf("%d ", &MIN_BINS); scanf("%d ", &BITALLOC_SIZE); band_allocation = celt_alloc(sizeof(int)*BARK_BANDS*BITALLOC_SIZE); for (i=0;i<BARK_BANDS*BITALLOC_SIZE;i++) { scanf("%d ", band_allocation+i); } #endif #ifdef STATIC_MODES const CELTMode *m = NULL; CELTMode *mode=NULL; ALLOC_STACK; for (i=0;i<TOTAL_MODES;i++) { if (Fs == static_mode_list[i]->Fs && channels == static_mode_list[i]->nbChannels && frame_size == static_mode_list[i]->mdctSize) { m = static_mode_list[i]; break; } } if (m == NULL) { celt_warning("Mode not included as part of the static modes"); if (error) *error = CELT_BAD_ARG; return NULL; } mode = (CELTMode*)celt_alloc(sizeof(CELTMode)); CELT_COPY(mode, m, 1); #else int res; CELTMode *mode; celt_word16_t *window; ALLOC_STACK; /* The good thing here is that permutation of the arguments will automatically be invalid */ if (Fs < 32000 || Fs > 96000) { celt_warning("Sampling rate must be between 32 kHz and 96 kHz"); if (error) *error = CELT_BAD_ARG; return NULL; } if (channels < 0 || channels > 2) { celt_warning("Only mono and stereo supported"); if (error) *error = CELT_BAD_ARG; return NULL; } if (frame_size < 64 || frame_size > 512 || frame_size%2!=0) { celt_warning("Only even frame sizes from 64 to 512 are supported"); if (error) *error = CELT_BAD_ARG; return NULL; } res = (Fs+frame_size)/(2*frame_size); mode = celt_alloc(sizeof(CELTMode)); mode->Fs = Fs; mode->mdctSize = frame_size; mode->nbChannels = channels; mode->eBands = compute_ebands(Fs, frame_size, &mode->nbEBands); compute_pbands(mode, res); mode->ePredCoef = QCONST16(.8f,15); if (frame_size > 384 && (frame_size%8)==0) { mode->nbShortMdcts = 4; } else if (frame_size > 384 && (frame_size%10)==0) { mode->nbShortMdcts = 5; } else if (frame_size > 256 && (frame_size%6)==0) { mode->nbShortMdcts = 3; } else if (frame_size > 256 && (frame_size%8)==0) { mode->nbShortMdcts = 4; } else if (frame_size > 64 && (frame_size%4)==0) { mode->nbShortMdcts = 2; } else if (frame_size > 128 && (frame_size%6)==0) { mode->nbShortMdcts = 3; } else { mode->nbShortMdcts = 1; } if (mode->nbShortMdcts > 1) mode->overlap = ((frame_size/mode->nbShortMdcts)>>2)<<2; /* Overlap must be divisible by 4 */ else
static void compute_allocation_table(CELTMode *mode, int res) { int i, j, eband, nBark; celt_int32_t *allocVectors; celt_int16_t *allocVectorsS; celt_int16_t *allocEnergy; const int C = CHANNELS(mode); /* Find the number of critical bands supported by our sampling rate */ for (nBark=1;nBark<BARK_BANDS;nBark++) if (bark_freq[nBark+1]*2 >= mode->Fs) break; mode->nbAllocVectors = BITALLOC_SIZE; allocVectors = celt_alloc(sizeof(celt_int32_t)*(BITALLOC_SIZE*mode->nbEBands)); allocEnergy = celt_alloc(sizeof(celt_int16_t)*(mode->nbAllocVectors*(mode->nbEBands+1))); /* Compute per-codec-band allocation from per-critical-band matrix */ for (i=0;i<BITALLOC_SIZE;i++) { eband = 0; for (j=0;j<nBark;j++) { int edge, low; celt_int32_t alloc; edge = mode->eBands[eband+1]*res; alloc = band_allocation[i*BARK_BANDS+j]; alloc = alloc*C*mode->mdctSize/256; if (edge < bark_freq[j+1]) { int num, den; num = alloc * (edge-bark_freq[j]); den = bark_freq[j+1]-bark_freq[j]; low = (num+den/2)/den; allocVectors[i*mode->nbEBands+eband] += low; eband++; allocVectors[i*mode->nbEBands+eband] += alloc-low; } else { allocVectors[i*mode->nbEBands+eband] += alloc; } } } /* Compute fine energy resolution and update the pulse allocation table to subtract that */ for (i=0;i<mode->nbAllocVectors;i++) { int sum = 0; for (j=0;j<mode->nbEBands;j++) { int ebits; int min_bits=0; if (allocVectors[i*mode->nbEBands+j] > 0) min_bits = 1; ebits = IMAX(min_bits , allocVectors[i*mode->nbEBands+j] / (C*(mode->eBands[j+1]-mode->eBands[j]))); if (ebits>7) ebits=7; /* The bits used for fine allocation can't be used for pulses */ /* However, we give two "free" bits to all modes to compensate for the fact that some energy resolution is needed regardless of the frame size. */ if (ebits>1) allocVectors[i*mode->nbEBands+j] -= C*(ebits-2); if (allocVectors[i*mode->nbEBands+j] < 0) allocVectors[i*mode->nbEBands+j] = 0; sum += ebits; allocEnergy[i*(mode->nbEBands+1)+j] = ebits; } allocEnergy[i*(mode->nbEBands+1)+mode->nbEBands] = sum; } mode->energy_alloc = allocEnergy; allocVectorsS = celt_alloc(sizeof(celt_int16_t)*(BITALLOC_SIZE*mode->nbEBands)); for(i=0;i<(BITALLOC_SIZE*mode->nbEBands);i++) allocVectorsS[i] = (celt_int16_t)allocVectors[i]; mode->allocVectors = allocVectorsS; }
{ mode->nbShortMdcts = 3; } else { mode->nbShortMdcts = 1; } if (mode->nbShortMdcts > 1) mode->overlap = ((frame_size/mode->nbShortMdcts)>>2)<<2; /* Overlap must be divisible by 4 */ else mode->overlap = (frame_size>>3)<<2; compute_allocation_table(mode, res); /*printf ("%d bands\n", mode->nbEBands);*/ window = (celt_word16_t*)celt_alloc(mode->overlap*sizeof(celt_word16_t)); #ifndef FIXED_POINT for (i=0;i<mode->overlap;i++) window[i] = Q15ONE*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap)); #else for (i=0;i<mode->overlap;i++) window[i] = MIN32(32767,32768.*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap))); #endif mode->window = window; mode->bits = (const celt_int16_t **)compute_alloc_cache(mode, 1); if (mode->nbChannels>=2) mode->bits_stereo = (const celt_int16_t **)compute_alloc_cache(mode, mode->nbChannels); #ifndef SHORTCUTS
CELTEncoder *celt_encoder_create(const CELTMode *mode, int channels, int *error) { int N, C; CELTEncoder *st; if (check_mode(mode) != CELT_OK) { if (error) *error = CELT_INVALID_MODE; return NULL; } #ifdef DISABLE_STEREO if (channels > 1) { celt_warning("Stereo support was disable from this build"); if (error) *error = CELT_BAD_ARG; return NULL; } #endif if (channels < 0 || channels > 2) { celt_warning("Only mono and stereo supported"); if (error) *error = CELT_BAD_ARG; return NULL; } N = mode->mdctSize; C = channels; st = celt_alloc(sizeof(CELTEncoder)); if (st==NULL) { if (error) *error = CELT_ALLOC_FAIL; return NULL; } st->marker = ENCODERPARTIAL; st->mode = mode; st->frame_size = N; st->block_size = N; st->overlap = mode->overlap; st->channels = channels; st->vbr_rate = 0; st->pitch_enabled = 1; st->pitch_permitted = 1; st->pitch_available = 1; st->force_intra = 0; st->delayedIntra = 1; st->tonal_average = QCONST16(1.,8); st->fold_decision = 1; st->in_mem = celt_alloc(st->overlap*C*sizeof(celt_sig)); st->out_mem = celt_alloc((MAX_PERIOD+st->overlap)*C*sizeof(celt_sig)); st->pitch_buf = celt_alloc(((MAX_PERIOD>>1)+2)*sizeof(celt_word16)); st->oldBandE = (celt_word16*)celt_alloc(C*mode->nbEBands*sizeof(celt_word16)); st->preemph_memE = (celt_word16*)celt_alloc(C*sizeof(celt_word16)); st->preemph_memD = (celt_sig*)celt_alloc(C*sizeof(celt_sig)); if ((st->in_mem!=NULL) && (st->out_mem!=NULL) && (st->oldBandE!=NULL) && (st->preemph_memE!=NULL) && (st->preemph_memD!=NULL)) { if (error) *error = CELT_OK; st->marker = ENCODERVALID; return st; } /* If the setup fails for some reason deallocate it. */ celt_encoder_destroy(st); if (error) *error = CELT_ALLOC_FAIL; return NULL; }
void ec_byte_writeinit(ec_byte_buffer *_b){ _b->ptr=_b->buf=celt_alloc(EC_BUFFER_INCREMENT*sizeof(char)); _b->storage=EC_BUFFER_INCREMENT; _b->resizable = 1; }