+static float get_floor_average(floor_t * fc, float * coeffs, int i) {
+ int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
+ int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
+ int j;
+ float average = 0;
+
+ for (j = begin; j < end; j++)
+ average += fabs(coeffs[j]);
+ return average / (end - begin);
+}
+
+static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, uint_fast16_t * posts, int samples) {
+ int range = 255 / fc->multiplier + 1;
+ int i;
+ float tot_average = 0.;
+ float averages[fc->values];
+ for (i = 0; i < fc->values; i++){
+ averages[i] = get_floor_average(fc, coeffs, i);
+ tot_average += averages[i];
+ }
+ tot_average /= fc->values;
+ tot_average /= venc->quality;
+
+ for (i = 0; i < fc->values; i++) {
+ int position = fc->list[fc->list[i].sort].x;
+ float average = averages[i];
+ int j;
+
+ average *= pow(tot_average / average, 0.5) * pow(1.25, position/200.); // MAGIC!
+ for (j = 0; j < range - 1; j++)
+ if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average) break;
+ posts[fc->list[i].sort] = j;
+ }
+}
+
+static int render_point(int x0, int y0, int x1, int y1, int x) {
+ return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
+}
+
+static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, uint_fast16_t * posts, float * floor, int samples) {
+ int range = 255 / fc->multiplier + 1;
+ int coded[fc->values]; // first 2 values are unused
+ int i, counter;
+
+ put_bits(pb, 1, 1); // non zero
+ put_bits(pb, ilog(range - 1), posts[0]);
+ put_bits(pb, ilog(range - 1), posts[1]);
+ coded[0] = coded[1] = 1;
+
+ for (i = 2; i < fc->values; i++) {
+ int predicted = render_point(fc->list[fc->list[i].low].x,
+ posts[fc->list[i].low],
+ fc->list[fc->list[i].high].x,
+ posts[fc->list[i].high],
+ fc->list[i].x);
+ int highroom = range - predicted;
+ int lowroom = predicted;
+ int room = FFMIN(highroom, lowroom);
+ if (predicted == posts[i]) {
+ coded[i] = 0; // must be used later as flag!
+ continue;
+ } else {
+ if (!coded[fc->list[i].low ]) coded[fc->list[i].low ] = -1;
+ if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1;
+ }
+ if (posts[i] > predicted) {
+ if (posts[i] - predicted > room)
+ coded[i] = posts[i] - predicted + lowroom;
+ else
+ coded[i] = (posts[i] - predicted) << 1;
+ } else {
+ if (predicted - posts[i] > room)
+ coded[i] = predicted - posts[i] + highroom - 1;
+ else
+ coded[i] = ((predicted - posts[i]) << 1) - 1;
+ }
+ }
+
+ counter = 2;
+ for (i = 0; i < fc->partitions; i++) {
+ floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
+ int k, cval = 0, csub = 1<<c->subclass;
+ if (c->subclass) {
+ codebook_t * book = &venc->codebooks[c->masterbook];
+ int cshift = 0;
+ for (k = 0; k < c->dim; k++) {
+ int l;
+ for (l = 0; l < csub; l++) {
+ int maxval = 1;
+ if (c->books[l] != -1)
+ maxval = venc->codebooks[c->books[l]].nentries;
+ // coded could be -1, but this still works, cause that is 0
+ if (coded[counter + k] < maxval) break;
+ }
+ assert(l != csub);
+ cval |= l << cshift;
+ cshift += c->subclass;
+ }
+ put_codeword(pb, book, cval);
+ }
+ for (k = 0; k < c->dim; k++) {
+ int book = c->books[cval & (csub-1)];
+ int entry = coded[counter++];
+ cval >>= c->subclass;
+ if (book == -1) continue;
+ if (entry == -1) entry = 0;
+ put_codeword(pb, &venc->codebooks[book], entry);
+ }
+ }
+
+ ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded, fc->multiplier, floor, samples);
+}
+
+static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
+ int i, entry = -1;
+ float distance = FLT_MAX;
+ assert(book->dimentions);
+ for (i = 0; i < book->nentries; i++) {
+ float * vec = book->dimentions + i * book->ndimentions, d = book->pow2[i];
+ int j;
+ if (!book->lens[i]) continue;
+ for (j = 0; j < book->ndimentions; j++)
+ d -= vec[j] * num[j];
+ if (distance > d) {
+ entry = i;
+ distance = d;
+ }
+ }
+ put_codeword(pb, book, entry);
+ return &book->dimentions[entry * book->ndimentions];
+}
+
+static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) {
+ int pass, i, j, p, k;
+ int psize = rc->partition_size;
+ int partitions = (rc->end - rc->begin) / psize;
+ int channels = (rc->type == 2) ? 1 : real_ch;
+ int classes[channels][partitions];
+ int classwords = venc->codebooks[rc->classbook].ndimentions;
+
+ assert(rc->type == 2);
+ assert(real_ch == 2);
+ for (p = 0; p < partitions; p++) {
+ float max1 = 0., max2 = 0.;
+ int s = rc->begin + p * psize;
+ for (k = s; k < s + psize; k += 2) {
+ max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
+ max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
+ }
+
+ for (i = 0; i < rc->classifications - 1; i++) {
+ if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1]) break;
+ }
+ classes[0][p] = i;
+ }
+
+ for (pass = 0; pass < 8; pass++) {
+ p = 0;
+ while (p < partitions) {
+ if (pass == 0)
+ for (j = 0; j < channels; j++) {
+ codebook_t * book = &venc->codebooks[rc->classbook];
+ int entry = 0;
+ for (i = 0; i < classwords; i++) {
+ entry *= rc->classifications;
+ entry += classes[j][p + i];
+ }
+ put_codeword(pb, book, entry);
+ }
+ for (i = 0; i < classwords && p < partitions; i++, p++) {
+ for (j = 0; j < channels; j++) {
+ int nbook = rc->books[classes[j][p]][pass];
+ codebook_t * book = &venc->codebooks[nbook];
+ float * buf = coeffs + samples*j + rc->begin + p*psize;
+ if (nbook == -1) continue;
+
+ assert(rc->type == 0 || rc->type == 2);
+ assert(!(psize % book->ndimentions));
+
+ if (rc->type == 0) {
+ for (k = 0; k < psize; k += book->ndimentions) {
+ float * a = put_vector(book, pb, &buf[k]);
+ int l;
+ for (l = 0; l < book->ndimentions; l++)
+ buf[k + l] -= a[l];
+ }
+ } else {
+ int s = rc->begin + p * psize, a1, b1;
+ a1 = (s % real_ch) * samples;
+ b1 = s / real_ch;
+ s = real_ch * samples;
+ for (k = 0; k < psize; k += book->ndimentions) {
+ int dim, a2 = a1, b2 = b1;
+ float vec[book->ndimentions], * pv = vec;
+ for (dim = book->ndimentions; dim--; ) {
+ *pv++ = coeffs[a2 + b2];
+ if ((a2 += samples) == s) {
+ a2=0;
+ b2++;
+ }
+ }
+ pv = put_vector(book, pb, vec);
+ for (dim = book->ndimentions; dim--; ) {
+ coeffs[a1 + b1] -= *pv++;
+ if ((a1 += samples) == s) {
+ a1=0;
+ b1++;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+static int apply_window_and_mdct(venc_context_t * venc, signed short * audio, int samples) {
+ int i, j, channel;
+ const float * win = venc->win[0];
+ int window_len = 1 << (venc->log2_blocksize[0] - 1);
+ float n = (float)(1 << venc->log2_blocksize[0]) / 4.;
+ // FIXME use dsp
+
+ if (!venc->have_saved && !samples) return 0;
+
+ if (venc->have_saved) {
+ for (channel = 0; channel < venc->channels; channel++) {
+ memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
+ }
+ } else {
+ for (channel = 0; channel < venc->channels; channel++) {
+ memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
+ }
+ }
+
+ if (samples) {
+ for (channel = 0; channel < venc->channels; channel++) {
+ float * offset = venc->samples + channel*window_len*2 + window_len;
+ j = channel;
+ for (i = 0; i < samples; i++, j += venc->channels)
+ offset[i] = -audio[j] / 32768. / n * win[window_len - i - 1]; //FIXME find out why the sign has to be fliped
+ }
+ } else {
+ for (channel = 0; channel < venc->channels; channel++) {
+ memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
+ }
+ }
+
+ for (channel = 0; channel < venc->channels; channel++) {
+ ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
+ }
+
+ if (samples) {
+ for (channel = 0; channel < venc->channels; channel++) {
+ float * offset = venc->saved + channel*window_len;
+ j = channel;
+ for (i = 0; i < samples; i++, j += venc->channels)
+ offset[i] = -audio[j] / 32768. / n * win[i]; //FIXME find out why the sign has to be fliped
+ }
+ venc->have_saved = 1;
+ } else {
+ venc->have_saved = 0;
+ }
+ return 1;
+}
+
+static av_cold int vorbis_encode_init(AVCodecContext * avccontext)