| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185 |
- using Ryujinx.Common.Memory;
- using System;
- using System.Buffers.Binary;
- using System.Diagnostics;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using Ryujinx.Graphics.Nvdec.Vp9.Common;
- using Ryujinx.Graphics.Nvdec.Vp9.Dsp;
- using Ryujinx.Graphics.Nvdec.Vp9.Types;
- using Ryujinx.Graphics.Video;
- using Mv = Ryujinx.Graphics.Nvdec.Vp9.Types.Mv;
- 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.ToSpan(), dst16, stride, eob, xd.Bd);
- }
- else
- {
- switch (txSize)
- {
- case TxSize.Tx4x4:
- Idct.HighbdIdct4x4Add(dqcoeff.ToSpan(), dst16, stride, eob, xd.Bd);
- break;
- case TxSize.Tx8x8:
- Idct.HighbdIdct8x8Add(dqcoeff.ToSpan(), dst16, stride, eob, xd.Bd);
- break;
- case TxSize.Tx16x16:
- Idct.HighbdIdct16x16Add(dqcoeff.ToSpan(), dst16, stride, eob, xd.Bd);
- break;
- case TxSize.Tx32x32:
- Idct.HighbdIdct32x32Add(dqcoeff.ToSpan(), dst16, stride, eob, xd.Bd);
- break;
- default: Debug.Assert(false, "Invalid transform size"); break;
- }
- }
- }
- else
- {
- if (xd.Lossless)
- {
- Idct.Iwht4x4Add(dqcoeff.ToSpan(), dst, stride, eob);
- }
- else
- {
- switch (txSize)
- {
- case TxSize.Tx4x4: Idct.Idct4x4Add(dqcoeff.ToSpan(), dst, stride, eob); break;
- case TxSize.Tx8x8: Idct.Idct8x8Add(dqcoeff.ToSpan(), dst, stride, eob); break;
- case TxSize.Tx16x16: Idct.Idct16x16Add(dqcoeff.ToSpan(), dst, stride, eob); break;
- case TxSize.Tx32x32: Idct.Idct32x32Add(dqcoeff.ToSpan(), dst, stride, eob); break;
- default: Debug.Assert(false, "Invalid transform size"); return;
- }
- }
- }
- if (eob == 1)
- {
- dqcoeff.ToSpan()[0] = 0;
- }
- else
- {
- if (txSize <= TxSize.Tx16x16 && eob <= 10)
- {
- dqcoeff.ToSpan().Slice(0, 4 * (4 << (int)txSize)).Fill(0);
- }
- else if (txSize == TxSize.Tx32x32 && eob <= 34)
- {
- dqcoeff.ToSpan().Slice(0, 256).Fill(0);
- }
- else
- {
- dqcoeff.ToSpan().Slice(0, 16 << ((int)txSize << 1)).Fill(0);
- }
- }
- }
- 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.ToSpan(), dst16, stride, eob, xd.Bd);
- }
- else
- {
- switch (txSize)
- {
- case TxSize.Tx4x4:
- Idct.HighbdIht4x4Add(txType, dqcoeff.ToSpan(), dst16, stride, eob, xd.Bd);
- break;
- case TxSize.Tx8x8:
- Idct.HighbdIht8x8Add(txType, dqcoeff.ToSpan(), dst16, stride, eob, xd.Bd);
- break;
- case TxSize.Tx16x16:
- Idct.HighbdIht16x16Add(txType, dqcoeff.ToSpan(), dst16, stride, eob, xd.Bd);
- break;
- case TxSize.Tx32x32:
- Idct.HighbdIdct32x32Add(dqcoeff.ToSpan(), dst16, stride, eob, xd.Bd);
- break;
- default: Debug.Assert(false, "Invalid transform size"); break;
- }
- }
- }
- else
- {
- if (xd.Lossless)
- {
- Idct.Iwht4x4Add(dqcoeff.ToSpan(), dst, stride, eob);
- }
- else
- {
- switch (txSize)
- {
- case TxSize.Tx4x4: Idct.Iht4x4Add(txType, dqcoeff.ToSpan(), dst, stride, eob); break;
- case TxSize.Tx8x8: Idct.Iht8x8Add(txType, dqcoeff.ToSpan(), dst, stride, eob); break;
- case TxSize.Tx16x16: Idct.Iht16x16Add(txType, dqcoeff.ToSpan(), dst, stride, eob); break;
- case TxSize.Tx32x32: Idct.Idct32x32Add(dqcoeff.ToSpan(), dst, stride, eob); break;
- default: Debug.Assert(false, "Invalid transform size"); return;
- }
- }
- }
- if (eob == 1)
- {
- dqcoeff.ToSpan()[0] = 0;
- }
- else
- {
- if (txType == TxType.DctDct && txSize <= TxSize.Tx16x16 && eob <= 10)
- {
- dqcoeff.ToSpan().Slice(0, 4 * (4 << (int)txSize)).Fill(0);
- }
- else if (txSize == TxSize.Tx32x32 && eob <= 34)
- {
- dqcoeff.ToSpan().Slice(0, 256).Fill(0);
- }
- else
- {
- dqcoeff.ToSpan().Slice(0, 16 << ((int)txSize << 1)).Fill(0);
- }
- }
- }
- 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.ToSpan().Slice(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.ToSpan().Slice(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).ToSpan();
- 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.Slice(0, bw).Fill(Luts.PartitionContextLookup[(int)subsize].Above);
- leftCtx.Slice(0, 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,
- ref Vp9Common cm,
- int miRow,
- int miCol,
- BlockSize bsize,
- int n4x4L2)
- {
- int n8x8L2 = n4x4L2 - 1;
- int num8x8Wh = 1 << n8x8L2;
- int hbs = num8x8Wh >> 1;
- PartitionType partition;
- BlockSize subsize;
- bool hasRows = (miRow + hbs) < cm.MiRows;
- bool hasCols = (miCol + hbs) < cm.MiCols;
- ref MacroBlockD xd = ref twd.Xd;
- if (miRow >= cm.MiRows || miCol >= cm.MiCols)
- {
- return;
- }
- partition = ReadPartition(ref twd, miRow, miCol, hasRows ? 1 : 0, hasCols ? 1 : 0, n8x8L2);
- subsize = Luts.SubsizeLookup[(int)partition][(int)bsize];
- if (hbs == 0)
- {
- // Calculate bmode block dimensions (log 2)
- xd.BmodeBlocksWl = (byte)(1 >> ((partition & PartitionType.PartitionVert) != 0 ? 1 : 0));
- xd.BmodeBlocksHl = (byte)(1 >> ((partition & PartitionType.PartitionHorz) != 0 ? 1 : 0));
- DecodeBlock(ref twd, ref cm, miRow, miCol, subsize, 1, 1);
- }
- else
- {
- switch (partition)
- {
- case PartitionType.PartitionNone:
- DecodeBlock(ref twd, ref cm, miRow, miCol, subsize, n4x4L2, n4x4L2);
- break;
- case PartitionType.PartitionHorz:
- DecodeBlock(ref twd, ref cm, miRow, miCol, subsize, n4x4L2, n8x8L2);
- if (hasRows)
- {
- DecodeBlock(ref twd, ref cm, miRow + hbs, miCol, subsize, n4x4L2, n8x8L2);
- }
- break;
- case PartitionType.PartitionVert:
- DecodeBlock(ref twd, ref cm, miRow, miCol, subsize, n8x8L2, n4x4L2);
- if (hasCols)
- {
- DecodeBlock(ref twd, ref cm, miRow, miCol + hbs, subsize, n8x8L2, n4x4L2);
- }
- break;
- case PartitionType.PartitionSplit:
- DecodePartition(ref twd, ref cm, miRow, miCol, subsize, n8x8L2);
- DecodePartition(ref twd, ref cm, miRow, miCol + hbs, subsize, n8x8L2);
- DecodePartition(ref twd, ref cm, miRow + hbs, miCol, subsize, n8x8L2);
- DecodePartition(ref twd, ref cm, miRow + hbs, miCol + hbs, subsize, n8x8L2);
- break;
- default: Debug.Assert(false, "Invalid partition type"); break;
- }
- }
- // Update partition context
- if (bsize >= BlockSize.Block8x8 && (bsize == BlockSize.Block8x8 || partition != PartitionType.PartitionSplit))
- {
- DecUpdatePartitionContext(ref twd, miRow, miCol, subsize, num8x8Wh);
- }
- }
- private static void SetupTokenDecoder(
- ArrayPtr<byte> data,
- int readSize,
- ref InternalErrorInfo errorInfo,
- ref Reader r)
- {
- // Validate the calculated partition length. If the buffer described by the
- // partition can't be fully read then throw an error.
- if (!ReadIsValid(data, readSize))
- {
- errorInfo.InternalError(CodecErr.CodecCorruptFrame, "Truncated packet or corrupt tile length");
- }
- if (r.Init(data, readSize))
- {
- errorInfo.InternalError(CodecErr.CodecMemError, "Failed to allocate bool decoder 1");
- }
- }
- // Reads the next tile returning its size and adjusting '*data' accordingly
- // based on 'isLast'.
- private static void GetTileBuffer(
- bool isLast,
- ref InternalErrorInfo errorInfo,
- ref ArrayPtr<byte> data,
- ref TileBuffer buf)
- {
- int size;
- if (!isLast)
- {
- if (!ReadIsValid(data, 4))
- {
- errorInfo.InternalError(CodecErr.CodecCorruptFrame, "Truncated packet or corrupt tile length");
- }
- size = BinaryPrimitives.ReadInt32BigEndian(data.ToSpan());
- data = data.Slice(4);
- if (size > data.Length)
- {
- errorInfo.InternalError(CodecErr.CodecCorruptFrame, "Truncated packet or corrupt tile size");
- }
- }
- else
- {
- size = data.Length;
- }
- buf.Data = data;
- buf.Size = size;
- data = data.Slice(size);
- }
- private static void GetTileBuffers(
- ref Vp9Common cm,
- ArrayPtr<byte> data,
- int tileCols,
- int tileRows,
- ref Array4<Array64<TileBuffer>> tileBuffers)
- {
- int r, c;
- for (r = 0; r < tileRows; ++r)
- {
- for (c = 0; c < tileCols; ++c)
- {
- bool isLast = (r == tileRows - 1) && (c == tileCols - 1);
- ref TileBuffer buf = ref tileBuffers[r][c];
- GetTileBuffer(isLast, ref cm.Error, ref data, ref buf);
- }
- }
- }
- public static unsafe ArrayPtr<byte> DecodeTiles(ref Vp9Common cm, ArrayPtr<byte> data)
- {
- int alignedCols = TileInfo.MiColsAlignedToSb(cm.MiCols);
- int tileCols = 1 << cm.Log2TileCols;
- int tileRows = 1 << cm.Log2TileRows;
- Array4<Array64<TileBuffer>> tileBuffers = new Array4<Array64<TileBuffer>>();
- int tileRow, tileCol;
- int miRow, miCol;
- Debug.Assert(tileRows <= 4);
- Debug.Assert(tileCols <= (1 << 6));
- // Note: this memset assumes above_context[0], [1] and [2]
- // are allocated as part of the same buffer.
- MemoryUtil.Fill(cm.AboveContext.ToPointer(), (sbyte)0, Constants.MaxMbPlane * 2 * alignedCols);
- MemoryUtil.Fill(cm.AboveSegContext.ToPointer(), (sbyte)0, alignedCols);
- LoopFilter.ResetLfm(ref cm);
- GetTileBuffers(ref cm, data, tileCols, tileRows, ref tileBuffers);
- // Load all tile information into tile_data.
- for (tileRow = 0; tileRow < tileRows; ++tileRow)
- {
- for (tileCol = 0; tileCol < tileCols; ++tileCol)
- {
- ref TileBuffer buf = ref tileBuffers[tileRow][tileCol];
- ref TileWorkerData tileData = ref cm.TileWorkerData[tileCols * tileRow + tileCol];
- tileData.Xd = cm.Mb;
- tileData.Xd.Corrupted = false;
- tileData.Xd.Counts = cm.Counts;
- tileData.Dqcoeff = new Array32<Array32<int>>();
- tileData.Xd.Tile.Init(ref cm, tileRow, tileCol);
- SetupTokenDecoder(buf.Data, buf.Size, ref cm.Error, ref tileData.BitReader);
- cm.InitMacroBlockD(ref tileData.Xd, new ArrayPtr<int>(ref tileData.Dqcoeff[0][0], 32 * 32));
- }
- }
- for (tileRow = 0; tileRow < tileRows; ++tileRow)
- {
- TileInfo tile = new TileInfo();
- tile.SetRow(ref cm, tileRow);
- for (miRow = tile.MiRowStart; miRow < tile.MiRowEnd; miRow += Constants.MiBlockSize)
- {
- for (tileCol = 0; tileCol < tileCols; ++tileCol)
- {
- int col = tileCol;
- ref TileWorkerData tileData = ref cm.TileWorkerData[tileCols * tileRow + col];
- tile.SetCol(ref cm, col);
- tileData.Xd.LeftContext = new Array3<Array16<sbyte>>();
- tileData.Xd.LeftSegContext = new Array8<sbyte>();
- for (miCol = tile.MiColStart; miCol < tile.MiColEnd; miCol += Constants.MiBlockSize)
- {
- DecodePartition(ref tileData, ref cm, miRow, miCol, BlockSize.Block64x64, 4);
- }
- cm.Mb.Corrupted |= tileData.Xd.Corrupted;
- if (cm.Mb.Corrupted)
- {
- cm.Error.InternalError(CodecErr.CodecCorruptFrame, "Failed to decode tile data");
- };
- }
- }
- }
- // Get last tile data.
- return cm.TileWorkerData[tileCols * tileRows - 1].BitReader.FindEnd();
- }
- }
- }
|