The function for analyzing is fairly straightforward: after some setup, it simply loops through all the pixels in the buffer, and compares the color of each pixel to the transparent color. The solution we came up with was to write a function that analyzes each frame to determine which method to use, and then write the frame using the correct method. The reduction in file size that comes from the DND method can be significant, so we want to be able to use it when possible, but also use RTB when needed. You can read the full spec if you are curious and this article has more non-technical info on disposal methods and how they work.) If you are thinking that clearly the solution was to add support for the RTB method, you’re right, and that’s just what we did. The problem in FFMPEG was that it only supported the DND method, so single frame GIFs and GIFs without transparency both worked fine, but GIFs that had multiple frames and transparency did not. This sticker is a rare exception.Īlthough this method isn’t generally useful for stickers, it can be very useful in cases where GIF encoders want to use different sets of colors on different frames ( which can be used to improve the quality), and for reducing file size because multiple transparencies in a row are easier to compress than a bunch of different colors. Most stickers have different transparent pixels from frame to frame. DND could be used for Stickers if the pixel transparency doesn’t change from frame to frame, but that’s an unusual use-case for animation. When using the DND method, only pixels that were transparent in the previous frame can be made transparent again in the current frame. Transparency can be used to optimize GIFs like this. For example, in the following GIF, a lot of pixels don’t change color between frames, so we can use transparency for all the unchanged pixels. With this method, transparent pixels serve the purpose of preserving the color of the pixel from the previous frame. The second disposal method is called “Do not dispose,” and it turns out that using the “Do not dispose” (or DND) method, GIF viewers can, instead, build a new frame using the last frame as the starting point. It does what you’d expect: when a new frame is drawn, the area of the image is set to be completely transparent and only non-transparent pixels get drawn on top of that. The first disposal method is called “Restore to background color” in the GIF specification. However, it turns out that if there are multiple frames, (i.e., an animated GIF), there are several possible ways to handle transparencies. If the GIF is a single-frame (i.e., a still image), how to handle the “transparent” color is pretty obvious: the tool rendering the GIF just needs to know to not draw that pixel. Non-transparencies are 100% opaque, meaning the GIF format does not support an “alpha” in the way that some formats, like PNG, do. Optionally, one of those colors can be transparent. In a GIF, any pixel can take on any one of 256 colors defined in a palette. To understand the issue that FFMPEG had writing transparent GIFs, you need to understand exactly how transparencies work in the GIF format, and how FFMPEG was handling it. Why FFMPEG Doesn’t Support Transparencies For this reason, we decided to tackle the issue ourselves. Although we have other tools at our disposal, such as Gifsicle, we want to use as many of the same tools as possible, especially ones that are powerful, popular and fast like FFMPEG. Until recently, however, FFMPEG did not properly support writing GIFs that are animated and have transparent pixels. At GIPHY, we use FFMPEG to process all uploaded content – including GIFs and Stickers.įFMPEG is fast, high quality, and open source. One of those tools is FFMPEG, an extremely popular package for working with video in a wide variety of formats. The distinction exists in our engineering toolchain as well – some tools struggle to correctly support Stickers (usually due to transparency). However, we still use the GIF format to store stickers – all they really are is GIFs with transparent pixels. Here at GIPHY, we differentiate between GIFs and Stickers in our business language, as the two products are served to different searches and customers. A sticker (left) is just a GIF (right) with transparent pixels.
0 Comments
Leave a Reply. |