heif() with compression 'hevc' fails with 'Invalid parameter value (5.2006)' due to default tune 'auto' · Issue #4621 · lovell/sharp · GitHub
Skip to content

heif() with compression 'hevc' fails with 'Invalid parameter value (5.2006)' due to default tune 'auto' #4621

Open

Description

@zoellner

Possible bug

Is this a possible bug in a feature of sharp, unrelated to installation?

  • Running npm install sharp completes without error.
  • Running node -e "import 'sharp'" completes without error.

Are you using the latest version of sharp?

  • I am using the latest version of sharp as reported by npm view sharp dist-tags.latest.

What is the output of running npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp?

Not available. This runs inside the public.ecr.aws/lambda/nodejs:24 image (Amazon Linux 2023, x86_64, Node.js 24), not on a host where I can run envinfo. The setup is:

  • sharp 0.35.5, built from source (SHARP_FORCE_GLOBAL_LIBVIPS=1 npm run build) against a globally installed libvips
  • libvips 8.18.7
  • libheif 1.23.6 with the x265 4.2 encoder, libde265 1.1.3 and libaom 3.15.1

The build is https://github.com/zoellner/sharp-heic-lambda-layer, an AWS Lambda layer that provides HEIC support. The prebuilt sharp binaries don't include x265, so this probably only shows up with a globally installed libvips whose libheif has the x265 encoder.

Does this problem relate to file caching?

  • Adding sharp.cache(false) does not fix this problem.

N/A, this is an encoder parameter error.

Does this problem relate to images appearing to have been rotated by 90 degrees?

  • Using rotate() or keepExif() does not fix this problem.

N/A.

What are the steps to reproduce?

With sharp using a libvips whose libheif has the x265 encoder, encode HEIF with compression: 'hevc' and don't set tune:

Error: heif: Invalid parameter value (5.2006)
    at Sharp.toBuffer (.../node_modules/sharp/dist/output.cjs:159:17)

5.2006 is heif_error_Usage_error / heif_suberror_Invalid_parameter_value.

The cause seems to be this chain:

  1. sharp always sets tune, which defaults to 'auto' (heifTune: 'auto', passed via ->set("tune", baton->heifTune.c_str()) in src/pipeline.cc).
  2. libvips heifsave forwards it to heif_encoder_set_parameter_string(encoder, "tune", ...). It ignores only heif_suberror_Unsupported_parameter, not Invalid_parameter_value.
  3. libheif's AV1 (libaom) plugin accepts auto, but its x265 plugin only accepts psnr, ssim, grain and fastdecode. It returns Invalid_parameter_value for auto, so every HEVC encode fails unless tune is set explicitly. There is also no way to choose "no tune" from sharp.

Setting tune: 'ssim' or tune: 'psnr' explicitly works around it.

What is the expected behaviour?

heif({ compression: 'hevc' }) should encode successfully with the default options, as it did before tune was added.

A possible fix is to skip setting tune when it is 'auto'. libheif's aom plugin already treats auto as its default, so AV1 output would be unchanged. An alternative is to set tune only when compression is 'av1'.

Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem

const sharp = require('sharp');

(async () => {
  // Throws: heif: Invalid parameter value (5.2006)
  await sharp('input.heic').resize({ width: 320 }).heif({ compression: 'hevc', quality: 50 }).toBuffer();
})();
// Works
await sharp('input.heic').resize({ width: 320 }).heif({ compression: 'hevc', quality: 50, tune: 'ssim' }).toBuffer();

Please provide sample image(s) that help explain this problem

Any input image reproduces it. The error happens when the HEVC encoder is configured, before any pixels are encoded.

Activity

  1. added this to the v0.35.6 milestone on Oct 9, 2026
  2. lovell commented on Oct 9, 2026

    @lovell
    Owner

    Thanks for reporting, I recall somewhere along the lines that libheif was planning to allow "auto" for other encoders, but that's clearly not the case so commits 43cc2dc and 203f6e6 fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions