Skip to content

feat: Enable tuning of the WebP encoder settings. #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 18, 2025

Conversation

skidder
Copy link
Contributor

@skidder skidder commented Mar 13, 2025

This PR adds fine-grained control over WebP encoding, particularly useful for animated WebP outputs. The changes introduce several new configuration options that allow users to optimize the encoding process for different use cases, from ultra-fast encoding to high-quality compression.

Key Changes

New WebP Encoding Options

Added support for the following WebP encoder parameters:

  • WebpMethod (0-6): Controls compression method, trading speed for efficiency
  • WebpFilterStrength (0-100): Controls spatial filter strength
  • WebpFilterType (0-1): Allows choosing between simple and strong filtering
  • WebpAutofilter (0-1): Enables automatic filter strength adjustment
  • WebpPartitions (0-3): Controls partition count for encoding
  • WebpSegments (1-4): Controls segment count
  • WebpPreprocessing (0-1): Enables preprocessing filter
  • WebpThreadLevel (0-1): Controls multi-threading
  • WebpPalette (0-1): Enables palette-based optimization

Implementation Details

  • Modified webp.cpp to properly handle these options through the WebPConfig structure
  • Integrated options with both single-frame and animated WebP encoding paths
  • Maintained backward compatibility with existing encoder behavior
  • Switched WebP composition process to use the high-level WebPAnimEncoderAssemble set of libwebp APIs which automatically apply key-frame/partial-frame optimizations; previously only key-frames were produced, leading to larger files with more image data to compress.

The improvements in file size are evident when describing an encoded animated WebP image with webpmux. Although this introduces a step of determining the optimal size for non-key-frames, it's offset by having to compress less image data for those frames which is typically far more expensive.

Before

→ webpmux -info video_errors/peaky-prod.webp
Canvas size: 550 x 297
Features present: animation
Background color : 0xFF152221  Loop Count : 0
Number of frames: 106
No.: width height alpha x_offset y_offset duration   dispose blend image_size  compression
  1:   550   297    no        0        0       40       none    no      24378       lossy
  2:   550   297    no        0        0       40       none   yes      24370       lossy
  3:   550   297    no        0        0       40       none   yes      24368       lossy
  4:   550   297    no        0        0       40       none   yes      24342       lossy
  5:   550   297    no        0        0       40       none   yes      24318       lossy
  6:   550   297    no        0        0       40       none   yes      24420       lossy
  7:   550   297    no        0        0       40       none   yes      24356       lossy
  8:   550   297    no        0        0       40       none   yes      24466       lossy
  9:   550   297    no        0        0       40       none   yes      24280       lossy
 10:   550   297    no        0        0       40       none   yes      24318       lossy
 11:   550   297    no        0        0       40       none   yes      24320       lossy
 12:   550   297    no        0        0       40       none   yes      24360       lossy

After

→ webpmux -info video_errors/peaky-kmin3.webp
Canvas size: 550 x 297
Features present: animation transparency
Background color : 0xFF152221  Loop Count : 0
Number of frames: 106
No.: width height alpha x_offset y_offset duration   dispose blend image_size  compression
  1:   550   297    no        0        0       40       none    no      26094       lossy
  2:   305   150   yes      112       24       40       none   yes       3192       lossy
  3:   331   171   yes      112        4       40       none   yes       3700       lossy
  4:   315   161   yes      112       14       40       none   yes       3840       lossy
  5:   550   297    no        0        0       40       none    no      26032       lossy
  6:   332   174   yes      110       14       40       none   yes       3556       lossy
  7:   304   179   yes      102        0       40       none   yes       3604       lossy
  8:   333   160   yes      106       22       40       none   yes       3284       lossy
  9:   550   297    no        0        0       40       none    no      26052       lossy
 10:   306   164   yes      114       20       40       none   yes       2644       lossy
 11:   260   158   yes      150       20       40       none   yes       3018       lossy
 12:   326   167   yes      108       12       40       none   yes       3466       lossy

Testing

  • Added comprehensive benchmark tests with various parameter combinations
  • Included test cases for both static and animated WebP outputs

Notes

  • All new options are optional and maintain backward compatibility
  • Default values are chosen to match previous behavior
  • Memory usage may vary based on parameter combinations

@skidder skidder merged commit 3c22ea9 into master Mar 18, 2025
2 checks passed
@skidder skidder deleted the skidder/webp-encoder-optimizations branch March 25, 2025 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants