{"record":{"id":"d6aa5a48dcd4e5af","repo":"LykosAI/StabilityMatrix","slug":"could-not-decode-frame-i-of-codec-framecount","errorCode":null,"errorMessage":"Could not decode frame {i} of {codec.FrameCount}.","messagePattern":"Could not decode frame (.+?) of (.+?)\\.","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Animation/GifConverter.cs","lineNumber":29,"sourceCode":"        using var webp = new SKManagedStream(webpSource);\n        using var codec = SKCodec.Create(webp);\n\n        var info = new SKImageInfo(codec.Info.Width, codec.Info.Height);\n\n        for (var i = 0; i < codec.FrameCount; i++)\n        {\n            using var tempSurface = new SKBitmap(info);\n\n            codec.GetFrameInfo(i, out var frameInfo);\n\n            var decodeInfo = info.WithAlphaType(frameInfo.AlphaType);\n\n            tempSurface.TryAllocPixels(decodeInfo);\n\n            var result = codec.GetPixels(decodeInfo, tempSurface.GetPixels(), new SKCodecOptions(i));\n\n            if (result != SKCodecResult.Success)\n                throw new InvalidDataException($\"Could not decode frame {i} of {codec.FrameCount}.\");\n\n            using var peekPixels = tempSurface.PeekPixels();\n\n            yield return peekPixels.GetReadableBitmapData(WorkingColorSpace.Default);\n        }\n    }\n\n    public static Task ConvertAnimatedWebpToGifAsync(Stream webpSource, Stream gifOutput)\n    {\n        var gifBitmaps = EnumerateAnimatedWebp(webpSource);\n\n        return GifEncoder.EncodeAnimationAsync(\n            new AnimatedGifConfiguration(gifBitmaps, TimeSpan.FromMilliseconds(150))\n            {\n                Quantizer = OptimizedPaletteQuantizer.Wu(alphaThreshold: 0),\n                AllowDeltaFrames = true\n            },\n            gifOutput","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Animation/GifConverter.cs#L11-L47","documentation":"EnumerateAnimatedWebP in GifConverter throws InvalidDataException when SkiaSharp's SKCodec fails to decode a frame of an animated WebP (GetPixels returns a result other than SKCodecResult.Success). It indicates the encoded frame data is corrupt, unsupported, or the codec errored mid-animation while converting WebP frames to GIF.","triggerScenarios":"Calling GifConverter.EnumerateAnimatedWebP (via gifBitmaps) on an animated WebP whose frame i cannot be decoded by SKCodec with SKCodecOptions(i).","commonSituations":"Truncated or partially downloaded WebP files, WebP variants SkiaSharp's bundled libwebp cannot decode (e.g. exotic compression), corrupted downloads from Civitai image CDN.","solutions":["Re-download the WebP file and verify it is complete","Test the file with an independent WebP decoder to confirm it is valid","Upgrade SkiaSharp to a version with newer libwebp support","Wrap the enumeration in error handling and skip undecodable files"],"exampleFix":"// before\nvar frames = GifConverter.EnumerateAnimatedWebp(corruptFile).ToList();\n// after\nList<SKBitmap> frames;\ntry { frames = GifConverter.EnumerateAnimatedWebp(file).ToList(); }\ncatch (InvalidDataException) { frames = null; /* skip file */ }","handlingStrategy":"try-catch","validationCode":"if (!File.Exists(path) || new FileInfo(path).Length == 0) throw new InvalidDataException(\"WebP file missing or empty\");","typeGuard":"bool IsPlausibleWebp(byte[] b) => b.Length > 12 && b.AsSpan(0,4).SequenceEqual(\"RIFF\"u8) && b.AsSpan(8,4).SequenceEqual(\"WEBP\"u8);","tryCatchPattern":"try { var frames = GifConverter.EnumerateAnimatedWebp(stream).ToList(); }\ncatch (InvalidDataException ex) { Logger.Warn(ex, \"Animated WebP frame decode failed\"); /* fallback: static thumbnail */ }","preventionTips":["Verify downloads complete before conversion","Keep SkiaSharp updated for newer libwebp codecs","Pre-check the RIFF/WEBP magic bytes","Handle partial failures by skipping bad frames/files"],"tags":["skia","webp","image-decoding","corrupt-data"],"backgroundTag":"file-read-failed","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}