| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- using Ryujinx.Common.Memory;
- using System.Runtime.CompilerServices;
- using static Ryujinx.Graphics.Nvdec.Vp9.Dsp.Convolve;
- using static Ryujinx.Graphics.Nvdec.Vp9.Dsp.Filter;
- namespace Ryujinx.Graphics.Nvdec.Vp9.Types
- {
- internal struct ScaleFactors
- {
- private const int RefScaleShift = 14;
- private const int RefNoScale = (1 << RefScaleShift);
- private const int RefInvalidScale = -1;
- private unsafe delegate void ConvolveFn(
- byte* src,
- int srcStride,
- byte* dst,
- int dstStride,
- Array8<short>[] filter,
- int x0Q4,
- int xStepQ4,
- int y0Q4,
- int yStepQ4,
- int w,
- int h);
- private unsafe delegate void HighbdConvolveFn(
- ushort* src,
- int srcStride,
- ushort* dst,
- int dstStride,
- Array8<short>[] filter,
- int x0Q4,
- int xStepQ4,
- int y0Q4,
- int yStepQ4,
- int w,
- int h,
- int bd);
- private static readonly unsafe ConvolveFn[][][] PredictX16Y16 = new ConvolveFn[][][]
- {
- new ConvolveFn[][]
- {
- new ConvolveFn[]
- {
- ConvolveCopy,
- ConvolveAvg
- },
- new ConvolveFn[]
- {
- Convolve8Vert,
- Convolve8AvgVert
- }
- },
- new ConvolveFn[][]
- {
- new ConvolveFn[]
- {
- Convolve8Horiz,
- Convolve8AvgHoriz
- },
- new ConvolveFn[]
- {
- Convolve8,
- Convolve8Avg
- }
- }
- };
- private static readonly unsafe ConvolveFn[][][] PredictX16 = new ConvolveFn[][][]
- {
- new ConvolveFn[][]
- {
- new ConvolveFn[]
- {
- ScaledVert,
- ScaledAvgVert
- },
- new ConvolveFn[]
- {
- ScaledVert,
- ScaledAvgVert
- }
- },
- new ConvolveFn[][]
- {
- new ConvolveFn[]
- {
- Scaled2D,
- ScaledAvg2D
- },
- new ConvolveFn[]
- {
- Scaled2D,
- ScaledAvg2D
- }
- }
- };
- private static readonly unsafe ConvolveFn[][][] PredictY16 = new ConvolveFn[][][]
- {
- new ConvolveFn[][]
- {
- new ConvolveFn[]
- {
- ScaledHoriz,
- ScaledAvgHoriz
- },
- new ConvolveFn[]
- {
- Scaled2D,
- ScaledAvg2D
- }
- },
- new ConvolveFn[][]
- {
- new ConvolveFn[]
- {
- ScaledHoriz,
- ScaledAvgHoriz
- },
- new ConvolveFn[]
- {
- Scaled2D,
- ScaledAvg2D
- }
- }
- };
- private static readonly unsafe ConvolveFn[][][] Predict = new ConvolveFn[][][]
- {
- new ConvolveFn[][]
- {
- new ConvolveFn[]
- {
- Scaled2D,
- ScaledAvg2D
- },
- new ConvolveFn[]
- {
- Scaled2D,
- ScaledAvg2D
- }
- },
- new ConvolveFn[][]
- {
- new ConvolveFn[]
- {
- Scaled2D,
- ScaledAvg2D
- },
- new ConvolveFn[]
- {
- Scaled2D,
- ScaledAvg2D
- }
- }
- };
- private static readonly unsafe HighbdConvolveFn[][][] HighbdPredictX16Y16 = new HighbdConvolveFn[][][]
- {
- new HighbdConvolveFn[][]
- {
- new HighbdConvolveFn[]
- {
- HighbdConvolveCopy,
- HighbdConvolveAvg
- },
- new HighbdConvolveFn[]
- {
- HighbdConvolve8Vert,
- HighbdConvolve8AvgVert
- }
- },
- new HighbdConvolveFn[][]
- {
- new HighbdConvolveFn[]
- {
- HighbdConvolve8Horiz,
- HighbdConvolve8AvgHoriz
- },
- new HighbdConvolveFn[]
- {
- HighbdConvolve8,
- HighbdConvolve8Avg
- }
- }
- };
- private static readonly unsafe HighbdConvolveFn[][][] HighbdPredictX16 = new HighbdConvolveFn[][][]
- {
- new HighbdConvolveFn[][]
- {
- new HighbdConvolveFn[]
- {
- HighbdConvolve8Vert,
- HighbdConvolve8AvgVert
- },
- new HighbdConvolveFn[]
- {
- HighbdConvolve8Vert,
- HighbdConvolve8AvgVert
- }
- },
- new HighbdConvolveFn[][]
- {
- new HighbdConvolveFn[]
- {
- HighbdConvolve8,
- HighbdConvolve8Avg
- },
- new HighbdConvolveFn[]
- {
- HighbdConvolve8,
- HighbdConvolve8Avg
- }
- }
- };
- private static readonly unsafe HighbdConvolveFn[][][] HighbdPredictY16 = new HighbdConvolveFn[][][]
- {
- new HighbdConvolveFn[][]
- {
- new HighbdConvolveFn[]
- {
- HighbdConvolve8Horiz,
- HighbdConvolve8AvgHoriz
- },
- new HighbdConvolveFn[]
- {
- HighbdConvolve8,
- HighbdConvolve8Avg
- }
- },
- new HighbdConvolveFn[][]
- {
- new HighbdConvolveFn[]
- {
- HighbdConvolve8Horiz,
- HighbdConvolve8AvgHoriz
- },
- new HighbdConvolveFn[]
- {
- HighbdConvolve8,
- HighbdConvolve8Avg
- }
- }
- };
- private static readonly unsafe HighbdConvolveFn[][][] HighbdPredict = new HighbdConvolveFn[][][]
- {
- new HighbdConvolveFn[][]
- {
- new HighbdConvolveFn[]
- {
- HighbdConvolve8,
- HighbdConvolve8Avg
- },
- new HighbdConvolveFn[]
- {
- HighbdConvolve8,
- HighbdConvolve8Avg
- }
- },
- new HighbdConvolveFn[][]
- {
- new HighbdConvolveFn[]
- {
- HighbdConvolve8,
- HighbdConvolve8Avg
- },
- new HighbdConvolveFn[]
- {
- HighbdConvolve8,
- HighbdConvolve8Avg
- }
- }
- };
- public int XScaleFP; // Horizontal fixed point scale factor
- public int YScaleFP; // Vertical fixed point scale factor
- public int XStepQ4;
- public int YStepQ4;
- public int ScaleValueX(int val)
- {
- return IsScaled() ? ScaledX(val) : val;
- }
- public int ScaleValueY(int val)
- {
- return IsScaled() ? ScaledY(val) : val;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public unsafe void InterPredict(
- int horiz,
- int vert,
- int avg,
- byte* src,
- int srcStride,
- byte* dst,
- int dstStride,
- int subpelX,
- int subpelY,
- int w,
- int h,
- Array8<short>[] kernel,
- int xs,
- int ys)
- {
- if (XStepQ4 == 16)
- {
- if (YStepQ4 == 16)
- {
- // No scaling in either direction.
- PredictX16Y16[horiz][vert][avg](src, srcStride, dst, dstStride, kernel, subpelX, xs, subpelY, ys, w, h);
- }
- else
- {
- // No scaling in x direction. Must always scale in the y direction.
- PredictX16[horiz][vert][avg](src, srcStride, dst, dstStride, kernel, subpelX, xs, subpelY, ys, w, h);
- }
- }
- else
- {
- if (YStepQ4 == 16)
- {
- // No scaling in the y direction. Must always scale in the x direction.
- PredictY16[horiz][vert][avg](src, srcStride, dst, dstStride, kernel, subpelX, xs, subpelY, ys, w, h);
- }
- else
- {
- // Must always scale in both directions.
- Predict[horiz][vert][avg](src, srcStride, dst, dstStride, kernel, subpelX, xs, subpelY, ys, w, h);
- }
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public unsafe void HighbdInterPredict(
- int horiz,
- int vert,
- int avg,
- ushort* src,
- int srcStride,
- ushort* dst,
- int dstStride,
- int subpelX,
- int subpelY,
- int w,
- int h,
- Array8<short>[] kernel,
- int xs,
- int ys,
- int bd)
- {
- if (XStepQ4 == 16)
- {
- if (YStepQ4 == 16)
- {
- // No scaling in either direction.
- HighbdPredictX16Y16[horiz][vert][avg](src, srcStride, dst, dstStride, kernel, subpelX, xs, subpelY, ys, w, h, bd);
- }
- else
- {
- // No scaling in x direction. Must always scale in the y direction.
- HighbdPredictX16[horiz][vert][avg](src, srcStride, dst, dstStride, kernel, subpelX, xs, subpelY, ys, w, h, bd);
- }
- }
- else
- {
- if (YStepQ4 == 16)
- {
- // No scaling in the y direction. Must always scale in the x direction.
- HighbdPredictY16[horiz][vert][avg](src, srcStride, dst, dstStride, kernel, subpelX, xs, subpelY, ys, w, h, bd);
- }
- else
- {
- // Must always scale in both directions.
- HighbdPredict[horiz][vert][avg](src, srcStride, dst, dstStride, kernel, subpelX, xs, subpelY, ys, w, h, bd);
- }
- }
- }
- private int ScaledX(int val)
- {
- return (int)((long)val * XScaleFP >> RefScaleShift);
- }
- private int ScaledY(int val)
- {
- return (int)((long)val * YScaleFP >> RefScaleShift);
- }
- private static int GetFixedPointScaleFactor(int otherSize, int thisSize)
- {
- // Calculate scaling factor once for each reference frame
- // and use fixed point scaling factors in decoding and encoding routines.
- // Hardware implementations can calculate scale factor in device driver
- // and use multiplication and shifting on hardware instead of division.
- return (otherSize << RefScaleShift) / thisSize;
- }
- public Mv32 ScaleMv(ref Mv mv, int x, int y)
- {
- int xOffQ4 = ScaledX(x << SubpelBits) & SubpelMask;
- int yOffQ4 = ScaledY(y << SubpelBits) & SubpelMask;
- Mv32 res = new Mv32()
- {
- Row = ScaledY(mv.Row) + yOffQ4,
- Col = ScaledX(mv.Col) + xOffQ4
- };
- return res;
- }
- public bool IsValidScale()
- {
- return XScaleFP != RefInvalidScale && YScaleFP != RefInvalidScale;
- }
- public bool IsScaled()
- {
- return IsValidScale() && (XScaleFP != RefNoScale || YScaleFP != RefNoScale);
- }
- public static bool ValidRefFrameSize(int refWidth, int refHeight, int thisWidth, int thisHeight)
- {
- return 2 * thisWidth >= refWidth &&
- 2 * thisHeight >= refHeight &&
- thisWidth <= 16 * refWidth &&
- thisHeight <= 16 * refHeight;
- }
- public void SetupScaleFactorsForFrame(int otherW, int otherH, int thisW, int thisH)
- {
- if (!ValidRefFrameSize(otherW, otherH, thisW, thisH))
- {
- XScaleFP = RefInvalidScale;
- YScaleFP = RefInvalidScale;
- return;
- }
- XScaleFP = GetFixedPointScaleFactor(otherW, thisW);
- YScaleFP = GetFixedPointScaleFactor(otherH, thisH);
- XStepQ4 = ScaledX(16);
- YStepQ4 = ScaledY(16);
- }
- }
- }
|