gohugoio/hugo · error

Error creating WebPAnimEncoder\n

Error message

Error creating WebPAnimEncoder\n

What it means

Printed by genwebp's encodeNRGBAAnimated (webp.c:211) when WebPAnimEncoderNew returns NULL. The animated encoder allocation fails for invalid canvas dimensions (over WebP's 16383 cap, or non-positive), or when libwebp cannot allocate the internal animation state. The function returns NULL and the Go host sees 'Error encoding NRGBA to WebP'.

Source

Thrown at internal/warpc/genwebp/webp.c:211

    if (!ok)
    {
        WebPMemoryWriterClear(&wrt);
        return NULL;
    }
    *output_size = wrt.size;
    return wrt.mem;
}

static uint8_t *encodeNRGBAAnimated(WebPConfig *config, InputParams params, const uint8_t *all_frames_data, size_t *output_size)
{
    WebPAnimEncoderOptions anim_options;
    WebPAnimEncoderOptionsInit(&anim_options);
    anim_options.anim_params.loop_count = params.loopCount;

    WebPAnimEncoder *enc = WebPAnimEncoderNew(params.width, params.height, &anim_options);
    if (enc == NULL)
    {
        fprintf(stderr, "Error creating WebPAnimEncoder\n");
        return NULL;
    }

    int timestamp = 0;
    size_t frame_rgba_size = (size_t)params.stride * params.height;

    for (int i = 0; i < params.frameCount; i++)
    {
        WebPPicture pic;
        if (!WebPPictureInit(&pic))
        {
            fprintf(stderr, "WebPPictureInit failed\n");
            WebPAnimEncoderDelete(enc);
            return NULL;
        }
        pic.use_argb = 1;
        pic.width = params.width;
        pic.height = params.height;

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Validate params.width and params.height are in [1, 16383] before entering the animated encode path; downscale or reject otherwise.
  2. Validate params.frameCount matches the actual number of frames supplied and is reasonable (< a few hundred) for the available memory.
  3. If animating large canvases, raise the worker's memory limit or reduce frame resolution.
  4. Prefer skipping the animated WebP output and falling back to a static resize for inputs that overflow the limit.
  5. Log the params struct on the Go side at call time to correlate with this stderr line.

Example fix

// before: animated encode attempted on a 20000px-canvas GIF
encodeWebpAnimated(frames, 20000, 8000, durations)

// after: reject oversized canvas and fall back
if w > 16383 || h > 16383 {
    return fmt.Errorf("canvas %dx%d exceeds WebP limit; skipping animated WebP", w, h)
}
encodeWebpAnimated(frames, w, h, durations)
Defensive patterns

Strategy: validation

Validate before calling

func validateAnimParams(w, h, frameCount int) error {
    const max = 16383
    if w <= 0 || h <= 0 || w > max || h > max {
        return fmt.Errorf("canvas %dx%d out of range [1,%d]", w, h, max)
    }
    if frameCount <= 0 { return fmt.Errorf("frameCount %d must be > 0", frameCount) }
    return nil
}

Prevention

When it happens

Trigger: Calling the animated path with params.width or params.height that exceed WebP limits or are <= 0, with an extremely high frameCount that exhausts memory during encoder construction, or under overall process memory pressure.

Common situations: Decoding a large animated source (GIF/animated WebP) whose canvas exceeds 16383px, decoding a GIF with hundreds of frames on a memory-constrained worker, or passing zero/negative dimensions because the params struct was not populated.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/e72cfef71161e560. Report an issue: GitHub.