Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
1232 lines (1111 loc) · 45.9 KB
/
Copy pathmain.cpp
File metadata and controls
1232 lines (1111 loc) · 45.9 KB
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 <cstring>
#include <iostream>
#include <fstream>
#include <mmintrin.h>
#include <vector>
#include "jpg.h"
#include "avi.h"
#include <io.h>
#include <direct.h>
#include <cassert>
#include <limits>
#include "libpng/png.h"
//#define debug
typedef union __declspec(aligned(8)) __m64_i{
unsigned __int64 m64_u64;
__m64 m64_tm64;
__int32 m64_i32[2];
__int64 m64_i64;
unsigned __int32 m64_u32[2];
short m64_i16[4];
unsigned char m64_u8[8];
} __m64_i;
/*struct ARGBPixel{
union{
short r;
short V;
};
union{
short g;
short U;
};
union{
short b;
short Y;
};
byte a;
};*/
struct ARGBPixel{
union{
byte r;
byte V;
};
union{
byte g;
byte U;
};
union{
byte b;
byte Y;
};
byte a;
};
struct BCUCycle
{
byte H;
byte W;
};
struct jbpdInfo {
uint width = 0;
uint height = 0;
//int unk1;
//int unk2;
//int unk3;
//int unk4;
//int unk5;
//int unk6;
QuantizationTable quantizationTables[3];
//int unk8;
//int unk9;
//int unk10;
//byte* frameAddr;
//uint frameSize;
//int ret;
//int unk11;
//uint unk12;
//uint width2;
//uint height2;
//uint colorBits;
//int unk13;
//short unk14;
//int unk15;
};
struct cmv_head {
byte magic[3]; //CMV
byte cmv_version;
int cmv_frame_start_offset;
int cmv_size;
int unk;
int cmv_frame_count;
int unk2;
int unk3;
int frame_width;
int frame_height;
int unk4[2];
};
struct cmv_frame_index {
int frame_index;
int cmv_frame_size;
int cmv_original_frame_size; // height * width * 3 (RGB)
int frame_type; //2 == video, 0 == audio ?
int offset; //in file : cmv_frame_start_offset + offset
};
struct cmvVideo{
cmv_head head;
cmv_frame_index* index;
};
#pragma region Consts
const byte zigZagMap_ex1[64] = {
1 , 8 , 16, 9 , 2 , 3 , 10,
17, 24, 32, 25, 18, 11, 4 , 5 ,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6 , 7 , 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63,0
};
const __m64_i mmxOpConstant[40]={
0x0001000100010001, 0x0000040000000400,
0x0020002000200020, 0x001F001F001F001F,
0x32EC32EC32EC32EC, 0x6A0A6A0A6A0A6A0A,
0xAB0EAB0EAB0EAB0E, 0xB505B505B505B505,
0xC000400040004000, 0xAC6122A322A3539F,
0x40004000C0004000, 0xDD5DAC61539FDD5D,
0xA73B4B42324958C5, 0xCDB7EE5811A84B42,
0x4B4211A811A83249, 0xA73BCDB74B42A73B,
0xA73B58C558C558C5, 0x8C04300B300B73FC,
0x58C558C5A73B58C5, 0xCFF58C0473FCCFF5,
0x84DF686245BF7B21, 0xBA41E782187E6862,
0x6862187E187E45BF, 0x84DFBA41686284DF,
0xAC61539F539F539F, 0x92BF2D412D416D41,
0x539F539FAC61539F, 0xD2BF92BF6D41D2BF,
0x8C04625441B373FC, 0xBE4DE8EE17126254,
0x62541712171241B3, 0x8C04BE4D62548C04,
0xB4BE4B424B424B42, 0x9DAC28BA28BA6254,
0x4B424B42B4BE4B42, 0xD7469DAC6254D746,
0x979E587E3B216862, 0xC4DFEB3D14C3587E,
0x587E14C314C33B21, 0x979EC4DF587E979E
};
__m64_i mmxOpConstant2[5] = {
0x5810581058105810, 0x5B605B605B605B60,
0x7164716471647164, 0x59BC59BC59BC59BC,
0x0080008000800080
};
int mmxOpCIndex[] = {
8,16,24,32,
8,32,24,16
};
static int huff_tbl[1024];
static int skipZeros[128]{0 };
#pragma endregion
#ifdef useless
void initJBPDInfo(jbpdInfo* result){
result->unk2 = 24;
result->unk4 = 0;
result->unk5 = 1;
result->unk1 = 1;
result->unk3 = 1;
result->unk11 = 0;
result->height = 0;
result->unk6 = 80;
result->frameSize = 0;
result->width = 0;
result->unk8 = 1;
result->frameAddr = nullptr;
result->ret = 0;
result->colorBits = 24;
result->unk9 = 1;
result->unk10 = 0;
result->unk15 = 0;
}
void GetInfoFromFrameFlag(jbpdInfo* info){
if(!info->frameAddr)
return;
int* frame = reinterpret_cast<int*>(info->frameAddr);
info->width = *reinterpret_cast<short*>(reinterpret_cast<short*>(frame) + 8);
info->height = *reinterpret_cast<short*>(reinterpret_cast<short*>(frame) + 9);
info->unk3 = (frame[2] >> 30) & 1;
info->unk4 = frame[2] >> 31;
switch((frame[2] >> 28) & 3){
case 3:
info->unk5 = -1;
break;
case 2:
info->unk5 = 2;
break;
case 1:
info->unk5 = 1;
break;
default:
info->unk5 = 0;
break;
}
switch(info->unk5){
case 0:
info->width2 = (info->width + 7) & 0xFFFFFFF8;
info->height2 = (info->height + 7) & 0xFFFFFFF8;
break;
case 1:
info->width2 = (info->width + 15) & 0xFFFFFFF0;
info->height2 = (info->height + 15) & 0xFFFFFFF0;
break;
case 2:
info->width2 = (info->width + 31) & 0xFFFFFFE0;
info->height2 = (info->height + 15) & 0xFFFFFFF0;
break;
default:
return;
}
info->unk1 = *reinterpret_cast<short *>(reinterpret_cast<byte *>(frame) + 8);
info->unk2 = frame[5];
info->unk6 = frame[3];
info->unk8 = (info->width & 7) == 0 && (info->height & 7) == 0;
info->unk9 = (frame[2] >> 23) & 1;
info->unk10 = (frame[2] >> 22) & 1;
info->unk12 = (info->width2 * info->colorBits) >> 3;
}
#endif
#pragma region mmxFunc
void DCT(__m64_i *input)
{
__m64 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26;
//a total of 8 times
for(int i = 0; i < 16; i+=2){
v1 = _m_punpcklwd(_mm_cvtsi32_si64(input[i].m64_i32[0]), _mm_cvtsi32_si64(input[i + 1].m64_u32[0]));
v2 = _m_punpcklwd(_mm_cvtsi32_si64(input[i].m64_u32[1]), _mm_cvtsi32_si64(input[i + 1].m64_u32[1]));
v3 = _m_punpckldq(v1, v1);
v4 = _m_punpckhdq(v1, v1);
v5 = _m_punpckldq(v2, v2);
v6 = _m_punpckhdq(v2, v2);
v7 = _m_paddd(_m_paddd(_m_pmaddwd(mmxOpConstant[mmxOpCIndex[i / 2] + 2].m64_tm64, v3), _m_pmaddwd(mmxOpConstant[mmxOpCIndex[i / 2] + 3].m64_tm64, v5)), mmxOpConstant[1].m64_tm64);
v8 = _m_paddd(_m_paddd(_m_pmaddwd(v3, mmxOpConstant[mmxOpCIndex[i / 2]].m64_tm64), _m_pmaddwd(v5, mmxOpConstant[mmxOpCIndex[i / 2] + 1].m64_tm64)), mmxOpConstant[1].m64_tm64);
v9 = _m_paddd(_m_pmaddwd(mmxOpConstant[mmxOpCIndex[i / 2] + 6].m64_tm64, v4), _m_pmaddwd(mmxOpConstant[mmxOpCIndex[i / 2] + 7].m64_tm64, v6));
v10 = _m_paddd(_m_pmaddwd(v4, mmxOpConstant[mmxOpCIndex[i / 2] + 4].m64_tm64), _m_pmaddwd(v6, mmxOpConstant[mmxOpCIndex[i / 2] + 5].m64_tm64));
input[i].m64_tm64 = _m_packssdw(_m_psradi(_m_paddd(v10, v8), 0xBu), _m_psradi(_m_paddd(v9, v7), 0xBu));
v11 = _m_packssdw(_m_psradi(_m_psubd(v7, v9), 0xBu), _m_psradi(_m_psubd(v8, v10), 0xBu));
input[i + 1].m64_tm64 = _m_por(_m_psrldi(v11, 0x10u), _m_pslldi(v11, 0x10u));
}
//2times
for(int i = 0; i < 2; ++i){
v1 = input[i + 10].m64_tm64;
v2 = input[i + 6].m64_tm64;
v3 = input[i + 14].m64_tm64;
v4 = _m_paddsw(_m_paddsw(_m_pmulhw(v1, mmxOpConstant[6].m64_tm64), v1), v2);
v5 = _m_psubsw(v1, _m_paddsw(_m_pmulhw(mmxOpConstant[6].m64_tm64, v2), v2));
v6 = _m_paddsw(_m_pmulhw(v3, mmxOpConstant[4].m64_tm64), input[i + 2].m64_tm64);
v7 = _m_paddsw(_m_pmulhw(input[i + 12].m64_tm64, mmxOpConstant[5].m64_tm64), input[i + 4].m64_tm64);
v8 = _m_psubsw(_m_pmulhw(mmxOpConstant[4].m64_tm64, input[i + 2].m64_tm64), v3);
v9 = _m_psubsw(_m_pmulhw(mmxOpConstant[5].m64_tm64, input[i + 4].m64_tm64), input[i + 12].m64_tm64);
v10 = _m_paddsw(_m_psubsw(v8, v5), mmxOpConstant[0].m64_tm64);
input[i + 14].m64_tm64= _m_paddsw(_m_paddsw(v4, v6), mmxOpConstant[0].m64_tm64);
v11 = _m_psubsw(v6, v4);
v12 = _m_paddsw(v11, v10);
input[i + 6].m64_tm64 = _m_paddsw(v8, v5);
v13 = _m_psubsw(v11, v10);
v14 = _m_por(_m_paddsw(v12, _m_pmulhw(mmxOpConstant[7].m64_tm64, v12)), mmxOpConstant[0].m64_tm64);
v15 = _m_por(_m_paddsw(_m_pmulhw(mmxOpConstant[7].m64_tm64, v13), v13), mmxOpConstant[0].m64_tm64);
v16 = _m_paddsw(input[i + 8].m64_tm64, input[i + 0].m64_tm64);
v17 = _m_psubsw(input[i + 0].m64_tm64, input[i + 8].m64_tm64);
v18 = _m_paddsw(_m_paddsw(v16, v7), mmxOpConstant[2].m64_tm64);
v19 = _m_paddsw(_m_paddsw(v17, v9), mmxOpConstant[2].m64_tm64);
v20 = _m_psubsw(v17, v9);
v21 = _m_paddsw(_m_psubsw(v16, v7), mmxOpConstant[3].m64_tm64);
v22 = v19;
input[i].m64_tm64 = _m_psrawi(_m_paddsw(input[i + 14].m64_tm64, v18), 6u);
v23 = _m_paddsw(v20, mmxOpConstant[3].m64_tm64);
input[i + 2].m64_tm64 = _m_psrawi(_m_paddsw(v19, v14), 6u);
v24 = _m_paddsw(input[i + 6].m64_tm64, v21);
v25 = _m_psubsw(v21, input[i + 6].m64_tm64);
input[i + 4].m64_tm64 = _m_psrawi(_m_paddsw(v23, v15), 6u);
v26 = _m_psubsw(v18, input[i + 14].m64_tm64);
input[i + 6].m64_tm64 = _m_psrawi(v24, 6u);
input[i + 8].m64_tm64 = _m_psrawi(v25, 6u);
input[i + 10].m64_tm64 = _m_psrawi(_m_psubsw(v23, v15), 6u);
input[i + 12].m64_tm64 = _m_psrawi(_m_psubsw(v22, v14), 6u);
input[i + 14].m64_tm64 = _m_psrawi(v26, 6u);
}
}
void ycbcrToRGB(byte *dst, int dstSize, __m64_i *input)
{
__m64_i *v3, *v4, *v5;
__m64 v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29,
v30, v31, v32, v33, v34, v35, v36, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53;
v4 = input;
v5 = input + 64;
for(int j = 0; j < 2; ++j){
v3 = reinterpret_cast<__m64_i *>(dst) + (4 * j);
for(int i = 0; i < 4; ++i){
v7 = v5[16].m64_tm64;
v8 = _m_pmullw(_m_paddw(_m_pmulhw(mmxOpConstant2[0].m64_tm64, v5[0].m64_tm64), _m_psllwi(_m_pmulhw(mmxOpConstant2[1].m64_tm64, v7), 1u)),_mm_set_pi64x(-1));
v9 = _m_pmulhw(mmxOpConstant2[2].m64_tm64, _m_psllwi(v5->m64_tm64, 2u));
v10 = _m_pmulhw(mmxOpConstant2[3].m64_tm64, _m_psllwi(v7, 2u));
v11 = _m_punpckldq(_m_punpcklwd(v9, v8), v10);
v12 = _m_psrlqi(v10, 0x10u);
v13 = _m_psrlqi(v9, 0x10u);
v14 = _m_psrlqi(v8, 0x10u);
v15 = _m_punpckldq(_m_punpcklwd(v13, v14), v12);
v16 = _m_psrlqi(v12, 0x10u);
v17 = _m_psrlqi(v13, 0x10u);
v18 = _m_psrlqi(v14, 0x10u);
v19 = _m_punpckldq(_m_punpcklwd(v17, v18), v16);
v20 = _m_punpckldq(_m_punpcklwd(_m_psrlqi(v17, 0x10u), _m_psrlqi(v18, 0x10u)), _m_psrlqi(v16, 0x10u));
v21 = _m_paddw(v4[0].m64_tm64, mmxOpConstant2[4].m64_tm64);
v22 = _m_punpcklwd(v21, v21);
v23 = _m_psrlqi(v21, 0x10u);
v24 = _m_punpcklwd(v23, v23);
v25 = _m_psrlqi(v23, 0x10u);
v3[0].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v22, v22), v11), _m_paddsw(_m_punpcklwd(v24, v24), v11));
v26 = _m_punpcklwd(v25, v25);
v27 = _m_psrlqi(v25, 0x10u);
v28 = _m_punpcklwd(v27, v27);
v29 = _m_paddw(v4[1].m64_tm64, mmxOpConstant2[4].m64_tm64);
v3[1].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v26, v26), v15), _m_paddsw(_m_punpcklwd(v28, v28), v15));
v30 = _m_punpcklwd(v29, v29);
v31 = _m_psrlqi(v29, 0x10u);
v32 = _m_punpcklwd(v31, v31);
v33 = _m_psrlqi(v31, 0x10u);
v3[2].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v30, v30), v19), _m_paddsw(_m_punpcklwd(v32, v32), v19));
v34 = _m_punpcklwd(v33, v33);
v35 = _m_psrlqi(v33, 0x10u);
v36 = _m_punpcklwd(v35, v35);
v3[3].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v34, v34), v20), _m_paddsw(_m_punpcklwd(v36, v36), v20));
v3 = reinterpret_cast<__m64_i *>(reinterpret_cast<byte *>(v3) + dstSize);
v38 = _m_paddw(v4[2].m64_tm64, mmxOpConstant2[4].m64_tm64);
v39 = _m_punpcklwd(v38, v38);
v40 = _m_psrlqi(v38, 0x10u);
v41 = _m_punpcklwd(v40, v40);
v42 = _m_psrlqi(v40, 0x10u);
v3[0].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v39, v39), v11), _m_paddsw(_m_punpcklwd(v41, v41), v11));
v43 = _m_punpcklwd(v42, v42);
v44 = _m_psrlqi(v42, 0x10u);
v45 = _m_punpcklwd(v44, v44);
v46 = _m_paddw(v4[3].m64_tm64, mmxOpConstant2[4].m64_tm64);
v3[1].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v43, v43), v15), _m_paddsw(_m_punpcklwd(v45, v45), v15));
v47 = _m_punpcklwd(v46, v46);
v48 = _m_psrlqi(v46, 0x10u);
v49 = _m_punpcklwd(v48, v48);
v50 = _m_psrlqi(v48, 0x10u);
v3[2].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v47, v47), v19), _m_paddsw(_m_punpcklwd(v49, v49), v19));
v51 = _m_punpcklwd(v50, v50);
v52 = _m_psrlqi(v50, 0x10u);
v53 = _m_punpcklwd(v52, v52);
v3[3].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v51, v51), v20), _m_paddsw(_m_punpcklwd(v53, v53), v20));
v3 = reinterpret_cast<__m64_i *>(reinterpret_cast<byte *>(v3) + dstSize);
v5 += 2;
v4 += 4;
}
v5 -= 7;
}
dst += dstSize * sizeof(__m64_i);
v4 = input + 32;
v5 = input + 72;
for(int j = 0; j < 2; ++j){
v3 = reinterpret_cast<__m64_i *>(dst) + (4 * j);
for(int i = 0; i < 4; ++i){
v7 = v5[16].m64_tm64;
v8 = _m_pmullw(_m_paddw(_m_pmulhw(mmxOpConstant2[0].m64_tm64, v5[0].m64_tm64), _m_psllwi(_m_pmulhw(mmxOpConstant2[1].m64_tm64, v7), 1u)),_mm_set_pi64x(-1));
v9 = _m_pmulhw(mmxOpConstant2[2].m64_tm64, _m_psllwi(v5[0].m64_tm64, 2u));
v10 = _m_pmulhw(mmxOpConstant2[3].m64_tm64, _m_psllwi(v7, 2u));
v11 = _m_punpckldq(_m_punpcklwd(v9, v8), v10);
v12 = _m_psrlqi(v10, 0x10u);
v13 = _m_psrlqi(v9, 0x10u);
v14 = _m_psrlqi(v8, 0x10u);
v15 = _m_punpckldq(_m_punpcklwd(v13, v14), v12);
v16 = _m_psrlqi(v12, 0x10u);
v17 = _m_psrlqi(v13, 0x10u);
v18 = _m_psrlqi(v14, 0x10u);
v19 = _m_punpckldq(_m_punpcklwd(v17, v18), v16);
v20 = _m_punpckldq(_m_punpcklwd(_m_psrlqi(v17, 0x10u), _m_psrlqi(v18, 0x10u)), _m_psrlqi(v16, 0x10u));
v21 = _m_paddw(v4[0].m64_tm64, mmxOpConstant2[4].m64_tm64);
v22 = _m_punpcklwd(v21, v21);
v23 = _m_psrlqi(v21, 0x10u);
v24 = _m_punpcklwd(v23, v23);
v25 = _m_psrlqi(v23, 0x10u);
v3[0].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v22, v22), v11), _m_paddsw(_m_punpcklwd(v24, v24), v11));
v26 = _m_punpcklwd(v25, v25);
v27 = _m_psrlqi(v25, 0x10u);
v28 = _m_punpcklwd(v27, v27);
v29 = _m_paddw(v4[1].m64_tm64, mmxOpConstant2[4].m64_tm64);
v3[1].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v26, v26), v15), _m_paddsw(_m_punpcklwd(v28, v28), v15));
v30 = _m_punpcklwd(v29, v29);
v31 = _m_psrlqi(v29, 0x10u);
v32 = _m_punpcklwd(v31, v31);
v33 = _m_psrlqi(v31, 0x10u);
v3[2].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v30, v30), v19), _m_paddsw(_m_punpcklwd(v32, v32), v19));
v34 = _m_punpcklwd(v33, v33);
v35 = _m_psrlqi(v33, 0x10u);
v36 = _m_punpcklwd(v35, v35);
v3[3].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v34, v34), v20), _m_paddsw(_m_punpcklwd(v36, v36), v20));
v3 = reinterpret_cast<__m64_i *>(reinterpret_cast<byte *>(v3) + dstSize);
v38 = _m_paddw(v4[2].m64_tm64, mmxOpConstant2[4].m64_tm64);
v39 = _m_punpcklwd(v38, v38);
v40 = _m_psrlqi(v38, 0x10u);
v41 = _m_punpcklwd(v40, v40);
v42 = _m_psrlqi(v40, 0x10u);
v3[0].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v39, v39), v11), _m_paddsw(_m_punpcklwd(v41, v41), v11));
v43 = _m_punpcklwd(v42, v42);
v44 = _m_psrlqi(v42, 0x10u);
v45 = _m_punpcklwd(v44, v44);
v46 = _m_paddw(v4[3].m64_tm64, mmxOpConstant2[4].m64_tm64);
v3[1].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v43, v43), v15), _m_paddsw(_m_punpcklwd(v45, v45), v15));
v47 = _m_punpcklwd(v46, v46);
v48 = _m_psrlqi(v46, 0x10u);
v49 = _m_punpcklwd(v48, v48);
v50 = _m_psrlqi(v48, 0x10u);
v3[2].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v47, v47), v19), _m_paddsw(_m_punpcklwd(v49, v49), v19));
v51 = _m_punpcklwd(v50, v50);
v52 = _m_psrlqi(v50, 0x10u);
v53 = _m_punpcklwd(v52, v52);
v3[3].m64_tm64 = _m_packuswb(_m_paddsw(_m_punpcklwd(v51, v51), v20), _m_paddsw(_m_punpcklwd(v53, v53), v20));
v3 = reinterpret_cast<__m64_i *>(reinterpret_cast<byte *>(v3) + dstSize);
v5 += 2;
v4 += 4;
}
v5 -= 7;
}
}
#pragma endregion
class bitHelper{
private:
int bitShift;
int* readPos;
uint current;
public:
bitHelper(){
bitShift = 0;
current = 0;
readPos = nullptr;
}
int get_bit(){
int bit = current & 1;
current = current >> 1;
bitShift++;
if(bitShift == 32){
bitShift = 0;
readPos++;
current = *readPos;
}
return bit;
}
void resetRead(int* pos){
//std::cout << "Read position has been reset to " << pos << std::endl;
readPos = pos;
current = *pos;
bitShift = 0;
}
int get_num(uint bits)
{
int ret = 0;
for(uint j = bits; j > 0; --j)
{
ret = (ret << 1) | get_bit();
}
return ret;
}
};
int MakeTree(int size, int* arr){
memset(huff_tbl, 0, 4096);
for(int i = -size;; --i){
int index_small = 0;
int index_small2 = -100;
int val_small = 0x7D2B74FF;
for(int k = 0; k < size; ++k){//找到数组里最小的数
if(arr[k] < val_small){
index_small = k;
val_small = arr[k];
}
}
val_small = 0x7D2B74FF;
for(int l = 0; l < size; ++l){//找到数组里第二小的数
if(l != index_small && arr[l] < val_small){
index_small2 = l;
val_small = arr[l];
}
}
if(index_small < 0 || index_small2 < 0)//找不到最小值意味着树已经建完了
break;
huff_tbl[size] = index_small;//树的左边
huff_tbl[size + 512] = index_small2;//树的右边
arr[size] = arr[index_small] + arr[index_small2];//把两个节点的值相加组成新节点
size ++;
arr[index_small] = 0x7D2B7500;//删除组成新节点的两个节点
arr[index_small2] = 0x7D2B7500;
}
return size;
}
int Search(bitHelper &bh, int end, int start){
//end是终止值
int result = end - 1;
//只要找到的节点值不在0~15之内,认为其不是叶节点并继续
while ( result >= start ){
result = huff_tbl[0x200 * bh.get_bit() + result];
}
//返回找到的叶节点的值
return result;
}
void emptyM64Arrs(__m64_i *arrs, uint count){
for(int i = 0; i < count; ++i)
arrs[i].m64_i64 = 0ll;
}
void cutM64Arrs(__m64_i *src, __m64_i *dst, uint count){
for(int i = 0; i < count; ++i)
{
dst[i].m64_i64 = src[i].m64_i64;
src[i].m64_i64 = 0ll;
}
}
int getSkipCount(bitHelper &bh)
{
int* pos = skipZeros;
while ( bh.get_bit() ){
++pos;
};
return *pos;
}
static uint jt[16] = {
0,0,2,2,4,4,6,6,8,8,10,10,12,12,14,14
};
typedef __m64_i MCU[16];
typedef MCU BCU[2][2];
void decompressCMVFrame(ARGBPixel** pBitsArr, const int* PitsArr, byte* const frame, const int frameSize, const int flag)
{
int* lpintData = reinterpret_cast<int *>(frame);
bool hasExSection = false;
jbpdInfo info{};
int freq_dc[32]{ 0 };
int freq_ac[32]{ 0 };
short dcTmp[32768]{0 };
//我知道,我知道。这里浪费了不少空间。但这也是为了下面的步骤好看(
BCU Pix[3];
bitHelper bh;
info.width = *reinterpret_cast<short*>(reinterpret_cast<short*>(frame) + 8);
info.height = *reinterpret_cast<short*>(reinterpret_cast<short*>(frame) + 9);
//lpCurData = frame[frame->headerSize + 4],frame->headerSize : offset = 4, 指向freq_dc起始处
byte* lpCurData = frame + (lpintData[1] + 4);
uint jbpFlag = lpintData[2];
//freq是每种长度的编码出现的次数
memcpy(freq_dc, lpCurData, 64);
memcpy(freq_ac, lpCurData + 64, 64);
lpCurData += 128;
//读取一个数组,用来在解压AC系数时判断该跳过多少个系数
for(uint i = 0; i < 16; ++i)
skipZeros[i] = lpCurData[i] + 1;
lpCurData +=16;
//读取两个量化表
if(jbpFlag & 0x8000000)
{
for (uint i = 0; i < 64; ++i)
{
info.quantizationTables[0].table[i] = lpCurData[zigZagMap[i]];//明度量化表
info.quantizationTables[1].table[i] = lpCurData[zigZagMap[i] + 64];//色度量化表
}
lpCurData += 128;
}
if(jbpFlag & 0x1000)
hasExSection = true;
uint v70 = *lpCurData;
lpCurData += 1;
uint v71 = *lpCurData;
lpCurData += 1;
//BCUCount是整张画面按256x256分割之后所需的最小块
//暂且称为BCU(Bigger Coded Unit)
uint BCUCount = *reinterpret_cast<short*>(lpCurData) + 1;
lpCurData += 2;
//虽然他直接在Cycles数组前记录了元素个数,但在此个数之前任然记录了BCU的长和宽。所以在这里进行一下断言(防止出现些奇怪的BUG)
assert(BCUCount - 1 == v70 * v71);
//复制BCU编码信息
auto Cycles = new BCUCycle[BCUCount];
memcpy(Cycles, lpCurData, BCUCount * sizeof(BCUCycle));
lpCurData += BCUCount * sizeof(BCUCycle);
//lpCurData += (*reinterpret_cast<short *>(lpCurData + 2) * 2) + 6;
//byte* pexSec = lpCurData;
if(hasExSection)
lpCurData += ((info.width / 16 * info.height / 16 + 7) >> 3) + 2;
if(!flag)
hasExSection = false;
//解压下DC的全部值,并暂存起来
bh.resetRead(reinterpret_cast<int *>(lpCurData));//section_1 scan of DC values
int root = MakeTree(16, freq_dc);
uint DCValCnt = (info.width / 16) * (info.height / 16) * 6;
//一共有多少DC值需要解压(明度通道是14400,每个色度通道是3600),一共21600
//上面的计算方式是按每个BCU都有6个DC值计算的
for(uint i = 0; i < DCValCnt; ++i)
{
uint length = Search(bh, root, 16);
int num = bh.get_num(length);
if(num < (1 << (length - 1)))
num -= (1 << length) - 1;
if(i != 0)
num += dcTmp[i - 1];
dcTmp[i] = static_cast<short>(num);
}
bh.resetRead(reinterpret_cast<int *>(lpCurData + lpintData[7]));//section_2 scan of AC values
root = MakeTree(16, freq_ac);
short* pdcValTmp = dcTmp;
if(hasExSection && BCUCount)
{
//Executes when have extra block and flag!=0
//只有当flag不为0的时候才会执行到这里
}
else if(BCUCount)
{
for(uint i = 0; i < BCUCount; ++i)
{
//pBits的起始位置,指向一个BCU
byte* unit = reinterpret_cast<byte*>(pBitsArr[i]);
int unitSize = PitsArr[i]; //每一个元素都是1024(0x400)
int blockSize = 16 * unitSize;//控制了MCU?的高度
//16 * Cycles[i].H 为画面高度
for(uint j = 0; j < Cycles[i].H; ++j)
{
int unitTmp = 0;//在这个内存块中的偏移
//16 * Cycles[i].H 为画面宽度
for(uint k = 0; k < Cycles[i].W; ++k)
{
//每次都要清空!
for( auto q:Pix)
{
emptyM64Arrs(q[0][0], 16);
emptyM64Arrs(q[0][1], 16);
emptyM64Arrs(q[1][0], 16);
emptyM64Arrs(q[1][1], 16);
}
//指向__m64[0],共计768字节,index最大允许384
auto* pPix = reinterpret_cast<short *>(Pix);
for(uint l = 0; l < 6; ++l)
{
//YYYYUV
//l >> 2 : 0~3(前四个MCU)用第一个量化表,4~5用色度量化表
pPix[0] = info.quantizationTables[l >> 2].table[0] * *pdcValTmp;//MCU左上角的DC值
pdcValTmp++;
for(int m = 0; m < 63;)
{
int length = Search(bh, root, 16);
if(length == 0xF)//在此之后系数全为0
break;
if(length)
{
int coefficient = bh.get_num(length);//get Coefficient
if(coefficient < (1 << (length - 1)))//如果最高位bit为0
coefficient -= (1 << length) - 1;//减去在此长度最高位为1的数减1,得到的就是对应的负值
pPix[zigZagMap_ex1[m]] = info.quantizationTables[l >> 2].table[m + 1] * coefficient;
m++;
}
else//跳过一定数量的系数
{
//获得的数不可能是0
m += getSkipCount(bh);
}
}
pPix += 64;//即已经填充16个 __m64
}//循环一共填充 16 * 6 = 96 个 __m64 -> 一个BCU,包含4个明度通道以及2个色度通道
//移动一下,方便之后操作
cutM64Arrs(Pix[1][0][1], Pix[2][0][0], 16);
//Pix[i][x][y] -> __m64_i[16]
//__m64_i布局为2(列)x8(行),每个像素为signed short
//下面6次DCT共处理16x16的像素,即4个MCU
//这里各通道是分开存放的
//明度通道处理,每单元8x8,每次处理128字节数据
// ■□
// □□
DCT(Pix[0][0][0]);
// □■
// □□
DCT(Pix[0][0][1]);
// □□
// ■□
DCT(Pix[0][1][0]);
// □□
// □■
DCT(Pix[0][1][1]);
//色度通道的处理,每单元16*16
//但处理时,仍按照8x8存放,稍后再拓展到16x16
//YUV4:2:0?
//Cb
DCT(Pix[1][0][0]);
//Cr
DCT(Pix[2][0][0]);
cutM64Arrs(Pix[2][0][0], Pix[1][0][1], 16);
//这里除了进行色彩空间转换之外,还拷贝到了目标区域
// 写入的地址 写入的大小 源
ycbcrToRGB(unit + unitTmp, unitSize, Pix[0][0][0]);
unitTmp += 64;
/* MCU Cb, Cr;
cutM64Arrs(Pix[1][0][0], Cb, 16);
cutM64Arrs(Pix[2][0][0], Cr, 16);
for(uint x = 0; x < 8; ++x)
{
for(uint y = 0; y < 2; ++y)
{
Pix[0][0][y/8][(x/2)*2].m64_i16[0] = Cb[(x*2)+y].m64_i16[0];
Pix[0][0][y/8][(x/2)*2].m64_i16[2] = Cb[(x*2)+y].m64_i16[1];
Pix[0][0][y/8][(x/2)*2 + 1].m64_i16[0] = Cb[(x*2)+y].m64_i16[2];
Pix[0][0][y/8][(x/2)*2 + 1].m64_i16[2] = Cb[(x*2)+y].m64_i16[3];
}
}*/
//别问,问就是我也看不懂
//整理、复制DCT后的数据到目标区域内
//暂时不能用,因为CbCr通道的处理可能不正确
/* for(uint y = 0; y < 16; ++y)
{
for(uint x = 0; x < 4; ++x)
{
uint pixelIndex = 4096 * j + 16 * k + 256 * y + 4 * x;
//y
pBitsArr[i][pixelIndex + 0].Y = Pix[0][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_i16[0];
pBitsArr[i][pixelIndex + 1].Y = Pix[0][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_i16[1];
pBitsArr[i][pixelIndex + 2].Y = Pix[0][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_i16[2];
pBitsArr[i][pixelIndex + 3].Y = Pix[0][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_i16[3];
//420
//Cb
*//* pBitsArr[i][pixelIndex + 0].g = Pix[1][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_u16[0];
pBitsArr[i][pixelIndex + 1].g = Pix[1][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_u16[1];
pBitsArr[i][pixelIndex + 2].g = Pix[1][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_u16[2];
pBitsArr[i][pixelIndex + 3].g = Pix[1][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_u16[3];
//Cr
pBitsArr[i][pixelIndex + 0].r = Pix[2][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_u16[0];
pBitsArr[i][pixelIndex + 1].r = Pix[2][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_u16[1];
pBitsArr[i][pixelIndex + 2].r = Pix[2][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_u16[2];
pBitsArr[i][pixelIndex + 3].r = Pix[2][y / 8][x / 2][(y * 2) % 16 + x % 2].m64_u16[3];*//*
//420嗯转444
pBitsArr[i][pixelIndex + 0].U = Pix[1][0][0][jt[y]+x/2].m64_i16[0];
pBitsArr[i][pixelIndex + 1].U = Pix[1][0][0][jt[y]+x/2].m64_i16[1];
pBitsArr[i][pixelIndex + 2].U = Pix[1][0][0][jt[y]+x/2].m64_i16[2];
pBitsArr[i][pixelIndex + 3].U = Pix[1][0][0][jt[y]+x/2].m64_i16[3];
pBitsArr[i][pixelIndex + 0].V = Pix[2][0][0][jt[y]+x/2].m64_i16[0];
pBitsArr[i][pixelIndex + 1].V = Pix[2][0][0][jt[y]+x/2].m64_i16[1];
pBitsArr[i][pixelIndex + 2].V = Pix[2][0][0][jt[y]+x/2].m64_i16[2];
pBitsArr[i][pixelIndex + 3].V = Pix[2][0][0][jt[y]+x/2].m64_i16[3];
}
}*/
}
unit += blockSize;
}
}
}
delete[] Cycles;
_m_empty();
}
void YCC2RGBPix(ARGBPixel* Pixel)
{
int r;
int g;
int b;
r = Pixel->Y + 1.772f * Pixel->U + 128;
g = Pixel->Y - 0.344f * Pixel->U - 0.714f * Pixel->V + 128;
b = Pixel->Y + 1.402f * Pixel->V + 128;
if (r < 0) r = 0;
if (r > 255) r = 255;
if (g < 0) g = 0;
if (g > 255) g = 255;
if (b < 0) b = 0;
if (b > 255) b = 255;
Pixel->r = r;
Pixel->g = g;
Pixel->b = b;
}
void YCC2RGB(uint height, uint width, ARGBPixel** unitBuff, uint uWidth)
{
for(uint y = 0; y < height; ++y){
const uint mcuRow = y / 256;
const uint pixelRow = y % 256;
for(uint x = 0; x < width; x++){
const uint mcuColumn = x / 256;
const uint pixelColumn = x % 256;
const uint mcuIndex = mcuRow * uWidth + mcuColumn;
const uint pixelIndex = pixelRow * 256 + pixelColumn;
YCC2RGBPix(&unitBuff[mcuIndex][pixelIndex]);
}
}
}
void fConvert(uint height, uint width, short* bimage, ARGBPixel** unitBuff, BMPImage * image, uint uWidth)
{
uint imgindex = 0;
for(uint y = 0; y < height; ++y){
const uint mcuRow = y / 256;
const uint pixelRow = y % 256;
for(uint x = 0; x < width; x++){
const uint mcuColumn = x / 256;
const uint pixelColumn = x % 256;
const uint mcuIndex = mcuRow * uWidth + mcuColumn;
const uint pixelIndex = pixelRow * 256 + pixelColumn;
bimage[imgindex + 0] = unitBuff[mcuIndex][pixelIndex].b;
bimage[imgindex + 1] = unitBuff[mcuIndex][pixelIndex].g;
bimage[imgindex + 2] = unitBuff[mcuIndex][pixelIndex].r;
imgindex += 3;
}
}
imgindex = 0;
for(uint y = 0; y < height; ++y){
const uint blockRow = y / 8;
const uint pixelRow = y % 8;
for(uint x = 0; x < width; x++){
const uint blockColumn = x / 8;
const uint pixelColumn = x % 8;
const uint blockIndex = blockRow * image->blockWidth + blockColumn;
const uint pixelIndex = pixelRow * 8 + pixelColumn;
image->blocks[blockIndex].r[pixelIndex] = bimage[imgindex + 0];
image->blocks[blockIndex].g[pixelIndex] = bimage[imgindex + 1];
image->blocks[blockIndex].b[pixelIndex] = bimage[imgindex + 2];
imgindex += 3;
}
}
}
int write_png_file(const char *file_name , ARGBPixel* *unitBuff, uint height, uint width, uint uWidth)
{
int temp;
png_byte color_type;
png_structp png_ptr;
png_infop info_ptr;
png_bytep * row_pointers;
/* create file */
FILE *fp = fopen(file_name, "wb");
if (!fp)
{
printf("[write_png_file] File %s could not be opened for writing", file_name);
return -1;
}
/* initialize stuff */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png_ptr)
{
printf("[write_png_file] png_create_write_struct failed");
return -1;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
printf("[write_png_file] png_create_info_struct failed");
return -1;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
printf("[write_png_file] Error during init_io");
return -1;
}
png_init_io(png_ptr, fp);
/* write header */
if (setjmp(png_jmpbuf(png_ptr)))
{
printf("[write_png_file] Error during writing header");
return -1;
}
color_type = PNG_COLOR_TYPE_RGB;
png_set_IHDR(png_ptr, info_ptr, width, height,
8, color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
/* write bytes */
if (setjmp(png_jmpbuf(png_ptr)))
{
printf("[write_png_file] Error during writing bytes");
return -1;
}
temp = (3 * width);
//printf("[write_png_file] File %s is writing\n", file_name);
row_pointers = (png_bytep*)malloc(height*sizeof(png_bytep));
for(uint i = 0; i < height; i++)
{
row_pointers[i] = (png_bytep)malloc(sizeof(unsigned char)*temp);
const uint mcuRow = i / 256;
const uint pixelRow = i % 256;
for(uint x = 0; x < width; x++){
const uint mcuColumn = x / 256;
const uint pixelColumn = x % 256;
const uint mcuIndex = mcuRow * uWidth + mcuColumn;
const uint pixelIndex = pixelRow * 256 + pixelColumn;
row_pointers[i][x*3] = unitBuff[mcuIndex][pixelIndex].b; //b -> r
row_pointers[i][x*3+1] = unitBuff[mcuIndex][pixelIndex].g; //g
row_pointers[i][x*3+2] = unitBuff[mcuIndex][pixelIndex].r; //r -> b
}
}
png_write_image(png_ptr, row_pointers);
/* end write */
if (setjmp(png_jmpbuf(png_ptr)))
{
printf("[write_png_file] Error during end of write");
return -1;
}
png_write_end(png_ptr, nullptr);
/* cleanup heap allocation */
png_destroy_write_struct(&png_ptr, &info_ptr);
for (uint j=0; j<height; j++)
free(row_pointers[j]);
free(row_pointers);
fclose(fp);
return 0;
}
bool writeCMVFramesAVI(std::ifstream &cmvFile, cmvVideo* cmv, const std::string& outFilename, int *Pitch, ARGBPixel**pBits, short* bimage, BMPImage& image)
{
byte* frame = nullptr;
uint uWidth = (cmv->head.frame_width + 255) / 256;
std::vector<int> frameSizes;
frameSizes.reserve(0x64000);
unsigned long aviSize = 0;
std::ofstream ot(outFilename, std::ios::binary);
writeAVI(ot, image.width, image.height, 24, cmv->head.cmv_frame_count, 1);
int startVal = 0;
std::vector<byte> jpgBuff;
jpgBuff.reserve(0x64000);
long long fstart = cmv->head.cmv_frame_start_offset;
for(int i = startVal; i <= cmv->head.cmv_frame_count; ++i)
{
frame = new byte[cmv->index[i].cmv_frame_size];
cmvFile.seekg(fstart + cmv->index[i].offset, std::ios::beg);
cmvFile.read(reinterpret_cast<char *>(frame), cmv->index[i].cmv_frame_size);
if(cmv->index[i].frame_type!=2)
{