- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btac1c_mini0.h
1688 lines (1437 loc) · 39.7 KB
/
btac1c_mini0.h
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
/*
Copyright (C) 2016 by Brendan G Bohannon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
* BTAC1C Mini 0
*
* BTAC1C is an extended variant of MS-IMA-ADPCM.
* It is partially backwards compatible with normal ADPCM.
* Mono is backwards compatible for decode.
* Mono encoding can be forced to produce MS-IMA-ADPCM
* By setting ridx->bcfcn to 0x40.
* Stereo is incompatible mostly due to different bitrate/block-size.
*
* Total block size is 4 bits per logical-sample in both mono and stereo.
* To make this true, the lg2 encode/decode functions are used.
* These will insert/remove 8 (mono) or 16 (stereo) samples to get a 2^n size.
*
* So, for example, a block size of 512 bytes will encode 1024 logical samples.
*
* Nominally, this is stored in WAV files with a wFormatTag of 0x7B1C.
* For the raw ADPCM case, the tag will be 0x0011.
*/
/* Header stuff */
#ifndef BTAC1C_MINI0_H
#define BTAC1C_MINI0_H
typedef unsigned char btac1c_byte;
typedef unsigned short btac1c_u16;
typedef signed short btac1c_s16;
typedef unsigned int btac1c_u32;
typedef signed int btac1c_s32;
typedef unsigned long long btac1c_u64;
typedef signed long long btac1c_s64;
typedef struct btac1c_idxstate_s btac1c_idxstate;
struct btac1c_idxstate_s {
btac1c_u16 idx; /* step index values */
btac1c_s16 lpred; /* left/center predictor value */
btac1c_s16 rpred; /* right/side predictor value */
btac1c_byte tag; /* last (or forced) block type */
btac1c_byte bcfcn; /* center(left/right) function */
btac1c_byte bsfcn; /* side function */
btac1c_byte usefx; /* number of FIR filters to use */
btac1c_s16 firfx[4][8]; /* FIR filter coeffs (8.8) */
};
/*
* btac1c_initstate
* Initialize state to default values.
* Used for encode or decode prior to beginning.
*
* BTAC1C2_DecodeBlockMono(ibuf, obuf, len, ridx)
* BTAC1C2_DecodeBlockStereo(ibuf, obuf, len, ridx)
* Decode a block (in terms of raw ADPCM samples).
* ibuf is the input block (bytes).
* obuf is the output samples.
* len is the length (in samples of a block).
* ridx is the index state.
*
* BTAC1C2_DecodeBlockMonoLg2(ibuf, obuf, lg2, ridx)
* BTAC1C2_DecodeBlockStereoLg2(ibuf, obuf, lg2, ridx)
* Decode a block a block padded to a power-of-2 size.
* This is used so that both the block size and samples are power-of-2.
* This will insert the missing samples to make up the difference.
*
* Note that decoding for blocks may occur in any order.
* Likewise, any given block may be decoded any number of times.
*
*
* BTAC1C2_EncodeBlockMono(ibuf, obuf, len, ridx)
* BTAC1C2_EncodeBlockStereo(ibuf, obuf, len, ridx)
* Encode a block (in terms of raw ADPCM samples).
* ibuf is the input samples.
* obuf is the output block (bytes).
* len is the length (in samples of a block).
* ridx is the index state.
*
* BTAC1C2_EncodeBlockMonoLg2(ibuf, obuf, lg2, ridx)
* BTAC1C2_EncodeBlockStereoLg2(ibuf, obuf, lg2, ridx)
* Encode a block in terms of a block padded to a power-of-2 size.
*
* Note that unlike the decoder, sample blocks will need to be encoded
* sequentially. This is because encoder state will carry over between
* blocks (needed to minimize introduction of crackling and pops).
*/
#ifndef BTAC1C_HAPI
#define BTAC1C_HAPI static
#endif
/* Prototypes */
BTAC1C_HAPI void btac1c_initstate(btac1c_idxstate *ridx);
BTAC1C_HAPI int BTAC1C2_PredictSample(
int *psamp, int idx, int pfcn, btac1c_idxstate *ridx);
BTAC1C_HAPI void *BTAC1C2_GetPredictFunc(int pfcn);
BTAC1C_HAPI void BTAC1C2_DecodeBlockMono(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len, btac1c_idxstate *ridx);
BTAC1C_HAPI void BTAC1C2_DecodeMonoBlockStereo(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len, btac1c_idxstate *ridx);
BTAC1C_HAPI void BTAC1C2_DecodeJointBlockStereo(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len, btac1c_idxstate *ridx);
BTAC1C_HAPI void BTAC1C2_DecodeStereoBlockStereoI(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len, btac1c_idxstate *ridx);
BTAC1C_HAPI void BTAC1C2_DecodeStereoBlockStereo(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len, btac1c_idxstate *ridx);
BTAC1C_HAPI void BTAC1C2_DecodeBlockStereo(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len, btac1c_idxstate *ridx);
BTAC1C_HAPI void BTAC1C2_DecodeBlockStereoLg2(
btac1c_byte *ibuf, btac1c_s16 *obuf, int lg2, btac1c_idxstate *ridx);
BTAC1C_HAPI void BTAC1C2_DecodeBlockMonoLg2(
btac1c_byte *ibuf, btac1c_s16 *obuf, int lg2, btac1c_idxstate *ridx);
BTAC1C_HAPI int BTAC1C2_ErrorBlockMonoSamples(
btac1c_s16 *ibuf0, btac1c_s16 *ibuf1, int len);
BTAC1C_HAPI int BTAC1C2_ErrorBlockStereoSamples(
btac1c_s16 *ibuf0, btac1c_s16 *ibuf1, int len);
BTAC1C_HAPI int BTAC1C2_EncodeQuantUni(
int tgt, int pred, int step, int lsbit);
BTAC1C_HAPI int BTAC1C2_EncodeQuantUni2(
int tgt, int tgt2, int pred, int step, int lsbit);
BTAC1C_HAPI int BTAC1C2_EncodeQuantUni3(
int uni, int step, int pred, int tgt, int tgt2, int lsbit);
BTAC1C_HAPI void BTAC1C2_PredictSampleMulti(
int *psamp, int idx, btac1c_idxstate *ridx, int *prvec);
BTAC1C_HAPI int BTAC1C2_SelectFilterBlockMono(
btac1c_s16 *ibuf, int len, btac1c_idxstate *ridx);
BTAC1C_HAPI int BTAC1C2_SelectFilterBlockStereo(
btac1c_s16 *ibuf, int len, btac1c_idxstate *ridx,
int *rpcfcn, int *rpsfcn);
BTAC1C_HAPI void BTAC1C2_EncodeBlockMonoPfcn(
btac1c_s16 *ibuf, btac1c_byte *obuf, int len,
btac1c_idxstate *ridx, int pfcn);
BTAC1C_HAPI void BTAC1C2_EncodeBlockMono(
btac1c_s16 *ibuf, btac1c_byte *obuf, int len, btac1c_idxstate *ridx);
BTAC1C_HAPI void BTAC1C2_EncodeBlockStereoJS_Pfcn(
btac1c_s16 *ibuf, btac1c_byte *obuf, int len,
btac1c_idxstate *ridx, int pcfcn, int psfcn);
BTAC1C_HAPI int BTAC1C2_StereoBlockSizeFromSamples(int len);
BTAC1C_HAPI void BTAC1C2_EncodeBlockStereoJS(
btac1c_s16 *ibuf, btac1c_byte *obuf, int len, btac1c_idxstate *ridx);
BTAC1C_HAPI void BTAC1C2_EncodeStereoBlockStereoI(
btac1c_s16 *ibuf, btac1c_byte *obuf, int len,
btac1c_idxstate *ridx, int pfcn);
BTAC1C_HAPI void BTAC1C2_EncodeStereoBlockStereo2Pfcn(
btac1c_s16 *ibuf, btac1c_byte *obuf, int len,
btac1c_idxstate *ridx, int pfcn);
BTAC1C_HAPI void BTAC1C2_EncodeMonoBlockStereoPfcn(
btac1c_s16 *ibuf, btac1c_byte *obuf, int len,
btac1c_idxstate *ridx, int pfcn);
BTAC1C_HAPI void BTAC1C2_EncodeBlockStereo(
btac1c_s16 *ibuf, btac1c_byte *obuf, int len,
btac1c_idxstate *ridx);
BTAC1C_HAPI void BTAC1C2_EncodeBlockStereoLg2(
btac1c_s16 *ibuf, btac1c_byte *obuf, int lg2,
btac1c_idxstate *ridx);
BTAC1C_HAPI void BTAC1C2_EncodeBlockMonoLg2(
btac1c_s16 *ibuf, btac1c_byte *obuf, int lg2,
btac1c_idxstate *ridx);
/* Common */
#ifndef BTAC1C_SKIPCODE
static const int btac1c_index_table[16] = {
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8
};
static const int btac1c_step_table[128] = {
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};
BTAC1C_HAPI int BTAC1C2_PredictSample(
int *psamp, int idx, int pfcn,
btac1c_idxstate *ridx)
{
int pred, p0, p1;
int i;
i=idx;
switch(pfcn)
{
case 0:
pred=psamp[(i-1)&7];
break;
case 1:
pred=2*psamp[(i-1)&7]-psamp[(i-2)&7];
break;
case 2:
pred=(3*psamp[(i-1)&7]-psamp[(i-2)&7])>>1;
break;
case 3:
pred=(5*psamp[(i-1)&7]-psamp[(i-2)&7])>>2;
break;
case 4:
p0=(psamp[(i-1)&7]+psamp[(i-2)&7]);
p1=(psamp[(i-2)&7]+psamp[(i-3)&7]);
pred=p0-(p1>>1);
break;
case 5:
p0=(psamp[(i-1)&7]+psamp[(i-2)&7]);
p1=(psamp[(i-2)&7]+psamp[(i-3)&7]);
pred=(3*p0-p1)>>2;
break;
case 6:
p0=(psamp[(i-1)&7]+psamp[(i-2)&7]);
p1=(psamp[(i-2)&7]+psamp[(i-3)&7]);
pred=(5*p0-p1)>>3;
break;
case 7:
pred=( 18*psamp[(i-1)&7]-4*psamp[(i-2)&7]+
3*psamp[(i-3)&7]-2*psamp[(i-4)&7]+
1*psamp[(i-5)&7])/16;
break;
case 8:
pred=( 72*psamp[(i-1)&7]-16*psamp[(i-2)&7]+
12*psamp[(i-3)&7]- 8*psamp[(i-4)&7]+
5*psamp[(i-5)&7]- 3*psamp[(i-6)&7]+
3*psamp[(i-7)&7]- 1*psamp[(i-8)&7])/64;
break;
case 9:
pred=( 76*psamp[(i-1)&7]-17*psamp[(i-2)&7]+
10*psamp[(i-3)&7]- 7*psamp[(i-4)&7]+
5*psamp[(i-5)&7]- 4*psamp[(i-6)&7]+
4*psamp[(i-7)&7]- 3*psamp[(i-8)&7])/64;
break;
case 10:
p0=(psamp[(i-1)&7]+psamp[(i-2)&7]+psamp[(i-3)&7]+psamp[(i-4)&7]);
p1=(psamp[(i-5)&7]+psamp[(i-6)&7]+psamp[(i-7)&7]+psamp[(i-8)&7]);
pred=(5*p0-p1)>>4;
break;
case 11:
p0=(psamp[(i-1)&7]+psamp[(i-2)&7]+psamp[(i-3)&7]+psamp[(i-4)&7]);
p1=(psamp[(i-5)&7]+psamp[(i-6)&7]+psamp[(i-7)&7]+psamp[(i-8)&7]);
pred=(p0+p1)>>3;
break;
case 12: case 13:
case 14: case 15:
pred=(
ridx->firfx[pfcn-12][0]*psamp[(i-1)&7]+
ridx->firfx[pfcn-12][1]*psamp[(i-2)&7]+
ridx->firfx[pfcn-12][2]*psamp[(i-3)&7]+
ridx->firfx[pfcn-12][3]*psamp[(i-4)&7]+
ridx->firfx[pfcn-12][4]*psamp[(i-5)&7]+
ridx->firfx[pfcn-12][5]*psamp[(i-6)&7]+
ridx->firfx[pfcn-12][6]*psamp[(i-7)&7]+
ridx->firfx[pfcn-12][7]*psamp[(i-8)&7])/256;
break;
default:
pred=0;
break;
}
return(pred);
}
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn0(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{ return(psamp[(idx-1)&7]); }
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn1(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{ return(2*psamp[(idx-1)&7]-psamp[(idx-2)&7]); }
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn2(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{ return((3*psamp[(idx-1)&7]-psamp[(idx-2)&7])>>1); }
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn3(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{ return((5*psamp[(idx-1)&7]-psamp[(idx-2)&7])>>2); }
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn4(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{
int p0, p1;
p0=(psamp[(idx-1)&7]+psamp[(idx-2)&7]);
p1=(psamp[(idx-2)&7]+psamp[(idx-3)&7]);
return(p0-(p1>>1));
}
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn5(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{
int p0, p1;
p0=(psamp[(idx-1)&7]+psamp[(idx-2)&7]);
p1=(psamp[(idx-2)&7]+psamp[(idx-3)&7]);
return((3*p0-p1)>>2);
}
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn6(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{
int p0, p1;
p0=(psamp[(idx-1)&7]+psamp[(idx-2)&7]);
p1=(psamp[(idx-2)&7]+psamp[(idx-3)&7]);
return((5*p0-p1)>>3);
}
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn7(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{
return((18*psamp[(idx-1)&7]-4*psamp[(idx-2)&7]+
3*psamp[(idx-3)&7]-2*psamp[(idx-4)&7]+
1*psamp[(idx-5)&7])/16);
}
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn8(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{
return(( 72*psamp[(idx-1)&7]-16*psamp[(idx-2)&7]+
12*psamp[(idx-3)&7]- 8*psamp[(idx-4)&7]+
5*psamp[(idx-5)&7]- 3*psamp[(idx-6)&7]+
3*psamp[(idx-7)&7]- 1*psamp[(idx-8)&7])/64);
}
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn9(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{
return(( 76*psamp[(idx-1)&7]-17*psamp[(idx-2)&7]+
10*psamp[(idx-3)&7]- 7*psamp[(idx-4)&7]+
5*psamp[(idx-5)&7]- 4*psamp[(idx-6)&7]+
4*psamp[(idx-7)&7]- 3*psamp[(idx-8)&7])/64);
}
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn10(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{
int p0, p1;
p0=(psamp[(idx-1)&7]+psamp[(idx-2)&7]+psamp[(idx-3)&7]+psamp[(idx-4)&7]);
p1=(psamp[(idx-5)&7]+psamp[(idx-6)&7]+psamp[(idx-7)&7]+psamp[(idx-8)&7]);
return((5*p0-p1)>>3);
}
BTAC1C_HAPI int BTAC1C2_PredictSample_Pfn11(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx)
{
int p0, p1;
p0=(psamp[(idx-1)&7]+psamp[(idx-2)&7]+psamp[(idx-3)&7]+psamp[(idx-4)&7]);
p1=(psamp[(idx-5)&7]+psamp[(idx-6)&7]+psamp[(idx-7)&7]+psamp[(idx-8)&7]);
return((p0+p1)>>1);
}
BTAC1C_HAPI void *BTAC1C2_GetPredictFunc(int pfcn)
{
void *fcn;
switch(pfcn)
{
case 0: fcn=(void *)BTAC1C2_PredictSample_Pfn0; break;
case 1: fcn=(void *)BTAC1C2_PredictSample_Pfn1; break;
case 2: fcn=(void *)BTAC1C2_PredictSample_Pfn2; break;
case 3: fcn=(void *)BTAC1C2_PredictSample_Pfn3; break;
case 4: fcn=(void *)BTAC1C2_PredictSample_Pfn4; break;
case 5: fcn=(void *)BTAC1C2_PredictSample_Pfn5; break;
case 6: fcn=(void *)BTAC1C2_PredictSample_Pfn6; break;
case 7: fcn=(void *)BTAC1C2_PredictSample_Pfn7; break;
case 8: fcn=(void *)BTAC1C2_PredictSample_Pfn8; break;
case 9: fcn=(void *)BTAC1C2_PredictSample_Pfn9; break;
case 10: fcn=(void *)BTAC1C2_PredictSample_Pfn10; break;
case 11: fcn=(void *)BTAC1C2_PredictSample_Pfn11; break;
default: fcn=(void *)BTAC1C2_PredictSample; break;
}
return(fcn);
}
BTAC1C_HAPI void btac1c_memset(void *dst, int val, int sz)
{
int i;
for(i=0; i<sz; i++)
((btac1c_byte *)dst)[i]=0x00;
}
BTAC1C_HAPI void btac1c_memcpy(void *dst, void *src, int sz)
{
int i;
for(i=0; i<sz; i++)
((btac1c_byte *)dst)[i]=((btac1c_byte *)src)[i];
}
BTAC1C_HAPI void btac1c_initstate(btac1c_idxstate *ridx)
{
btac1c_memset(ridx, 0, sizeof(btac1c_idxstate));
}
/* Decoder */
BTAC1C_HAPI void BTAC1C2_DecodeBlockMono(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len, btac1c_idxstate *ridx)
{
int psamp[8];
int (*pfptr)(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx);
int pred, pfcn, index, step, diff, uni, sni;
int i, j;
pred=(btac1c_s16)(ibuf[0]+(ibuf[1]<<8));
index=ibuf[2]&127;
pfcn=ibuf[3]&15;
psamp[0]=pred; psamp[1]=pred; psamp[2]=pred; psamp[3]=pred;
psamp[4]=pred; psamp[5]=pred; psamp[6]=pred; psamp[7]=pred;
step=btac1c_step_table[index&127];
pfptr=(int (*)(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx))
BTAC1C2_GetPredictFunc(pfcn);
for(i=0; i<len; i++)
{
j=(ibuf[4+(i>>1)]>>((i&1)*4))&15;
uni=j;
sni=(j&8)?(-(j&7)):(j&7);
pred=pfptr(psamp, i, pfcn, ridx);
index=index+btac1c_index_table[uni];
index=(index<0)?0:((index>88)?88:index);
diff=((2*(uni&7)+1)*step)/8;
if(uni&8)diff=-diff;
pred=pred+diff;
step=btac1c_step_table[index];
pred=(pred<(-32768))?(-32768):((pred>32767)?32767:pred);
obuf[i]=pred;
psamp[i&7]=pred;
}
}
BTAC1C_HAPI void BTAC1C2_DecodeMonoBlockStereo(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len,
btac1c_idxstate *ridx)
{
int psamp[8];
int (*pfptr)(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx);
int pred, index, step, diff, uni, sni, ofs;
int lpred, rpred;
int pfcn;
int lp, rp, itr;
int i, j, k, l;
pred=(btac1c_s16)(ibuf[0]+(ibuf[1]<<8));
index=ibuf[2]&127;
ofs=(btac1c_s16)(ibuf[4]+(ibuf[5]<<8));
pfcn=ibuf[3]&15;
itr=ibuf[7]&3;
psamp[0]=pred; psamp[1]=pred;
psamp[2]=pred; psamp[3]=pred;
psamp[4]=pred; psamp[5]=pred;
psamp[6]=pred; psamp[7]=pred;
step=btac1c_step_table[index&127];
pfptr=(int (*)(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx))
BTAC1C2_GetPredictFunc(pfcn);
for(i=0; i<len; i++)
{
uni=(ibuf[8+(i>>1)]>>((i&1)*4))&15;
pred=pfptr(psamp, i, pfcn, ridx);
index=index+btac1c_index_table[uni];
index=(index<0)?0:((index>88)?88:index);
diff=((2*(uni&7)+1)*step)/8;
if(uni&8)diff=-diff;
pred=pred+diff;
step=btac1c_step_table[index];
pred=(pred<(-32768))?(-32768):((pred>32767)?32767:pred);
psamp[i&7]=pred;
lp=pred+ofs; rp=lp-(ofs<<1);
lp=(lp<(-32768))?(-32768):((lp>32767)?32767:lp);
rp=(rp<(-32768))?(-32768):((rp>32767)?32767:rp);
obuf[i*2+0]=lp;
obuf[i*2+1]=rp;
}
if(itr)
{
l=len-2;
for(i=0; i<l; i+=2)
{
lp=(obuf[(i+0)*2+0]+obuf[(i+2)*2+0])>>1;
obuf[(i+1)*2+0]=lp;
}
for(i=1; i<l; i+=2)
{
rp=(obuf[(i+0)*2+1]+obuf[(i+2)*2+1])>>1;
obuf[(i+1)*2+1]=rp;
}
}
}
BTAC1C_HAPI void BTAC1C2_DecodeJointBlockStereo(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len,
btac1c_idxstate *ridx)
{
int psamp[8];
int (*pfptr)(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx);
int pred, index, step, diff, uni, sni, ofs;
int lp, rp, pfcn, ispf;
int p0, p1, p2, p3;
int i, j, k, l;
/* decode center channel */
pred=(btac1c_s16)(ibuf[0]+(ibuf[1]<<8));
index=ibuf[2]&127;
pfcn=ibuf[3]&15;
step=btac1c_step_table[index&127];
psamp[0]=pred; psamp[1]=pred;
psamp[2]=pred; psamp[3]=pred;
psamp[4]=pred; psamp[5]=pred;
psamp[6]=pred; psamp[7]=pred;
ispf=(ibuf[3]!=0)|(ibuf[7]!=0);
pfptr=(int (*)(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx))
BTAC1C2_GetPredictFunc(pfcn);
for(i=0; i<len; i++)
{
k=ibuf[8+(i>>2)*2+0]|((ibuf[8+(i>>2)*2+1])<<8);
j=(k>>((i&3)*3))&7;
uni=j<<1;
uni|=(uni>>1)&(uni>>2)&ispf;
pred=pfptr(psamp, i, pfcn, ridx);
index=index+btac1c_index_table[uni];
index=(index<0)?0:((index>88)?88:index);
diff=((2*(uni&7)+1)*step)/8;
if(uni&8)diff=-diff;
pred=pred+diff;
step=btac1c_step_table[index];
if(pred!=((btac1c_s16)pred))
pred=(pred<(-32768))?(-32768):((pred>32767)?32767:pred);
obuf[i*2+0]=pred;
psamp[i&7]=pred;
}
l=len>>2;
/* decode side channel */
pred=(btac1c_s16)(ibuf[4]+(ibuf[5]<<8));
index=ibuf[6]&127;
pfcn=ibuf[7]&15;
step=btac1c_step_table[index&127];
psamp[0]=pred; psamp[1]=pred;
psamp[2]=pred; psamp[3]=pred;
psamp[4]=pred; psamp[5]=pred;
psamp[6]=pred; psamp[7]=pred;
pfptr=(int (*)(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx))
BTAC1C2_GetPredictFunc(pfcn);
for(i=0; i<l; i++)
{
k=ibuf[8+i*2+0]|((ibuf[8+i*2+1])<<8);
uni=(k>>12)&15;
pred=pfptr(psamp, i, pfcn, ridx);
index=index+btac1c_index_table[uni];
index=(index<0)?0:((index>88)?88:index);
diff=((2*(uni&7)+1)*step)/8;
if(uni&8)diff=-diff;
pred=pred+diff;
step=btac1c_step_table[index];
if(pred!=((btac1c_s16)pred))
pred=(pred<(-32768))?(-32768):((pred>32767)?32767:pred);
obuf[i*8+1]=pred;
psamp[i&7]=pred;
}
/* center/side -> stereo */
for(i=0; i<l; i++)
{
ofs=obuf[i*8+1];
for(j=0; j<4; j++)
{
k=(i*4+j)*2;
pred=obuf[k+0];
lp=pred+ofs; rp=lp-(ofs<<1);
if(((lp+32768)|(rp+32768))>>16)
{
lp=(lp<(-32768))?(-32768):((lp>32767)?32767:lp);
rp=(rp<(-32768))?(-32768):((rp>32767)?32767:rp);
}
obuf[k+0]=lp; obuf[k+1]=rp;
}
}
}
BTAC1C_HAPI void BTAC1C2_DecodeStereoBlockStereoI(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len, btac1c_idxstate *ridx)
{
int plsamp[8], prsamp[8];
int (*pfptr)(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx);
int lpred, lindex, lstep, ldiff, luni, lsni;
int rpred, rindex, rstep, rdiff, runi, rsni;
int pfcn;
int i, j, k;
lpred=(btac1c_s16)(ibuf[0]+(ibuf[1]<<8));
rpred=(btac1c_s16)(ibuf[4]+(ibuf[5]<<8));
lindex=ibuf[2];
rindex=ibuf[6];
pfcn=ibuf[3]&15;
lstep=btac1c_step_table[lindex&127];
rstep=btac1c_step_table[rindex&127];
plsamp[0]=lpred; plsamp[1]=lpred;
plsamp[2]=lpred; plsamp[3]=lpred;
plsamp[4]=lpred; plsamp[5]=lpred;
plsamp[6]=lpred; plsamp[7]=lpred;
prsamp[0]=rpred; prsamp[1]=rpred;
prsamp[2]=rpred; prsamp[3]=rpred;
prsamp[4]=rpred; prsamp[5]=rpred;
prsamp[6]=rpred; prsamp[7]=rpred;
pfptr=(int (*)(int *psamp, int idx,
int pfcn, btac1c_idxstate *ridx))
BTAC1C2_GetPredictFunc(pfcn);
for(i=0; i<len; i++)
{
k=((i>>3)*8)+((i&7)>>1);
luni=(ibuf[ 8+k]>>((i&1)*4))&15;
runi=(ibuf[12+k]>>((i&1)*4))&15;
lpred=pfptr(plsamp, i, pfcn, ridx);
rpred=pfptr(prsamp, i, pfcn, ridx);
lindex=lindex+btac1c_index_table[luni];
lindex=(lindex<0)?0:((lindex>88)?88:lindex);
ldiff=((2*(luni&7)+1)*lstep)/8;
if(luni&8)ldiff=-ldiff;
lpred=lpred+ldiff;
lstep=btac1c_step_table[lindex];
rindex=rindex+btac1c_index_table[runi];
rindex=(rindex<0)?0:((rindex>88)?88:rindex);
rdiff=((2*(runi&7)+1)*rstep)/8;
if(runi&8)rdiff=-rdiff;
rpred=rpred+rdiff;
rstep=btac1c_step_table[rindex];
lpred=(lpred<(-32768))?(-32768):((lpred>32767)?32767:lpred);
rpred=(rpred<(-32768))?(-32768):((rpred>32767)?32767:rpred);
obuf[i*2+0]=lpred;
obuf[i*2+1]=rpred;
plsamp[i&7]=lpred;
prsamp[i&7]=rpred;
}
obuf[i*2+0]=lpred;
obuf[i*2+1]=rpred;
}
BTAC1C_HAPI void BTAC1C2_DecodeStereoBlockStereo(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len, btac1c_idxstate *ridx)
{
int p0, p1, p2, p3;
int p4, p5, p6, p7;
int i0, i1, i2, i3;
int m0, m1;
int itr;
int i, j, k, l;
BTAC1C2_DecodeStereoBlockStereoI(ibuf, obuf, len>>1, ridx);
itr=ibuf[7]&3;
#if 1
if(itr==3)
{
l=len>>1;
for(i=len-1; i>0; i--)
{
i0=(i+0)>>1; i1=(i+1)>>1;
i2=(i+0)>>1; i3=(i-1)>>1;
obuf[i*2+0]=(obuf[i0*2+0]+obuf[i1*2+0])>>1;
obuf[i*2+1]=(obuf[i2*2+1]+obuf[i3*2+1])>>1;
}
}else if((itr==1) || (itr==2))
{
l=len>>1;
for(i=len-1; i>0; i--)
{
i0=(i+0)>>1; i1=(i+((itr==1)*2-1))>>1;
obuf[i*2+0]=(obuf[i0*2+0]+obuf[i1*2+0])>>1;
obuf[i*2+1]=(obuf[i0*2+1]+obuf[i1*2+1])>>1;
}
}else
{
l=len>>1;
for(i=len-1; i>0; i--)
{
j=i>>1;
obuf[i*2+0]=obuf[j*2+0];
obuf[i*2+1]=obuf[j*2+1];
}
}
#endif
}
BTAC1C_HAPI void BTAC1C2_DecodeBlockStereo(
btac1c_byte *ibuf, btac1c_s16 *obuf, int len, btac1c_idxstate *ridx)
{
int i;
i=ibuf[6];
if(i<89)
{
BTAC1C2_DecodeStereoBlockStereo(ibuf, obuf, len, ridx);
return;
}
if(i==89)
{
BTAC1C2_DecodeMonoBlockStereo(ibuf, obuf, len, ridx);
return;
}
if((i>=128) && (i<217))
{
BTAC1C2_DecodeJointBlockStereo(ibuf, obuf, len, ridx);
return;
}
for(i=0; i<len; i++)
{
obuf[i*2+0]=0;
obuf[i*2+1]=0;
}
}
BTAC1C_HAPI void BTAC1C2_DecodeBlockStereoLg2(
btac1c_byte *ibuf, btac1c_s16 *obuf, int lg2, btac1c_idxstate *ridx)
{
int i, j, k, l;
BTAC1C2_DecodeBlockStereo(ibuf, obuf, (1<<lg2)-16, ridx);
l=1<<lg2;
i=l-1;
j=(i+1)>>(lg2-4);
obuf[i*2+0]=obuf[(i-j)*2+0];
obuf[i*2+1]=obuf[(i-j)*2+1];
for(i=l-2; i>0; i--)
{
j=(i+1)>>(lg2-4);
k=(i+0)>>(lg2-4);
obuf[i*2+0]=(obuf[(i-j)*2+0]+obuf[(i-k)*2+0])>>1;
obuf[i*2+1]=(obuf[(i-j)*2+1]+obuf[(i-k)*2+1])>>1;
}
}
BTAC1C_HAPI void BTAC1C2_DecodeBlockMonoLg2(
btac1c_byte *ibuf, btac1c_s16 *obuf, int lg2, btac1c_idxstate *ridx)
{
int i, j, k, l;
BTAC1C2_DecodeBlockMono(ibuf, obuf, (1<<lg2)-8, ridx);
l=1<<lg2;
i=l-1;
j=(i+1)>>(lg2-3);
obuf[i]=obuf[i-j];
for(i=l-2; i>0; i--)
{
j=(i+1)>>(lg2-3); k=(i+0)>>(lg2-3);
obuf[i]=(obuf[i-j]+obuf[i-k])>>1;
}
}
/* Encoder */
BTAC1C_HAPI btac1c_s64 btac1c2_fakesqrt(btac1c_s64 val)
{
btac1c_s64 v, v1;
int i, j;
if(val<0)
return(-btac1c2_fakesqrt(-val));
v=val;
while(v>(1LL<<30))
{ v=v>>1; }
while((v*v)>val)
{ v=v>>1; }
for(i=1; i<17; i++)
{
v1=v+(v>>i); j=16;
if(v1==v)break;
while(((v1*v1)<=val) && (j--))
{ v=v1; v1=v+(v>>i); }
}
return(v);
}
BTAC1C_HAPI int BTAC1C2_ErrorBlockMonoSamples(
btac1c_s16 *ibuf0, btac1c_s16 *ibuf1, int len)
{
int p0, p1;
btac1c_s64 e, d;
int i, j, k;
e=0; p0=0; p1=0;
for(i=0; i<len; i++)
{
d=ibuf0[i]-ibuf1[i];
e+=d*d;
}
e=btac1c2_fakesqrt(e);
if((e<0) || (e>(1<<30)))
e=(1<<30);
return(e);
}
BTAC1C_HAPI int BTAC1C2_ErrorBlockStereoSamples(
btac1c_s16 *ibuf0, btac1c_s16 *ibuf1, int len)
{
int p0, p1, p2, p3, pc0, ps0, pc1, ps1;
btac1c_s64 e, d, dc, ds;
int i, j, k;
e=0; p0=0; p1=0;
for(i=0; i<len; i++)
{
p0=ibuf0[i*2+0];
p1=ibuf0[i*2+1];
p2=ibuf1[i*2+0];
p3=ibuf1[i*2+1];
pc0=(p0+p1)>>1;
ps0=p0-p1;
pc1=(p2+p3)>>1;
ps1=p2-p3;
dc=pc0-pc1;
ds=ps0-ps1;
ds=ds>>2;
d=dc*dc+ds*ds;
e+=d;
}
e=btac1c2_fakesqrt(e);
if((e<0) || (e>(1<<30)))
e=(1<<30);
return(e);
}
BTAC1C_HAPI int BTAC1C2_EncodeQuantUni(
int tgt, int pred, int step, int lsbit)
{
return(BTAC1C2_EncodeQuantUni2(
tgt, tgt, pred, step, lsbit));
}
BTAC1C_HAPI int BTAC1C2_EncodeQuantUni2(
int tgt, int tgt2, int pred, int step, int lsbit)
{
int uni, sni, diff;
diff=tgt-pred;
if(diff>=0)
{
sni=(diff*8-1)/(step*2);
uni=(sni>7)?7:sni;
}else
{
sni=(-diff*8-1)/(step*2);
uni=(sni>7)?15:(8|sni);
}
uni=BTAC1C2_EncodeQuantUni3(uni, step, pred, tgt, tgt2, lsbit);
return(uni);
}
BTAC1C_HAPI int BTAC1C2_EncodeQuantUni3(
int uni, int step, int pred, int tgt, int tgt2, int lsbit)
{
int uni1, uni2;
int diff, p0, p1, p2, p3, d0, d1, d2, d3;
uni1=uni+1;
uni2=uni-1;
if((uni^uni1)&(~7))uni1=uni;
if((uni^uni2)&(~7))uni2=uni;
if(lsbit)
{
if(lsbit==4)
{
uni&=~1;
uni1&=~1;
uni2&=~1;
uni|=(uni>>1)&(uni>>2)&1;
uni1|=(uni1>>1)&(uni1>>2)&1;
uni2|=(uni2>>1)&(uni2>>2)&1;
}else if(lsbit&1)
{ uni|=1; uni1|=1; uni2|=1; }
else
{ uni&=~1; uni1&=~1; uni2&=~1; }
}
diff=((2*(uni&7)+1)*step)/8;
if(uni&8)diff=-diff;
p0=pred+diff; d0=tgt-p0;
d0=d0^(d0>>31);
diff=((2*(uni1&7)+1)*step)/8;
if(uni1&8)diff=-diff;
p1=pred+diff; d1=tgt-p1;
d1=d1^(d1>>31);
diff=((2*(uni2&7)+1)*step)/8;
if(uni2&8)diff=-diff;
p2=pred+diff; d2=tgt-p2;
d2=d2^(d2>>31);
d3=tgt2-p0; d3=d3^(d3>>31); d0+=d3>>5;
d3=tgt2-p1; d3=d3^(d3>>31); d1+=d3>>5;
d3=tgt2-p2; d3=d3^(d3>>31); d2+=d3>>5;
if(d1<d0)uni=uni1;
if(d2<d0)uni=uni2;
return(uni);
}
BTAC1C_HAPI void BTAC1C2_PredictSampleMulti(
int *psamp, int idx, btac1c_idxstate *ridx,
int *prvec)
{
int fcn;
prvec[0]=BTAC1C2_PredictSample_Pfn0(psamp, idx, 0, ridx);
prvec[1]=BTAC1C2_PredictSample_Pfn1(psamp, idx, 1, ridx);
prvec[2]=BTAC1C2_PredictSample_Pfn2(psamp, idx, 2, ridx);
prvec[3]=BTAC1C2_PredictSample_Pfn3(psamp, idx, 3, ridx);
prvec[4]=BTAC1C2_PredictSample_Pfn4(psamp, idx, 4, ridx);