324
327
for(i=0;i<15;i++) {
327
norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
330
norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
328
331
scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
329
332
scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
330
333
scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
331
334
dprintf("%d: norm=%x s=%x %x %x\n",
333
336
scale_factor_mult[i][0],
334
337
scale_factor_mult[i][1],
335
338
scale_factor_mult[i][2]);
339
/* max = 18760, max sum over all 16 coefs : 44736 */
344
v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
341
ff_mpa_synth_init(window);
353
343
/* huffman decode tables */
354
huff_code_table[0] = NULL;
355
344
for(i=1;i<16;i++) {
356
345
const HuffTable *h = &mpa_huff_tables[i];
348
uint8_t tmp_bits [512];
349
uint16_t tmp_codes[512];
351
memset(tmp_bits , 0, sizeof(tmp_bits ));
352
memset(tmp_codes, 0, sizeof(tmp_codes));
361
354
xsize = h->xsize;
362
355
n = xsize * xsize;
358
for(x=0;x<xsize;x++) {
359
for(y=0;y<xsize;y++){
360
tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
361
tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
363
365
/* XXX: fail test */
364
init_vlc(&huff_vlc[i], 8, n,
365
h->bits, 1, 1, h->codes, 2, 2);
367
code_table = av_mallocz(n);
369
for(x=0;x<xsize;x++) {
371
code_table[j++] = (x << 4) | y;
373
huff_code_table[i] = code_table;
366
init_vlc(&huff_vlc[i], 7, 512,
367
tmp_bits, 1, 1, tmp_codes, 2, 2, 1);
375
369
for(i=0;i<2;i++) {
376
init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
377
mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1);
370
init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
371
mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
380
374
for(i=0;i<9;i++) {
519
520
/* cos(i*pi/64) */
521
#define COS0_0 FIXR(0.50060299823519630134)
522
#define COS0_1 FIXR(0.50547095989754365998)
523
#define COS0_2 FIXR(0.51544730992262454697)
524
#define COS0_3 FIXR(0.53104259108978417447)
525
#define COS0_4 FIXR(0.55310389603444452782)
526
#define COS0_5 FIXR(0.58293496820613387367)
527
#define COS0_6 FIXR(0.62250412303566481615)
528
#define COS0_7 FIXR(0.67480834145500574602)
529
#define COS0_8 FIXR(0.74453627100229844977)
530
#define COS0_9 FIXR(0.83934964541552703873)
531
#define COS0_10 FIXR(0.97256823786196069369)
532
#define COS0_11 FIXR(1.16943993343288495515)
533
#define COS0_12 FIXR(1.48416461631416627724)
534
#define COS0_13 FIXR(2.05778100995341155085)
535
#define COS0_14 FIXR(3.40760841846871878570)
536
#define COS0_15 FIXR(10.19000812354805681150)
538
#define COS1_0 FIXR(0.50241928618815570551)
539
#define COS1_1 FIXR(0.52249861493968888062)
540
#define COS1_2 FIXR(0.56694403481635770368)
541
#define COS1_3 FIXR(0.64682178335999012954)
542
#define COS1_4 FIXR(0.78815462345125022473)
543
#define COS1_5 FIXR(1.06067768599034747134)
544
#define COS1_6 FIXR(1.72244709823833392782)
545
#define COS1_7 FIXR(5.10114861868916385802)
547
#define COS2_0 FIXR(0.50979557910415916894)
548
#define COS2_1 FIXR(0.60134488693504528054)
549
#define COS2_2 FIXR(0.89997622313641570463)
550
#define COS2_3 FIXR(2.56291544774150617881)
552
#define COS3_0 FIXR(0.54119610014619698439)
553
#define COS3_1 FIXR(1.30656296487637652785)
555
#define COS4_0 FIXR(0.70710678118654752439)
522
#define COS0_0 FIXHR(0.50060299823519630134/2)
523
#define COS0_1 FIXHR(0.50547095989754365998/2)
524
#define COS0_2 FIXHR(0.51544730992262454697/2)
525
#define COS0_3 FIXHR(0.53104259108978417447/2)
526
#define COS0_4 FIXHR(0.55310389603444452782/2)
527
#define COS0_5 FIXHR(0.58293496820613387367/2)
528
#define COS0_6 FIXHR(0.62250412303566481615/2)
529
#define COS0_7 FIXHR(0.67480834145500574602/2)
530
#define COS0_8 FIXHR(0.74453627100229844977/2)
531
#define COS0_9 FIXHR(0.83934964541552703873/2)
532
#define COS0_10 FIXHR(0.97256823786196069369/2)
533
#define COS0_11 FIXHR(1.16943993343288495515/4)
534
#define COS0_12 FIXHR(1.48416461631416627724/4)
535
#define COS0_13 FIXHR(2.05778100995341155085/8)
536
#define COS0_14 FIXHR(3.40760841846871878570/8)
537
#define COS0_15 FIXHR(10.19000812354805681150/32)
539
#define COS1_0 FIXHR(0.50241928618815570551/2)
540
#define COS1_1 FIXHR(0.52249861493968888062/2)
541
#define COS1_2 FIXHR(0.56694403481635770368/2)
542
#define COS1_3 FIXHR(0.64682178335999012954/2)
543
#define COS1_4 FIXHR(0.78815462345125022473/2)
544
#define COS1_5 FIXHR(1.06067768599034747134/4)
545
#define COS1_6 FIXHR(1.72244709823833392782/4)
546
#define COS1_7 FIXHR(5.10114861868916385802/16)
548
#define COS2_0 FIXHR(0.50979557910415916894/2)
549
#define COS2_1 FIXHR(0.60134488693504528054/2)
550
#define COS2_2 FIXHR(0.89997622313641570463/2)
551
#define COS2_3 FIXHR(2.56291544774150617881/8)
553
#define COS3_0 FIXHR(0.54119610014619698439/2)
554
#define COS3_1 FIXHR(1.30656296487637652785/4)
556
#define COS4_0 FIXHR(0.70710678118654752439/2)
557
558
/* butterfly operator */
559
#define BF(a, b, c, s)\
560
561
tmp0 = tab[a] + tab[b];\
561
562
tmp1 = tab[a] - tab[b];\
563
tab[b] = MULL(tmp1, c);\
564
tab[b] = MULH(tmp1<<(s), c);\
566
567
#define BF1(a, b, c, d)\
569
BF(a, b, COS4_0, 1);\
570
BF(c, d,-COS4_0, 1);\
570
571
tab[c] += tab[d];\
573
574
#define BF2(a, b, c, d)\
576
BF(a, b, COS4_0, 1);\
577
BF(c, d,-COS4_0, 1);\
577
578
tab[c] += tab[d];\
578
579
tab[a] += tab[c];\
579
580
tab[c] += tab[b];\
870
883
w2 = window + 31;
873
886
p = synth_buf + 16;
874
887
SUM8(sum, +=, w, p);
875
888
p = synth_buf + 48;
876
889
SUM8(sum, -=, w + 32, p);
877
*samples = round_sample(sum);
890
*samples = round_sample(&sum);
881
894
/* we calculate two samples at the same time to avoid one memory
882
895
access per two sample */
883
896
for(j=1;j<16;j++) {
886
898
p = synth_buf + 16 + j;
887
899
SUM8P2(sum, +=, sum2, -=, w, w2, p);
888
900
p = synth_buf + 48 - j;
889
901
SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
891
*samples = round_sample(sum);
903
*samples = round_sample(&sum);
893
*samples2 = round_sample(sum2);
906
*samples2 = round_sample(&sum);
894
907
samples2 -= incr;
899
912
p = synth_buf + 32;
901
913
SUM8(sum, -=, w + 32, p);
902
*samples = round_sample(sum);
914
*samples = round_sample(&sum);
904
917
offset = (offset - 32) & 511;
905
s1->synth_buf_offset[ch] = offset;
909
#define C1 FIXR(0.99144486137381041114)
910
#define C3 FIXR(0.92387953251128675612)
911
#define C5 FIXR(0.79335334029123516458)
912
#define C7 FIXR(0.60876142900872063941)
913
#define C9 FIXR(0.38268343236508977173)
914
#define C11 FIXR(0.13052619222005159154)
916
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
918
static void imdct12(int *out, int *in)
921
int64_t in1_3, in1_9, in4_3, in4_9;
923
in1_3 = MUL64(in[1], C3);
924
in1_9 = MUL64(in[1], C9);
925
in4_3 = MUL64(in[4], C3);
926
in4_9 = MUL64(in[4], C9);
928
tmp = FRAC_RND(MUL64(in[0], C7) - in1_3 - MUL64(in[2], C11) +
929
MUL64(in[3], C1) - in4_9 - MUL64(in[5], C5));
932
tmp = FRAC_RND(MUL64(in[0] - in[3], C9) - in1_3 +
933
MUL64(in[2] + in[5], C3) - in4_9);
936
tmp = FRAC_RND(MUL64(in[0], C11) - in1_9 + MUL64(in[2], C7) -
937
MUL64(in[3], C5) + in4_3 - MUL64(in[5], C1));
940
tmp = FRAC_RND(MUL64(-in[0], C5) + in1_9 + MUL64(in[2], C1) +
941
MUL64(in[3], C11) - in4_3 - MUL64(in[5], C7));
944
tmp = FRAC_RND(MUL64(-in[0] + in[3], C3) - in1_9 +
945
MUL64(in[2] + in[5], C9) + in4_3);
948
tmp = FRAC_RND(-MUL64(in[0], C1) - in1_3 - MUL64(in[2], C5) -
949
MUL64(in[3], C7) - in4_9 - MUL64(in[5], C11));
962
#define C1 FIXR(0.98480775301220805936)
963
#define C2 FIXR(0.93969262078590838405)
964
#define C3 FIXR(0.86602540378443864676)
965
#define C4 FIXR(0.76604444311897803520)
966
#define C5 FIXR(0.64278760968653932632)
968
#define C7 FIXR(0.34202014332566873304)
969
#define C8 FIXR(0.17364817766693034885)
918
*synth_buf_offset = offset;
921
#define C3 FIXHR(0.86602540378443864676/2)
971
923
/* 0.5 / cos(pi*(2*i+1)/36) */
972
924
static const int icos36[9] = {
973
925
FIXR(0.50190991877167369479),
974
FIXR(0.51763809020504152469),
926
FIXR(0.51763809020504152469), //0
975
927
FIXR(0.55168895948124587824),
976
928
FIXR(0.61038729438072803416),
977
FIXR(0.70710678118654752439),
929
FIXR(0.70710678118654752439), //1
978
930
FIXR(0.87172339781054900991),
979
931
FIXR(1.18310079157624925896),
980
FIXR(1.93185165257813657349),
932
FIXR(1.93185165257813657349), //2
981
933
FIXR(5.73685662283492756461),
984
static const int icos72[18] = {
985
/* 0.5 / cos(pi*(2*i+19)/72) */
986
FIXR(0.74009361646113053152),
987
FIXR(0.82133981585229078570),
988
FIXR(0.93057949835178895673),
989
FIXR(1.08284028510010010928),
990
FIXR(1.30656296487637652785),
991
FIXR(1.66275476171152078719),
992
FIXR(2.31011315767264929558),
993
FIXR(3.83064878777019433457),
994
FIXR(11.46279281302667383546),
996
/* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
997
FIXR(-0.67817085245462840086),
998
FIXR(-0.63023620700513223342),
999
FIXR(-0.59284452371708034528),
1000
FIXR(-0.56369097343317117734),
1001
FIXR(-0.54119610014619698439),
1002
FIXR(-0.52426456257040533932),
1003
FIXR(-0.51213975715725461845),
1004
FIXR(-0.50431448029007636036),
1005
FIXR(-0.50047634258165998492),
936
/* 0.5 / cos(pi*(2*i+1)/36) */
937
static const int icos36h[9] = {
938
FIXHR(0.50190991877167369479/2),
939
FIXHR(0.51763809020504152469/2), //0
940
FIXHR(0.55168895948124587824/2),
941
FIXHR(0.61038729438072803416/2),
942
FIXHR(0.70710678118654752439/2), //1
943
FIXHR(0.87172339781054900991/2),
944
FIXHR(1.18310079157624925896/4),
945
FIXHR(1.93185165257813657349/4), //2
946
// FIXHR(5.73685662283492756461),
949
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
951
static void imdct12(int *out, int *in)
953
int in0, in1, in2, in3, in4, in5, t1, t2;
956
in1= in[1*3] + in[0*3];
957
in2= in[2*3] + in[1*3];
958
in3= in[3*3] + in[2*3];
959
in4= in[4*3] + in[3*3];
960
in5= in[5*3] + in[4*3];
964
in2= MULH(2*in2, C3);
965
in3= MULH(4*in3, C3);
968
t2 = MULH(2*(in1 - in5), icos36h[4]);
978
in1 = MULH(in5 + in3, icos36h[1]);
985
in5 = MULH(2*(in5 - in3), icos36h[7]);
993
#define C1 FIXHR(0.98480775301220805936/2)
994
#define C2 FIXHR(0.93969262078590838405/2)
995
#define C3 FIXHR(0.86602540378443864676/2)
996
#define C4 FIXHR(0.76604444311897803520/2)
997
#define C5 FIXHR(0.64278760968653932632/2)
998
#define C6 FIXHR(0.5/2)
999
#define C7 FIXHR(0.34202014332566873304/2)
1000
#define C8 FIXHR(0.17364817766693034885/2)
1008
1003
/* using Lee like decomposition followed by hand coded 9 points DCT */
1009
static void imdct36(int *out, int *in)
1004
static void imdct36(int *out, int *buf, int *in, int *win)
1011
1006
int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1012
1007
int tmp[18], *tmp1, *in1;
1013
int64_t in3_3, in6_6;
1015
1009
for(i=17;i>=1;i--)
1016
1010
in[i] += in[i-1];
1020
1014
for(j=0;j<2;j++) {
1021
1015
tmp1 = tmp + j;
1024
in3_3 = MUL64(in1[2*3], C3);
1025
in6_6 = MUL64(in1[2*6], C6);
1027
tmp1[0] = FRAC_RND(MUL64(in1[2*1], C1) + in3_3 +
1028
MUL64(in1[2*5], C5) + MUL64(in1[2*7], C7));
1029
tmp1[2] = in1[2*0] + FRAC_RND(MUL64(in1[2*2], C2) +
1030
MUL64(in1[2*4], C4) + in6_6 +
1031
MUL64(in1[2*8], C8));
1032
tmp1[4] = FRAC_RND(MUL64(in1[2*1] - in1[2*5] - in1[2*7], C3));
1033
tmp1[6] = FRAC_RND(MUL64(in1[2*2] - in1[2*4] - in1[2*8], C6)) -
1034
in1[2*6] + in1[2*0];
1035
tmp1[8] = FRAC_RND(MUL64(in1[2*1], C5) - in3_3 -
1036
MUL64(in1[2*5], C7) + MUL64(in1[2*7], C1));
1037
tmp1[10] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C8) -
1038
MUL64(in1[2*4], C2) + in6_6 +
1039
MUL64(in1[2*8], C4));
1040
tmp1[12] = FRAC_RND(MUL64(in1[2*1], C7) - in3_3 +
1041
MUL64(in1[2*5], C1) -
1042
MUL64(in1[2*7], C5));
1043
tmp1[14] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C4) +
1044
MUL64(in1[2*4], C8) + in6_6 -
1045
MUL64(in1[2*8], C2));
1046
tmp1[16] = in1[2*0] - in1[2*2] + in1[2*4] - in1[2*6] + in1[2*8];
1018
//more accurate but slower
1019
int64_t t0, t1, t2, t3;
1020
t2 = in1[2*4] + in1[2*8] - in1[2*2];
1022
t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1023
t1 = in1[2*0] - in1[2*6];
1024
tmp1[ 6] = t1 - (t2>>1);
1027
t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1028
t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1029
t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1031
tmp1[10] = (t3 - t0 - t2) >> 32;
1032
tmp1[ 2] = (t3 + t0 + t1) >> 32;
1033
tmp1[14] = (t3 + t2 - t1) >> 32;
1035
tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1036
t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1037
t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1038
t0 = MUL64(2*in1[2*3], C3);
1040
t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1042
tmp1[ 0] = (t2 + t3 + t0) >> 32;
1043
tmp1[12] = (t2 + t1 - t0) >> 32;
1044
tmp1[ 8] = (t3 - t1 - t0) >> 32;
1046
t2 = in1[2*4] + in1[2*8] - in1[2*2];
1048
t3 = in1[2*0] + (in1[2*6]>>1);
1049
t1 = in1[2*0] - in1[2*6];
1050
tmp1[ 6] = t1 - (t2>>1);
1053
t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1054
t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1055
t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1057
tmp1[10] = t3 - t0 - t2;
1058
tmp1[ 2] = t3 + t0 + t1;
1059
tmp1[14] = t3 + t2 - t1;
1061
tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1062
t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1063
t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1064
t0 = MULH(2*in1[2*3], C3);
1066
t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1068
tmp1[ 0] = t2 + t3 + t0;
1069
tmp1[12] = t2 + t1 - t0;
1070
tmp1[ 8] = t3 - t1 - t0;
1056
1081
t2 = tmp[i + 1];
1057
1082
t3 = tmp[i + 3];
1058
s1 = MULL(t3 + t2, icos36[j]);
1083
s1 = MULH(2*(t3 + t2), icos36h[j]);
1059
1084
s3 = MULL(t3 - t2, icos36[8 - j]);
1061
t0 = MULL(s0 + s1, icos72[9 + 8 - j]);
1062
t1 = MULL(s0 - s1, icos72[8 - j]);
1063
out[18 + 9 + j] = t0;
1064
out[18 + 8 - j] = t0;
1068
t0 = MULL(s2 + s3, icos72[9+j]);
1069
t1 = MULL(s2 - s3, icos72[j]);
1070
out[18 + 9 + (8 - j)] = t0;
1072
out[9 + (8 - j)] = -t1;
1088
out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1089
out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1090
buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1091
buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1095
out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1096
out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1097
buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1098
buf[ + j] = MULH(t0, win[18 + j]);
1078
s1 = MULL(tmp[17], icos36[4]);
1079
t0 = MULL(s0 + s1, icos72[9 + 4]);
1080
t1 = MULL(s0 - s1, icos72[4]);
1081
out[18 + 9 + 4] = t0;
1082
out[18 + 8 - 4] = t0;
1087
/* fast header check for resync */
1088
static int check_header(uint32_t header)
1091
if ((header & 0xffe00000) != 0xffe00000)
1094
if (((header >> 17) & 3) == 0)
1097
if (((header >> 12) & 0xf) == 0xf)
1100
if (((header >> 10) & 3) == 3)
1105
/* header + layer + bitrate + freq + lsf/mpeg25 */
1106
#define SAME_HEADER_MASK \
1107
(0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
1103
s1 = MULH(2*tmp[17], icos36h[4]);
1106
out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1107
out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1108
buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1109
buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1109
1112
/* header decoding. MUST check the header before because no
1110
1113
consistency check is done there. Return 1 if free format found and
1652
1626
l = mpa_huff_data[k][0];
1653
1627
linbits = mpa_huff_data[k][1];
1654
1628
vlc = &huff_vlc[l];
1655
code_table = huff_code_table[l];
1631
memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1657
1636
/* read huffcode and compute each couple */
1658
1637
for(;j>0;j--) {
1659
if (get_bits_count(&s->gb) >= end_pos)
1662
code = get_vlc(&s->gb, vlc);
1665
y = code_table[code];
1638
int exponent, x, y, v;
1639
int pos= get_bits_count(&s->gb);
1641
if (pos >= end_pos){
1642
// av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1643
switch_buffer(s, &pos, &end_pos, &end_pos2);
1644
// av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1648
y = get_vlc2(&s->gb, vlc->table, 7, 3);
1651
g->sb_hybrid[s_index ] =
1652
g->sb_hybrid[s_index+1] = 0;
1657
exponent= exponents[s_index];
1659
dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1660
i, g->region_size[i] - j, x, y, exponent);
1672
dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1673
i, g->region_size[i] - j, x, y, exponents[s_index]);
1665
v = expval_table[ exponent ][ x ];
1666
// v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1676
1668
x += get_bitsz(&s->gb, linbits);
1677
v = l3_unscale(x, exponents[s_index]);
1669
v = l3_unscale(x, exponent);
1678
1671
if (get_bits1(&s->gb))
1683
g->sb_hybrid[s_index++] = v;
1673
g->sb_hybrid[s_index] = v;
1675
v = expval_table[ exponent ][ y ];
1686
1677
y += get_bitsz(&s->gb, linbits);
1687
v = l3_unscale(y, exponents[s_index]);
1688
if (get_bits1(&s->gb))
1678
v = l3_unscale(y, exponent);
1680
if (get_bits1(&s->gb))
1682
g->sb_hybrid[s_index+1] = v;
1688
v = expval_table[ exponent ][ x ];
1690
x += get_bitsz(&s->gb, linbits);
1691
v = l3_unscale(x, exponent);
1693
if (get_bits1(&s->gb))
1695
g->sb_hybrid[s_index+!!y] = v;
1696
g->sb_hybrid[s_index+ !y] = 0;
1693
g->sb_hybrid[s_index++] = v;
1697
1702
/* high frequencies */
1698
1703
vlc = &huff_quad_vlc[g->count1table_select];
1699
last_gb.buffer = NULL;
1700
1705
while (s_index <= 572) {
1701
1707
pos = get_bits_count(&s->gb);
1702
1708
if (pos >= end_pos) {
1703
if (pos > end_pos && last_gb.buffer != NULL) {
1709
if (pos > end_pos2 && last_pos){
1704
1710
/* some encoders generate an incorrect size for this
1705
1711
part. We must go back into the data */
1713
skip_bits_long(&s->gb, last_pos - pos);
1714
av_log(NULL, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1715
if(s->error_resilience >= FF_ER_COMPLIANT)
1719
// av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1720
switch_buffer(s, &pos, &end_pos, &end_pos2);
1721
// av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1713
code = get_vlc(&s->gb, vlc);
1727
code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1714
1728
dprintf("t=%d code=%d\n", g->count1table_select, code);
1718
if (code & (8 >> i)) {
1719
/* non zero value. Could use a hand coded function for
1721
v = l3_unscale(1, exponents[s_index]);
1722
if(get_bits1(&s->gb))
1727
g->sb_hybrid[s_index++] = v;
1729
g->sb_hybrid[s_index+0]=
1730
g->sb_hybrid[s_index+1]=
1731
g->sb_hybrid[s_index+2]=
1732
g->sb_hybrid[s_index+3]= 0;
1734
const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1736
int pos= s_index+idxtab[code];
1737
code ^= 8>>idxtab[code];
1738
v = exp_table[ exponents[pos] ];
1739
// v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1740
if(get_bits1(&s->gb))
1742
g->sb_hybrid[pos] = v;
1730
while (s_index < 576)
1731
g->sb_hybrid[s_index++] = 0;
1746
/* skip extension bits */
1747
bits_left = end_pos2 - get_bits_count(&s->gb);
1748
//av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1749
if (bits_left < 0/* || bits_left > 500*/) {
1750
av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1752
}else if(bits_left > 0 && s->error_resilience >= FF_ER_AGGRESSIVE){
1753
av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1756
memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1757
skip_bits_long(&s->gb, bits_left);
1759
i= get_bits_count(&s->gb);
1760
switch_buffer(s, &i, &end_pos, &end_pos2);
2411
2529
for(ch=0;ch<s->nb_channels;ch++) {
2412
2530
samples_ptr = samples + ch;
2413
2531
for(i=0;i<nb_frames;i++) {
2414
synth_filter(s, ch, samples_ptr, s->nb_channels,
2532
ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2533
window, &s->dither_state,
2534
samples_ptr, s->nb_channels,
2415
2535
s->sb_samples[ch][i]);
2416
2536
samples_ptr += 32 * s->nb_channels;
2422
return nb_frames * 32 * sizeof(short) * s->nb_channels;
2542
return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2425
2545
static int decode_frame(AVCodecContext * avctx,
2426
void *data, int *data_size,
2427
uint8_t * buf, int buf_size)
2429
MPADecodeContext *s = avctx->priv_data;
2546
void *data, int *data_size,
2547
uint8_t * buf, int buf_size)
2549
MPADecodeContext *s = avctx->priv_data;
2552
OUT_INT *out_samples = data;
2555
if(buf_size < HEADER_SIZE)
2558
header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2559
if(ff_mpa_check_header(header) < 0){
2562
av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n");
2566
if (decode_header(s, header) == 1) {
2567
/* free format: prepare to compute frame size */
2571
/* update codec info */
2572
avctx->channels = s->nb_channels;
2573
avctx->bit_rate = s->bit_rate;
2574
avctx->sub_id = s->layer;
2577
avctx->frame_size = 384;
2580
avctx->frame_size = 1152;
2584
avctx->frame_size = 576;
2586
avctx->frame_size = 1152;
2590
if(s->frame_size<=0 || s->frame_size > buf_size){
2591
av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2593
}else if(s->frame_size < buf_size){
2594
av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2597
out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2599
*data_size = out_size;
2600
avctx->sample_rate = s->sample_rate;
2601
//FIXME maybe move the other codec info stuff from above here too
2603
av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2608
static void flush(AVCodecContext *avctx){
2609
MPADecodeContext *s = avctx->priv_data;
2610
s->last_buf_size= 0;
2613
#ifdef CONFIG_MP3ADU_DECODER
2614
static int decode_frame_adu(AVCodecContext * avctx,
2615
void *data, int *data_size,
2616
uint8_t * buf, int buf_size)
2618
MPADecodeContext *s = avctx->priv_data;
2432
2620
int len, out_size;
2433
short *out_samples = data;
2437
while (buf_size > 0) {
2438
len = s->inbuf_ptr - s->inbuf;
2439
if (s->frame_size == 0) {
2440
/* special case for next header for first frame in free
2441
format case (XXX: find a simpler method) */
2442
if (s->free_format_next_header != 0) {
2443
s->inbuf[0] = s->free_format_next_header >> 24;
2444
s->inbuf[1] = s->free_format_next_header >> 16;
2445
s->inbuf[2] = s->free_format_next_header >> 8;
2446
s->inbuf[3] = s->free_format_next_header;
2447
s->inbuf_ptr = s->inbuf + 4;
2448
s->free_format_next_header = 0;
2451
/* no header seen : find one. We need at least HEADER_SIZE
2452
bytes to parse it */
2453
len = HEADER_SIZE - len;
2457
memcpy(s->inbuf_ptr, buf_ptr, len);
2460
s->inbuf_ptr += len;
2462
if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2464
header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2465
(s->inbuf[2] << 8) | s->inbuf[3];
2467
if (check_header(header) < 0) {
2468
/* no sync found : move by one byte (inefficient, but simple!) */
2469
memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2471
dprintf("skip %x\n", header);
2472
/* reset free format frame size to give a chance
2473
to get a new bitrate */
2474
s->free_format_frame_size = 0;
2476
if (decode_header(s, header) == 1) {
2477
/* free format: prepare to compute frame size */
2480
/* update codec info */
2481
avctx->sample_rate = s->sample_rate;
2482
avctx->channels = s->nb_channels;
2483
avctx->bit_rate = s->bit_rate;
2484
avctx->sub_id = s->layer;
2487
avctx->frame_size = 384;
2490
avctx->frame_size = 1152;
2494
avctx->frame_size = 576;
2496
avctx->frame_size = 1152;
2501
} else if (s->frame_size == -1) {
2502
/* free format : find next sync to compute frame size */
2503
len = MPA_MAX_CODED_FRAME_SIZE - len;
2507
/* frame too long: resync */
2509
memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2516
memcpy(s->inbuf_ptr, buf_ptr, len);
2517
/* check for header */
2518
p = s->inbuf_ptr - 3;
2519
pend = s->inbuf_ptr + len - 4;
2521
header = (p[0] << 24) | (p[1] << 16) |
2523
header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2524
(s->inbuf[2] << 8) | s->inbuf[3];
2525
/* check with high probability that we have a
2527
if ((header & SAME_HEADER_MASK) ==
2528
(header1 & SAME_HEADER_MASK)) {
2529
/* header found: update pointers */
2530
len = (p + 4) - s->inbuf_ptr;
2534
/* compute frame size */
2535
s->free_format_next_header = header;
2536
s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2537
padding = (header1 >> 9) & 1;
2539
s->free_format_frame_size -= padding * 4;
2541
s->free_format_frame_size -= padding;
2542
dprintf("free frame size=%d padding=%d\n",
2543
s->free_format_frame_size, padding);
2544
decode_header(s, header1);
2549
/* not found: simply increase pointers */
2551
s->inbuf_ptr += len;
2554
} else if (len < s->frame_size) {
2555
if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2556
s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2557
len = s->frame_size - len;
2560
memcpy(s->inbuf_ptr, buf_ptr, len);
2562
s->inbuf_ptr += len;
2566
if (s->frame_size > 0 &&
2567
(s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2568
if (avctx->parse_only) {
2569
/* simply return the frame data */
2570
*(uint8_t **)data = s->inbuf;
2571
out_size = s->inbuf_ptr - s->inbuf;
2573
out_size = mp_decode_frame(s, out_samples);
2575
s->inbuf_ptr = s->inbuf;
2577
*data_size = out_size;
2581
return buf_ptr - buf;
2621
OUT_INT *out_samples = data;
2625
// Discard too short frames
2626
if (buf_size < HEADER_SIZE) {
2632
if (len > MPA_MAX_CODED_FRAME_SIZE)
2633
len = MPA_MAX_CODED_FRAME_SIZE;
2635
// Get header and restore sync word
2636
header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
2638
if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2643
decode_header(s, header);
2644
/* update codec info */
2645
avctx->sample_rate = s->sample_rate;
2646
avctx->channels = s->nb_channels;
2647
avctx->bit_rate = s->bit_rate;
2648
avctx->sub_id = s->layer;
2650
avctx->frame_size=s->frame_size = len;
2652
if (avctx->parse_only) {
2653
out_size = buf_size;
2655
out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2658
*data_size = out_size;
2661
#endif /* CONFIG_MP3ADU_DECODER */
2663
#ifdef CONFIG_MP3ON4_DECODER
2664
/* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2665
static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2666
static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2667
/* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2668
static int chan_offset[9][5] = {
2673
{2,0,3}, // C FLR BS
2674
{4,0,2}, // C FLR BLRS
2675
{4,0,2,5}, // C FLR BLRS LFE
2676
{4,0,2,6,5}, // C FLR BLRS BLR LFE
2681
static int decode_init_mp3on4(AVCodecContext * avctx)
2683
MP3On4DecodeContext *s = avctx->priv_data;
2686
if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2687
av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2691
s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2692
s->frames = mp3Frames[s->chan_cfg];
2694
av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2697
avctx->channels = mp3Channels[s->chan_cfg];
2699
/* Init the first mp3 decoder in standard way, so that all tables get builded
2700
* We replace avctx->priv_data with the context of the first decoder so that
2701
* decode_init() does not have to be changed.
2702
* Other decoders will be inited here copying data from the first context
2704
// Allocate zeroed memory for the first decoder context
2705
s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2706
// Put decoder context in place to make init_decode() happy
2707
avctx->priv_data = s->mp3decctx[0];
2709
// Restore mp3on4 context pointer
2710
avctx->priv_data = s;
2711
s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2713
/* Create a separate codec/context for each frame (first is already ok).
2714
* Each frame is 1 or 2 channels - up to 5 frames allowed
2716
for (i = 1; i < s->frames; i++) {
2717
s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2718
s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2719
s->mp3decctx[i]->adu_mode = 1;
2726
static int decode_close_mp3on4(AVCodecContext * avctx)
2728
MP3On4DecodeContext *s = avctx->priv_data;
2731
for (i = 0; i < s->frames; i++)
2732
if (s->mp3decctx[i])
2733
av_free(s->mp3decctx[i]);
2739
static int decode_frame_mp3on4(AVCodecContext * avctx,
2740
void *data, int *data_size,
2741
uint8_t * buf, int buf_size)
2743
MP3On4DecodeContext *s = avctx->priv_data;
2744
MPADecodeContext *m;
2745
int len, out_size = 0;
2747
OUT_INT *out_samples = data;
2748
OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2749
OUT_INT *outptr, *bp;
2751
unsigned char *start2 = buf, *start;
2753
int off = avctx->channels;
2754
int *coff = chan_offset[s->chan_cfg];
2758
// Discard too short frames
2759
if (buf_size < HEADER_SIZE) {
2764
// If only one decoder interleave is not needed
2765
outptr = s->frames == 1 ? out_samples : decoded_buf;
2767
for (fr = 0; fr < s->frames; fr++) {
2769
fsize = (start[0] << 4) | (start[1] >> 4);
2774
if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2775
fsize = MPA_MAX_CODED_FRAME_SIZE;
2776
m = s->mp3decctx[fr];
2780
header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
2782
if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2787
decode_header(m, header);
2788
mp_decode_frame(m, decoded_buf, start, fsize);
2790
n = MPA_FRAME_SIZE * m->nb_channels;
2791
out_size += n * sizeof(OUT_INT);
2793
/* interleave output data */
2794
bp = out_samples + coff[fr];
2795
if(m->nb_channels == 1) {
2796
for(j = 0; j < n; j++) {
2797
*bp = decoded_buf[j];
2801
for(j = 0; j < n; j++) {
2802
bp[0] = decoded_buf[j++];
2803
bp[1] = decoded_buf[j];
2810
/* update codec info */
2811
avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2812
avctx->frame_size= buf_size;
2813
avctx->bit_rate = 0;
2814
for (i = 0; i < s->frames; i++)
2815
avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2817
*data_size = out_size;
2820
#endif /* CONFIG_MP3ON4_DECODER */
2822
#ifdef CONFIG_MP2_DECODER
2584
2823
AVCodec mp2_decoder =