LykosAI/StabilityMatrix · error · InvalidDataException

Could not decode frame

Error message

Could not decode frame {i} of {codec.FrameCount}.

What it means

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.

Solutions

  1. Re-download the WebP file and verify it is complete
  2. Test the file with an independent WebP decoder to confirm it is valid
  3. Upgrade SkiaSharp to a version with newer libwebp support
  4. Wrap the enumeration in error handling and skip undecodable files

Example fix

// before
var frames = GifConverter.EnumerateAnimatedWebp(corruptFile).ToList();
// after
List<SKBitmap> frames;
try { frames = GifConverter.EnumerateAnimatedWebp(file).ToList(); }
catch (InvalidDataException) { frames = null; /* skip file */ }
Defensive patterns

Strategy: try-catch

Validate before calling

if (!File.Exists(path) || new FileInfo(path).Length == 0) throw new InvalidDataException("WebP file missing or empty");

Type guard

bool IsPlausibleWebp(byte[] b) => b.Length > 12 && b.AsSpan(0,4).SequenceEqual("RIFF"u8) && b.AsSpan(8,4).SequenceEqual("WEBP"u8);

Try / catch

try { var frames = GifConverter.EnumerateAnimatedWebp(stream).ToList(); }
catch (InvalidDataException ex) { Logger.Warn(ex, "Animated WebP frame decode failed"); /* fallback: static thumbnail */ }

Prevention

When it happens

Trigger: Calling GifConverter.EnumerateAnimatedWebP (via gifBitmaps) on an animated WebP whose frame i cannot be decoded by SKCodec with SKCodecOptions(i).

Common situations: Truncated or partially downloaded WebP files, WebP variants SkiaSharp's bundled libwebp cannot decode (e.g. exotic compression), corrupted downloads from Civitai image CDN.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/d6aa5a48dcd4e5af. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Core/Animation/GifConverter.cs:29

        using var webp = new SKManagedStream(webpSource);
        using var codec = SKCodec.Create(webp);

        var info = new SKImageInfo(codec.Info.Width, codec.Info.Height);

        for (var i = 0; i < codec.FrameCount; i++)
        {
            using var tempSurface = new SKBitmap(info);

            codec.GetFrameInfo(i, out var frameInfo);

            var decodeInfo = info.WithAlphaType(frameInfo.AlphaType);

            tempSurface.TryAllocPixels(decodeInfo);

            var result = codec.GetPixels(decodeInfo, tempSurface.GetPixels(), new SKCodecOptions(i));

            if (result != SKCodecResult.Success)
                throw new InvalidDataException($"Could not decode frame {i} of {codec.FrameCount}.");

            using var peekPixels = tempSurface.PeekPixels();

            yield return peekPixels.GetReadableBitmapData(WorkingColorSpace.Default);
        }
    }

    public static Task ConvertAnimatedWebpToGifAsync(Stream webpSource, Stream gifOutput)
    {
        var gifBitmaps = EnumerateAnimatedWebp(webpSource);

        return GifEncoder.EncodeAnimationAsync(
            new AnimatedGifConfiguration(gifBitmaps, TimeSpan.FromMilliseconds(150))
            {
                Quantizer = OptimizedPaletteQuantizer.Wu(alphaThreshold: 0),
                AllowDeltaFrames = true
            },
            gifOutput

View on GitHub (pinned to af93d6ef57)