[vlc-commits] [Git][videolan/vlc][3.0.x] demux: image: hand off animated GIFs to libavformat
Felix Paul Kühne (@fkuehne)
gitlab at videolan.org
Mon Oct 26 07:28:02 UTC 2026
Felix Paul Kühne pushed to branch 3.0.x at VideoLAN / VLC
Commits:
3807b94e by Tristan Matthews at 2026-10-24T17:21:49-04:00
demux: image: hand off animated GIFs to libavformat
The rationale for not handing off ALL GIFs to libavformat is that the
default behavior for static GIFs would change, it would render them for
e.g. 1 frame instead of the default 10s we currently have.
(cherry picked from commit d460ecd30f7eb4e2d866754a18f4c2eb3ded9aa3)
- - - - -
1 changed file:
- modules/demux/image.c
Changes:
=====================================
modules/demux/image.c
=====================================
@@ -137,6 +137,52 @@ static block_t *Load(demux_t *demux)
return block;
}
+/* Size in bytes of a GIF color table described by a packed field, or 0 */
+static size_t GifColorTableSize(uint8_t packed)
+{
+ return (packed & 0x80) ? 3u << ((packed & 0x07) + 1) : 0;
+}
+
+/* Skip GIF data sub-blocks up to and including the block terminator */
+static size_t GifSkipSubBlocks(const uint8_t *data, size_t size, size_t pos)
+{
+ while (pos < size && data[pos] != 0)
+ pos += 1 + data[pos];
+ return pos + 1;
+}
+
+/* Walk the GIF block structure and report whether it holds more than one
+ * image (i.e. an animation). A truncated or corrupt stream is reported as a
+ * still image. */
+static bool IsAnimatedGif(const uint8_t *data, size_t size)
+{
+ /* Header (6) + Logical Screen Descriptor (7) + Global Color Table */
+ if (size < 13)
+ return false;
+
+ size_t pos = 13 + GifColorTableSize(data[10]);
+ unsigned images = 0;
+ while (pos < size) {
+ switch (data[pos++]) {
+ case 0x21: /* Extension: skip the label */
+ pos++;
+ break;
+ case 0x2C: /* Image Descriptor */
+ if (++images > 1)
+ return true;
+ if (size - pos < 9)
+ return false;
+ /* Descriptor, Local Color Table, LZW minimum code size */
+ pos += 9 + GifColorTableSize(data[pos + 8]) + 1;
+ break;
+ default: /* Trailer or unknown block */
+ return false;
+ }
+ pos = GifSkipSubBlocks(data, size, pos);
+ }
+ return false;
+}
+
static block_t *Decode(demux_t *demux,
video_format_t *fmt, vlc_fourcc_t chroma, block_t *data)
{
@@ -709,6 +755,13 @@ static int Open(vlc_object_t *object)
fmt.video.i_chroma = fmt.i_codec;
block_t *data = Load(demux);
+ if (data && img->codec == VLC_CODEC_GIF &&
+ IsAnimatedGif(data->p_buffer, data->i_buffer)) {
+ msg_Dbg(demux, "animated GIF, letting avformat demux it");
+ block_Release(data);
+ es_format_Clean(&fmt);
+ return VLC_EGENERIC;
+ }
if (data && var_InheritBool(demux, "image-decode")) {
char *string = var_InheritString(demux, "image-chroma");
vlc_fourcc_t chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, string);
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/3807b94e0c2ce49aaffa6a47e31fdd36c914c017
--
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/3807b94e0c2ce49aaffa6a47e31fdd36c914c017
You're receiving this email because of your account on code.videolan.org. Manage all notifications: https://code.videolan.org/-/profile/notifications | Help: https://code.videolan.org/help
More information about the vlc-commits
mailing list