1 /*
2 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Native Vorbis encoder.
24 * @author Oded Shimon <ods15@ods15.dyndns.org>
25 */
26
28
35
36 #define BITSTREAM_WRITER_LE
38
39 #undef NDEBUG
40 #include <assert.h>
41
55
62
73
84
94
99
109 float *
floor;
// also used for tmp values for mdct
110 float *
coeffs;
// also used for residue after floor
112
115
118
121
124
127
130
131 #define MAX_CHANNELS 2
132 #define MAX_CODEBOOK_DIM 8
133
134 #define MAX_FLOOR_CLASS_DIM 4
135 #define NUM_FLOOR_PARTITIONS 8
136 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
137
138 #define RESIDUE_SIZE 1600
139 #define RESIDUE_PART_SIZE 32
140 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
141
143 int entry)
144 {
151 return 0;
152 }
153
155 {
156 if (lookup == 1)
158 else if (lookup == 2)
159 return dimensions *entries;
160 return 0;
161 }
162
164 {
165 int i;
166
168
171 } else {
177 for (i = 0; i < cb->
nentries; i++) {
178 float last = 0;
179 int j;
180 int div = 1;
182 int off;
184 off = (i / div) % vals; // lookup type 1
185 else
187
192 div *= vals;
193 }
195 }
196 }
197 return 0;
198 }
199
201 {
202 int i;
208 int j;
210 for (j = 0; j < 8; j++)
211 if (rc->
books[i][j] != -1)
212 break;
213 if (j == 8) // zero
214 continue;
218
219 for (j = 0; j < cb->
nentries; j++) {
222 continue;
224 if (a > rc->
maxes[i][0])
227 if (a > rc->
maxes[i][1])
229 }
230 }
231 // small bias
233 rc->
maxes[i][0] += 0.8;
234 rc->
maxes[i][1] += 0.8;
235 }
236 return 0;
237 }
238
241 {
246
250
255
256 // codebook 0..14 - floor1 book, values 0..255
257 // codebook 15 residue masterbook
258 // codebook 16..29 residue
259 for (book = 0; book < venc->
ncodebooks; book++) {
261 int vals;
268
275
281 for (i = 0; i < vals; i++)
283 } else {
285 }
288 }
289
294
295 // just 1 floor
303 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
306 }
311 for (i = 0; i < fc->
nclasses; i++) {
313 int j, books;
321 for (j = 0; j < books; j++)
323 }
326
330
336 for (i = 2; i < fc->
values; i++) {
337 static const int a[] = {
338 93, 23,372, 6, 46,186,750, 14, 33, 65,
339 130,260,556, 3, 10, 18, 28, 39, 55, 79,
340 111,158,220,312,464,650,850
341 };
342 fc->
list[i].
x = a[i - 2];
343 }
346
351
352 // single residue
363 {
364 static const int8_t
a[10][8] = {
365 { -1, -1, -1, -1, -1, -1, -1, -1, },
366 { -1, -1, 16, -1, -1, -1, -1, -1, },
367 { -1, -1, 17, -1, -1, -1, -1, -1, },
368 { -1, -1, 18, -1, -1, -1, -1, -1, },
369 { -1, -1, 19, -1, -1, -1, -1, -1, },
370 { -1, -1, 20, -1, -1, -1, -1, -1, },
371 { -1, -1, 21, -1, -1, -1, -1, -1, },
372 { 22, 23, -1, -1, -1, -1, -1, -1, },
373 { 24, 25, -1, -1, -1, -1, -1, -1, },
374 { 26, 27, 28, -1, -1, -1, -1, -1, },
375 };
376 memcpy(rc->
books, a,
sizeof a);
377 }
380
385
386 // single mapping
392 for (i = 0; i < venc->
channels; i++)
398 for (i = 0; i < mc->
submaps; i++) {
401 }
410 }
411
416
417 // single mode
420
428
431
436
437 return 0;
438 }
439
441 {
442 int exp, mant;
443 uint32_t res = 0;
444 mant = (int)ldexp(frexp(f, &exp), 20);
445 exp += 788 - 20;
446 if (mant < 0) {
448 mant = -mant;
449 }
450 res |= mant | (exp << 21);
452 }
453
455 {
456 int i;
457 int ordered = 0;
458
462
465 break;
467 ordered = 1;
468
470 if (ordered) {
473 i = 0;
474 while (i < cb->nentries) {
475 int j;
476 for (j = 0; j+i < cb->
nentries; j++)
477 if (cb->
lens[j+i] != len)
478 break;
480 i += j;
481 len++;
482 }
483 } else {
484 int sparse = 0;
487 break;
489 sparse = 1;
491
492 for (i = 0; i < cb->
nentries; i++) {
493 if (sparse)
497 }
498 }
499
504
505 for (i = 1; i < tmp; i++)
507
510
513
514 for (i = 0; i < tmp; i++)
516 }
517 }
518
520 {
521 int i;
522
523 put_bits(pb, 16, 1);
// type, only floor1 is supported
524
526
529
530 for (i = 0; i < fc->
nclasses; i++) {
531 int j, books;
532
535
538
540
541 for (j = 0; j < books; j++)
543 }
544
547
548 for (i = 2; i < fc->
values; i++)
550 }
551
553 {
554 int i;
555
557
563
565 int j, tmp = 0;
566 for (j = 0; j < 8; j++)
567 tmp |= (rc->
books[i][j] != -1) << j;
568
571
572 if (tmp > 7)
574 }
575
577 int j;
578 for (j = 0; j < 8; j++)
579 if (rc->
books[i][j] != -1)
581 }
582 }
583
585 {
586 int i;
589 int buffer_len = 50000;
591 if (!buffer)
593
594 // identification header
597 for (i = 0; "vorbis"[i]; i++)
608
611 buffer_len -= hlens[0];
612 p += hlens[0];
613
614 // comment header
617 for (i = 0; "vorbis"[i]; i++)
622
625 buffer_len -= hlens[1];
626 p += hlens[1];
627
628 // setup header
631 for (i = 0; "vorbis"[i]; i++)
633
634 // codebooks
638
639 // time domain, reserved, zero
642
643 // floors
645 for (i = 0; i < venc->
nfloors; i++)
647
648 // residues
652
653 // mappings
657 int j;
658 put_bits(&pb, 16, 0);
// mapping type
659
663
670 }
671 }
672
674
676 for (j = 0; j < venc->
channels; j++)
678
679 for (j = 0; j < mc->
submaps; j++) {
680 put_bits(&pb, 8, 0);
// reserved time configuration
683 }
684 }
685
686 // modes
688 for (i = 0; i < venc->
nmodes; i++) {
690 put_bits(&pb, 16, 0);
// reserved window type
691 put_bits(&pb, 16, 0);
// reserved transform type
693 }
694
696
699
700 len = hlens[0] + hlens[1] + hlens[2];
702 if (!p)
704
705 *p++ = 2;
708 buffer_len = 0;
709 for (i = 0; i < 3; i++) {
710 memcpy(p, buffer + buffer_len, hlens[i]);
711 p += hlens[i];
712 buffer_len += hlens[i];
713 }
714
717 }
718
720 {
723 int j;
724 float average = 0;
725
726 for (j = begin; j <
end; j++)
727 average += fabs(coeffs[j]);
728 return average / (end - begin);
729 }
730
732 float *coeffs, uint16_t *posts, int samples)
733 {
735 int i;
736 float tot_average = 0.0;
738 for (i = 0; i < fc->
values; i++) {
740 tot_average += averages[i];
741 }
742 tot_average /= fc->
values;
744
745 for (i = 0; i < fc->
values; i++) {
747 float average = averages[i];
748 int j;
749
750 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
751 for (j = 0; j < range - 1; j++)
753 break;
755 }
756 }
757
759 {
760 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
761 }
762
765 float *floor, int samples)
766 {
769 int i, counter;
770
776 coded[0] = coded[1] = 1;
777
778 for (i = 2; i < fc->
values; i++) {
784 int highroom = range - predicted;
785 int lowroom = predicted;
786 int room =
FFMIN(highroom, lowroom);
787 if (predicted == posts[i]) {
788 coded[i] = 0; // must be used later as flag!
789 continue;
790 } else {
795 }
796 if (posts[i] > predicted) {
797 if (posts[i] - predicted > room)
798 coded[i] = posts[i] - predicted + lowroom;
799 else
800 coded[i] = (posts[i] - predicted) << 1;
801 } else {
802 if (predicted - posts[i] > room)
803 coded[i] = predicted - posts[i] + highroom - 1;
804 else
805 coded[i] = ((predicted - posts[i]) << 1) - 1;
806 }
807 }
808
809 counter = 2;
812 int k, cval = 0, csub = 1<<c->
subclass;
815 int cshift = 0;
816 for (k = 0; k < c->
dim; k++) {
817 int l;
818 for (l = 0; l < csub; l++) {
819 int maxval = 1;
820 if (c->
books[l] != -1)
822 // coded could be -1, but this still works, cause that is 0
823 if (coded[counter + k] < maxval)
824 break;
825 }
826 assert(l != csub);
827 cval |= l << cshift;
829 }
832 }
833 for (k = 0; k < c->
dim; k++) {
834 int book = c->
books[cval & (csub-1)];
835 int entry = coded[counter++];
837 if (book == -1)
838 continue;
839 if (entry == -1)
840 entry = 0;
843 }
844 }
845
848
849 return 0;
850 }
851
853 float *num)
854 {
855 int i, entry = -1;
858 for (i = 0; i < book->
nentries; i++) {
860 int j;
862 continue;
864 d -= vec[j] * num[j];
865 if (distance > d) {
866 entry = i;
867 distance = d;
868 }
869 }
873 }
874
877 int real_ch)
878 {
879 int pass, i, j, p, k;
881 int partitions = (rc->
end - rc->
begin) / psize;
882 int channels = (rc->
type == 2) ? 1 : real_ch;
885
888 for (p = 0; p < partitions; p++) {
889 float max1 = 0.0, max2 = 0.0;
890 int s = rc->
begin + p * psize;
891 for (k = s; k < s + psize; k += 2) {
892 max1 =
FFMAX(max1, fabs(coeffs[ k / real_ch]));
893 max2 =
FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
894 }
895
897 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
898 break;
899 classes[0][p] = i;
900 }
901
902 for (pass = 0; pass < 8; pass++) {
903 p = 0;
904 while (p < partitions) {
905 if (pass == 0)
906 for (j = 0; j < channels; j++) {
908 int entry = 0;
909 for (i = 0; i < classwords; i++) {
911 entry += classes[j][p + i];
912 }
915 }
916 for (i = 0; i < classwords && p < partitions; i++, p++) {
917 for (j = 0; j < channels; j++) {
918 int nbook = rc->
books[classes[j][p]][
pass];
920 float *
buf = coeffs + samples*j + rc->
begin + p*psize;
921 if (nbook == -1)
922 continue;
923
924 assert(rc->
type == 0 || rc->
type == 2);
926
929 int l;
931 if (!a)
934 buf[k + l] -= a[l];
935 }
936 } else {
937 int s = rc->
begin + p * psize,
a1, b1;
938 a1 = (s % real_ch) * samples;
939 b1 = s / real_ch;
940 s = real_ch * samples;
945 *pv++ = coeffs[a2 + b2];
946 if ((a2 += samples) ==
s) {
947 a2 = 0;
948 b2++;
949 }
950 }
952 if (!pv)
955 coeffs[a1 + b1] -= *pv++;
956 if ((a1 += samples) == s) {
957 a1 = 0;
958 b1++;
959 }
960 }
961 }
962 }
963 }
964 }
965 }
966 }
967 return 0;
968 }
969
971 float **audio, int samples)
972 {
973 int i, channel;
974 const float * win = venc->
win[0];
977 // FIXME use dsp
978
980 return 0;
981
983 for (channel = 0; channel < venc->
channels; channel++)
984 memcpy(venc->
samples + channel * window_len * 2,
985 venc->
saved + channel * window_len,
sizeof(
float) * window_len);
986 } else {
987 for (channel = 0; channel < venc->
channels; channel++)
988 memset(venc->
samples + channel * window_len * 2, 0,
989 sizeof(float) * window_len);
990 }
991
992 if (samples) {
993 for (channel = 0; channel < venc->
channels; channel++) {
994 float *
offset = venc->
samples + channel*window_len*2 + window_len;
995 for (i = 0; i < samples; i++)
996 offset[i] = audio[channel][i] / n * win[window_len - i - 1];
997 }
998 } else {
999 for (channel = 0; channel < venc->
channels; channel++)
1000 memset(venc->
samples + channel * window_len * 2 + window_len,
1001 0, sizeof(float) * window_len);
1002 }
1003
1004 for (channel = 0; channel < venc->
channels; channel++)
1006 venc->
samples + channel * window_len * 2);
1007
1008 if (samples) {
1009 for (channel = 0; channel < venc->
channels; channel++) {
1010 float *
offset = venc->
saved + channel * window_len;
1011 for (i = 0; i < samples; i++)
1012 offset[i] = audio[channel][i] / n * win[i];
1013 }
1015 } else {
1017 }
1018 return 1;
1019 }
1020
1023 {
1031
1033 return 0;
1035
1038
1040
1044 }
1045
1047
1049
1052 if (
mode->blockflag) {
1055 }
1056
1057 for (i = 0; i < venc->
channels; i++) {
1064 }
1065 }
1066
1067 for (i = 0; i < venc->
channels * samples; i++)
1069
1070 for (i = 0; i < mapping->coupling_steps; i++) {
1071 float *mag = venc->
coeffs + mapping->magnitude[i] * samples;
1072 float *ang = venc->
coeffs + mapping->angle[i] * samples;
1073 int j;
1074 for (j = 0; j < samples; j++) {
1076 ang[j] -= mag[j];
1077 if (mag[j] > 0)
1078 ang[j] = -ang[j];
1079 if (ang[j] < 0)
1081 }
1082 }
1083
1088 }
1089
1092
1094 if (frame) {
1097 } else {
1099 }
1102
1103 *got_packet_ptr = 1;
1104 return 0;
1105 }
1106
1107
1109 {
1111 int i;
1112
1120 }
1122
1124 for (i = 0; i < venc->
nfloors; i++) {
1125 int j;
1132 }
1134
1139 }
1141
1149 }
1151
1153
1158
1161
1163
1164 return 0 ;
1165 }
1166
1168 {
1171
1173 av_log(avctx,
AV_LOG_ERROR,
"Current FFmpeg Vorbis encoder only supports 2 channels.\n");
1174 return -1;
1175 }
1176
1178 goto error;
1179
1183 else
1186
1188 goto error;
1190
1192
1193 return 0;
1194 error:
1197 }
1198
1211 };