private long OneStatPass(int width, int height, int yStride, int uvStride, Vp8RdLevel rdOpt, int nbMbs, PassStats stats) { Span <byte> y = this.Y.GetSpan(); Span <byte> u = this.U.GetSpan(); Span <byte> v = this.V.GetSpan(); var it = new Vp8EncIterator(this.YTop, this.UvTop, this.Nz, this.MbInfo, this.Preds, this.TopDerr, this.Mbw, this.Mbh); long size = 0; long sizeP0 = 0; long distortion = 0; long pixelCount = nbMbs * 384; it.Init(); this.SetLoopParams(stats.Q); var info = new Vp8ModeScore(); do { info.Clear(); it.Import(y, u, v, yStride, uvStride, width, height, false); if (this.Decimate(it, ref info, rdOpt)) { // Just record the number of skips and act like skipProba is not used. ++this.Proba.NbSkip; } this.RecordResiduals(it, info); size += info.R + info.H; sizeP0 += info.H; distortion += info.D; it.SaveBoundary(); }while (it.Next() && --nbMbs > 0); sizeP0 += this.SegmentHeader.Size; if (stats.DoSizeSearch) { size += this.Proba.FinalizeSkipProba(this.Mbw, this.Mbh); size += this.Proba.FinalizeTokenProbas(); size = ((size + sizeP0 + 1024) >> 11) + HeaderSizeEstimate; stats.Value = size; } else { stats.Value = GetPsnr(distortion, pixelCount); } return(sizeP0); }
public static void PickBestUv(Vp8EncIterator it, ref Vp8ModeScore rd, Vp8SegmentInfo[] segmentInfos, Vp8EncProba proba) { const int numBlocks = 8; Vp8SegmentInfo dqm = segmentInfos[it.CurrentMacroBlockInfo.Segment]; int lambda = dqm.LambdaUv; Span <byte> src = it.YuvIn.AsSpan(Vp8EncIterator.UOffEnc); Span <byte> tmpDst = it.YuvOut2.AsSpan(Vp8EncIterator.UOffEnc); Span <byte> dst0 = it.YuvOut.AsSpan(Vp8EncIterator.UOffEnc); Span <byte> dst = dst0; var rdBest = new Vp8ModeScore(); var rdUv = new Vp8ModeScore(); var res = new Vp8Residual(); int mode; rd.ModeUv = -1; rdBest.InitScore(); for (mode = 0; mode < WebpConstants.NumPredModes; ++mode) { rdUv.Clear(); // Reconstruct rdUv.Nz = (uint)ReconstructUv(it, dqm, rdUv, tmpDst, mode); // Compute RD-score rdUv.D = LossyUtils.Vp8_Sse16X8(src, tmpDst); rdUv.SD = 0; // not calling TDisto here: it tends to flatten areas. rdUv.H = WebpConstants.Vp8FixedCostsUv[mode]; rdUv.R = it.GetCostUv(rdUv, proba, res); if (mode > 0 && IsFlat(rdUv.UvLevels, numBlocks, WebpConstants.FlatnessLimitIUv)) { rdUv.R += WebpConstants.FlatnessPenality * numBlocks; } rdUv.SetRdScore(lambda); if (mode == 0 || rdUv.Score < rdBest.Score) { rdBest.CopyScore(rdUv); rd.ModeUv = mode; rdUv.UvLevels.CopyTo(rd.UvLevels.AsSpan()); for (int i = 0; i < 2; i++) { rd.Derr[i, 0] = rdUv.Derr[i, 0]; rd.Derr[i, 1] = rdUv.Derr[i, 1]; rd.Derr[i, 2] = rdUv.Derr[i, 2]; } Span <byte> tmp = dst; dst = tmpDst; tmpDst = tmp; } } it.SetIntraUvMode(rd.ModeUv); rd.AddScore(rdBest); if (dst != dst0) { // copy 16x8 block if needed. LossyUtils.Vp8Copy16X8(dst, dst0); } // Store diffusion errors for next block. it.StoreDiffusionErrors(rd); }
/// <summary> /// Encodes the image to the specified stream from the <see cref="Image{TPixel}"/>. /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="image">The <see cref="Image{TPixel}"/> to encode from.</param> /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param> public void Encode <TPixel>(Image <TPixel> image, Stream stream) where TPixel : unmanaged, IPixel <TPixel> { int width = image.Width; int height = image.Height; Span <byte> y = this.Y.GetSpan(); Span <byte> u = this.U.GetSpan(); Span <byte> v = this.V.GetSpan(); YuvConversion.ConvertRgbToYuv(image, this.configuration, this.memoryAllocator, y, u, v); int yStride = width; int uvStride = (yStride + 1) >> 1; var it = new Vp8EncIterator(this.YTop, this.UvTop, this.Nz, this.MbInfo, this.Preds, this.TopDerr, this.Mbw, this.Mbh); int[] alphas = new int[WebpConstants.MaxAlpha + 1]; this.alpha = this.MacroBlockAnalysis(width, height, it, y, u, v, yStride, uvStride, alphas, out this.uvAlpha); int totalMb = this.Mbw * this.Mbw; this.alpha /= totalMb; this.uvAlpha /= totalMb; // Analysis is done, proceed to actual encoding. this.SegmentHeader = new Vp8EncSegmentHeader(4); this.AssignSegments(alphas); this.SetLoopParams(this.quality); // Initialize the bitwriter. int averageBytesPerMacroBlock = this.averageBytesPerMb[this.BaseQuant >> 4]; int expectedSize = this.Mbw * this.Mbh * averageBytesPerMacroBlock; this.bitWriter = new Vp8BitWriter(expectedSize, this); // TODO: EncodeAlpha(); bool hasAlpha = false; // Stats-collection loop. this.StatLoop(width, height, yStride, uvStride); it.Init(); it.InitFilter(); var info = new Vp8ModeScore(); var residual = new Vp8Residual(); do { bool dontUseSkip = !this.Proba.UseSkipProba; info.Clear(); it.Import(y, u, v, yStride, uvStride, width, height, false); // Warning! order is important: first call VP8Decimate() and // *then* decide how to code the skip decision if there's one. if (!this.Decimate(it, ref info, this.rdOptLevel) || dontUseSkip) { this.CodeResiduals(it, info, residual); } else { it.ResetAfterSkip(); } it.SaveBoundary(); }while (it.Next()); // Store filter stats. this.AdjustFilterStrength(); // Write bytes from the bitwriter buffer to the stream. image.Metadata.SyncProfiles(); this.bitWriter.WriteEncodedImageToStream(stream, image.Metadata.ExifProfile, (uint)width, (uint)height, hasAlpha); }
public static bool PickBestIntra4(Vp8EncIterator it, ref Vp8ModeScore rd, Vp8SegmentInfo[] segmentInfos, Vp8EncProba proba, int maxI4HeaderBits) { Vp8SegmentInfo dqm = segmentInfos[it.CurrentMacroBlockInfo.Segment]; int lambda = dqm.LambdaI4; int tlambda = dqm.TLambda; Span <byte> src0 = it.YuvIn.AsSpan(Vp8EncIterator.YOffEnc); Span <byte> bestBlocks = it.YuvOut2.AsSpan(Vp8EncIterator.YOffEnc); Span <int> scratch = it.Scratch3; int totalHeaderBits = 0; var rdBest = new Vp8ModeScore(); if (maxI4HeaderBits == 0) { return(false); } rdBest.InitScore(); rdBest.H = 211; // '211' is the value of VP8BitCost(0, 145) rdBest.SetRdScore(dqm.LambdaMode); it.StartI4(); var rdi4 = new Vp8ModeScore(); var rdTmp = new Vp8ModeScore(); var res = new Vp8Residual(); Span <short> tmpLevels = new short[16]; do { int numBlocks = 1; rdi4.Clear(); int mode; int bestMode = -1; Span <byte> src = src0.Slice(WebpLookupTables.Vp8Scan[it.I4]); short[] modeCosts = it.GetCostModeI4(rd.ModesI4); Span <byte> bestBlock = bestBlocks.Slice(WebpLookupTables.Vp8Scan[it.I4]); Span <byte> tmpDst = it.Scratch.AsSpan(); tmpDst.Clear(); rdi4.InitScore(); it.MakeIntra4Preds(); for (mode = 0; mode < WebpConstants.NumBModes; ++mode) { rdTmp.Clear(); tmpLevels.Clear(); // Reconstruct. rdTmp.Nz = (uint)ReconstructIntra4(it, dqm, tmpLevels, src, tmpDst, mode); // Compute RD-score. rdTmp.D = LossyUtils.Vp8_Sse4X4(src, tmpDst); rdTmp.SD = tlambda != 0 ? Mult8B(tlambda, LossyUtils.Vp8Disto4X4(src, tmpDst, WeightY, scratch)) : 0; rdTmp.H = modeCosts[mode]; // Add flatness penalty, to avoid flat area to be mispredicted by a complex mode. if (mode > 0 && IsFlat(tmpLevels, numBlocks, WebpConstants.FlatnessLimitI4)) { rdTmp.R = WebpConstants.FlatnessPenality * numBlocks; } else { rdTmp.R = 0; } // Early-out check. rdTmp.SetRdScore(lambda); if (bestMode >= 0 && rdTmp.Score >= rdi4.Score) { continue; } // Finish computing score. rdTmp.R += it.GetCostLuma4(tmpLevels, proba, res); rdTmp.SetRdScore(lambda); if (bestMode < 0 || rdTmp.Score < rdi4.Score) { rdi4.CopyScore(rdTmp); bestMode = mode; Span <byte> tmp = tmpDst; tmpDst = bestBlock; bestBlock = tmp; tmpLevels.CopyTo(rdBest.YAcLevels.AsSpan(it.I4 * 16, 16)); } } rdi4.SetRdScore(dqm.LambdaMode); rdBest.AddScore(rdi4); if (rdBest.Score >= rd.Score) { return(false); } totalHeaderBits += (int)rdi4.H; // <- equal to modeCosts[bestMode]; if (totalHeaderBits > maxI4HeaderBits) { return(false); } // Copy selected samples to the right place. LossyUtils.Vp8Copy4X4(bestBlock, bestBlocks.Slice(WebpLookupTables.Vp8Scan[it.I4])); rd.ModesI4[it.I4] = (byte)bestMode; it.TopNz[it.I4 & 3] = it.LeftNz[it.I4 >> 2] = rdi4.Nz != 0 ? 1 : 0; }while (it.RotateI4(bestBlocks)); // Finalize state. rd.CopyScore(rdBest); it.SetIntra4Mode(rd.ModesI4); it.SwapOut(); rdBest.YAcLevels.AsSpan().CopyTo(rd.YAcLevels); // Select intra4x4 over intra16x16. return(true); }