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
27 #include <float.h>
33
34 #define BITSTREAM_WRITER_LE
36
37 #undef NDEBUG
38 #include <assert.h>
39
53
60
71
82
92
97
101 int log2_blocksize[2];
107 float *
floor;
// also used for tmp values for mdct
108 float *
coeffs;
// also used for residue after floor
110
113
116
119
122
125
128
129 #define MAX_CHANNELS 2
130 #define MAX_CODEBOOK_DIM 8
131
132 #define MAX_FLOOR_CLASS_DIM 4
133 #define NUM_FLOOR_PARTITIONS 8
134 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
135
136 #define RESIDUE_SIZE 1600
137 #define RESIDUE_PART_SIZE 32
138 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
139
141 int entry)
142 {
149 return 0;
150 }
151
153 {
154 if (lookup == 1)
156 else if (lookup == 2)
157 return dimensions *entries;
158 return 0;
159 }
160
162 {
163 int i;
164
166
169 } else {
175 for (i = 0; i < cb->
nentries; i++) {
176 float last = 0;
177 int j;
178 int div = 1;
182 off = (i / div) % vals; // lookup type 1
183 else
185
190 div *= vals;
191 }
193 }
194 }
195 return 0;
196 }
197
199 {
200 int i;
206 int j;
208 for (j = 0; j < 8; j++)
209 if (rc->
books[i][j] != -1)
210 break;
211 if (j == 8) // zero
212 continue;
216
217 for (j = 0; j < cb->
nentries; j++) {
220 continue;
222 if (a > rc->
maxes[i][0])
225 if (a > rc->
maxes[i][1])
227 }
228 }
229 // small bias
231 rc->
maxes[i][0] += 0.8;
232 rc->
maxes[i][1] += 0.8;
233 }
234 return 0;
235 }
236
239 {
244
248
253
254 // codebook 0..14 - floor1 book, values 0..255
255 // codebook 15 residue masterbook
256 // codebook 16..29 residue
257 for (book = 0; book < venc->
ncodebooks; book++) {
259 int vals;
266
273
279 for (i = 0; i < vals; i++)
281 } else {
283 }
286 }
287
292
293 // just 1 floor
301 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
304 }
309 for (i = 0; i < fc->
nclasses; i++) {
311 int j, books;
319 for (j = 0; j < books; j++)
321 }
324
328
334 for (i = 2; i < fc->
values; i++) {
335 static const int a[] = {
336 93, 23,372, 6, 46,186,750, 14, 33, 65,
337 130,260,556, 3, 10, 18, 28, 39, 55, 79,
338 111,158,220,312,464,650,850
339 };
340 fc->
list[i].
x = a[i - 2];
341 }
344
349
350 // single residue
361 {
362 static const int8_t
a[10][8] = {
363 { -1, -1, -1, -1, -1, -1, -1, -1, },
364 { -1, -1, 16, -1, -1, -1, -1, -1, },
365 { -1, -1, 17, -1, -1, -1, -1, -1, },
366 { -1, -1, 18, -1, -1, -1, -1, -1, },
367 { -1, -1, 19, -1, -1, -1, -1, -1, },
368 { -1, -1, 20, -1, -1, -1, -1, -1, },
369 { -1, -1, 21, -1, -1, -1, -1, -1, },
370 { 22, 23, -1, -1, -1, -1, -1, -1, },
371 { 24, 25, -1, -1, -1, -1, -1, -1, },
372 { 26, 27, 28, -1, -1, -1, -1, -1, },
373 };
374 memcpy(rc->
books, a,
sizeof a);
375 }
378
383
384 // single mapping
390 for (i = 0; i < venc->
channels; i++)
396 for (i = 0; i < mc->
submaps; i++) {
399 }
408 }
409
414
415 // single mode
418
426
429
434
435 return 0;
436 }
437
439 {
440 int exp, mant;
442 mant = (int)ldexp(frexp(f, &exp), 20);
443 exp += 788 - 20;
444 if (mant < 0) {
446 mant = -mant;
447 }
448 res |= mant | (exp << 21);
450 }
451
453 {
454 int i;
455 int ordered = 0;
456
460
463 break;
465 ordered = 1;
466
468 if (ordered) {
471 i = 0;
472 while (i < cb->nentries) {
473 int j;
474 for (j = 0; j+i < cb->
nentries; j++)
475 if (cb->
lens[j+i] != len)
476 break;
478 i += j;
479 len++;
480 }
481 } else {
482 int sparse = 0;
485 break;
487 sparse = 1;
489
490 for (i = 0; i < cb->
nentries; i++) {
491 if (sparse)
495 }
496 }
497
502
503 for (i = 1; i < tmp; i++)
505
508
511
512 for (i = 0; i < tmp; i++)
514 }
515 }
516
518 {
519 int i;
520
521 put_bits(pb, 16, 1);
// type, only floor1 is supported
522
524
527
528 for (i = 0; i < fc->
nclasses; i++) {
529 int j, books;
530
533
536
538
539 for (j = 0; j < books; j++)
541 }
542
545
546 for (i = 2; i < fc->
values; i++)
548 }
549
551 {
552 int i;
553
555
561
563 int j, tmp = 0;
564 for (j = 0; j < 8; j++)
565 tmp |= (rc->
books[i][j] != -1) << j;
566
569
570 if (tmp > 7)
572 }
573
575 int j;
576 for (j = 0; j < 8; j++)
577 if (rc->
books[i][j] != -1)
579 }
580 }
581
583 {
584 int i;
587 int buffer_len =
sizeof buffer;
589
590 // identification header
593 for (i = 0; "vorbis"[i]; i++)
604
607 buffer_len -= hlens[0];
608 p += hlens[0];
609
610 // comment header
613 for (i = 0; "vorbis"[i]; i++)
618
621 buffer_len -= hlens[1];
622 p += hlens[1];
623
624 // setup header
627 for (i = 0; "vorbis"[i]; i++)
629
630 // codebooks
634
635 // time domain, reserved, zero
638
639 // floors
641 for (i = 0; i < venc->
nfloors; i++)
643
644 // residues
648
649 // mappings
653 int j;
654 put_bits(&pb, 16, 0);
// mapping type
655
659
666 }
667 }
668
670
672 for (j = 0; j < venc->
channels; j++)
674
675 for (j = 0; j < mc->
submaps; j++) {
676 put_bits(&pb, 8, 0);
// reserved time configuration
679 }
680 }
681
682 // modes
684 for (i = 0; i < venc->
nmodes; i++) {
686 put_bits(&pb, 16, 0);
// reserved window type
687 put_bits(&pb, 16, 0);
// reserved transform type
689 }
690
692
695
696 len = hlens[0] + hlens[1] + hlens[2];
698 if (!p)
700
701 *p++ = 2;
704 buffer_len = 0;
705 for (i = 0; i < 3; i++) {
706 memcpy(p, buffer + buffer_len, hlens[i]);
707 p += hlens[i];
708 buffer_len += hlens[i];
709 }
710
712 }
713
715 {
718 int j;
719 float average = 0;
720
721 for (j = begin; j <
end; j++)
722 average += fabs(coeffs[j]);
723 return average / (end - begin);
724 }
725
727 float *
coeffs, uint16_t *posts,
int samples)
728 {
730 int i;
731 float tot_average = 0.0;
733 for (i = 0; i < fc->
values; i++) {
735 tot_average += averages[i];
736 }
737 tot_average /= fc->
values;
739
740 for (i = 0; i < fc->
values; i++) {
742 float average = averages[i];
743 int j;
744
745 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
746 for (j = 0; j < range - 1; j++)
748 break;
750 }
751 }
752
754 {
755 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
756 }
757
760 float *floor, int samples)
761 {
764 int i, counter;
765
771 coded[0] = coded[1] = 1;
772
773 for (i = 2; i < fc->
values; i++) {
779 int highroom = range - predicted;
780 int lowroom = predicted;
781 int room =
FFMIN(highroom, lowroom);
782 if (predicted == posts[i]) {
783 coded[i] = 0; // must be used later as flag!
784 continue;
785 } else {
790 }
791 if (posts[i] > predicted) {
792 if (posts[i] - predicted > room)
793 coded[i] = posts[i] - predicted + lowroom;
794 else
795 coded[i] = (posts[i] - predicted) << 1;
796 } else {
797 if (predicted - posts[i] > room)
798 coded[i] = predicted - posts[i] + highroom - 1;
799 else
800 coded[i] = ((predicted - posts[i]) << 1) - 1;
801 }
802 }
803
804 counter = 2;
807 int k, cval = 0, csub = 1<<c->
subclass;
810 int cshift = 0;
811 for (k = 0; k < c->
dim; k++) {
812 int l;
813 for (l = 0; l < csub; l++) {
814 int maxval = 1;
815 if (c->
books[l] != -1)
817 // coded could be -1, but this still works, cause that is 0
818 if (coded[counter + k] < maxval)
819 break;
820 }
821 assert(l != csub);
822 cval |= l << cshift;
824 }
827 }
828 for (k = 0; k < c->
dim; k++) {
829 int book = c->
books[cval & (csub-1)];
830 int entry = coded[counter++];
832 if (book == -1)
833 continue;
834 if (entry == -1)
835 entry = 0;
838 }
839 }
840
843
844 return 0;
845 }
846
848 float *num)
849 {
850 int i, entry = -1;
853 for (i = 0; i < book->
nentries; i++) {
855 int j;
857 continue;
859 d -= vec[j] * num[j];
860 if (distance > d) {
861 entry = i;
862 distance = d;
863 }
864 }
866 return NULL;
868 }
869
872 int real_ch)
873 {
874 int pass, i, j, p, k;
876 int partitions = (rc->
end - rc->
begin) / psize;
877 int channels = (rc->
type == 2) ? 1 : real_ch;
880
883 for (p = 0; p < partitions; p++) {
884 float max1 = 0.0, max2 = 0.0;
885 int s = rc->
begin + p * psize;
886 for (k = s; k < s + psize; k += 2) {
887 max1 =
FFMAX(max1, fabs(coeffs[ k / real_ch]));
888 max2 =
FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
889 }
890
892 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
893 break;
894 classes[0][p] = i;
895 }
896
897 for (pass = 0; pass < 8; pass++) {
898 p = 0;
899 while (p < partitions) {
900 if (pass == 0)
901 for (j = 0; j < channels; j++) {
903 int entry = 0;
904 for (i = 0; i < classwords; i++) {
906 entry += classes[j][p + i];
907 }
910 }
911 for (i = 0; i < classwords && p < partitions; i++, p++) {
912 for (j = 0; j < channels; j++) {
913 int nbook = rc->
books[classes[j][p]][
pass];
915 float *
buf = coeffs + samples*j + rc->
begin + p*psize;
916 if (nbook == -1)
917 continue;
918
919 assert(rc->
type == 0 || rc->
type == 2);
921
924 int l;
926 if (!a)
929 buf[k + l] -= a[l];
930 }
931 } else {
932 int s = rc->
begin + p * psize,
a1, b1;
933 a1 = (s % real_ch) * samples;
934 b1 = s / real_ch;
935 s = real_ch * samples;
940 *pv++ = coeffs[a2 + b2];
941 if ((a2 += samples) ==
s) {
942 a2 = 0;
943 b2++;
944 }
945 }
947 if (!pv)
950 coeffs[a1 + b1] -= *pv++;
951 if ((a1 += samples) == s) {
952 a1 = 0;
953 b1++;
954 }
955 }
956 }
957 }
958 }
959 }
960 }
961 }
962 return 0;
963 }
964
966 float **audio, int samples)
967 {
968 int i, channel;
969 const float * win = venc->
win[0];
972 // FIXME use dsp
973
975 return 0;
976
978 for (channel = 0; channel < venc->
channels; channel++)
979 memcpy(venc->
samples + channel * window_len * 2,
980 venc->
saved + channel * window_len,
sizeof(
float) * window_len);
981 } else {
982 for (channel = 0; channel < venc->
channels; channel++)
983 memset(venc->
samples + channel * window_len * 2, 0,
984 sizeof(float) * window_len);
985 }
986
987 if (samples) {
988 for (channel = 0; channel < venc->
channels; channel++) {
989 float *
offset = venc->
samples + channel*window_len*2 + window_len;
990 for (i = 0; i < samples; i++)
991 offset[i] = audio[channel][i] / n * win[window_len - i - 1];
992 }
993 } else {
994 for (channel = 0; channel < venc->
channels; channel++)
995 memset(venc->
samples + channel * window_len * 2 + window_len,
996 0, sizeof(float) * window_len);
997 }
998
999 for (channel = 0; channel < venc->
channels; channel++)
1001 venc->
samples + channel * window_len * 2);
1002
1003 if (samples) {
1004 for (channel = 0; channel < venc->
channels; channel++) {
1005 float *
offset = venc->
saved + channel * window_len;
1006 for (i = 0; i < samples; i++)
1007 offset[i] = audio[channel][i] / n * win[i];
1008 }
1010 } else {
1012 }
1013 return 1;
1014 }
1015
1018 {
1020 float **audio = frame ? (
float **)frame->
extended_data : NULL;
1026
1028 return 0;
1030
1033
1035
1039 }
1040
1042
1044
1047 if (
mode->blockflag) {
1050 }
1051
1052 for (i = 0; i < venc->
channels; i++) {
1059 }
1060 }
1061
1062 for (i = 0; i < venc->
channels * samples; i++)
1064
1065 for (i = 0; i < mapping->coupling_steps; i++) {
1066 float *mag = venc->
coeffs + mapping->magnitude[i] * samples;
1067 float *ang = venc->
coeffs + mapping->angle[i] * samples;
1068 int j;
1069 for (j = 0; j < samples; j++) {
1071 ang[j] -= mag[j];
1072 if (mag[j] > 0)
1073 ang[j] = -ang[j];
1074 if (ang[j] < 0)
1076 }
1077 }
1078
1083 }
1084
1087
1089 if (frame) {
1092 } else
1096
1097 *got_packet_ptr = 1;
1098 return 0;
1099 }
1100
1101
1103 {
1105 int i;
1106
1114 }
1116
1118 for (i = 0; i < venc->
nfloors; i++) {
1119 int j;
1126 }
1128
1133 }
1135
1143 }
1145
1147
1152
1155
1157
1158 return 0 ;
1159 }
1160
1162 {
1165
1167 av_log(avctx,
AV_LOG_ERROR,
"Current FFmpeg Vorbis encoder only supports 2 channels.\n");
1168 return -1;
1169 }
1170
1172 goto error;
1173
1177 else
1180
1182 goto error;
1184
1186
1187 return 0;
1188 error:
1191 }
1192
1205 };