-
Notifications
You must be signed in to change notification settings - Fork 4
/
ogdecode.c
4866 lines (4087 loc) · 133 KB
/
ogdecode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
//make it easy on the folks that want to compile the libs with a
//different malloc than stdlib
#define _ogg_malloc malloc
#define _ogg_calloc calloc
#define _ogg_realloc realloc
#define _ogg_free free
#include "ogdecode.h"
#define vorbis_ftoi(f) ((int)(f+100000.5)-100000)
#define rint(x) (floor((x)+.5f))
typedef struct {
unsigned char *header;
long header_len;
unsigned char *body;
long body_len;
} ogg_page;
typedef struct {
unsigned char *packet;
long bytes;
long b_o_s;
long e_o_s;
ogg_int64_t granulepos;
ogg_int64_t packetno;
} ogg_packet;
struct alloc_chain {
void *ptr;
struct alloc_chain *next;
};
typedef struct static_codebook {
long dim;
long entries;
long *lengthlist;
int maptype;
long q_min;
long q_delta;
int q_quant;
int q_sequencep;
long *quantlist;
int allocedp;
} static_codebook;
typedef struct decode_aux {
long *tab;
int *tabl;
int tabn;
long *ptr0;
long *ptr1;
long aux;
} decode_aux;
typedef struct codebook {
long dim;
long entries;
const static_codebook *c;
float *valuelist;
long *codelist;
struct decode_aux *decode_tree;
} codebook;
typedef void vorbis_look_mapping;
typedef void vorbis_look_floor;
typedef void vorbis_look_residue;
typedef void vorbis_look_transform;
typedef struct backend_lookup_state {
float **window[2][2][2];
vorbis_look_transform **transform[2];
codebook *fullbooks;
int modebits;
vorbis_look_mapping **mode;
float ampmax;
} backend_lookup_state;
typedef struct {
int blockflag;
int windowtype;
int transformtype;
int mapping;
} vorbis_info_mode;
typedef void vorbis_info_floor;
typedef void vorbis_info_residue;
typedef void vorbis_info_mapping;
typedef struct codec_setup_info {
long blocksizes[2];
int modes;
int maps;
int times;
int floors;
int residues;
int books;
vorbis_info_mode *mode_param[64];
vorbis_info_mapping *map_param[64];
int floor_type[64];
vorbis_info_floor *floor_param[64];
int residue_type[64];
vorbis_info_residue *residue_param[64];
static_codebook *book_param[256];
} codec_setup_info;
typedef struct {
vorbis_info_floor *(*unpack)(vorbis_info *, oggpack_buffer *);
vorbis_look_floor *(*look) (vorbis_dsp_state *, vorbis_info_mode *,
vorbis_info_floor *);
void (*free_info) (vorbis_info_floor *);
void (*free_look) (vorbis_look_floor *);
void *(*inverse1) (struct vorbis_block *, vorbis_look_floor *);
int (*inverse2) (struct vorbis_block *, vorbis_look_floor *,
void *buffer, float *);
} vorbis_func_floor;
typedef struct {
int order;
long rate;
long barkmap;
int ampbits;
int ampdB;
int numbooks;
int books[16];
} vorbis_info_floor0;
typedef struct {
int partitions;
int partitionclass[31];
int class_dim[16];
int class_subs[16];
int class_book[16];
int class_subbook[16][8];
int mult;
int postlist[63+2];
} vorbis_info_floor1;
typedef struct {
vorbis_info_residue *(*unpack)(vorbis_info *, oggpack_buffer *);
vorbis_look_residue *(*look) (vorbis_dsp_state *, vorbis_info_mode *,
vorbis_info_residue *);
void (*free_info) (vorbis_info_residue *);
void (*free_look) (vorbis_look_residue *);
int (*inverse) (struct vorbis_block *, vorbis_look_residue *,
float **, int *, int);
} vorbis_func_residue;
typedef struct vorbis_info_residue0{
long begin;
long end;
int grouping;
int partitions;
int groupbook;
int secondstages[64];
int booklist[256];
} vorbis_info_residue0;
typedef struct {
vorbis_info_mapping *(*unpack)(vorbis_info *, oggpack_buffer *);
vorbis_look_mapping *(*look) (vorbis_dsp_state *, vorbis_info_mode *,
vorbis_info_mapping *);
void (*free_info) (vorbis_info_mapping *);
void (*free_look) (vorbis_look_mapping *);
int (*inverse) (struct vorbis_block *vb, vorbis_look_mapping *);
} vorbis_func_mapping;
typedef struct vorbis_info_mapping0 {
int submaps;
int chmuxlist[256];
int floorsubmap[16];
int residuesubmap[16];
int coupling_steps;
int coupling_mag[256];
int coupling_ang[256];
} vorbis_info_mapping0;
typedef struct {
long n;
int ln;
int m;
int *linearmap;
vorbis_info_floor0 *vi;
float *lsp_look;
} vorbis_look_floor0;
typedef struct {
int sorted_index[63+2];
int forward_index[63+2];
int reverse_index[63+2];
int hineighbor[63];
int loneighbor[63];
int posts;
int n;
int quant_q;
vorbis_info_floor1 *vi;
} vorbis_look_floor1;
typedef struct {
vorbis_info_residue0 *info;
int map;
int parts;
int stages;
codebook *fullbooks;
codebook *phrasebook;
codebook ***partbooks;
int partvals;
int **decodemap;
} vorbis_look_residue0;
typedef struct {
vorbis_info_mode *mode;
vorbis_info_mapping0 *map;
vorbis_look_floor **floor_look;
vorbis_look_residue **residue_look;
vorbis_func_floor **floor_func;
vorbis_func_residue **residue_func;
int ch;
} vorbis_look_mapping0;
typedef struct {
int n;
int log2n;
float *trig;
int *bitrev;
float scale;
} mdct_lookup;
#define NOTOPEN 0
#define PARTOPEN 1
#define OPENED 2
#define STREAMSET 3
#define INITSET 4
static void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes);
static long oggpack_look(oggpack_buffer *b,int bits);
static void oggpack_adv(oggpack_buffer *b,int bits);
static long oggpack_read(oggpack_buffer *b,int bits);
static long oggpack_read1(oggpack_buffer *b);
static int ogg_sync_init(ogg_sync_state *oy);
static int ogg_sync_clear(ogg_sync_state *oy);
static char *ogg_sync_buffer(ogg_sync_state *oy, long size);
static int ogg_sync_wrote(ogg_sync_state *oy, long bytes);
static long ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og);
static int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og);
static int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og);
static int ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op);
static int ogg_stream_init(ogg_stream_state *os,int serialno);
static int ogg_stream_clear(ogg_stream_state *os);
static int ogg_page_version(ogg_page *og);
static int ogg_page_continued(ogg_page *og);
static int ogg_page_bos(ogg_page *og);
static int ogg_page_eos(ogg_page *og);
static ogg_int64_t ogg_page_granulepos(ogg_page *og);
static int ogg_page_serialno(ogg_page *og);
static long ogg_page_pageno(ogg_page *og);
static int ogg_page_packets(ogg_page *og);
static void vorbis_info_init(vorbis_info *vi);
static void vorbis_info_clear(vorbis_info *vi);
static int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb);
static int vorbis_block_clear(vorbis_block *vb);
static void vorbis_dsp_clear(vorbis_dsp_state *v);
static int vorbis_synthesis_headerin(vorbis_info *vi,ogg_packet *op);
static int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi);
static int vorbis_synthesis(vorbis_block *vb,ogg_packet *op);
static int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb);
static int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm);
static int vorbis_synthesis_read(vorbis_dsp_state *v,int samples);
static void vorbis_staticbook_clear(static_codebook *b);
static void vorbis_staticbook_destroy(static_codebook *b);
static int vorbis_book_init_decode(codebook *dest,const static_codebook *source);
static void vorbis_book_clear(codebook *b);
static float *_book_unquantize(const static_codebook *b);
static float _float32_unpack(long val);
static long _book_maptype1_quantvals(const static_codebook *b);
static int vorbis_staticbook_unpack(oggpack_buffer *b,static_codebook *c);
static long vorbis_book_decode(codebook *book, oggpack_buffer *b);
static long vorbis_book_decodevs_add(codebook *book, float *a, oggpack_buffer *b,int n);
static long vorbis_book_decodev_set(codebook *book, float *a, oggpack_buffer *b,int n);
static long vorbis_book_decodev_add(codebook *book, float *a, oggpack_buffer *b,int n);
static long vorbis_book_decodevv_add(codebook *book, float **a, long off,int ch, oggpack_buffer *b,int n);
static void vorbis_lsp_to_curve(float *curve, int *map, int n, int ln, float *lsp, int m, float amp, float ampoffset);
static void *_vorbis_block_alloc(vorbis_block *vb, long bytes);
static void _vorbis_block_ripcord(vorbis_block *vb);
static void mdct_init(mdct_lookup *lookup,int n);
static void mdct_clear(mdct_lookup *l);
static void mdct_backward(mdct_lookup *init, float *in, float *out);
static float *_vorbis_window(int type,int window,int left,int right);
static int _ilog(unsigned int v);
static int ilog(unsigned int v);
static int ilog2(unsigned int v);
extern int ov_clear(OggVorbis_File *vf);
extern int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes);
extern int ov_open_callbacks(void *datasource, OggVorbis_File *vf,
char *initial, long ibytes, ov_callbacks callbacks);
extern int ov_test(FILE *f,OggVorbis_File *vf,char *initial,long ibytes);
extern int ov_test_callbacks(void *datasource, OggVorbis_File *vf,
char *initial, long ibytes, ov_callbacks callbacks);
extern int ov_test_open(OggVorbis_File *vf);
extern long ov_bitrate(OggVorbis_File *vf,int i);
extern long ov_bitrate_instant(OggVorbis_File *vf);
extern long ov_streams(OggVorbis_File *vf);
extern long ov_seekable(OggVorbis_File *vf);
extern long ov_serialnumber(OggVorbis_File *vf,int i);
extern ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i);
extern ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i);
extern double ov_time_total(OggVorbis_File *vf,int i);
extern int ov_raw_seek(OggVorbis_File *vf,long pos);
extern int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos);
extern int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos);
extern int ov_time_seek(OggVorbis_File *vf,double pos);
extern int ov_time_seek_page(OggVorbis_File *vf,double pos);
extern ogg_int64_t ov_raw_tell(OggVorbis_File *vf);
extern ogg_int64_t ov_pcm_tell(OggVorbis_File *vf);
extern double ov_time_tell(OggVorbis_File *vf);
extern vorbis_info *ov_info(OggVorbis_File *vf,int link);
extern long ov_read(OggVorbis_File *vf,char *buffer,int length,
int bigendianp,int word,int sgned,int *bitstream);
static void floor0_free_info(vorbis_info_floor *i) {
if (i) {
memset(i, 0, sizeof(vorbis_info_floor0));
_ogg_free(i);
}
}
static void floor0_free_look(vorbis_look_floor *i) {
vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
if (i) {
if (look->linearmap) _ogg_free(look->linearmap);
if (look->lsp_look) _ogg_free(look->lsp_look);
memset(look, 0, sizeof(vorbis_look_floor0));
_ogg_free(look);
}
}
static vorbis_info_floor *floor0_unpack (vorbis_info *vi, oggpack_buffer *opb) {
codec_setup_info *ci=vi->codec_setup;
int j;
vorbis_info_floor0 *info=_ogg_malloc(sizeof(vorbis_info_floor0));
info->order=oggpack_read(opb, 8);
info->rate=oggpack_read(opb, 16);
info->barkmap=oggpack_read(opb, 16);
info->ampbits=oggpack_read(opb, 6);
info->ampdB=oggpack_read(opb, 8);
info->numbooks=oggpack_read(opb, 4)+1;
if (info->order<1) goto err_out;
if (info->rate<1) goto err_out;
if (info->barkmap<1) goto err_out;
if (info->numbooks<1) goto err_out;
for (j=0; j<info->numbooks; j++) {
info->books[j]=oggpack_read(opb, 8);
if (info->books[j]<0 || info->books[j]>=ci->books) goto err_out;
}
return info;
err_out:
floor0_free_info(info);
return NULL;
}
static vorbis_look_floor *floor0_look (vorbis_dsp_state *vd, vorbis_info_mode *mi,
vorbis_info_floor *i) {
int j;
float scale;
vorbis_info *vi=vd->vi;
codec_setup_info *ci=vi->codec_setup;
vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
vorbis_look_floor0 *look=_ogg_calloc(1, sizeof(vorbis_look_floor0));
look->m=info->order;
look->n=ci->blocksizes[mi->blockflag]/2;
look->ln=info->barkmap;
look->vi=info;
scale=look->ln/(13.1f*(float)atan(.00074f*(info->rate*.5f))+2.24f*(float)atan((info->rate*.5f)*(info->rate*.5f)*1.85e-8f)+1e-4f*(info->rate*.5f));
look->linearmap=_ogg_malloc((look->n+1)*sizeof(int));
for (j=0; j<look->n; j++) {
int val=(int)floor((13.1f*(float)atan(.00074f*((info->rate*.5f)/look->n*j))+2.24f*(float)atan(((info->rate*.5f)/look->n*j)*((info->rate*.5f)/look->n*j)*1.85e-8f)+1e-4f*((info->rate*.5f)/look->n*j)) *scale);
if (val>=look->ln) val=look->ln; /* guard against the approximation */
look->linearmap[j]=val;
}
look->linearmap[j]=-1;
look->lsp_look=_ogg_malloc(look->ln*sizeof(float));
for (j=0; j<look->ln; j++)
look->lsp_look[j]=2*(float)cos((3.1415926536f)/look->ln*j);
return look;
}
static void *floor0_inverse1(vorbis_block *vb, vorbis_look_floor *i) {
vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
vorbis_info_floor0 *info=look->vi;
int j, k;
int ampraw=oggpack_read(&vb->opb, info->ampbits);
if (ampraw>0) {
long maxval=(1<<info->ampbits)-1;
float amp=(float)ampraw/maxval*info->ampdB;
int booknum=oggpack_read(&vb->opb, _ilog(info->numbooks));
if (booknum!=-1 && booknum<info->numbooks) {
backend_lookup_state *be=vb->vd->backend_state;
codebook *b=be->fullbooks+info->books[booknum];
float last=0.f;
float *lsp=_vorbis_block_alloc(vb, sizeof(float)*(look->m+1));
for (j=0; j<look->m; j+=b->dim)
if (vorbis_book_decodev_set(b, lsp+j, &vb->opb, b->dim)==-1) goto eop;
for (j=0; j<look->m; ) {
for (k=0; k<b->dim; k++, j++) lsp[j]+=last;
last=lsp[j-1];
}
lsp[look->m]=amp;
return lsp;
}
}
eop:
return NULL;
}
static int floor0_inverse2(vorbis_block *vb, vorbis_look_floor *i,
void *memo, float *out) {
vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
vorbis_info_floor0 *info=look->vi;
if (memo) {
float *lsp=(float *)memo;
float amp=lsp[look->m];
vorbis_lsp_to_curve(out, look->linearmap, look->n, look->ln,
lsp, look->m, amp, (float)info->ampdB);
return 1;
}
memset(out, 0, sizeof(float)*look->n);
return 0;
}
static void floor1_free_info(vorbis_info_floor *i) {
if (i) {
memset(i, 0, sizeof(vorbis_info_floor1));
_ogg_free(i);
}
}
static void floor1_free_look(vorbis_look_floor *i) {
vorbis_look_floor1 *look=(vorbis_look_floor1 *)i;
if (i) {
memset(look, 0, sizeof(vorbis_look_floor1));
free(i);
}
}
static vorbis_info_floor *floor1_unpack (vorbis_info *vi, oggpack_buffer *opb) {
codec_setup_info *ci=vi->codec_setup;
int j, k, count=0, maxclass=-1, rangebits;
vorbis_info_floor1 *info=_ogg_calloc(1, sizeof(vorbis_info_floor1));
info->partitions=oggpack_read(opb, 5);
for (j=0; j<info->partitions; j++) {
info->partitionclass[j]=oggpack_read(opb, 4);
if (maxclass<info->partitionclass[j]) maxclass=info->partitionclass[j];
}
for (j=0; j<maxclass+1; j++) {
info->class_dim[j]=oggpack_read(opb, 3)+1;
info->class_subs[j]=oggpack_read(opb, 2);
if (info->class_subs[j]<0)
goto err_out;
if (info->class_subs[j]) info->class_book[j]=oggpack_read(opb, 8);
if (info->class_book[j]<0 || info->class_book[j]>=ci->books)
goto err_out;
for (k=0; k<(1<<info->class_subs[j]); k++) {
info->class_subbook[j][k]=oggpack_read(opb, 8)-1;
if (info->class_subbook[j][k]<-1 || info->class_subbook[j][k]>=ci->books)
goto err_out;
}
}
info->mult=oggpack_read(opb, 2)+1;
rangebits=oggpack_read(opb, 4);
for (j=0, k=0; j<info->partitions; j++) {
count+=info->class_dim[info->partitionclass[j]];
for (; k<count; k++) {
int t=info->postlist[k+2]=oggpack_read(opb, rangebits);
if (t<0 || t>=1<<rangebits)
goto err_out;
}
}
info->postlist[0]=0;
info->postlist[1]=1<<rangebits;
return info;
err_out:
floor1_free_info(info);
return NULL;
}
static int icomp(const void *a, const void *b) {
return **(int **)a-**(int **)b;
}
static int render_point(int x0, int x1, int y0, int y1, int x) {
y0&=0x7fff;
y1&=0x7fff;
{
int dy=y1-y0;
int adx=x1-x0;
int ady=abs(dy);
int err=ady*(x-x0);
int off=err/adx;
if (dy<0) return y0-off;
return y0+off;
}
}
static const float FLOOR_fromdB_LOOKUP[256]={
1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F,
1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F,
1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F,
2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F,
2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F,
3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F,
4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F,
6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F,
7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F,
1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F,
1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F,
1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F,
2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F,
2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F,
3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F,
4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F,
5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F,
7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F,
9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F,
1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F,
1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F,
2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F,
2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F,
3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F,
4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F,
5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F,
7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F,
9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F,
0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F,
0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F,
0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F,
0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F,
0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F,
0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F,
0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F,
0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F,
0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F,
0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F,
0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F,
0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F,
0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F,
0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F,
0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F,
0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F,
0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F,
0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F,
0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F,
0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F,
0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F,
0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F,
0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F,
0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F,
0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F,
0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F,
0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F,
0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F,
0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F,
0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F,
0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F,
0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F,
0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F,
0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F,
0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F,
0.82788260F, 0.88168307F, 0.9389798F, 1.F,
};
static void render_line(int x0, int x1, int y0, int y1, float *d) {
int dy=y1-y0;
int adx=x1-x0;
int ady=abs(dy);
int base=dy/adx;
int sy=dy<0?base-1:base+1;
int x=x0;
int y=y0;
int err=0;
ady-=abs(base*adx);
d[x]*=FLOOR_fromdB_LOOKUP[y];
while (++x<x1) {
err=err+ady;
if (err>=adx) {
err-=adx;
y+=sy;
} else {
y+=base;
}
d[x]*=FLOOR_fromdB_LOOKUP[y];
}
}
static vorbis_look_floor *floor1_look(vorbis_dsp_state *vd, vorbis_info_mode *mi,
vorbis_info_floor *in) {
int *sortpointer[63+2];
vorbis_info_floor1 *info=(vorbis_info_floor1 *)in;
vorbis_look_floor1 *look=_ogg_calloc(1, sizeof(vorbis_look_floor1));
int i, j, n=0;
look->vi=info;
look->n=info->postlist[1];
for (i=0; i<info->partitions; i++) n+=info->class_dim[info->partitionclass[i]];
n+=2;
look->posts=n;
for (i=0; i<n; i++) sortpointer[i]=info->postlist+i;
qsort(sortpointer, n, sizeof(int), icomp);
for (i=0; i<n; i++) look->forward_index[i]=sortpointer[i]-info->postlist;
for (i=0; i<n; i++) look->reverse_index[look->forward_index[i]]=i;
for (i=0; i<n; i++) look->sorted_index[i]=info->postlist[look->forward_index[i]];
switch (info->mult) {
case 1: look->quant_q=256; break;
case 2: look->quant_q=128; break;
case 3: look->quant_q=86; break;
case 4: look->quant_q=64; break;
}
for (i=0; i<n-2; i++) {
int lo=0;
int hi=1;
int lx=0;
int hx=look->n;
int currentx=info->postlist[i+2];
for (j=0; j<i+2; j++) {
int x=info->postlist[j];
if (x>lx && x<currentx) {
lo=j;
lx=x;
}
if (x<hx && x>currentx) {
hi=j;
hx=x;
}
}
look->loneighbor[i]=lo;
look->hineighbor[i]=hi;
}
return look;
}
static void *floor1_inverse1(vorbis_block *vb, vorbis_look_floor *in) {
vorbis_look_floor1 *look=(vorbis_look_floor1 *)in;
vorbis_info_floor1 *info=look->vi;
codec_setup_info *ci=vb->vd->vi->codec_setup;
int i, j, k;
codebook *books=((backend_lookup_state *)(vb->vd->backend_state))->fullbooks;
if (oggpack_read(&vb->opb, 1)==1) {
int *fit_value=_vorbis_block_alloc(vb, (look->posts)*sizeof(int));
fit_value[0]=oggpack_read(&vb->opb, ilog(look->quant_q-1));
fit_value[1]=oggpack_read(&vb->opb, ilog(look->quant_q-1));
for (i=0, j=2; i<info->partitions; i++) {
int class=info->partitionclass[i];
int cdim=info->class_dim[class];
int csubbits=info->class_subs[class];
int csub=1<<csubbits;
int cval=0;
if (csubbits) {
cval=vorbis_book_decode(books+info->class_book[class], &vb->opb);
if (cval==-1) goto eop;
}
for (k=0; k<cdim; k++) {
int book=info->class_subbook[class][cval&(csub-1)];
cval>>=csubbits;
if (book>=0) {
if ((fit_value[j+k]=vorbis_book_decode(books+book, &vb->opb))==-1)
goto eop;
} else {
fit_value[j+k]=0;
}
}
j+=cdim;
}
for (i=2; i<look->posts; i++) {
int predicted=render_point(info->postlist[look->loneighbor[i-2]],
info->postlist[look->hineighbor[i-2]],
fit_value[look->loneighbor[i-2]],
fit_value[look->hineighbor[i-2]],
info->postlist[i]);
int hiroom=look->quant_q-predicted;
int loroom=predicted;
int room=(hiroom<loroom?hiroom:loroom)<<1;
int val=fit_value[i];
if (val) {
if (val>=room) {
if (hiroom>loroom) {
val = val-loroom;
} else {
val = -1-(val-hiroom);
}
} else {
if (val&1) {
val= -((val+1)>>1);
} else {
val>>=1;
}
}
fit_value[i]=val+predicted;
fit_value[look->loneighbor[i-2]]&=0x7fff;
fit_value[look->hineighbor[i-2]]&=0x7fff;
} else {
fit_value[i]=predicted|0x8000;
}
}
return fit_value;
}
eop:
return NULL;
}
static int floor1_inverse2(vorbis_block *vb, vorbis_look_floor *in, void *memo,
float *out) {
vorbis_look_floor1 *look=(vorbis_look_floor1 *)in;
vorbis_info_floor1 *info=look->vi;
codec_setup_info *ci=vb->vd->vi->codec_setup;
int n=ci->blocksizes[vb->mode]/2;
int j;
if (memo) {
int *fit_value=(int *)memo;
int hx;
int lx=0;
int ly=fit_value[0]*info->mult;
for (j=1; j<look->posts; j++) {
int current=look->forward_index[j];
int hy=fit_value[current]&0x7fff;
if (hy==fit_value[current]) {
hy*=info->mult;
hx=info->postlist[current];
render_line(lx, hx, ly, hy, out);
lx=hx;
ly=hy;
}
}
for (j=hx; j<n; j++) out[j]*=out[j-1];
return 1;
}
memset(out, 0, sizeof(float)*n);
return 0;
}
static int icount(unsigned int v) {
int ret=0;
while (v) {
ret+=v&1;
v>>=1;
}
return ret;
}
static int _01inverse(vorbis_block *vb, vorbis_look_residue *vl,
float **in, int ch,
long (*decodepart)(codebook *, float *,
oggpack_buffer *, int)) {
long i, j, k, l, s;
vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
vorbis_info_residue0 *info=look->info;
int samples_per_partition=info->grouping;
int partitions_per_word=look->phrasebook->dim;
int n=info->end-info->begin;
int partvals=n/samples_per_partition;
int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
int ***partword=alloca(ch*sizeof(int **));
partvals=partwords*partitions_per_word;
for (j=0; j<ch; j++)
partword[j]=_vorbis_block_alloc(vb, partwords*sizeof(int *));
for (s=0; s<look->stages; s++) {
for (i=info->begin, l=0; i<info->end; l++) {
if (s==0) {
for (j=0; j<ch; j++) {
int temp=vorbis_book_decode(look->phrasebook, &vb->opb);
if (temp==-1) goto eopbreak;
partword[j][l]=look->decodemap[temp];
if (partword[j][l]==NULL) goto errout;
}
}
for (k=0; k<partitions_per_word; k++, i+=samples_per_partition)
for (j=0; j<ch; j++) {
if (info->secondstages[partword[j][l][k]]&(1<<s)) {
codebook *stagebook=look->partbooks[partword[j][l][k]][s];
if (stagebook) {
if (decodepart(stagebook, in[j]+i, &vb->opb,
samples_per_partition)==-1) goto eopbreak;
}
}
}
}
}
errout:
eopbreak:
return 0;
}
static void res0_free_info(vorbis_info_residue *i) {
if (i) {
memset(i, 0, sizeof(vorbis_info_residue0));
_ogg_free(i);
}
}
static vorbis_info_residue *res0_unpack(vorbis_info *vi, oggpack_buffer *opb) {
int j, acc=0;
vorbis_info_residue0 *info=_ogg_calloc(1, sizeof(vorbis_info_residue0));
codec_setup_info *ci=vi->codec_setup;
info->begin=oggpack_read(opb, 24);
info->end=oggpack_read(opb, 24);
info->grouping=oggpack_read(opb, 24)+1;
info->partitions=oggpack_read(opb, 6)+1;
info->groupbook=oggpack_read(opb, 8);
for (j=0; j<info->partitions; j++) {
int cascade=oggpack_read(opb, 3);
if (oggpack_read(opb, 1)) cascade|=oggpack_read(opb, 5)<<3;
info->secondstages[j]=cascade;
acc+=icount(cascade);
}
for (j=0; j<acc; j++)
info->booklist[j]=oggpack_read(opb, 8);
if (info->groupbook>=ci->books) goto errout;
for (j=0; j<acc; j++)
if (info->booklist[j]>=ci->books) goto errout;
return info;
errout:
res0_free_info(info);
return NULL;
}
static vorbis_look_residue *res0_look (vorbis_dsp_state *vd, vorbis_info_mode *vm,
vorbis_info_residue *vr) {
vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
vorbis_look_residue0 *look=_ogg_calloc(1, sizeof(vorbis_look_residue0));
backend_lookup_state *be=vd->backend_state;
int j, k, acc=0;
int dim;
int maxstage=0;
look->info=info;
look->map=vm->mapping;
look->parts=info->partitions;
look->fullbooks=be->fullbooks;
look->phrasebook=be->fullbooks+info->groupbook;
dim=look->phrasebook->dim;
look->partbooks=_ogg_calloc(look->parts, sizeof(codebook **));
for (j=0; j<look->parts; j++) {
int stages=ilog(info->secondstages[j]);
if (stages) {
if (stages>maxstage) maxstage=stages;
look->partbooks[j]=_ogg_calloc(stages, sizeof(codebook *));
for (k=0; k<stages; k++)
if (info->secondstages[j]&(1<<k))
look->partbooks[j][k]=be->fullbooks+info->booklist[acc++];
}
}
look->partvals=(int)floor(pow(look->parts, dim)+0.5f);
look->stages=maxstage;
look->decodemap=_ogg_malloc(look->partvals*sizeof(int *));
for (j=0; j<look->partvals; j++) {
long val=j;
long mult=look->partvals/look->parts;
look->decodemap[j]=_ogg_malloc(dim*sizeof(int));
for (k=0; k<dim; k++) {
long deco=val/mult;
val-=deco*mult;
mult/=look->parts;
look->decodemap[j][k]=deco;
}
}
return look;
}
static void res0_free_look(vorbis_look_residue *i) {
int j;
if (i) {
vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
vorbis_info_residue0 *info=look->info;
for (j=0; j<look->parts; j++)
if (look->partbooks[j]) _ogg_free(look->partbooks[j]);
_ogg_free(look->partbooks);
for (j=0; j<look->partvals; j++)
_ogg_free(look->decodemap[j]);
_ogg_free(look->decodemap);
memset(i, 0, sizeof(vorbis_look_residue0));
_ogg_free(i);
}
}
static int res0_inverse(vorbis_block *vb, vorbis_look_residue *vl,
float **in, int *nonzero, int ch) {
int i, used=0;
for (i=0; i<ch; i++)
if (nonzero[i])
in[used++]=in[i];
if (used)
return _01inverse(vb, vl, in, used, vorbis_book_decodevs_add);
else
return 0;
}
static int res1_inverse(vorbis_block *vb, vorbis_look_residue *vl,
float **in, int *nonzero, int ch) {
int i, used=0;
for (i=0; i<ch; i++)
if (nonzero[i])