forked from Ryubing/Ryujinx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecodeFrame.cs
1387 lines (1238 loc) · 49.9 KB
/
DecodeFrame.cs
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
using Ryujinx.Common.Memory;
using Ryujinx.Graphics.Nvdec.Vp9.Common;
using Ryujinx.Graphics.Nvdec.Vp9.Dsp;
using Ryujinx.Graphics.Nvdec.Vp9.Types;
using Ryujinx.Graphics.Video;
using System;
using System.Buffers.Binary;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace Ryujinx.Graphics.Nvdec.Vp9
{
static class DecodeFrame
{
private static bool ReadIsValid(ArrayPtr<byte> start, int len)
{
return len != 0 && len <= start.Length;
}
private static void InverseTransformBlockInter(ref MacroBlockD xd, int plane, TxSize txSize, Span<byte> dst, int stride, int eob)
{
ref MacroBlockDPlane pd = ref xd.Plane[plane];
ArrayPtr<int> dqcoeff = pd.DqCoeff;
Debug.Assert(eob > 0);
if (xd.CurBuf.HighBd)
{
Span<ushort> dst16 = MemoryMarshal.Cast<byte, ushort>(dst);
if (xd.Lossless)
{
Idct.HighbdIwht4x4Add(dqcoeff.AsSpan(), dst16, stride, eob, xd.Bd);
}
else
{
switch (txSize)
{
case TxSize.Tx4x4:
Idct.HighbdIdct4x4Add(dqcoeff.AsSpan(), dst16, stride, eob, xd.Bd);
break;
case TxSize.Tx8x8:
Idct.HighbdIdct8x8Add(dqcoeff.AsSpan(), dst16, stride, eob, xd.Bd);
break;
case TxSize.Tx16x16:
Idct.HighbdIdct16x16Add(dqcoeff.AsSpan(), dst16, stride, eob, xd.Bd);
break;
case TxSize.Tx32x32:
Idct.HighbdIdct32x32Add(dqcoeff.AsSpan(), dst16, stride, eob, xd.Bd);
break;
default:
Debug.Assert(false, "Invalid transform size");
break;
}
}
}
else
{
if (xd.Lossless)
{
Idct.Iwht4x4Add(dqcoeff.AsSpan(), dst, stride, eob);
}
else
{
switch (txSize)
{
case TxSize.Tx4x4:
Idct.Idct4x4Add(dqcoeff.AsSpan(), dst, stride, eob);
break;
case TxSize.Tx8x8:
Idct.Idct8x8Add(dqcoeff.AsSpan(), dst, stride, eob);
break;
case TxSize.Tx16x16:
Idct.Idct16x16Add(dqcoeff.AsSpan(), dst, stride, eob);
break;
case TxSize.Tx32x32:
Idct.Idct32x32Add(dqcoeff.AsSpan(), dst, stride, eob);
break;
default:
Debug.Assert(false, "Invalid transform size");
return;
}
}
}
if (eob == 1)
{
dqcoeff.AsSpan()[0] = 0;
}
else
{
if (txSize <= TxSize.Tx16x16 && eob <= 10)
{
dqcoeff.AsSpan()[..(4 * (4 << (int)txSize))].Clear();
}
else if (txSize == TxSize.Tx32x32 && eob <= 34)
{
dqcoeff.AsSpan()[..256].Clear();
}
else
{
dqcoeff.AsSpan()[..(16 << ((int)txSize << 1))].Clear();
}
}
}
private static void InverseTransformBlockIntra(
ref MacroBlockD xd,
int plane,
TxType txType,
TxSize txSize,
Span<byte> dst,
int stride,
int eob)
{
ref MacroBlockDPlane pd = ref xd.Plane[plane];
ArrayPtr<int> dqcoeff = pd.DqCoeff;
Debug.Assert(eob > 0);
if (xd.CurBuf.HighBd)
{
Span<ushort> dst16 = MemoryMarshal.Cast<byte, ushort>(dst);
if (xd.Lossless)
{
Idct.HighbdIwht4x4Add(dqcoeff.AsSpan(), dst16, stride, eob, xd.Bd);
}
else
{
switch (txSize)
{
case TxSize.Tx4x4:
Idct.HighbdIht4x4Add(txType, dqcoeff.AsSpan(), dst16, stride, eob, xd.Bd);
break;
case TxSize.Tx8x8:
Idct.HighbdIht8x8Add(txType, dqcoeff.AsSpan(), dst16, stride, eob, xd.Bd);
break;
case TxSize.Tx16x16:
Idct.HighbdIht16x16Add(txType, dqcoeff.AsSpan(), dst16, stride, eob, xd.Bd);
break;
case TxSize.Tx32x32:
Idct.HighbdIdct32x32Add(dqcoeff.AsSpan(), dst16, stride, eob, xd.Bd);
break;
default:
Debug.Assert(false, "Invalid transform size");
break;
}
}
}
else
{
if (xd.Lossless)
{
Idct.Iwht4x4Add(dqcoeff.AsSpan(), dst, stride, eob);
}
else
{
switch (txSize)
{
case TxSize.Tx4x4:
Idct.Iht4x4Add(txType, dqcoeff.AsSpan(), dst, stride, eob);
break;
case TxSize.Tx8x8:
Idct.Iht8x8Add(txType, dqcoeff.AsSpan(), dst, stride, eob);
break;
case TxSize.Tx16x16:
Idct.Iht16x16Add(txType, dqcoeff.AsSpan(), dst, stride, eob);
break;
case TxSize.Tx32x32:
Idct.Idct32x32Add(dqcoeff.AsSpan(), dst, stride, eob);
break;
default:
Debug.Assert(false, "Invalid transform size");
return;
}
}
}
if (eob == 1)
{
dqcoeff.AsSpan()[0] = 0;
}
else
{
if (txType == TxType.DctDct && txSize <= TxSize.Tx16x16 && eob <= 10)
{
dqcoeff.AsSpan()[..(4 * (4 << (int)txSize))].Clear();
}
else if (txSize == TxSize.Tx32x32 && eob <= 34)
{
dqcoeff.AsSpan()[..256].Clear();
}
else
{
dqcoeff.AsSpan()[..(16 << ((int)txSize << 1))].Clear();
}
}
}
private static unsafe void PredictAndReconstructIntraBlock(
ref TileWorkerData twd,
ref ModeInfo mi,
int plane,
int row,
int col,
TxSize txSize)
{
ref MacroBlockD xd = ref twd.Xd;
ref MacroBlockDPlane pd = ref xd.Plane[plane];
PredictionMode mode = (plane == 0) ? mi.Mode : mi.UvMode;
int dstOffset = 4 * row * pd.Dst.Stride + 4 * col;
byte* dst = &pd.Dst.Buf.ToPointer()[dstOffset];
Span<byte> dstSpan = pd.Dst.Buf.AsSpan()[dstOffset..];
if (mi.SbType < BlockSize.Block8x8)
{
if (plane == 0)
{
mode = xd.Mi[0].Value.Bmi[(row << 1) + col].Mode;
}
}
ReconIntra.PredictIntraBlock(ref xd, pd.N4Wl, txSize, mode, dst, pd.Dst.Stride, dst, pd.Dst.Stride, col, row, plane);
if (mi.Skip == 0)
{
TxType txType =
(plane != 0 || xd.Lossless) ? TxType.DctDct : ReconIntra.IntraModeToTxTypeLookup[(int)mode];
var sc = (plane != 0 || xd.Lossless)
? Luts.Vp9DefaultScanOrders[(int)txSize]
: Luts.Vp9ScanOrders[(int)txSize][(int)txType];
int eob = Detokenize.DecodeBlockTokens(ref twd, plane, sc, col, row, txSize, mi.SegmentId);
if (eob > 0)
{
InverseTransformBlockIntra(ref xd, plane, txType, txSize, dstSpan, pd.Dst.Stride, eob);
}
}
}
private static int ReconstructInterBlock(
ref TileWorkerData twd,
ref ModeInfo mi,
int plane,
int row,
int col,
TxSize txSize)
{
ref MacroBlockD xd = ref twd.Xd;
ref MacroBlockDPlane pd = ref xd.Plane[plane];
var sc = Luts.Vp9DefaultScanOrders[(int)txSize];
int eob = Detokenize.DecodeBlockTokens(ref twd, plane, sc, col, row, txSize, mi.SegmentId);
Span<byte> dst = pd.Dst.Buf.AsSpan()[(4 * row * pd.Dst.Stride + 4 * col)..];
if (eob > 0)
{
InverseTransformBlockInter(ref xd, plane, txSize, dst, pd.Dst.Stride, eob);
}
return eob;
}
private static unsafe void BuildMcBorder(
byte* src,
int srcStride,
byte* dst,
int dstStride,
int x,
int y,
int bW,
int bH,
int w,
int h)
{
// Get a pointer to the start of the real data for this row.
byte* refRow = src - x - y * srcStride;
if (y >= h)
{
refRow += (h - 1) * srcStride;
}
else if (y > 0)
{
refRow += y * srcStride;
}
do
{
int right = 0, copy;
int left = x < 0 ? -x : 0;
if (left > bW)
{
left = bW;
}
if (x + bW > w)
{
right = x + bW - w;
}
if (right > bW)
{
right = bW;
}
copy = bW - left - right;
if (left != 0)
{
MemoryUtil.Fill(dst, refRow[0], left);
}
if (copy != 0)
{
MemoryUtil.Copy(dst + left, refRow + x + left, copy);
}
if (right != 0)
{
MemoryUtil.Fill(dst + left + copy, refRow[w - 1], right);
}
dst += dstStride;
++y;
if (y > 0 && y < h)
{
refRow += srcStride;
}
} while (--bH != 0);
}
private static unsafe void HighBuildMcBorder(
byte* src8,
int srcStride,
ushort* dst,
int dstStride,
int x,
int y,
int bW,
int bH,
int w,
int h)
{
// Get a pointer to the start of the real data for this row.
ushort* src = (ushort*)src8;
ushort* refRow = src - x - y * srcStride;
if (y >= h)
{
refRow += (h - 1) * srcStride;
}
else if (y > 0)
{
refRow += y * srcStride;
}
do
{
int right = 0, copy;
int left = x < 0 ? -x : 0;
if (left > bW)
{
left = bW;
}
if (x + bW > w)
{
right = x + bW - w;
}
if (right > bW)
{
right = bW;
}
copy = bW - left - right;
if (left != 0)
{
MemoryUtil.Fill(dst, refRow[0], left);
}
if (copy != 0)
{
MemoryUtil.Copy(dst + left, refRow + x + left, copy);
}
if (right != 0)
{
MemoryUtil.Fill(dst + left + copy, refRow[w - 1], right);
}
dst += dstStride;
++y;
if (y > 0 && y < h)
{
refRow += srcStride;
}
} while (--bH != 0);
}
[SkipLocalsInit]
private static unsafe void ExtendAndPredict(
byte* bufPtr1,
int preBufStride,
int x0,
int y0,
int bW,
int bH,
int frameWidth,
int frameHeight,
int borderOffset,
byte* dst,
int dstBufStride,
int subpelX,
int subpelY,
Array8<short>[] kernel,
ref ScaleFactors sf,
ref MacroBlockD xd,
int w,
int h,
int refr,
int xs,
int ys)
{
ushort* mcBufHigh = stackalloc ushort[80 * 2 * 80 * 2];
if (xd.CurBuf.HighBd)
{
HighBuildMcBorder(bufPtr1, preBufStride, mcBufHigh, bW, x0, y0, bW, bH, frameWidth, frameHeight);
ReconInter.HighbdInterPredictor(
mcBufHigh + borderOffset,
bW,
(ushort*)dst,
dstBufStride,
subpelX,
subpelY,
ref sf,
w,
h,
refr,
kernel,
xs,
ys,
xd.Bd);
}
else
{
BuildMcBorder(bufPtr1, preBufStride, (byte*)mcBufHigh, bW, x0, y0, bW, bH, frameWidth, frameHeight);
ReconInter.InterPredictor(
(byte*)mcBufHigh + borderOffset,
bW,
dst,
dstBufStride,
subpelX,
subpelY,
ref sf,
w,
h,
refr,
kernel,
xs,
ys);
}
}
private static unsafe void DecBuildInterPredictors(
ref MacroBlockD xd,
int plane,
int bw,
int bh,
int x,
int y,
int w,
int h,
int miX,
int miY,
Array8<short>[] kernel,
ref ScaleFactors sf,
ref Buf2D preBuf,
ref Buf2D dstBuf,
ref Mv mv,
ref Surface refFrameBuf,
bool isScaled,
int refr)
{
ref MacroBlockDPlane pd = ref xd.Plane[plane];
byte* dst = dstBuf.Buf.ToPointer() + dstBuf.Stride * y + x;
Mv32 scaledMv;
int xs, ys, x0, y0, x0_16, y0_16, frameWidth, frameHeight, bufStride, subpelX, subpelY;
byte* refFrame;
byte* bufPtr;
// Get reference frame pointer, width and height.
if (plane == 0)
{
frameWidth = refFrameBuf.Width;
frameHeight = refFrameBuf.Height;
refFrame = refFrameBuf.YBuffer.ToPointer();
}
else
{
frameWidth = refFrameBuf.UvWidth;
frameHeight = refFrameBuf.UvHeight;
refFrame = plane == 1 ? refFrameBuf.UBuffer.ToPointer() : refFrameBuf.VBuffer.ToPointer();
}
if (isScaled)
{
Mv mvQ4 = ReconInter.ClampMvToUmvBorderSb(ref xd, ref mv, bw, bh, pd.SubsamplingX, pd.SubsamplingY);
// Co-ordinate of containing block to pixel precision.
int xStart = (-xd.MbToLeftEdge >> (3 + pd.SubsamplingX));
int yStart = (-xd.MbToTopEdge >> (3 + pd.SubsamplingY));
// Co-ordinate of the block to 1/16th pixel precision.
x0_16 = (xStart + x) << Filter.SubpelBits;
y0_16 = (yStart + y) << Filter.SubpelBits;
// Co-ordinate of current block in reference frame
// to 1/16th pixel precision.
x0_16 = sf.ScaleValueX(x0_16);
y0_16 = sf.ScaleValueY(y0_16);
// Map the top left corner of the block into the reference frame.
x0 = sf.ScaleValueX(xStart + x);
y0 = sf.ScaleValueY(yStart + y);
// Scale the MV and incorporate the sub-pixel offset of the block
// in the reference frame.
scaledMv = sf.ScaleMv(ref mvQ4, miX + x, miY + y);
xs = sf.XStepQ4;
ys = sf.YStepQ4;
}
else
{
// Co-ordinate of containing block to pixel precision.
x0 = (-xd.MbToLeftEdge >> (3 + pd.SubsamplingX)) + x;
y0 = (-xd.MbToTopEdge >> (3 + pd.SubsamplingY)) + y;
// Co-ordinate of the block to 1/16th pixel precision.
x0_16 = x0 << Filter.SubpelBits;
y0_16 = y0 << Filter.SubpelBits;
scaledMv.Row = mv.Row * (1 << (1 - pd.SubsamplingY));
scaledMv.Col = mv.Col * (1 << (1 - pd.SubsamplingX));
xs = ys = 16;
}
subpelX = scaledMv.Col & Filter.SubpelMask;
subpelY = scaledMv.Row & Filter.SubpelMask;
// Calculate the top left corner of the best matching block in the
// reference frame.
x0 += scaledMv.Col >> Filter.SubpelBits;
y0 += scaledMv.Row >> Filter.SubpelBits;
x0_16 += scaledMv.Col;
y0_16 += scaledMv.Row;
// Get reference block pointer.
bufPtr = refFrame + y0 * preBuf.Stride + x0;
bufStride = preBuf.Stride;
// Do border extension if there is motion or the
// width/height is not a multiple of 8 pixels.
if (isScaled || scaledMv.Col != 0 || scaledMv.Row != 0 || (frameWidth & 0x7) != 0 || (frameHeight & 0x7) != 0)
{
int y1 = ((y0_16 + (h - 1) * ys) >> Filter.SubpelBits) + 1;
// Get reference block bottom right horizontal coordinate.
int x1 = ((x0_16 + (w - 1) * xs) >> Filter.SubpelBits) + 1;
int xPad = 0, yPad = 0;
if (subpelX != 0 || (sf.XStepQ4 != Filter.SubpelShifts))
{
x0 -= Constants.Vp9InterpExtend - 1;
x1 += Constants.Vp9InterpExtend;
xPad = 1;
}
if (subpelY != 0 || (sf.YStepQ4 != Filter.SubpelShifts))
{
y0 -= Constants.Vp9InterpExtend - 1;
y1 += Constants.Vp9InterpExtend;
yPad = 1;
}
// Skip border extension if block is inside the frame.
if (x0 < 0 || x0 > frameWidth - 1 || x1 < 0 || x1 > frameWidth - 1 ||
y0 < 0 || y0 > frameHeight - 1 || y1 < 0 || y1 > frameHeight - 1)
{
// Extend the border.
byte* bufPtr1 = refFrame + y0 * bufStride + x0;
int bW = x1 - x0 + 1;
int bH = y1 - y0 + 1;
int borderOffset = yPad * 3 * bW + xPad * 3;
ExtendAndPredict(
bufPtr1,
bufStride,
x0,
y0,
bW,
bH,
frameWidth,
frameHeight,
borderOffset,
dst,
dstBuf.Stride,
subpelX,
subpelY,
kernel,
ref sf,
ref xd,
w,
h,
refr,
xs,
ys);
return;
}
}
if (xd.CurBuf.HighBd)
{
ReconInter.HighbdInterPredictor(
(ushort*)bufPtr,
bufStride,
(ushort*)dst,
dstBuf.Stride,
subpelX,
subpelY,
ref sf,
w,
h,
refr,
kernel,
xs,
ys,
xd.Bd);
}
else
{
ReconInter.InterPredictor(
bufPtr,
bufStride,
dst,
dstBuf.Stride,
subpelX,
subpelY,
ref sf,
w,
h,
refr,
kernel,
xs,
ys);
}
}
private static void DecBuildInterPredictorsSb(ref Vp9Common cm, ref MacroBlockD xd, int miRow, int miCol)
{
int plane;
int miX = miCol * Constants.MiSize;
int miY = miRow * Constants.MiSize;
ref ModeInfo mi = ref xd.Mi[0].Value;
Array8<short>[] kernel = Luts.Vp9FilterKernels[mi.InterpFilter];
BlockSize sbType = mi.SbType;
int isCompound = mi.HasSecondRef() ? 1 : 0;
int refr;
bool isScaled;
for (refr = 0; refr < 1 + isCompound; ++refr)
{
int frame = mi.RefFrame[refr];
ref RefBuffer refBuf = ref cm.FrameRefs[frame - Constants.LastFrame];
ref ScaleFactors sf = ref refBuf.Sf;
ref Surface refFrameBuf = ref refBuf.Buf;
if (!sf.IsValidScale())
{
xd.ErrorInfo.Value.InternalError(CodecErr.CodecUnsupBitstream, "Reference frame has invalid dimensions");
}
isScaled = sf.IsScaled();
ReconInter.SetupPrePlanes(ref xd, refr, ref refFrameBuf, miRow, miCol, isScaled ? new Ptr<ScaleFactors>(ref sf) : Ptr<ScaleFactors>.Null);
xd.BlockRefs[refr] = new Ptr<RefBuffer>(ref refBuf);
if (sbType < BlockSize.Block8x8)
{
for (plane = 0; plane < Constants.MaxMbPlane; ++plane)
{
ref MacroBlockDPlane pd = ref xd.Plane[plane];
ref Buf2D dstBuf = ref pd.Dst;
int num4x4W = pd.N4W;
int num4x4H = pd.N4H;
int n4Wx4 = 4 * num4x4W;
int n4Hx4 = 4 * num4x4H;
ref Buf2D preBuf = ref pd.Pre[refr];
int i = 0, x, y;
for (y = 0; y < num4x4H; ++y)
{
for (x = 0; x < num4x4W; ++x)
{
Mv mv = ReconInter.AverageSplitMvs(ref pd, ref mi, refr, i++);
DecBuildInterPredictors(
ref xd,
plane,
n4Wx4,
n4Hx4,
4 * x,
4 * y,
4,
4,
miX,
miY,
kernel,
ref sf,
ref preBuf,
ref dstBuf,
ref mv,
ref refFrameBuf,
isScaled,
refr);
}
}
}
}
else
{
Mv mv = mi.Mv[refr];
for (plane = 0; plane < Constants.MaxMbPlane; ++plane)
{
ref MacroBlockDPlane pd = ref xd.Plane[plane];
ref Buf2D dstBuf = ref pd.Dst;
int num4x4W = pd.N4W;
int num4x4H = pd.N4H;
int n4Wx4 = 4 * num4x4W;
int n4Hx4 = 4 * num4x4H;
ref Buf2D preBuf = ref pd.Pre[refr];
DecBuildInterPredictors(
ref xd,
plane,
n4Wx4,
n4Hx4,
0,
0,
n4Wx4,
n4Hx4,
miX,
miY,
kernel,
ref sf,
ref preBuf,
ref dstBuf,
ref mv,
ref refFrameBuf,
isScaled,
refr);
}
}
}
}
private static unsafe void DecResetSkipContext(ref MacroBlockD xd)
{
int i;
for (i = 0; i < Constants.MaxMbPlane; i++)
{
ref MacroBlockDPlane pd = ref xd.Plane[i];
MemoryUtil.Fill(pd.AboveContext.ToPointer(), (sbyte)0, pd.N4W);
MemoryUtil.Fill(pd.LeftContext.ToPointer(), (sbyte)0, pd.N4H);
}
}
private static void SetPlaneN4(ref MacroBlockD xd, int bw, int bh, int bwl, int bhl)
{
int i;
for (i = 0; i < Constants.MaxMbPlane; i++)
{
xd.Plane[i].N4W = (ushort)((bw << 1) >> xd.Plane[i].SubsamplingX);
xd.Plane[i].N4H = (ushort)((bh << 1) >> xd.Plane[i].SubsamplingY);
xd.Plane[i].N4Wl = (byte)(bwl - xd.Plane[i].SubsamplingX);
xd.Plane[i].N4Hl = (byte)(bhl - xd.Plane[i].SubsamplingY);
}
}
private static ref ModeInfo SetOffsets(
ref Vp9Common cm,
ref MacroBlockD xd,
BlockSize bsize,
int miRow,
int miCol,
int bw,
int bh,
int xMis,
int yMis,
int bwl,
int bhl)
{
int offset = miRow * cm.MiStride + miCol;
int x, y;
ref TileInfo tile = ref xd.Tile;
xd.Mi = cm.MiGridVisible.Slice(offset);
xd.Mi[0] = new Ptr<ModeInfo>(ref cm.Mi[offset]);
xd.Mi[0].Value.SbType = bsize;
for (y = 0; y < yMis; ++y)
{
for (x = y == 0 ? 1 : 0; x < xMis; ++x)
{
xd.Mi[y * cm.MiStride + x] = xd.Mi[0];
}
}
SetPlaneN4(ref xd, bw, bh, bwl, bhl);
xd.SetSkipContext(miRow, miCol);
// Distance of Mb to the various image edges. These are specified to 8th pel
// as they are always compared to values that are in 1/8th pel units
xd.SetMiRowCol(ref tile, miRow, bh, miCol, bw, cm.MiRows, cm.MiCols);
ReconInter.SetupDstPlanes(ref xd.Plane, ref xd.CurBuf, miRow, miCol);
return ref xd.Mi[0].Value;
}
private static void DecodeBlock(
ref TileWorkerData twd,
ref Vp9Common cm,
int miRow,
int miCol,
BlockSize bsize,
int bwl,
int bhl)
{
bool less8x8 = bsize < BlockSize.Block8x8;
int bw = 1 << (bwl - 1);
int bh = 1 << (bhl - 1);
int xMis = Math.Min(bw, cm.MiCols - miCol);
int yMis = Math.Min(bh, cm.MiRows - miRow);
ref Reader r = ref twd.BitReader;
ref MacroBlockD xd = ref twd.Xd;
ref ModeInfo mi = ref SetOffsets(ref cm, ref xd, bsize, miRow, miCol, bw, bh, xMis, yMis, bwl, bhl);
if (bsize >= BlockSize.Block8x8 && (cm.SubsamplingX != 0 || cm.SubsamplingY != 0))
{
BlockSize uvSubsize = Luts.SsSizeLookup[(int)bsize][cm.SubsamplingX][cm.SubsamplingY];
if (uvSubsize == BlockSize.BlockInvalid)
{
xd.ErrorInfo.Value.InternalError(CodecErr.CodecCorruptFrame, "Invalid block size.");
}
}
DecodeMv.ReadModeInfo(ref twd, ref cm, miRow, miCol, xMis, yMis);
if (mi.Skip != 0)
{
DecResetSkipContext(ref xd);
}
if (!mi.IsInterBlock())
{
int plane;
for (plane = 0; plane < Constants.MaxMbPlane; ++plane)
{
ref MacroBlockDPlane pd = ref xd.Plane[plane];
TxSize txSize = plane != 0 ? mi.GetUvTxSize(ref pd) : mi.TxSize;
int num4x4W = pd.N4W;
int num4x4H = pd.N4H;
int step = 1 << (int)txSize;
int row, col;
int maxBlocksWide = num4x4W + (xd.MbToRightEdge >= 0 ? 0 : xd.MbToRightEdge >> (5 + pd.SubsamplingX));
int maxBlocksHigh = num4x4H + (xd.MbToBottomEdge >= 0 ? 0 : xd.MbToBottomEdge >> (5 + pd.SubsamplingY));
xd.MaxBlocksWide = (uint)(xd.MbToRightEdge >= 0 ? 0 : maxBlocksWide);
xd.MaxBlocksHigh = (uint)(xd.MbToBottomEdge >= 0 ? 0 : maxBlocksHigh);
for (row = 0; row < maxBlocksHigh; row += step)
{
for (col = 0; col < maxBlocksWide; col += step)
{
PredictAndReconstructIntraBlock(ref twd, ref mi, plane, row, col, txSize);
}
}
}
}
else
{
// Prediction
DecBuildInterPredictorsSb(ref cm, ref xd, miRow, miCol);
// Reconstruction
if (mi.Skip == 0)
{
int eobtotal = 0;
int plane;
for (plane = 0; plane < Constants.MaxMbPlane; ++plane)
{
ref MacroBlockDPlane pd = ref xd.Plane[plane];
TxSize txSize = plane != 0 ? mi.GetUvTxSize(ref pd) : mi.TxSize;
int num4x4W = pd.N4W;
int num4x4H = pd.N4H;
int step = 1 << (int)txSize;
int row, col;
int maxBlocksWide = num4x4W + (xd.MbToRightEdge >= 0 ? 0 : xd.MbToRightEdge >> (5 + pd.SubsamplingX));
int maxBlocksHigh = num4x4H + (xd.MbToBottomEdge >= 0 ? 0 : xd.MbToBottomEdge >> (5 + pd.SubsamplingY));
xd.MaxBlocksWide = (uint)(xd.MbToRightEdge >= 0 ? 0 : maxBlocksWide);
xd.MaxBlocksHigh = (uint)(xd.MbToBottomEdge >= 0 ? 0 : maxBlocksHigh);
for (row = 0; row < maxBlocksHigh; row += step)
{
for (col = 0; col < maxBlocksWide; col += step)
{
eobtotal += ReconstructInterBlock(ref twd, ref mi, plane, row, col, txSize);
}
}
}
if (!less8x8 && eobtotal == 0)
{
mi.Skip = 1; // Skip loopfilter
}
}
}
xd.Corrupted |= r.HasError();
if (cm.Lf.FilterLevel != 0)
{
LoopFilter.BuildMask(ref cm, ref mi, miRow, miCol, bw, bh);
}
}
private static int DecPartitionPlaneContext(ref TileWorkerData twd, int miRow, int miCol, int bsl)
{
ref sbyte aboveCtx = ref twd.Xd.AboveSegContext[miCol];
ref sbyte leftCtx = ref twd.Xd.LeftSegContext[miRow & Constants.MiMask];
int above = (aboveCtx >> bsl) & 1, left = (leftCtx >> bsl) & 1;
return (left * 2 + above) + bsl * Constants.PartitionPloffset;
}
private static void DecUpdatePartitionContext(
ref TileWorkerData twd,
int miRow,
int miCol,
BlockSize subsize,
int bw)
{
Span<sbyte> aboveCtx = twd.Xd.AboveSegContext.Slice(miCol).AsSpan();
Span<sbyte> leftCtx = MemoryMarshal.CreateSpan(ref twd.Xd.LeftSegContext[miRow & Constants.MiMask], 8 - (miRow & Constants.MiMask));
// Update the partition context at the end notes. Set partition bits
// of block sizes larger than the current one to be one, and partition
// bits of smaller block sizes to be zero.
aboveCtx[..bw].Fill(Luts.PartitionContextLookup[(int)subsize].Above);
leftCtx[..bw].Fill(Luts.PartitionContextLookup[(int)subsize].Left);
}
private static PartitionType ReadPartition(
ref TileWorkerData twd,
int miRow,
int miCol,
int hasRows,
int hasCols,
int bsl)
{
int ctx = DecPartitionPlaneContext(ref twd, miRow, miCol, bsl);
ReadOnlySpan<byte> probs = MemoryMarshal.CreateReadOnlySpan(ref twd.Xd.PartitionProbs[ctx][0], 3);
PartitionType p;
ref Reader r = ref twd.BitReader;
if (hasRows != 0 && hasCols != 0)
{
p = (PartitionType)r.ReadTree(Luts.Vp9PartitionTree, probs);
}
else if (hasRows == 0 && hasCols != 0)
{
p = r.Read(probs[1]) != 0 ? PartitionType.PartitionSplit : PartitionType.PartitionHorz;
}
else if (hasRows != 0 && hasCols == 0)
{
p = r.Read(probs[2]) != 0 ? PartitionType.PartitionSplit : PartitionType.PartitionVert;
}
else
{
p = PartitionType.PartitionSplit;
}
if (!twd.Xd.Counts.IsNull)
{
++twd.Xd.Counts.Value.Partition[ctx][(int)p];
}
return p;
}
private static void DecodePartition(
ref TileWorkerData twd,