[vlc-commits] [Git][videolan/vlc][master] demux: image: hand off animated GIFs to libavformat
Tristan Matthews (@tmatth)
gitlab at videolan.org
Sat Oct 24 21:07:14 UTC 2026
Tristan Matthews pushed to branch master at VideoLAN / VLC
Commits:
d460ecd3 by Tristan Matthews at 2026-10-24T20:55:06+00: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.
- - - - -
1 changed file:
- modules/demux/image.c
Changes:
=====================================
modules/demux/image.c
=====================================
@@ -138,6 +138,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,
es_format_t *fmt, vlc_fourcc_t chroma, block_t *data)
{
@@ -771,6 +817,13 @@ static int Open(vlc_object_t *object)
}
block_t *data = Load(demux);
+ if (data && 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/d460ecd30f7eb4e2d866754a18f4c2eb3ded9aa3
--
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/d460ecd30f7eb4e2d866754a18f4c2eb3ded9aa3
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