-
Notifications
You must be signed in to change notification settings - Fork 1
/
getpic.cpp
2431 lines (2105 loc) · 58.1 KB
/
getpic.cpp
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) 1996, MPEG Software Simulation Group. All Rights Reserved. */
/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any license fee or
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
* any and all warranties, whether express, implied, or statuary, including any
* implied warranties or merchantability or of fitness for a particular
* purpose. In no event shall the copyright-holder be liable for any
* incidental, punitive, or consequential damages of any kind whatsoever
* arising from the use of these programs.
*
* This disclaimer of warranty extends to the user of these programs and user's
* customers, employees, agents, transferees, successors, and assigns.
*
* The MPEG Software Simulation Group does not represent or warrant that the
* programs furnished hereunder are free of infringement of any third-party
* patents.
*
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
* are subject to royalty fees to patent holders. Many of these patents are
* general enough such that they are unavoidable regardless of implementation
* design.
*
*/
/* SSE2 optimized by Dmitry Rozhdestvensky */
#include "global.h"
#include "getbit.h"
static int cc_table[12] = {
0, 0, 0, 0, 1, 2, 1, 2, 1, 2, 1, 2
};
/* private prototypes*/
__forceinline static void Update_Picture_Buffers(void);
__forceinline static void picture_data(void);
__forceinline static void slice(int MBAmax, unsigned int code);
__forceinline static void macroblock_modes(int *pmacroblock_type, int *pmotion_type,
int *pmotion_vector_count, int *pmv_format, int *pdmv, int *pmvscale, int *pdct_type);
__forceinline static void Clear_Block(int count);
__forceinline static void Add_Block(int count, int bx, int by, int dct_type, int addflag);
__forceinline static void motion_compensation(int MBA, int macroblock_type, int motion_type,
int PMV[2][2][2], int motion_vertical_field_select[2][2], int dmvector[2], int dct_type);
__forceinline static void skipped_macroblock(int dc_dct_pred[3], int PMV[2][2][2],
int *motion_type, int motion_vertical_field_select[2][2], int *macroblock_type);
__forceinline static void decode_macroblock(int *macroblock_type, int *motion_type, int *dct_type,
int PMV[2][2][2], int dc_dct_pred[3], int motion_vertical_field_select[2][2], int dmvector[2]);
__forceinline static void Decode_MPEG1_Intra_Block(int comp, int dc_dct_pred[]);
__forceinline static void Decode_MPEG2_Intra_Block(int comp, int dc_dct_pred[]);
__forceinline static void Decode_MPEG1_Non_Intra_Block(int comp);
__forceinline static void Decode_MPEG2_Non_Intra_Block(int comp);
void motion_vector(int *PMV, int *dmvector, int h_r_size, int v_r_size,
int dmv, int mvscale, int full_pel_vector);
__forceinline static int Get_macroblock_type(void);
__forceinline static int Get_I_macroblock_type(void);
__forceinline static int Get_P_macroblock_type(void);
__forceinline static int Get_B_macroblock_type(void);
__forceinline static int Get_D_macroblock_type(void);
__forceinline static int Get_coded_block_pattern(void);
__forceinline static int Get_macroblock_address_increment(void);
__forceinline static int Get_Luma_DC_dct_diff(void);
__forceinline static int Get_Chroma_DC_dct_diff(void);
__forceinline static void form_predictions(int bx, int by, int macroblock_type, int motion_type, int PMV[2][2][2],
int motion_vertical_field_select[2][2], int dmvector[2]);
static void form_prediction(unsigned char *src[], int sfield, unsigned char *dst[], int dfield,
int lx, int lx2, int w, int h, int x, int y, int dx, int dy, int average_flag);
__forceinline static void form_component_prediction(unsigned char *src, unsigned char *dst,
int lx, int lx2, int w, int h, int x, int y, int dx, int dy, int average_flag);
/* decode one frame */
struct ENTRY
{
bool gop_start;
int lba;
__int64 position;
int pct;
int trf;
int pf;
int closed;
int pseq;
int matrix;
int vob_id;
int cell_id;
} gop_entries[MAX_PICTURES_PER_GOP];
int gop_entries_ndx = 0;
char D2VLine[2048];
void WriteD2VLine(int finish)
{
char temp[8], position[255];
int m, r;
int had_P = 0;
int mark = 0x80;
struct ENTRY ref = gop_entries [0]; // To avoid compiler warning.
struct ENTRY entries[MAX_PICTURES_PER_GOP];
unsigned int gop_marker = 0x800;
int num_to_skip, ndx;
// We need to keep a record of the history of the I frame positions
// to enable the following trick.
gop_positions[gop_positions_ndx] = gop_entries[0].position;
// An indexed unit (especially a pack) can contain multiple I frames.
// If we did nothing we would have multiple D2V lines with the same position
// and the navigation code in DGDecode would be confused. We detect this
// by seeing how many previous lines we have written with the same position.
// We store that number in the D2V file and DGDecode uses that as the
// number of I frames to skip before settling on the required one.
// So, in fact, we are indexing position plus number of I frames to skip.
num_to_skip = 0;
ndx = gop_positions_ndx;
while (ndx && gop_positions[ndx] == gop_positions[ndx-1])
{
num_to_skip++;
ndx--;
}
gop_positions_ndx++;
// Write the first part of the data line to the D2V file.
if (gop_entries[0].gop_start == true) gop_marker |= 0x100;
if (gop_entries[0].closed == 1) gop_marker |= 0x400;
if (gop_entries[0].pseq == 1) gop_marker |= 0x200;
_i64toa(gop_entries[0].position, position, 10);
sprintf(D2VLine,"%03x %d %d %s %d %d %d",
gop_marker, gop_entries[0].matrix, d2v_forward.file, position, num_to_skip,
gop_entries[0].vob_id, gop_entries[0].cell_id);
// Reorder frame bytes for display order.
for (m = 0, r = 0; m < gop_entries_ndx; m++)
{
// To speed up random access navigation, we mark frames that can
// be accessed by decoding just the GOP that they
// belong to. The mark is done by setting bit 0x10
// on the trf value. This is read by MPEG2DEC3dg
// and used to avoid having to decode the previous
// GOP when doing random access. B frames before or
// P frame in a non-closed GOP cannot be marked because
// they reference a frame in the previous GOP.
if (gop_entries[m].pct == I_TYPE)
{
gop_entries[m].trf |= mark;
ref = gop_entries[m];
}
else if (gop_entries[m].pct == P_TYPE)
{
entries[r++] = ref;
gop_entries[m].trf |= mark;
ref = gop_entries[m];
had_P = 1;
}
else if (gop_entries[m].pct == B_TYPE)
{
if (had_P || gop_entries[m].closed)
gop_entries[m].trf |= mark;
entries[r++] = gop_entries[m];
}
}
entries[r++] = ref;
for (m = 0; m < gop_entries_ndx; m++)
{
sprintf(temp," %02x", entries[m].trf | (entries[m].pct << 4) | (entries[m].pf << 6));
strcat(D2VLine, temp);
}
if (finish) strcat(D2VLine, " ff\n");
else strcat(D2VLine, "\n");
fprintf(D2VFile, "%s", D2VLine);
gop_entries_ndx = 0;
}
void SetFaultFlag(int val)
{
char fault[80];
Fault_Flag = val;
switch (val)
{
case 1:
sprintf(fault, "block error");
break;
case 2:
sprintf(fault, "mb type error");
break;
case 3:
sprintf(fault, "cbp error");
break;
case 4:
sprintf(fault, "null slice");
break;
case 5:
sprintf(fault, "mb addr inc error");
break;
case 6:
sprintf(fault, "motion code error");
break;
default:
sprintf(fault, "video error");
break;
}
SetDlgItemText(hDlg, IDC_INFO, fault);
}
void Decode_Picture()
{
__try
{
if (picture_structure==FRAME_PICTURE && Second_Field)
Second_Field = 0;
if (picture_coding_type!=B_TYPE)
{
d2v_forward = d2v_backward;
d2v_backward = d2v_current;
}
// D2V file generation rewritten by Donald Graft to support IBBPBBP...
if (D2V_Flag && (picture_structure==FRAME_PICTURE || !Second_Field))
{
if (picture_coding_type == I_TYPE && gop_entries_ndx > 0)
{
WriteD2VLine(0);
}
if (GOPSeen == true && picture_coding_type == I_TYPE)
gop_entries[gop_entries_ndx].gop_start = true;
else
gop_entries[gop_entries_ndx].gop_start = false;
GOPSeen = false;
gop_entries[gop_entries_ndx].lba = (int) d2v_current.lba;
gop_entries[gop_entries_ndx].position = d2v_current.position;
gop_entries[gop_entries_ndx].pf = progressive_frame;
gop_entries[gop_entries_ndx].pct = picture_coding_type;
gop_entries[gop_entries_ndx].trf = d2v_current.trf;
gop_entries[gop_entries_ndx].closed = ForceOpenGops ? 0 : closed_gop;
gop_entries[gop_entries_ndx].pseq = progressive_sequence;
gop_entries[gop_entries_ndx].matrix = matrix_coefficients;
gop_entries[gop_entries_ndx].vob_id = VOB_ID;
gop_entries[gop_entries_ndx].cell_id = CELL_ID;
if (gop_entries_ndx < MAX_PICTURES_PER_GOP - 1)
gop_entries_ndx++;
else
{
MessageBox(hWnd, "Too many pictures per GOP (>= 500).\nDGIndex will terminate.", NULL, MB_OK | MB_ICONERROR);
exit(1);
}
}
if (D2V_Flag)
{
if (Frame_Number && picture_structure==FRAME_PICTURE || Second_Field)
{
if (d2v_current.type==B_TYPE)
{
DetectVideoType(Frame_Number-1, d2v_current.trf);
}
else
switch (d2v_forward.type)
{
case P_TYPE:
DetectVideoType(Frame_Number-1, d2v_forward.trf);
break;
case I_TYPE:
DetectVideoType(Frame_Number-1, d2v_forward.trf);
break;
default:
SetDlgItemText(hDlg, IDC_INFO, "picture error");
break;
}
}
}
else if (!Decision_Flag)
{
/* update picture buffer pointers */
Update_Picture_Buffers();
/* decode picture data ISO/IEC 13818-2 section 6.2.3.7 */
picture_data();
/* write or display current or previously decoded reference frame */
/* ISO/IEC 13818-2 section 6.1.1.11: Frame reordering */
if (picture_structure == FRAME_PICTURE || Second_Field)
{
if (process.locate != LOCATE_RIP)
{
Write_Frame(backward_reference_frame, d2v_backward, 0);
ThreadKill(MISC_KILL);
}
else if (Frame_Number > 0)
{
if (picture_coding_type==B_TYPE)
Write_Frame(auxframe, d2v_current, Frame_Number-1);
else
Write_Frame(forward_reference_frame, d2v_forward, Frame_Number-1);
}
}
}
if (picture_structure!=FRAME_PICTURE)
Second_Field = !Second_Field;
if (!Second_Field)
Frame_Number++;
if (Info_Flag && process.locate==LOCATE_RIP && CLIPreview && Frame_Number >= 100)
{
CLIActive = 0;
SendMessage(hWnd, CLI_PREVIEW_DONE_MESSAGE, 0, 0);
ThreadKill(MISC_KILL);
}
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
if (MessageBox(hWnd, "Caught an exception during decoding! Continue?", "Exception!", MB_YESNO | MB_ICONERROR) == IDYES)
return;
else
ThreadKill(MISC_KILL);
}
}
/* reuse old picture buffers as soon as they are no longer needed */
static void Update_Picture_Buffers()
{
int cc;
unsigned char *tmp;
for (cc=0; cc<3; cc++)
{
/* B pictures do not need to be save for future reference */
if (picture_coding_type==B_TYPE)
current_frame[cc] = auxframe[cc];
else
{
if (!Second_Field)
{
/* only update at the beginning of the coded frame */
tmp = forward_reference_frame[cc];
/* the previously decoded reference frame is stored coincident with the
location where the backward reference frame is stored (backwards
prediction is not needed in P pictures) */
forward_reference_frame[cc] = backward_reference_frame[cc];
/* update pointer for potential future B pictures */
backward_reference_frame[cc] = tmp;
}
/* can erase over old backward reference frame since it is not used
in a P picture, and since any subsequent B pictures will use the
previously decoded I or P frame as the backward_reference_frame */
current_frame[cc] = backward_reference_frame[cc];
}
if (picture_structure==BOTTOM_FIELD)
current_frame[cc] += (cc==0) ? Coded_Picture_Width : ((chroma_format==CHROMA444) ? Coded_Picture_Width : Coded_Picture_Width>>1);
}
}
/* decode all macroblocks of the current picture */
/* stages described in ISO/IEC 13818-2 section 7 */
static void picture_data()
{
int MBAmax;
unsigned int code;
/* number of macroblocks per picture */
MBAmax = mb_width*mb_height;
if (picture_structure != FRAME_PICTURE)
MBAmax>>=1;
for (;;)
{
if (Stop_Flag == true)
break;
next_start_code();
code = Show_Bits(32);
if (code < SLICE_START_CODE_MIN || code > SLICE_START_CODE_MAX)
break;
Flush_Buffer(32);
slice(MBAmax, code);
}
}
/* decode all macroblocks of the current picture */
/* ISO/IEC 13818-2 section 6.3.16 */
static void slice(int MBAmax, unsigned int code)
{
int MBA, MBAinc;
int macroblock_type, motion_type, dct_type = 0;
int dc_dct_pred[3], PMV[2][2][2], motion_vertical_field_select[2][2], dmvector[2];
int slice_vert_pos_ext;
MBA = MBAinc = 0;
/* decode slice header (may change quantizer_scale) */
slice_vert_pos_ext = slice_header();
/* decode macroblock address increment */
Fault_Flag = 0;
MBAinc = Get_macroblock_address_increment();
if (MBAinc < 0)
{
// End of slice but we didn't process any macroblocks!
SetFaultFlag(4);
return;
}
/* set current location */
/* NOTE: the arithmetic used to derive macroblock_address below is
equivalent to ISO/IEC 13818-2 section 6.3.17: Macroblock */
MBA = ((slice_vert_pos_ext << 7) + (code & 255) - 1) * mb_width + MBAinc - 1;
MBAinc = 1; // first macroblock in slice: not skipped
/* reset all DC coefficient and motion vector predictors */
/* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
dc_dct_pred[0]=dc_dct_pred[1] = dc_dct_pred[2]=0;
/* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
// This while loop condition just prevents us from processing more than
// the maximum number of macroblocks possible in a picture. The loop is
// unlikely to ever terminate on this condition. Usually, it will
// terminate at the end of the slice. The end of a slice is indicated
// by 23 zeroes after a macroblock. To detect that, we use a trick in
// Get_macroblock_address_increment(). See that function for an
// explanation.
while (MBA < MBAmax)
{
if (MBAinc == 0)
{
/* decode macroblock address increment */
MBAinc = Get_macroblock_address_increment();
if (MBAinc < 0)
{
// End of slice.
break;
}
}
if (MBAinc==1)
{
decode_macroblock(¯oblock_type, &motion_type, &dct_type, PMV,
dc_dct_pred, motion_vertical_field_select, dmvector);
}
else
{
/* skipped macroblock */
/* ISO/IEC 13818-2 section 7.6.6 */
skipped_macroblock(dc_dct_pred, PMV, &motion_type, motion_vertical_field_select, ¯oblock_type);
}
if (Fault_Flag)
break;
/* ISO/IEC 13818-2 section 7.6 */
motion_compensation(MBA, macroblock_type, motion_type, PMV,
motion_vertical_field_select, dmvector, dct_type);
/* advance to next macroblock */
MBA++;
MBAinc--;
}
}
/* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
static void macroblock_modes(int *pmacroblock_type, int *pmotion_type,
int *pmotion_vector_count, int *pmv_format,
int *pdmv, int *pmvscale, int *pdct_type)
{
int macroblock_type, motion_type, motion_vector_count;
int mv_format, dmv, mvscale, dct_type;
/* get macroblock_type */
macroblock_type = Get_macroblock_type();
if (Fault_Flag) return;
/* get frame/field motion type */
if (macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD))
{
if (picture_structure==FRAME_PICTURE)
motion_type = frame_pred_frame_dct ? MC_FRAME : Get_Bits(2);
else
motion_type = Get_Bits(2);
}
else
motion_type = (picture_structure==FRAME_PICTURE) ? MC_FRAME : MC_FIELD;
/* derive motion_vector_count, mv_format and dmv, (table 6-17, 6-18) */
if (picture_structure==FRAME_PICTURE)
{
motion_vector_count = (motion_type==MC_FIELD) ? 2 : 1;
mv_format = (motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD;
}
else
{
motion_vector_count = (motion_type==MC_16X8) ? 2 : 1;
mv_format = MV_FIELD;
}
dmv = (motion_type==MC_DMV); /* dual prime */
/*
field mv predictions in frame pictures have to be scaled
ISO/IEC 13818-2 section 7.6.3.1 Decoding the motion vectors
*/
mvscale = (mv_format==MV_FIELD && picture_structure==FRAME_PICTURE);
/* get dct_type (frame DCT / field DCT) */
dct_type = picture_structure==FRAME_PICTURE && !frame_pred_frame_dct
&& (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA)) ? Get_Bits(1) : 0;
/* return values */
*pmacroblock_type = macroblock_type;
*pmotion_type = motion_type;
*pmotion_vector_count = motion_vector_count;
*pmv_format = mv_format;
*pdmv = dmv;
*pmvscale = mvscale;
*pdct_type = dct_type;
}
/* move/add 8x8-Block from block[comp] to backward_reference_frame */
/* copy reconstructed 8x8 block from block[comp] to current_frame[]
ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data
This stage also embodies some of the operations implied by:
- ISO/IEC 13818-2 section 7.6.7: Combining predictions
- ISO/IEC 13818-2 section 6.1.3: Macroblock
*/
static void Add_Block(int count, int bx, int by, int dct_type, int addflag)
{
static const __int64 mmmask_128 = 0x0080008000800080;
int Chroma_Width = (chroma_format==CHROMA444) ? Coded_Picture_Width : Coded_Picture_Width>>1;
int comp, cc, iincr, bxh, byh;
unsigned char *rfp;
short *Block_Ptr;
for (comp=0; comp<count; comp++)
{
Block_Ptr = block[comp];
cc = cc_table[comp];
bxh = bx; byh = by;
if (cc==0)
{
if (picture_structure==FRAME_PICTURE)
{
if (dct_type)
{
rfp = current_frame[0] + Coded_Picture_Width*(by+((comp&2)>>1)) + bx + ((comp&1)<<3);
iincr = Coded_Picture_Width<<1;
}
else
{
rfp = current_frame[0] + Coded_Picture_Width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
iincr = Coded_Picture_Width;
}
}
else
{
rfp = current_frame[0] + (Coded_Picture_Width<<1)*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
iincr = Coded_Picture_Width<<1;
}
}
else
{
if (chroma_format!=CHROMA444)
bxh >>= 1;
if (chroma_format==CHROMA420)
byh >>= 1;
if (picture_structure==FRAME_PICTURE)
{
if (dct_type && chroma_format!=CHROMA420)
{
// field DCT coding
rfp = current_frame[cc] + Chroma_Width*(byh+((comp&2)>>1)) + bxh + (comp&8);
iincr = Chroma_Width<<1;
}
else
{
// frame DCT coding
rfp = current_frame[cc] + Chroma_Width*(byh+((comp&2)<<2)) + bxh + (comp&8);
iincr = Chroma_Width;
}
}
else
{
// field picture
rfp = current_frame[cc] + (Chroma_Width<<1)*(byh+((comp&2)<<2)) + bxh + (comp&8);
iincr = Chroma_Width<<1;
}
}
if (cpu.sse2) // SSE2
{
if (addflag)
{
__asm
{
pxor xmm0, xmm0
mov eax, [rfp]
mov ebx, [Block_Ptr]
mov ecx, [iincr]
mov edx, ecx
add edx, edx
mov edi, edx
add edx, ecx
add edi, edi
add edi, ecx
mov esi, edx
add esi, esi
add esi, ecx
movq xmm1, qword ptr[eax]
punpcklbw xmm1, xmm0
paddsw xmm1, [ebx+16*0]
packuswb xmm1, xmm0
movq qword ptr[eax], xmm1
movq xmm2, qword ptr[eax+ecx]
punpcklbw xmm2, xmm0
paddsw xmm2, [ebx+16*1]
packuswb xmm2, xmm0
movq qword ptr[eax+ecx], xmm2
movq xmm3, qword ptr[eax+ecx*2]
punpcklbw xmm3, xmm0
paddsw xmm3, [ebx+16*2]
packuswb xmm3, xmm0
movq qword ptr[eax+ecx*2], xmm3
movq xmm4, qword ptr[eax+edx]
punpcklbw xmm4, xmm0
paddsw xmm4, [ebx+16*3]
packuswb xmm4, xmm0
movq qword ptr[eax+edx], xmm4
movq xmm5, qword ptr[eax+ecx*4]
punpcklbw xmm5, xmm0
paddsw xmm5, [ebx+16*4]
packuswb xmm5, xmm0
movq qword ptr[eax+ecx*4], xmm5
movq xmm6, qword ptr[eax+edi]
punpcklbw xmm6, xmm0
paddsw xmm6, [ebx+16*5]
packuswb xmm6, xmm0
movq qword ptr[eax+edi], xmm6
movq xmm7, qword ptr[eax+edx*2]
punpcklbw xmm7, xmm0
paddsw xmm7, [ebx+16*6]
packuswb xmm7, xmm0
movq qword ptr[eax+edx*2], xmm7
movq xmm1, qword ptr[eax+esi]
punpcklbw xmm1, xmm0
paddsw xmm1, [ebx+16*7]
packuswb xmm1, xmm0
movq qword ptr[eax+esi], xmm1
}
}
else
{
__asm
{
mov eax, 0x00800080
movd xmm7, eax
pshufd xmm7, xmm7, 0
mov eax, [rfp]
mov ebx, [Block_Ptr]
mov ecx, [iincr]
mov edx, ecx
add edx, edx
mov edi, edx
add edx, ecx
add edi, edi
add edi, ecx
mov esi, edx
add esi, esi
add esi, ecx
movdqa xmm0, [ebx+16*0]
paddsw xmm0, xmm7
packuswb xmm0, xmm0
movq qword ptr[eax], xmm0
movdqa xmm1, [ebx+16*1]
paddsw xmm1, xmm7
packuswb xmm1, xmm1
movq qword ptr[eax+ecx],xmm1
movdqa xmm2, [ebx+16*2]
paddsw xmm2, xmm7
packuswb xmm2, xmm2
movq qword ptr [eax+ecx*2], xmm2
movdqa xmm3, [ebx+16*3]
paddsw xmm3, xmm7
packuswb xmm3, xmm3
movq qword ptr [eax+edx], xmm3
movdqa xmm4, [ebx+16*4]
paddsw xmm4, xmm7
packuswb xmm4, xmm4
movq qword ptr[eax+ecx*4], xmm4
movdqa xmm5, [ebx+16*5]
paddsw xmm5, xmm7
packuswb xmm5, xmm5
movq qword ptr [eax+edi], xmm5
movdqa xmm6, [ebx+16*6]
paddsw xmm6, xmm7
packuswb xmm6, xmm6
movq qword ptr [eax+edx*2], xmm6
paddsw xmm7, [ebx+16*7]
packuswb xmm7, xmm7
movq qword ptr[eax+esi], xmm7
}
}
}
else // MMX
{
if (addflag)
{
__asm
{
pxor mm0, mm0
mov eax, [rfp]
mov ebx, [Block_Ptr]
mov edi, 8
addon:
movq mm2, [ebx+8]
movq mm3, [eax]
movq mm4, mm3
movq mm1, [ebx]
punpckhbw mm3, mm0
paddsw mm3, mm2
packuswb mm3, mm0
punpcklbw mm4, mm0
psllq mm3, 32
paddsw mm4, mm1
packuswb mm4, mm0
por mm3, mm4
add ebx, 16
dec edi
movq [eax], mm3
add eax, [iincr]
cmp edi, 0x00
jg addon
}
}
else
{
__asm
{
mov eax, [rfp]
mov ebx, [Block_Ptr]
mov edi, 8
pxor mm0, mm0
movq mm1, [mmmask_128]
addoff:
movq mm3, [ebx+8]
movq mm4, [ebx]
paddsw mm3, mm1
paddsw mm4, mm1
packuswb mm3, mm0
packuswb mm4, mm0
psllq mm3, 32
por mm3, mm4
add ebx, 16
dec edi
movq [eax], mm3
add eax, [iincr]
cmp edi, 0x00
jg addoff
}
}
}
}
__asm emms;
}
/* set scratch pad macroblock to zero */
static void Clear_Block(int count)
{
int comp;
short *Block_Ptr;
for (comp=0; comp<count; comp++)
{
Block_Ptr = block[comp];
if (cpu.sse2) // SSE2
{
__asm
{
mov eax, [Block_Ptr];
pxor xmm0, xmm0;
movdqa [eax+0 ], xmm0;
movdqa [eax+16 ], xmm0;
movdqa [eax+32 ], xmm0;
movdqa [eax+48 ], xmm0;
movdqa [eax+64 ], xmm0;
movdqa [eax+80 ], xmm0;
movdqa [eax+96 ], xmm0;
movdqa [eax+112 ], xmm0;
}
}
else // MMX
{
__asm
{
mov eax, [Block_Ptr];
pxor mm0, mm0;
movq [eax+0 ], mm0;
movq [eax+8 ], mm0;
movq [eax+16], mm0;
movq [eax+24], mm0;
movq [eax+32], mm0;
movq [eax+40], mm0;
movq [eax+48], mm0;
movq [eax+56], mm0;
movq [eax+64], mm0;
movq [eax+72], mm0;
movq [eax+80], mm0;
movq [eax+88], mm0;
movq [eax+96], mm0;
movq [eax+104],mm0;
movq [eax+112],mm0;
movq [eax+120],mm0;
}
}
}
__asm emms;
}
/* ISO/IEC 13818-2 section 7.6 */
static void motion_compensation(int MBA, int macroblock_type, int motion_type,
int PMV[2][2][2], int motion_vertical_field_select[2][2],
int dmvector[2], int dct_type)
{
int bx, by;
int comp;
/* derive current macroblock position within picture */
/* ISO/IEC 13818-2 section 6.3.1.6 and 6.3.1.7 */
bx = 16*(MBA%mb_width);
by = 16*(MBA/mb_width);
/* motion compensation */
if (!(macroblock_type & MACROBLOCK_INTRA))
form_predictions(bx, by, macroblock_type, motion_type, PMV,
motion_vertical_field_select, dmvector);
switch (iDCT_Flag)
{
case IDCT_MMX:
for (comp=0; comp<block_count; comp++)
MMX_IDCT(block[comp]);
break;
case IDCT_SSEMMX:
for (comp=0; comp<block_count; comp++)
SSEMMX_IDCT(block[comp]);
break;
case IDCT_FPU:
for (comp=0; comp<block_count; comp++)
FPU_IDCT(block[comp]);
break;
case IDCT_REF:
for (comp=0; comp<block_count; comp++)
REF_IDCT(block[comp]);
break;
case IDCT_SSE2MMX:
for (comp=0; comp<block_count; comp++)
SSE2MMX_IDCT(block[comp]);
break;
case IDCT_SKAL:
for (comp=0; comp<block_count; comp++)
Skl_IDct16_Sparse_SSE(block[comp]);
break;
case IDCT_SIMPLE:
for (comp=0; comp<block_count; comp++)
simple_idct_mmx(block[comp]);
break;
}
Add_Block(block_count, bx, by, dct_type, (macroblock_type & MACROBLOCK_INTRA)==0);
}
/* ISO/IEC 13818-2 section 7.6.6 */
static void skipped_macroblock(int dc_dct_pred[3], int PMV[2][2][2], int *motion_type,
int motion_vertical_field_select[2][2], int *macroblock_type)
{
Clear_Block(block_count);
/* reset intra_dc predictors */
/* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
/* reset motion vector predictors */
/* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
if (picture_coding_type==P_TYPE)
PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
/* derive motion_type */
if (picture_structure==FRAME_PICTURE)
*motion_type = MC_FRAME;
else
{
*motion_type = MC_FIELD;
motion_vertical_field_select[0][0] = motion_vertical_field_select[0][1] =
(picture_structure==BOTTOM_FIELD);
}
if (picture_coding_type == I_TYPE)
{
SetFaultFlag(1);
return;
}
/* clear MACROBLOCK_INTRA */
*macroblock_type&= ~MACROBLOCK_INTRA;
}
/* ISO/IEC 13818-2 sections 7.2 through 7.5 */
static void decode_macroblock(int *macroblock_type, int *motion_type, int *dct_type,
int PMV[2][2][2], int dc_dct_pred[3],
int motion_vertical_field_select[2][2], int dmvector[2])
{
int quantizer_scale_code, comp, motion_vector_count, mv_format;
int dmv, mvscale, coded_block_pattern;
/* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
macroblock_modes(macroblock_type, motion_type, &motion_vector_count, &mv_format,
&dmv, &mvscale, dct_type);
if (Fault_Flag)
return; // go to next slice
if (*macroblock_type & MACROBLOCK_QUANT)
{
quantizer_scale_code = Get_Bits(5);
/* ISO/IEC 13818-2 section 7.4.2.2: Quantizer scale factor */
if (mpeg_type == IS_MPEG2)
{
quantizer_scale = q_scale_type ?
Non_Linear_quantizer_scale[quantizer_scale_code] : (quantizer_scale_code << 1);
}
else
{
quantizer_scale = quantizer_scale_code;
}
}
/* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */
/* decode forward motion vectors */
if ((*macroblock_type & MACROBLOCK_MOTION_FORWARD)
|| ((*macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors))
{
if (mpeg_type == IS_MPEG2)
{
motion_vectors(PMV, dmvector, motion_vertical_field_select, 0,
motion_vector_count, mv_format, f_code[0][0]-1, f_code[0][1]-1, dmv, mvscale);
}
else
{
motion_vector(PMV[0][0], dmvector, forward_f_code-1, forward_f_code-1, dmv, mvscale, full_pel_forward_vector);