1 /**
2 * @file
3 * Vorbis I decoder
4 * @author Denes Balatoni ( dbalatoni programozo hu )
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Vorbis I decoder
26 * @author Denes Balatoni ( dbalatoni programozo hu )
27 */
28
29 #include <inttypes.h>
30 #include <math.h>
31
36
37 #define BITSTREAM_READER_LE
47
50 #define V_MAX_VLCS (1 << 16)
51 #define V_MAX_PARTITIONS (1 << 20)
52
61
66 typedef
98
111
121
128
134
137
161 } vorbis_context;
162
163 /* Helper functions */
164
166 (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x))
167
168 static const char idx_err_str[] =
"Index value %d out of range (0 - %d) for %s at %s:%i\n";
169 #define VALIDATE_INDEX(idx, limit) \
170 if (idx >= limit) {\
171 av_log(vc->avctx, AV_LOG_ERROR,\
172 idx_err_str,\
173 (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\
174 return AVERROR_INVALIDDATA;\
175 }
176 #define GET_VALIDATED_INDEX(idx, bits, limit) \
177 {\
178 idx = get_bits(gb, bits);\
179 VALIDATE_INDEX(idx, limit)\
180 }
181
183 {
184 float mant =
val & 0x1fffff;
185 int exp = (
val & 0x7fe00000) >> 21;
186 if (
val & 0x80000000)
187 mant = -mant;
189 }
190
191
192 // Free all allocated memory -----------------------------------------
193
195 {
197
201
202 if (vc->residues)
203 for (
i = 0;
i < vc->residue_count;
i++)
207
210
211 if (vc->codebooks)
212 for (
i = 0;
i < vc->codebook_count; ++
i) {
215 }
217
218 if (vc->floors)
219 for (
i = 0;
i < vc->floor_count; ++
i) {
220 if (vc->floors[
i].floor_type == 0) {
223 av_freep(&vc->floors[
i].data.t0.book_list);
225 } else {
227 }
228 }
230
231 if (vc->mappings)
232 for (
i = 0;
i < vc->mapping_count; ++
i) {
236 }
238 }
239
240 // Parse setup header -------------------------------------------------
241
242 // Process codebooks part
243
245 {
247 uint8_t *tmp_vlc_bits =
NULL;
248 uint32_t *tmp_vlc_codes =
NULL;
250 uint16_t *codebook_multiplicands =
NULL;
252
253 vc->codebook_count =
get_bits(gb, 8) + 1;
254
255 ff_dlog(
NULL,
" Codebooks: %d \n", vc->codebook_count);
256
257 vc->codebooks =
av_mallocz(vc->codebook_count *
sizeof(*vc->codebooks));
261 if (!vc->codebooks ||
262 !tmp_vlc_bits || !tmp_vlc_codes || !codebook_multiplicands) {
265 }
266
267 for (
cb = 0;
cb < vc->codebook_count; ++
cb) {
269 unsigned ordered, t, entries, used_entries = 0;
270
272
275 " %u. Codebook setup data corrupt.\n",
cb);
278 }
279
283 " %u. Codebook's dimension is invalid (%d).\n",
287 }
291 " %u. Codebook has too many entries (%u).\n",
295 }
296
298
299 ff_dlog(
NULL,
" codebook_dimensions %d, codebook_entries %u\n",
301
302 if (!ordered) {
305
307
308 if (sparse) {
310
311 used_entries = 0;
312 for (ce = 0; ce < entries; ++ce) {
315 tmp_vlc_bits[ce] =
get_bits(gb, 5) + 1;
316 ++used_entries;
317 } else
318 tmp_vlc_bits[ce] = 0;
319 }
320 } else {
322
323 used_entries = entries;
324 for (ce = 0; ce < entries; ++ce)
325 tmp_vlc_bits[ce] =
get_bits(gb, 5) + 1;
326 }
327 } else {
328 unsigned current_entry = 0;
329 unsigned current_length =
get_bits(gb, 5) + 1;
330
331 ff_dlog(
NULL,
" ordered, current length: %u\n", current_length);
//FIXME
332
333 used_entries = entries;
334 for (; current_entry < used_entries && current_length <= 32; ++current_length) {
336
338
340
342
343 for (
i = current_entry;
i < number+current_entry; ++
i)
344 if (
i < used_entries)
345 tmp_vlc_bits[
i] = current_length;
346
347 current_entry+=number;
348 }
349 if (current_entry>used_entries) {
353 }
354 }
355
357
360
361 // If the codebook is used for (inverse) VQ, calculate codevectors.
362
366
369 unsigned codebook_value_bits =
get_bits(gb, 4) + 1;
370 unsigned codebook_sequence_p =
get_bits1(gb);
371
375 }
376 ff_dlog(
NULL,
" We expect %d numbers for building the codevectors. \n",
377 codebook_lookup_values);
379 codebook_delta_value, codebook_minimum_value);
380
381 for (
i = 0;
i < codebook_lookup_values; ++
i) {
382 codebook_multiplicands[
i] =
get_bits(gb, codebook_value_bits);
383
384 ff_dlog(
NULL,
" multiplicands*delta+minmum : %e \n",
385 (
float)codebook_multiplicands[
i] * codebook_delta_value + codebook_minimum_value);
386 ff_dlog(
NULL,
" multiplicand %u\n", codebook_multiplicands[
i]);
387 }
388
389 // Weed out unused vlcs and build codevector vector
390 if (used_entries) {
397 }
398 } else
400
401 for (j = 0,
i = 0;
i < entries; ++
i) {
403
404 if (tmp_vlc_bits[
i]) {
405 float last = 0.0;
406 unsigned lookup_offset =
i;
407
408 ff_dlog(vc->avctx,
"Lookup offset %u ,",
i);
409
410 for (k = 0; k <
dim; ++k) {
411 unsigned multiplicand_offset = lookup_offset % codebook_lookup_values;
412 codebook_setup->
codevectors[j *
dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;
413 if (codebook_sequence_p)
415 lookup_offset/=codebook_lookup_values;
416 }
417 tmp_vlc_bits[j] = tmp_vlc_bits[
i];
418
419 ff_dlog(vc->avctx,
"real lookup offset %u, vector: ", j);
420 for (k = 0; k <
dim; ++k)
424
425 ++j;
426 }
427 }
428 if (j != used_entries) {
432 }
433 entries = used_entries;
438 }
439
440 // Initialize VLC table
445 }
447 for (t = 0; t < entries; ++t)
448 if (tmp_vlc_bits[t] >= codebook_setup->
maxdepth)
449 codebook_setup->
maxdepth = tmp_vlc_bits[t];
450
453 else
455
457
459 entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits),
460 sizeof(*tmp_vlc_bits), tmp_vlc_codes,
461 sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes),
465 }
466 }
467
470 av_free(codebook_multiplicands);
471 return 0;
472
473 // Error:
477 av_free(codebook_multiplicands);
479 }
480
481 // Process time domain transforms part (unused in Vorbis I)
482
484 {
486 unsigned i, vorbis_time_count =
get_bits(gb, 6) + 1;
487
488 for (
i = 0;
i < vorbis_time_count; ++
i) {
489 unsigned vorbis_tdtransform =
get_bits(gb, 16);
490
491 ff_dlog(
NULL,
" Vorbis time domain transform %u: %u\n",
492 vorbis_time_count, vorbis_tdtransform);
493
494 if (vorbis_tdtransform) {
497 }
498 }
499 return 0;
500 }
501
502 // Process floors part
503
506 static int create_map(vorbis_context *vc,
unsigned floor_number);
510 {
513
514 vc->floor_count =
get_bits(gb, 6) + 1;
515
516 vc->floors =
av_mallocz(vc->floor_count *
sizeof(*vc->floors));
517 if (!vc->floors)
519
520 for (
i = 0;
i < vc->floor_count; ++
i) {
522
524
526
528 int maximum_class = -1;
529 unsigned rangebits, rangemax, floor1_values = 2;
530
532
534
537
542
543 ff_dlog(
NULL,
" %d. floor %d partition class %d \n",
545
546 }
547
548 ff_dlog(
NULL,
" maximum class %d \n", maximum_class);
549
550 for (j = 0; j <= maximum_class; ++j) {
553
554 ff_dlog(
NULL,
" %d floor %d class dim: %d subclasses %d \n",
i, j,
557
560
562 }
563
569
571 }
572 }
573
576
579
584
588 "A rangebits value of 0 is not compliant with the Vorbis I specification.\n");
590 }
591 rangemax = (1 << rangebits);
592 if (rangemax > vc->blocksize[1] / 2) {
594 "Floor value is too large for blocksize: %u (%"PRIu32")\n",
595 rangemax, vc->blocksize[1] / 2);
597 }
600
604
605 ff_dlog(
NULL,
" %u. floor1 Y coord. %d\n", floor1_values,
607 }
608 }
609
610 // Precalculate order of x coordinates - needed for decode
615 }
617 unsigned max_codebook_dim = 0;
618
620
625 }
630 }
634 "Floor 0 bark map size is 0.\n");
636 }
640
641 /* allocate mem for booklist */
646 /* read book indexes */
647 {
648 int idx;
649 unsigned book_idx;
653 if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
654 max_codebook_dim = vc->codebooks[book_idx].dimensions;
655 }
656 }
657
660
661 /* codebook dim is for padding if codebook dim doesn't *
662 * divide order+1 then we need to read more data */
668
669 /* debug output parsed headers */
682 {
683 int idx;
687 }
688 }
689 } else {
692 }
693 }
694 return 0;
695 }
696
697 // Process residues part
698
700 {
703
704 vc->residue_count =
get_bits(gb, 6)+1;
705 vc->residues =
av_mallocz(vc->residue_count *
sizeof(*vc->residues));
706 if (!vc->residues)
708
709 ff_dlog(
NULL,
" There are %d residues. \n", vc->residue_count);
710
711 for (
i = 0;
i < vc->residue_count; ++
i) {
713 uint8_t cascade[64];
714 unsigned high_bits, low_bits;
715
717
719
723 /* Validations to prevent a buffer overflow later. */
724 if (res_setup->
begin>res_setup->
end ||
727 "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n",
731 }
732
735
739 vc->audio_channels *
743
744 ff_dlog(
NULL,
" begin %"PRIu32
" end %"PRIu32
" part.size %u classif.s %"PRIu8
" classbook %"PRIu8
"\n",
747
749 high_bits = 0;
753 cascade[j] = (high_bits << 3) + low_bits;
754
756 }
757
760 for (k = 0; k < 8; ++k) {
761 if (cascade[j]&(1 << k)) {
763
764 ff_dlog(
NULL,
" %u class cascade depth %u book: %d\n",
765 j, k, res_setup->
books[j][k]);
766
769 } else {
770 res_setup->
books[j][k] = -1;
771 }
772 }
773 }
774 }
775 return 0;
776 }
777
778 // Process mappings part
779
781 {
784
785 vc->mapping_count =
get_bits(gb, 6)+1;
786 vc->mappings =
av_mallocz(vc->mapping_count *
sizeof(*vc->mappings));
787 if (!vc->mappings)
789
790 ff_dlog(
NULL,
" There are %d mappings. \n", vc->mapping_count);
791
792 for (
i = 0;
i < vc->mapping_count; ++
i) {
794
796 av_log(vc->avctx,
AV_LOG_ERROR,
"Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
798 }
801 } else {
803 }
804
807 if (vc->audio_channels < 2) {
809 "Square polar channel mapping with less than two channels is not compliant with the Vorbis I specification.\n");
811 }
815 sizeof(*mapping_setup->
angle));
818
822 }
823 } else {
825 }
826
829
833 }
834
835 if (mapping_setup->
submaps>1) {
837 sizeof(*mapping_setup->
mux));
838 if (!mapping_setup->
mux)
840
841 for (j = 0; j < vc->audio_channels; ++j)
843 }
844
845 for (j = 0; j < mapping_setup->
submaps; ++j) {
849
850 ff_dlog(
NULL,
" %u mapping %u submap : floor %d, residue %d\n",
i, j,
853 }
854 }
855 return 0;
856 }
857
858 // Process modes part
859
860 static int create_map(vorbis_context *vc,
unsigned floor_number)
861 {
864 int idx;
865 int blockflag, n;
867
868 for (blockflag = 0; blockflag < 2; ++blockflag) {
869 n = vc->blocksize[blockflag] / 2;
870 floors[floor_number].
data.
t0.
map[blockflag] =
872 if (!floors[floor_number].
data.t0.map[blockflag])
874
877
878 for (idx = 0; idx < n; ++idx) {
880 (
vf->bark_map_size /
BARK(
vf->rate / 2.0f)));
881 if (
vf->bark_map_size-1 <
map[idx])
882 map[idx] =
vf->bark_map_size - 1;
883 }
885 vf->map_size[blockflag] = n;
886 }
887
888 for (idx = 0; idx <= n; ++idx) {
889 ff_dlog(
NULL,
"floor0 map: map at pos %d is %"PRId32
"\n", idx,
map[idx]);
890 }
891
892 return 0;
893 }
894
896 {
899
900 vc->mode_count =
get_bits(gb, 6) + 1;
901 vc->modes =
av_mallocz(vc->mode_count *
sizeof(*vc->modes));
902 if (!vc->modes)
904
905 ff_dlog(
NULL,
" There are %d modes.\n", vc->mode_count);
906
907 for (
i = 0;
i < vc->mode_count; ++
i) {
909
914
915 ff_dlog(
NULL,
" %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
918 }
919 return 0;
920 }
921
922 // Process the whole setup header using the functions above
923
925 {
928
932 av_log(vc->avctx,
AV_LOG_ERROR,
" Vorbis setup header packet corrupt (no vorbis signature). \n");
934 }
935
937 av_log(vc->avctx,
AV_LOG_ERROR,
" Vorbis setup header packet corrupt (codebooks). \n");
939 }
941 av_log(vc->avctx,
AV_LOG_ERROR,
" Vorbis setup header packet corrupt (time domain transforms). \n");
943 }
947 }
949 av_log(vc->avctx,
AV_LOG_ERROR,
" Vorbis setup header packet corrupt (residues). \n");
951 }
953 av_log(vc->avctx,
AV_LOG_ERROR,
" Vorbis setup header packet corrupt (mappings). \n");
955 }
959 }
961 av_log(vc->avctx,
AV_LOG_ERROR,
" Vorbis setup header packet corrupt (framing flag). \n");
963 }
964
965 return 0;
966 }
967
968 // Process the identification header
969
971 {
973 unsigned bl0, bl1;
976
980 av_log(vc->avctx,
AV_LOG_ERROR,
" Vorbis id header packet corrupt (no vorbis signature). \n");
982 }
983
985 vc->audio_channels =
get_bits(gb, 8);
986 if (vc->audio_channels <= 0) {
989 }
991 if (vc->audio_samplerate <= 0) {
994 }
1000 if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
1001 av_log(vc->avctx,
AV_LOG_ERROR,
" Vorbis id header packet corrupt (illegal blocksize). \n");
1003 }
1004 vc->blocksize[0] = (1 << bl0);
1005 vc->blocksize[1] = (1 << bl1);
1008
1010 av_log(vc->avctx,
AV_LOG_ERROR,
" Vorbis id header packet corrupt (framing flag not set). \n");
1012 }
1013
1014 vc->channel_residues =
av_malloc_array(vc->blocksize[1] / 2, vc->audio_channels *
sizeof(*vc->channel_residues));
1015 vc->saved =
av_calloc(vc->blocksize[1] / 4, vc->audio_channels *
sizeof(*vc->saved));
1016 if (!vc->channel_residues || !vc->saved)
1018
1019 vc->previous_window = -1;
1020
1022 vc->blocksize[0] >> 1, &
scale, 0);
1025
1027 vc->blocksize[1] >> 1, &
scale, 0);
1030
1032 if (!vc->fdsp)
1034
1035 ff_dlog(
NULL,
" vorbis version %"PRIu32
" \n audio_channels %"PRIu8
" \n audio_samplerate %"PRIu32
" \n bitrate_max %"PRIu32
" \n bitrate_nom %"PRIu32
" \n bitrate_min %"PRIu32
" \n blk_0 %"PRIu32
" blk_1 %"PRIu32
" \n ",
1036 vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
1037
1038 /*
1039 BLK = vc->blocksize[0];
1040 for (i = 0; i < BLK / 2; ++i) {
1041 vc->win[0][i] = sin(0.5*3.14159265358*(sin(((float)i + 0.5) / (float)BLK*3.14159265358))*(sin(((float)i + 0.5) / (float)BLK*3.14159265358)));
1042 }
1043 */
1044
1045 return 0;
1046 }
1047
1048 // Process the extradata using the functions above (identification header, setup header)
1049
1051 {
1055 const uint8_t *header_start[3];
1056 int header_len[3];
1059
1060 vc->avctx = avctx;
1062
1064
1065 if (!headers_len) {
1068 }
1069
1073 }
1074
1077 if (hdr_type != 1) {
1080 }
1085 }
1086
1089 if (hdr_type != 5) {
1093 }
1098 }
1099
1101 if (vc->audio_channels > 8) {
1104 } else {
1106 }
1107
1109
1110 return 0;
1111 }
1112
1113 // Decode audiopackets -------------------------------------------------
1114
1115 // Read and decode floor
1116
1119 {
1121 float *lsp =
vf->lsp;
1122 unsigned book_idx;
1123 uint64_t amplitude;
1124 unsigned blockflag = vc->modes[vc->mode_number].blockflag;
1125
1126 if (!
vf->amplitude_bits)
1127 return 1;
1128
1130 if (amplitude > 0) {
1131 float last = 0;
1132 unsigned idx, lsp_len = 0;
1134
1136 if (book_idx >=
vf->num_books) {
1138 book_idx = 0;
1139 }
1140 ff_dlog(
NULL,
"floor0 dec: booknumber: %u\n", book_idx);
1141 codebook = vc->codebooks[
vf->book_list[book_idx]];
1142 /* Invalid codebook! */
1145
1146 while (lsp_len<vf->order) {
1147 int vec_off;
1148
1151 /* read temp vector */
1154 if (vec_off < 0)
1157 ff_dlog(
NULL,
"floor0 dec: vector offset: %d\n", vec_off);
1158 /* copy each vector component and add last to it */
1159 for (idx = 0; idx <
codebook.dimensions; ++idx)
1160 lsp[lsp_len+idx] =
codebook.codevectors[vec_off+idx] + last;
1161 last = lsp[lsp_len+idx-1]; /* set last to last vector component */
1162
1164 }
1165 /* DEBUG: output lsp coeffs */
1166 {
1167 int idx;
1168 for (idx = 0; idx < lsp_len; ++idx)
1169 ff_dlog(
NULL,
"floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
1170 }
1171
1172 /* synthesize floor output vector */
1173 {
1175 int order =
vf->order;
1176 float wstep =
M_PI /
vf->bark_map_size;
1177
1178 for (
i = 0;
i < order;
i++)
1179 lsp[
i] = 2.0
f * cos(lsp[
i]);
1180
1181 ff_dlog(
NULL,
"floor0 synth: map_size = %"PRIu32
"; m = %d; wstep = %f\n",
1182 vf->map_size[blockflag], order, wstep);
1183
1185 while (i < vf->map_size[blockflag]) {
1186 int j, iter_cond =
vf->map[blockflag][
i];
1187 float p = 0.5f;
1188 float q = 0.5f;
1189 float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times
1190
1191 /* similar part for the q and p products */
1192 for (j = 0; j + 1 < order; j += 2) {
1193 q *= lsp[j] - two_cos_w;
1194 p *= lsp[j + 1] - two_cos_w;
1195 }
1196 if (j == order) { // even order
1197 p *= p * (2.0f - two_cos_w);
1198 q *= q * (2.0f + two_cos_w);
1199 } else { // odd order
1200 q *= two_cos_w-lsp[j]; // one more time for q
1201
1202 /* final step and square */
1203 p *= p * (4.f - two_cos_w * two_cos_w);
1204 q *= q;
1205 }
1206
1207 if (p + q == 0.0)
1209
1210 /* calculate linear floor value */
1211 q =
exp((((amplitude*
vf->amplitude_offset) /
1212 (((1ULL <<
vf->amplitude_bits) - 1) * sqrt(p + q)))
1213 -
vf->amplitude_offset) * .11512925f);
1214
1215 /* fill vector */
1216 do {
1218 }
while (
vf->map[blockflag][
i] == iter_cond);
1219 }
1220 }
1221 } else {
1222 /* this channel is unused */
1223 return 1;
1224 }
1225
1227
1228 return 0;
1229 }
1230
1233 {
1236 uint16_t range_v[4] = { 256, 128, 86, 64 };
1237 unsigned range = range_v[
vf->multiplier - 1];
1238 uint16_t floor1_Y[258];
1239 uint16_t floor1_Y_final[258];
1240 int floor1_flag[258];
1241 unsigned partition_class, cdim, cbits, csub, cval,
offset,
i, j;
1242 int book, adx, ady, dy, off, predicted, err;
1243
1244
1246 return 1;
1247
1248 // Read values (or differences) for the floor's points
1249
1252
1253 ff_dlog(
NULL,
"floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
1254
1256 for (
i = 0;
i <
vf->partitions; ++
i) {
1257 partition_class =
vf->partition_class[
i];
1258 cdim =
vf->class_dimensions[partition_class];
1259 cbits =
vf->class_subclasses[partition_class];
1260 csub = (1 << cbits) - 1;
1261 cval = 0;
1262
1264
1265 if (cbits) // this reads all subclasses for this partition's class
1266 cval =
get_vlc2(gb, vc->codebooks[
vf->class_masterbook[partition_class]].vlc.table,
1267 vc->codebooks[
vf->class_masterbook[partition_class]].nb_bits, 3);
1268
1269 for (j = 0; j < cdim; ++j) {
1270 book =
vf->subclass_books[partition_class][cval & csub];
1271
1272 ff_dlog(
NULL,
"book %d Cbits %u cval %u bits:%d\n",
1274
1275 cval = cval >> cbits;
1276 if (book > -1) {
1277 int v =
get_vlc2(gb, vc->codebooks[book].vlc.table,
1278 vc->codebooks[book].nb_bits, 3);
1279 if (v < 0)
1282 } else {
1284 }
1285
1288 }
1290 }
1291
1292 // Amplitude calculation from the differences
1293
1294 floor1_flag[0] = 1;
1295 floor1_flag[1] = 1;
1296 floor1_Y_final[0] = floor1_Y[0];
1297 floor1_Y_final[1] = floor1_Y[1];
1298
1299 for (
i = 2;
i <
vf->x_list_dim; ++
i) {
1300 unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
1301
1302 low_neigh_offs =
vf->list[
i].low;
1303 high_neigh_offs =
vf->list[
i].high;
1304 dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs]; // render_point begin
1305 adx =
vf->list[high_neigh_offs].x -
vf->list[low_neigh_offs].x;
1307 err = ady * (
vf->list[
i].x -
vf->list[low_neigh_offs].x);
1308 off = err / adx;
1309 if (dy < 0) {
1310 predicted = floor1_Y_final[low_neigh_offs] - off;
1311 } else {
1312 predicted = floor1_Y_final[low_neigh_offs] + off;
1313 } // render_point end
1314
1316 highroom =
range-predicted;
1317 lowroom = predicted;
1318 if (highroom < lowroom) {
1319 room = highroom * 2;
1320 } else {
1321 room = lowroom * 2; // SPEC misspelling
1322 }
1324 floor1_flag[low_neigh_offs] = 1;
1325 floor1_flag[high_neigh_offs] = 1;
1328 if (highroom > lowroom) {
1330 } else {
1332 }
1333 } else {
1336 } else {
1338 }
1339 }
1340 } else {
1343 }
1344
1345 ff_dlog(
NULL,
" Decoded floor(%d) = %u / val %u\n",
1346 vf->list[
i].x, floor1_Y_final[
i],
val);
1347 }
1348
1349 // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ?
1350
1352
1354
1355 return 0;
1356 }
1357
1360 uint8_t *do_not_decode,
1361 unsigned ch_used,
1362 int partition_count,
1363 int ptns_to_read
1364 )
1365 {
1368 unsigned c_p_c =
codebook->dimensions;
1371 for (p = 0, j = 0; j < ch_used; ++j) {
1372 if (!do_not_decode[j]) {
1375
1377
1379
1382 "Invalid vlc code decoding %d channel.", j);
1384 }
1385
1387 for (
i = partition_count + c_p_c - 1;
i >= partition_count;
i--) {
1388 if (
i < ptns_to_read)
1390 }
1391 } else {
1392 for (
i = partition_count + c_p_c - 1;
i >= partition_count;
i--) {
1393 temp2 = (((uint64_t)
temp) * inverse_class) >> 32;
1394
1395 if (
i < ptns_to_read)
1398 }
1399 }
1400 }
1401 p += ptns_to_read;
1402 }
1403 return 0;
1404 }
1405 // Read and decode residue
1406
1409 unsigned ch,
1410 uint8_t *do_not_decode,
1411 float *vec,
1412 unsigned vlen,
1413 unsigned ch_left,
1414 int vr_type)
1415 {
1417 unsigned c_p_c = vc->codebooks[vr->
classbook].dimensions;
1419 unsigned pass, ch_used,
i, j, k, l;
1420 unsigned max_output = (ch - 1) * vlen;
1422 int libvorbis_bug = 0;
1423
1424 if (vr_type == 2) {
1425 for (j = 1; j < ch; ++j)
1426 do_not_decode[0] &= do_not_decode[j]; // FIXME - clobbering input
1427 if (do_not_decode[0])
1428 return 0;
1429 ch_used = 1;
1430 max_output += vr->
end / ch;
1431 } else {
1432 ch_used = ch;
1433 max_output += vr->
end;
1434 }
1435
1436 if (max_output > ch_left * vlen) {
1437 if (max_output <= ch_left * vlen + vr->partition_size*ch_used/ch) {
1438 ptns_to_read--;
1439 libvorbis_bug = 1;
1440 } else {
1443 }
1444 }
1445
1446 ff_dlog(
NULL,
" residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
1447
1448 for (pass = 0; pass <= vr->
maxpass; ++pass) {
// FIXME OPTIMIZE?
1449 int voffset, partition_count, j_times_ptns_to_read;
1450
1451 voffset = vr->
begin;
1452 for (partition_count = 0; partition_count < ptns_to_read;) { // SPEC error
1453 if (!pass) {
1454 int ret =
setup_classifs(vc, vr, do_not_decode, ch_used, partition_count, ptns_to_read);
1457 }
1458 for (
i = 0; (
i < c_p_c) && (partition_count < ptns_to_read); ++
i) {
1459 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1460 unsigned voffs;
1461
1462 if (!do_not_decode[j]) {
1463 unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
1464 int vqbook = vr->
books[vqclass][pass];
1465
1466 if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
1467 int coffs;
1468 unsigned dim = vc->codebooks[vqbook].dimensions;
1471
1474 return 0;
1475 }
1476
1477 if (vr_type == 0) {
1478
1479 voffs = voffset+j*vlen;
1480 for (k = 0; k <
step; ++k) {
1482 if (coffs < 0)
1483 return coffs;
1485 for (l = 0; l <
dim; ++l)
1486 vec[voffs + k + l *
step] +=
codebook.codevectors[coffs + l];
1487 }
1488 } else if (vr_type == 1) {
1489 voffs = voffset + j * vlen;
1490 for (k = 0; k <
step; ++k) {
1492 if (coffs < 0)
1493 return coffs;
1495 for (l = 0; l <
dim; ++l, ++voffs) {
1496 vec[voffs]+=
codebook.codevectors[coffs+l];
1497
1498 ff_dlog(
NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
1499 pass, voffs, vec[voffs],
codebook.codevectors[coffs+l], coffs);
1500 }
1501 }
1502 }
else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (
dim & 1) == 0) {
// most frequent case optimized
1503 voffs = voffset >> 1;
1504
1506 for (k = 0; k <
step; ++k) {
1508 if (coffs < 0)
1509 return coffs;
1510 coffs *= 2;
1511 vec[voffs + k ] +=
codebook.codevectors[coffs ];
1512 vec[voffs + k + vlen] +=
codebook.codevectors[coffs + 1];
1513 }
1514 }
else if (
dim == 4) {
1515 for (k = 0; k <
step; ++k, voffs += 2) {
1517 if (coffs < 0)
1518 return coffs;
1519 coffs *= 4;
1520 vec[voffs ] +=
codebook.codevectors[coffs ];
1521 vec[voffs + 1 ] +=
codebook.codevectors[coffs + 2];
1522 vec[voffs + vlen ] +=
codebook.codevectors[coffs + 1];
1523 vec[voffs + vlen + 1] +=
codebook.codevectors[coffs + 3];
1524 }
1525 } else
1526 for (k = 0; k <
step; ++k) {
1528 if (coffs < 0)
1529 return coffs;
1531 for (l = 0; l <
dim; l += 2, voffs++) {
1532 vec[voffs ] +=
codebook.codevectors[coffs + l ];
1533 vec[voffs + vlen] +=
codebook.codevectors[coffs + l + 1];
1534
1535 ff_dlog(
NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1536 pass, voffset / ch + (voffs % ch) * vlen,
1537 vec[voffset / ch + (voffs % ch) * vlen],
1538 codebook.codevectors[coffs + l], coffs, l);
1539 }
1540 }
1541
1542 } else if (vr_type == 2) {
1543 unsigned voffs_div = ch == 1 ? voffset :
FASTDIV(voffset, ch);
1544 unsigned voffs_mod = voffset - voffs_div * ch;
1545
1546 for (k = 0; k <
step; ++k) {
1548 if (coffs < 0)
1549 return coffs;
1551 for (l = 0; l <
dim; ++l) {
1552 vec[voffs_div + voffs_mod * vlen] +=
1554
1555 ff_dlog(
NULL,
" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1556 pass, voffs_div + voffs_mod * vlen,
1557 vec[voffs_div + voffs_mod * vlen],
1558 codebook.codevectors[coffs + l], coffs, l);
1559
1560 if (++voffs_mod == ch) {
1561 voffs_div++;
1562 voffs_mod = 0;
1563 }
1564 }
1565 }
1566 }
1567 }
1568 }
1569 j_times_ptns_to_read += ptns_to_read;
1570 }
1571 ++partition_count;
1573 }
1574 }
1575 if (libvorbis_bug && !pass) {
1576 for (j = 0; j < ch_used; ++j) {
1577 if (!do_not_decode[j]) {
1579 vc->codebooks[vr->
classbook].nb_bits, 3);
1580 }
1581 }
1582 }
1583 }
1584 return 0;
1585 }
1586
1588 unsigned ch,
1589 uint8_t *do_not_decode,
1590 float *vec, unsigned vlen,
1591 unsigned ch_left)
1592 {
1595 else if (vr->
type == 1)
1597 else if (vr->
type == 0)
1599 else {
1602 }
1603 }
1604
1605 // Decode the audio packet using the functions above
1606
1608 {
1612 int previous_window = vc->previous_window;
1613 unsigned mode_number, blockflag, blocksize;
1615 uint8_t no_residue[255];
1616 uint8_t do_not_decode[255];
1618 float *ch_res_ptr = vc->channel_residues;
1619 uint8_t res_chan[255];
1620 unsigned res_num = 0;
1621 int retlen = 0;
1622 unsigned ch_left = vc->audio_channels;
1623 unsigned vlen;
1624
1628 }
1629
1630 if (vc->mode_count == 1) {
1631 mode_number = 0;
1632 } else {
1634 }
1635 vc->mode_number = mode_number;
1636 mapping = &vc->mappings[vc->modes[mode_number].mapping];
1637
1638 ff_dlog(
NULL,
" Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
1639 vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
1640
1641 blockflag = vc->modes[mode_number].blockflag;
1642 blocksize = vc->blocksize[blockflag];
1643 vlen = blocksize / 2;
1644 if (blockflag) {
1646 if (previous_window < 0)
1647 previous_window =
code>>1;
1648 } else if (previous_window < 0)
1649 previous_window = 0;
1650
1651 memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
1652 for (
i = 0;
i < vc->audio_channels; ++
i)
1653 memset(floor_ptr[
i], 0, vlen *
sizeof(floor_ptr[0][0]));
//FIXME can this be removed ?
1654
1655 // Decode floor
1656
1657 for (
i = 0;
i < vc->audio_channels; ++
i) {
1662 } else {
1664 }
1665
1667
1671 }
1672 no_residue[
i] =
ret;
1673 }
1674
1675 // Nonzero vector propagate
1676
1678 if (!(no_residue[mapping->
magnitude[
i]] & no_residue[mapping->
angle[
i]])) {
1680 no_residue[mapping->
angle[
i]] = 0;
1681 }
1682 }
1683
1684 // Decode residue
1685
1688 unsigned ch = 0;
1690
1691 for (j = 0; j < vc->audio_channels; ++j) {
1692 if ((mapping->
submaps == 1) || (
i == mapping->
mux[j])) {
1693 res_chan[j] = res_num;
1694 if (no_residue[j]) {
1695 do_not_decode[ch] = 1;
1696 } else {
1697 do_not_decode[ch] = 0;
1698 }
1699 ++ch;
1700 ++res_num;
1701 }
1702 }
1704 if (ch_left < ch) {
1707 }
1708 if (ch) {
1712 }
1713
1714 ch_res_ptr += ch * vlen;
1715 ch_left -= ch;
1716 }
1717
1718 if (ch_left > 0)
1720
1721 // Inverse coupling
1722
1723 for (
i = mapping->
coupling_steps - 1;
i >= 0; --
i) {
//warning: i has to be signed
1724 float *mag, *ang;
1725
1726 mag = vc->channel_residues+res_chan[mapping->
magnitude[
i]] * blocksize / 2;
1727 ang = vc->channel_residues+res_chan[mapping->
angle[
i]] * blocksize / 2;
1728 vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
1729 }
1730
1731 // Dotproduct, MDCT
1732
1733 mdct = vc->mdct[blockflag];
1734 mdct_fn = vc->mdct_fn[blockflag];
1735
1736 for (j = vc->audio_channels-1;j >= 0; j--) {
1737 ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
1738 vc->fdsp->vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
1739 mdct_fn(mdct, ch_res_ptr, floor_ptr[j], sizeof(float));
1740 }
1741
1742 // Overlap/add, save data for next overlapping
1743
1744 retlen = (blocksize + vc->blocksize[previous_window]) / 4;
1745 for (j = 0; j < vc->audio_channels; j++) {
1746 unsigned bs0 = vc->blocksize[0];
1747 unsigned bs1 = vc->blocksize[1];
1748 float *residue = vc->channel_residues + res_chan[j] * blocksize / 2;
1749 float *saved = vc->saved + j * bs1 / 4;
1750 float *
ret = floor_ptr[j];
1751 float *buf = residue;
1752 const float *
win = vc->win[blockflag & previous_window];
1753
1754 if (blockflag == previous_window) {
1755 vc->fdsp->vector_fmul_window(
ret, saved, buf,
win, blocksize / 4);
1756 } else if (blockflag > previous_window) {
1757 vc->fdsp->vector_fmul_window(
ret, saved, buf,
win, bs0 / 4);
1758 memcpy(
ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) *
sizeof(
float));
1759 } else {
1760 memcpy(
ret, saved, ((bs1 - bs0) / 4) *
sizeof(
float));
1761 vc->fdsp->vector_fmul_window(
ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf,
win, bs0 / 4);
1762 }
1763 memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
1764 }
1765
1766 vc->previous_window = blockflag;
1767 return retlen;
1768 }
1769
1770 // Return the decoded audio packet through the standard api
1771
1773 int *got_frame_ptr,
AVPacket *avpkt)
1774 {
1775 const uint8_t *buf = avpkt->
data;
1776 int buf_size = avpkt->
size;
1779 float *channel_ptrs[255];
1781
1783
1784 if (*buf == 1 && buf_size > 7) {
1787
1793 }
1794
1796 if (vc->audio_channels > 8) {
1799 } else {
1801 }
1802
1804 return buf_size;
1805 }
1806
1807 if (*buf == 3 && buf_size > 7) {
1809 return buf_size;
1810 }
1811
1812 if (*buf == 5 && buf_size > 7 && vc->channel_residues && !vc->modes) {
1815
1820 }
1821 return buf_size;
1822 }
1823
1824 if (!vc->channel_residues || !vc->modes) {
1827 }
1828
1829 /* get output buffer */
1830 frame->nb_samples = vc->blocksize[1] / 2;
1833
1834 if (vc->audio_channels > 8) {
1835 for (
i = 0;
i < vc->audio_channels;
i++)
1836 channel_ptrs[
i] = (
float *)
frame->extended_data[
i];
1837 } else {
1838 for (
i = 0;
i < vc->audio_channels;
i++) {
1840 channel_ptrs[ch] = (
float *)
frame->extended_data[
i];
1841 }
1842 }
1843
1846
1849
1850 if (!vc->first_frame) {
1851 vc->first_frame = 1;
1853 }
1854
1855 ff_dlog(
NULL,
"parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
1857
1859 *got_frame_ptr = 1;
1860
1861 return buf_size;
1862 }
1863
1864 // Close decoder
1865
1867 {
1869
1871
1872 return 0;
1873 }
1874
1876 {
1878
1879 if (vc->saved) {
1880 memset(vc->saved, 0, (vc->blocksize[1] / 4) * vc->audio_channels *
1881 sizeof(*vc->saved));
1882 }
1883 vc->previous_window = -1;
1884 vc->first_frame = 0;
1885 }
1886
1892 .priv_data_size = sizeof(vorbis_context),
1902 };