comparison vorbis_enc.c @ 10309:d157ccc267e7 libavcodec

brace placement and linebreak cosmetics
author diego
date Mon, 28 Sep 2009 21:13:47 +0000
parents 38ab367d4231
children d3644248015a
comparison
equal deleted inserted replaced
10308:28b0f87cfdf1 10309:d157ccc267e7
123 vorbis_enc_mode * modes; 123 vorbis_enc_mode * modes;
124 124
125 int64_t sample_count; 125 int64_t sample_count;
126 } vorbis_enc_context; 126 } vorbis_enc_context;
127 127
128 static inline void put_codeword(PutBitContext * pb, vorbis_enc_codebook * cb, int entry) { 128 static inline void put_codeword(PutBitContext * pb, vorbis_enc_codebook * cb,
129 int entry)
130 {
129 assert(entry >= 0); 131 assert(entry >= 0);
130 assert(entry < cb->nentries); 132 assert(entry < cb->nentries);
131 assert(cb->lens[entry]); 133 assert(cb->lens[entry]);
132 put_bits(pb, cb->lens[entry], cb->codewords[entry]); 134 put_bits(pb, cb->lens[entry], cb->codewords[entry]);
133 } 135 }
134 136
135 static int cb_lookup_vals(int lookup, int dimentions, int entries) { 137 static int cb_lookup_vals(int lookup, int dimentions, int entries)
136 if (lookup == 1) return ff_vorbis_nth_root(entries, dimentions); 138 {
137 else if (lookup == 2) return dimentions * entries; 139 if (lookup == 1)
140 return ff_vorbis_nth_root(entries, dimentions);
141 else if (lookup == 2)
142 return dimentions * entries;
138 return 0; 143 return 0;
139 } 144 }
140 145
141 static void ready_codebook(vorbis_enc_codebook * cb) { 146 static void ready_codebook(vorbis_enc_codebook * cb)
147 {
142 int i; 148 int i;
143 149
144 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries); 150 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
145 151
146 if (!cb->lookup) 152 if (!cb->lookup)
169 cb->pow2[i] /= 2.; 175 cb->pow2[i] /= 2.;
170 } 176 }
171 } 177 }
172 } 178 }
173 179
174 static void ready_residue(vorbis_enc_residue * rc, vorbis_enc_context * venc) { 180 static void ready_residue(vorbis_enc_residue * rc, vorbis_enc_context * venc)
181 {
175 int i; 182 int i;
176 assert(rc->type == 2); 183 assert(rc->type == 2);
177 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications); 184 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
178 for (i = 0; i < rc->classifications; i++) { 185 for (i = 0; i < rc->classifications; i++) {
179 int j; 186 int j;
180 vorbis_enc_codebook * cb; 187 vorbis_enc_codebook * cb;
181 for (j = 0; j < 8; j++) 188 for (j = 0; j < 8; j++)
182 if (rc->books[i][j] != -1) break; 189 if (rc->books[i][j] != -1)
183 if (j == 8) continue; // zero 190 break;
191 if (j == 8)
192 continue; // zero
184 cb = &venc->codebooks[rc->books[i][j]]; 193 cb = &venc->codebooks[rc->books[i][j]];
185 assert(cb->ndimentions >= 2); 194 assert(cb->ndimentions >= 2);
186 assert(cb->lookup); 195 assert(cb->lookup);
187 196
188 for (j = 0; j < cb->nentries; j++) { 197 for (j = 0; j < cb->nentries; j++) {
189 float a; 198 float a;
190 if (!cb->lens[j]) continue; 199 if (!cb->lens[j])
200 continue;
191 a = fabs(cb->dimentions[j * cb->ndimentions]); 201 a = fabs(cb->dimentions[j * cb->ndimentions]);
192 if (a > rc->maxes[i][0]) 202 if (a > rc->maxes[i][0])
193 rc->maxes[i][0] = a; 203 rc->maxes[i][0] = a;
194 a = fabs(cb->dimentions[j * cb->ndimentions + 1]); 204 a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
195 if (a > rc->maxes[i][1]) 205 if (a > rc->maxes[i][1])
201 rc->maxes[i][0] += 0.8; 211 rc->maxes[i][0] += 0.8;
202 rc->maxes[i][1] += 0.8; 212 rc->maxes[i][1] += 0.8;
203 } 213 }
204 } 214 }
205 215
206 static void create_vorbis_context(vorbis_enc_context * venc, AVCodecContext * avccontext) { 216 static void create_vorbis_context(vorbis_enc_context * venc,
217 AVCodecContext * avccontext)
218 {
207 vorbis_enc_floor * fc; 219 vorbis_enc_floor * fc;
208 vorbis_enc_residue * rc; 220 vorbis_enc_residue * rc;
209 vorbis_enc_mapping * mc; 221 vorbis_enc_mapping * mc;
210 int i, book; 222 int i, book;
211 223
361 373
362 ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0); 374 ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0);
363 ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0); 375 ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0);
364 } 376 }
365 377
366 static void put_float(PutBitContext * pb, float f) { 378 static void put_float(PutBitContext * pb, float f)
379 {
367 int exp, mant; 380 int exp, mant;
368 uint32_t res = 0; 381 uint32_t res = 0;
369 mant = (int)ldexp(frexp(f, &exp), 20); 382 mant = (int)ldexp(frexp(f, &exp), 20);
370 exp += 788 - 20; 383 exp += 788 - 20;
371 if (mant < 0) { res |= (1 << 31); mant = -mant; } 384 if (mant < 0) {
385 res |= (1 << 31);
386 mant = -mant;
387 }
372 res |= mant | (exp << 21); 388 res |= mant | (exp << 21);
373 put_bits(pb, 32, res); 389 put_bits(pb, 32, res);
374 } 390 }
375 391
376 static void put_codebook_header(PutBitContext * pb, vorbis_enc_codebook * cb) { 392 static void put_codebook_header(PutBitContext * pb, vorbis_enc_codebook * cb)
393 {
377 int i; 394 int i;
378 int ordered = 0; 395 int ordered = 0;
379 396
380 put_bits(pb, 24, 0x564342); //magic 397 put_bits(pb, 24, 0x564342); //magic
381 put_bits(pb, 16, cb->ndimentions); 398 put_bits(pb, 16, cb->ndimentions);
382 put_bits(pb, 24, cb->nentries); 399 put_bits(pb, 24, cb->nentries);
383 400
384 for (i = 1; i < cb->nentries; i++) 401 for (i = 1; i < cb->nentries; i++)
385 if (cb->lens[i] < cb->lens[i-1]) break; 402 if (cb->lens[i] < cb->lens[i-1])
403 break;
386 if (i == cb->nentries) 404 if (i == cb->nentries)
387 ordered = 1; 405 ordered = 1;
388 406
389 put_bits(pb, 1, ordered); 407 put_bits(pb, 1, ordered);
390 if (ordered) { 408 if (ordered) {
392 put_bits(pb, 5, len - 1); 410 put_bits(pb, 5, len - 1);
393 i = 0; 411 i = 0;
394 while (i < cb->nentries) { 412 while (i < cb->nentries) {
395 int j; 413 int j;
396 for (j = 0; j+i < cb->nentries; j++) 414 for (j = 0; j+i < cb->nentries; j++)
397 if (cb->lens[j+i] != len) break; 415 if (cb->lens[j+i] != len)
416 break;
398 put_bits(pb, ilog(cb->nentries - i), j); 417 put_bits(pb, ilog(cb->nentries - i), j);
399 i += j; 418 i += j;
400 len++; 419 len++;
401 } 420 }
402 } else { 421 } else {
403 int sparse = 0; 422 int sparse = 0;
404 for (i = 0; i < cb->nentries; i++) 423 for (i = 0; i < cb->nentries; i++)
405 if (!cb->lens[i]) break; 424 if (!cb->lens[i])
425 break;
406 if (i != cb->nentries) 426 if (i != cb->nentries)
407 sparse = 1; 427 sparse = 1;
408 put_bits(pb, 1, sparse); 428 put_bits(pb, 1, sparse);
409 429
410 for (i = 0; i < cb->nentries; i++) { 430 for (i = 0; i < cb->nentries; i++) {
411 if (sparse) put_bits(pb, 1, !!cb->lens[i]); 431 if (sparse)
412 if (cb->lens[i]) put_bits(pb, 5, cb->lens[i] - 1); 432 put_bits(pb, 1, !!cb->lens[i]);
433 if (cb->lens[i])
434 put_bits(pb, 5, cb->lens[i] - 1);
413 } 435 }
414 } 436 }
415 437
416 put_bits(pb, 4, cb->lookup); 438 put_bits(pb, 4, cb->lookup);
417 if (cb->lookup) { 439 if (cb->lookup) {
430 for (i = 0; i < tmp; i++) 452 for (i = 0; i < tmp; i++)
431 put_bits(pb, bits, cb->quantlist[i]); 453 put_bits(pb, bits, cb->quantlist[i]);
432 } 454 }
433 } 455 }
434 456
435 static void put_floor_header(PutBitContext * pb, vorbis_enc_floor * fc) { 457 static void put_floor_header(PutBitContext * pb, vorbis_enc_floor * fc)
458 {
436 int i; 459 int i;
437 460
438 put_bits(pb, 16, 1); // type, only floor1 is supported 461 put_bits(pb, 16, 1); // type, only floor1 is supported
439 462
440 put_bits(pb, 5, fc->partitions); 463 put_bits(pb, 5, fc->partitions);
462 485
463 for (i = 2; i < fc->values; i++) 486 for (i = 2; i < fc->values; i++)
464 put_bits(pb, fc->rangebits, fc->list[i].x); 487 put_bits(pb, fc->rangebits, fc->list[i].x);
465 } 488 }
466 489
467 static void put_residue_header(PutBitContext * pb, vorbis_enc_residue * rc) { 490 static void put_residue_header(PutBitContext * pb, vorbis_enc_residue * rc)
491 {
468 int i; 492 int i;
469 493
470 put_bits(pb, 16, rc->type); 494 put_bits(pb, 16, rc->type);
471 495
472 put_bits(pb, 24, rc->begin); 496 put_bits(pb, 24, rc->begin);
493 if (rc->books[i][j] != -1) 517 if (rc->books[i][j] != -1)
494 put_bits(pb, 8, rc->books[i][j]); 518 put_bits(pb, 8, rc->books[i][j]);
495 } 519 }
496 } 520 }
497 521
498 static int put_main_header(vorbis_enc_context * venc, uint8_t ** out) { 522 static int put_main_header(vorbis_enc_context * venc, uint8_t ** out)
523 {
499 int i; 524 int i;
500 PutBitContext pb; 525 PutBitContext pb;
501 uint8_t buffer[50000] = {0}, * p = buffer; 526 uint8_t buffer[50000] = {0}, * p = buffer;
502 int buffer_len = sizeof buffer; 527 int buffer_len = sizeof buffer;
503 int len, hlens[3]; 528 int len, hlens[3];
622 } 647 }
623 648
624 return p - *out; 649 return p - *out;
625 } 650 }
626 651
627 static float get_floor_average(vorbis_enc_floor * fc, float * coeffs, int i) { 652 static float get_floor_average(vorbis_enc_floor * fc, float * coeffs, int i)
653 {
628 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x; 654 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
629 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x; 655 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
630 int j; 656 int j;
631 float average = 0; 657 float average = 0;
632 658
633 for (j = begin; j < end; j++) 659 for (j = begin; j < end; j++)
634 average += fabs(coeffs[j]); 660 average += fabs(coeffs[j]);
635 return average / (end - begin); 661 return average / (end - begin);
636 } 662 }
637 663
638 static void floor_fit(vorbis_enc_context * venc, vorbis_enc_floor * fc, float * coeffs, uint_fast16_t * posts, int samples) { 664 static void floor_fit(vorbis_enc_context * venc, vorbis_enc_floor * fc,
665 float * coeffs, uint_fast16_t * posts, int samples)
666 {
639 int range = 255 / fc->multiplier + 1; 667 int range = 255 / fc->multiplier + 1;
640 int i; 668 int i;
641 float tot_average = 0.; 669 float tot_average = 0.;
642 float averages[fc->values]; 670 float averages[fc->values];
643 for (i = 0; i < fc->values; i++){ 671 for (i = 0; i < fc->values; i++){
652 float average = averages[i]; 680 float average = averages[i];
653 int j; 681 int j;
654 682
655 average *= pow(tot_average / average, 0.5) * pow(1.25, position/200.); // MAGIC! 683 average *= pow(tot_average / average, 0.5) * pow(1.25, position/200.); // MAGIC!
656 for (j = 0; j < range - 1; j++) 684 for (j = 0; j < range - 1; j++)
657 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average) break; 685 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
686 break;
658 posts[fc->list[i].sort] = j; 687 posts[fc->list[i].sort] = j;
659 } 688 }
660 } 689 }
661 690
662 static int render_point(int x0, int y0, int x1, int y1, int x) { 691 static int render_point(int x0, int y0, int x1, int y1, int x)
692 {
663 return y0 + (x - x0) * (y1 - y0) / (x1 - x0); 693 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
664 } 694 }
665 695
666 static void floor_encode(vorbis_enc_context * venc, vorbis_enc_floor * fc, PutBitContext * pb, uint_fast16_t * posts, float * floor, int samples) { 696 static void floor_encode(vorbis_enc_context * venc, vorbis_enc_floor * fc,
697 PutBitContext * pb, uint_fast16_t * posts,
698 float * floor, int samples)
699 {
667 int range = 255 / fc->multiplier + 1; 700 int range = 255 / fc->multiplier + 1;
668 int coded[fc->values]; // first 2 values are unused 701 int coded[fc->values]; // first 2 values are unused
669 int i, counter; 702 int i, counter;
670 703
671 put_bits(pb, 1, 1); // non zero 704 put_bits(pb, 1, 1); // non zero
684 int room = FFMIN(highroom, lowroom); 717 int room = FFMIN(highroom, lowroom);
685 if (predicted == posts[i]) { 718 if (predicted == posts[i]) {
686 coded[i] = 0; // must be used later as flag! 719 coded[i] = 0; // must be used later as flag!
687 continue; 720 continue;
688 } else { 721 } else {
689 if (!coded[fc->list[i].low ]) coded[fc->list[i].low ] = -1; 722 if (!coded[fc->list[i].low ])
690 if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1; 723 coded[fc->list[i].low ] = -1;
724 if (!coded[fc->list[i].high])
725 coded[fc->list[i].high] = -1;
691 } 726 }
692 if (posts[i] > predicted) { 727 if (posts[i] > predicted) {
693 if (posts[i] - predicted > room) 728 if (posts[i] - predicted > room)
694 coded[i] = posts[i] - predicted + lowroom; 729 coded[i] = posts[i] - predicted + lowroom;
695 else 730 else
714 for (l = 0; l < csub; l++) { 749 for (l = 0; l < csub; l++) {
715 int maxval = 1; 750 int maxval = 1;
716 if (c->books[l] != -1) 751 if (c->books[l] != -1)
717 maxval = venc->codebooks[c->books[l]].nentries; 752 maxval = venc->codebooks[c->books[l]].nentries;
718 // coded could be -1, but this still works, cause that is 0 753 // coded could be -1, but this still works, cause that is 0
719 if (coded[counter + k] < maxval) break; 754 if (coded[counter + k] < maxval)
755 break;
720 } 756 }
721 assert(l != csub); 757 assert(l != csub);
722 cval |= l << cshift; 758 cval |= l << cshift;
723 cshift += c->subclass; 759 cshift += c->subclass;
724 } 760 }
726 } 762 }
727 for (k = 0; k < c->dim; k++) { 763 for (k = 0; k < c->dim; k++) {
728 int book = c->books[cval & (csub-1)]; 764 int book = c->books[cval & (csub-1)];
729 int entry = coded[counter++]; 765 int entry = coded[counter++];
730 cval >>= c->subclass; 766 cval >>= c->subclass;
731 if (book == -1) continue; 767 if (book == -1)
732 if (entry == -1) entry = 0; 768 continue;
769 if (entry == -1)
770 entry = 0;
733 put_codeword(pb, &venc->codebooks[book], entry); 771 put_codeword(pb, &venc->codebooks[book], entry);
734 } 772 }
735 } 773 }
736 774
737 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded, fc->multiplier, floor, samples); 775 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded, fc->multiplier, floor, samples);
738 } 776 }
739 777
740 static float * put_vector(vorbis_enc_codebook * book, PutBitContext * pb, float * num) { 778 static float * put_vector(vorbis_enc_codebook * book, PutBitContext * pb, float * num)
779 {
741 int i, entry = -1; 780 int i, entry = -1;
742 float distance = FLT_MAX; 781 float distance = FLT_MAX;
743 assert(book->dimentions); 782 assert(book->dimentions);
744 for (i = 0; i < book->nentries; i++) { 783 for (i = 0; i < book->nentries; i++) {
745 float * vec = book->dimentions + i * book->ndimentions, d = book->pow2[i]; 784 float * vec = book->dimentions + i * book->ndimentions, d = book->pow2[i];
746 int j; 785 int j;
747 if (!book->lens[i]) continue; 786 if (!book->lens[i])
787 continue;
748 for (j = 0; j < book->ndimentions; j++) 788 for (j = 0; j < book->ndimentions; j++)
749 d -= vec[j] * num[j]; 789 d -= vec[j] * num[j];
750 if (distance > d) { 790 if (distance > d) {
751 entry = i; 791 entry = i;
752 distance = d; 792 distance = d;
754 } 794 }
755 put_codeword(pb, book, entry); 795 put_codeword(pb, book, entry);
756 return &book->dimentions[entry * book->ndimentions]; 796 return &book->dimentions[entry * book->ndimentions];
757 } 797 }
758 798
759 static void residue_encode(vorbis_enc_context * venc, vorbis_enc_residue * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) { 799 static void residue_encode(vorbis_enc_context * venc, vorbis_enc_residue * rc,
800 PutBitContext * pb, float * coeffs, int samples,
801 int real_ch)
802 {
760 int pass, i, j, p, k; 803 int pass, i, j, p, k;
761 int psize = rc->partition_size; 804 int psize = rc->partition_size;
762 int partitions = (rc->end - rc->begin) / psize; 805 int partitions = (rc->end - rc->begin) / psize;
763 int channels = (rc->type == 2) ? 1 : real_ch; 806 int channels = (rc->type == 2) ? 1 : real_ch;
764 int classes[channels][partitions]; 807 int classes[channels][partitions];
772 for (k = s; k < s + psize; k += 2) { 815 for (k = s; k < s + psize; k += 2) {
773 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch])); 816 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
774 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch])); 817 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
775 } 818 }
776 819
777 for (i = 0; i < rc->classifications - 1; i++) { 820 for (i = 0; i < rc->classifications - 1; i++)
778 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1]) break; 821 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
779 } 822 break;
780 classes[0][p] = i; 823 classes[0][p] = i;
781 } 824 }
782 825
783 for (pass = 0; pass < 8; pass++) { 826 for (pass = 0; pass < 8; pass++) {
784 p = 0; 827 p = 0;
796 for (i = 0; i < classwords && p < partitions; i++, p++) { 839 for (i = 0; i < classwords && p < partitions; i++, p++) {
797 for (j = 0; j < channels; j++) { 840 for (j = 0; j < channels; j++) {
798 int nbook = rc->books[classes[j][p]][pass]; 841 int nbook = rc->books[classes[j][p]][pass];
799 vorbis_enc_codebook * book = &venc->codebooks[nbook]; 842 vorbis_enc_codebook * book = &venc->codebooks[nbook];
800 float * buf = coeffs + samples*j + rc->begin + p*psize; 843 float * buf = coeffs + samples*j + rc->begin + p*psize;
801 if (nbook == -1) continue; 844 if (nbook == -1)
845 continue;
802 846
803 assert(rc->type == 0 || rc->type == 2); 847 assert(rc->type == 0 || rc->type == 2);
804 assert(!(psize % book->ndimentions)); 848 assert(!(psize % book->ndimentions));
805 849
806 if (rc->type == 0) { 850 if (rc->type == 0) {
839 } 883 }
840 } 884 }
841 } 885 }
842 } 886 }
843 887
844 static int apply_window_and_mdct(vorbis_enc_context * venc, signed short * audio, int samples) { 888 static int apply_window_and_mdct(vorbis_enc_context * venc,
889 signed short * audio, int samples)
890 {
845 int i, j, channel; 891 int i, j, channel;
846 const float * win = venc->win[0]; 892 const float * win = venc->win[0];
847 int window_len = 1 << (venc->log2_blocksize[0] - 1); 893 int window_len = 1 << (venc->log2_blocksize[0] - 1);
848 float n = (float)(1 << venc->log2_blocksize[0]) / 4.; 894 float n = (float)(1 << venc->log2_blocksize[0]) / 4.;
849 // FIXME use dsp 895 // FIXME use dsp
850 896
851 if (!venc->have_saved && !samples) return 0; 897 if (!venc->have_saved && !samples) return 0;
852 898
853 if (venc->have_saved) { 899 if (venc->have_saved)
854 for (channel = 0; channel < venc->channels; channel++) { 900 for (channel = 0; channel < venc->channels; channel++)
855 memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len); 901 memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
856 } 902 else
857 } else { 903 for (channel = 0; channel < venc->channels; channel++)
858 for (channel = 0; channel < venc->channels; channel++) {
859 memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len); 904 memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
860 }
861 }
862 905
863 if (samples) { 906 if (samples) {
864 for (channel = 0; channel < venc->channels; channel++) { 907 for (channel = 0; channel < venc->channels; channel++) {
865 float * offset = venc->samples + channel*window_len*2 + window_len; 908 float * offset = venc->samples + channel*window_len*2 + window_len;
866 j = channel; 909 j = channel;
867 for (i = 0; i < samples; i++, j += venc->channels) 910 for (i = 0; i < samples; i++, j += venc->channels)
868 offset[i] = -audio[j] / 32768. / n * win[window_len - i - 1]; //FIXME find out why the sign has to be fliped 911 offset[i] = -audio[j] / 32768. / n * win[window_len - i - 1]; //FIXME find out why the sign has to be fliped
869 } 912 }
870 } else { 913 } else {
871 for (channel = 0; channel < venc->channels; channel++) { 914 for (channel = 0; channel < venc->channels; channel++)
872 memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len); 915 memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
873 } 916 }
874 } 917
875 918 for (channel = 0; channel < venc->channels; channel++)
876 for (channel = 0; channel < venc->channels; channel++) {
877 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2); 919 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2);
878 }
879 920
880 if (samples) { 921 if (samples) {
881 for (channel = 0; channel < venc->channels; channel++) { 922 for (channel = 0; channel < venc->channels; channel++) {
882 float * offset = venc->saved + channel*window_len; 923 float * offset = venc->saved + channel*window_len;
883 j = channel; 924 j = channel;
926 vorbis_enc_mode * mode; 967 vorbis_enc_mode * mode;
927 vorbis_enc_mapping * mapping; 968 vorbis_enc_mapping * mapping;
928 PutBitContext pb; 969 PutBitContext pb;
929 int i; 970 int i;
930 971
931 if (!apply_window_and_mdct(venc, audio, samples)) return 0; 972 if (!apply_window_and_mdct(venc, audio, samples))
973 return 0;
932 samples = 1 << (venc->log2_blocksize[0] - 1); 974 samples = 1 << (venc->log2_blocksize[0] - 1);
933 975
934 init_put_bits(&pb, packets, buf_size); 976 init_put_bits(&pb, packets, buf_size);
935 977
936 put_bits(&pb, 1, 0); // magic bit 978 put_bits(&pb, 1, 0); // magic bit
949 uint_fast16_t posts[fc->values]; 991 uint_fast16_t posts[fc->values];
950 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples); 992 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
951 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples); 993 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
952 } 994 }
953 995
954 for (i = 0; i < venc->channels * samples; i++) { 996 for (i = 0; i < venc->channels * samples; i++)
955 venc->coeffs[i] /= venc->floor[i]; 997 venc->coeffs[i] /= venc->floor[i];
956 }
957 998
958 for (i = 0; i < mapping->coupling_steps; i++) { 999 for (i = 0; i < mapping->coupling_steps; i++) {
959 float * mag = venc->coeffs + mapping->magnitude[i] * samples; 1000 float * mag = venc->coeffs + mapping->magnitude[i] * samples;
960 float * ang = venc->coeffs + mapping->angle[i] * samples; 1001 float * ang = venc->coeffs + mapping->angle[i] * samples;
961 int j; 1002 int j;
962 for (j = 0; j < samples; j++) { 1003 for (j = 0; j < samples; j++) {
963 float a = ang[j]; 1004 float a = ang[j];
964 ang[j] -= mag[j]; 1005 ang[j] -= mag[j];
965 if (mag[j] > 0) ang[j] = -ang[j]; 1006 if (mag[j] > 0)
966 if (ang[j] < 0) mag[j] = a; 1007 ang[j] = -ang[j];
1008 if (ang[j] < 0)
1009 mag[j] = a;
967 } 1010 }
968 } 1011 }
969 1012
970 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels); 1013 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
971 1014