From: heipa <[email protected]>
---
Changelog | 1 +
configure | 4 +-
doc/general_contents.texi | 2 +-
libavcodec/Makefile | 1 +
libavcodec/allcodecs.c | 1 +
libavcodec/libwebpdec.c | 243 ++++++++++++++++++++++++++++++++++++++
libavcodec/version.h | 2 +-
7 files changed, 251 insertions(+), 3 deletions(-)
create mode 100644 libavcodec/libwebpdec.c
diff --git a/Changelog b/Changelog
index b0f8769..63c00de 100644
--- a/Changelog
+++ b/Changelog
@@ -10,6 +10,7 @@ version <next>:
- transpose_cuda filter
- Add AMF Frame Rate Converter (vf_frc_amf) filter
- SMPTE 2094-50 metadata support and passthrough
+- animated WebP decoding support via libwebpdemux
- ProRes RAW VideoToolbox hwaccel
diff --git a/configure b/configure
index 730b4ac..9ec9a37 100755
--- a/configure
+++ b/configure
@@ -3890,6 +3890,7 @@ libvpx_vp9_encoder_deps="libvpx"
libvvenc_encoder_deps="libvvenc"
libwebp_encoder_deps="libwebp"
libwebp_anim_encoder_deps="libwebp"
+libwebp_decoder_deps="libwebp"
libx262_encoder_deps="libx262"
libx264_encoder_deps="libx264"
libx264_encoder_select="atsc_a53 golomb"
@@ -7497,7 +7498,8 @@ enabled libvpx && {
enabled libvvenc && require_pkg_config libvvenc "libvvenc >= 1.6.1"
"vvenc/vvenc.h" vvenc_get_version
enabled libwebp && {
enabled libwebp_encoder && require_pkg_config libwebp "libwebp >=
0.2.0" webp/encode.h WebPGetEncoderVersion
- enabled libwebp_anim_encoder && check_pkg_config libwebp_anim_encoder
"libwebpmux >= 0.4.0" webp/mux.h WebPAnimEncoderOptionsInit; }
+ enabled libwebp_anim_encoder && check_pkg_config libwebp_anim_encoder
"libwebpmux >= 0.4.0" webp/mux.h WebPAnimEncoderOptionsInit
+ enabled libwebp_decoder && check_pkg_config libwebp_decoder
"libwebpdemux >= 1.5.0" webp/demux.h WebPAnimDecoderOptionsInitInternal; }
enabled libx264 && require_pkg_config libx264 x264 "stdint.h x264.h"
x264_encoder_encode &&
require_cpp_condition libx264 x264.h "X264_BUILD
>= 155" && {
[ "$toolchain" != "msvc" ] ||
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index 47ac198..83a41a5 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -878,7 +878,7 @@ following image formats are supported:
@item WBMP @tab X @tab X
@tab Wireless Application Protocol Bitmap image format
@item WebP @tab E @tab X
- @tab WebP image format, encoding supported through external library libwebp
+ @tab WebP image format, supported through external library libwebp
@item XBM @tab X @tab X
@tab X BitMap image format
@item XFace @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 85d3591..6c90a4a 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1225,6 +1225,7 @@ OBJS-$(CONFIG_LIBVPX_VP9_ENCODER) += libvpxenc.o
OBJS-$(CONFIG_LIBVVENC_ENCODER) += libvvenc.o
OBJS-$(CONFIG_LIBWEBP_ENCODER) += libwebpenc_common.o libwebpenc.o
OBJS-$(CONFIG_LIBWEBP_ANIM_ENCODER) += libwebpenc_common.o
libwebpenc_animencoder.o
+OBJS-$(CONFIG_LIBWEBP_DECODER) += libwebpdec.o
OBJS-$(CONFIG_LIBX262_ENCODER) += libx264.o
OBJS-$(CONFIG_LIBX264_ENCODER) += libx264.o
OBJS-$(CONFIG_LIBX265_ENCODER) += libx265.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 5e798c1..424e0f6 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -396,6 +396,7 @@ extern const FFCodec ff_vqc_decoder;
extern const FFCodec ff_vvc_decoder;
extern const FFCodec ff_wbmp_decoder;
extern const FFCodec ff_wbmp_encoder;
+extern const FFCodec ff_libwebp_decoder;
extern const FFCodec ff_webp_decoder;
extern const FFCodec ff_wcmv_decoder;
extern const FFCodec ff_wrapped_avframe_encoder;
diff --git a/libavcodec/libwebpdec.c b/libavcodec/libwebpdec.c
new file mode 100644
index 0000000..760cd82
--- /dev/null
+++ b/libavcodec/libwebpdec.c
@@ -0,0 +1,243 @@
+/*
+ * LibWebP decoder
+ * Copyright (c) 2025 Peter Xia
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * LibWebP decoder
+ */
+
+#include "avcodec.h"
+#include "codec_internal.h"
+#include "decode.h"
+#include "libavutil/avassert.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+
+#include <webp/demux.h>
+#include <webp/decode.h>
+
+typedef struct AnimatedWebPContext {
+ const AVClass *class;
+ WebPAnimDecoderOptions dec_options;
+ WebPAnimDecoder *dec;
+ AVBufferRef *file_content;
+ WebPData webp_data;
+ uint32_t loop_count;
+ uint32_t loop_sent;
+ uint32_t frame_count;
+ uint32_t frame_sent;
+ int prev_timestamp_ms;
+ int ignore_loop;
+ int infinite_loop;
+ int first_frame_pts;
+} AnimatedWebPContext;
+
+static av_cold int libwebp_decode_init(AVCodecContext *avctx)
+{
+ AnimatedWebPContext *s = avctx->priv_data;
+
+ if (!WebPAnimDecoderOptionsInit(&s->dec_options)) {
+ av_log(avctx, AV_LOG_ERROR, "Cannot initialize
WebPAnimDecoderOptions\n");
+ return AVERROR_EXTERNAL;
+ }
+ s->dec_options.color_mode = MODE_RGBA;
+ s->dec_options.use_threads = 1;
+ s->file_content = NULL;
+ s->dec = NULL;
+ s->loop_sent = 0;
+ s->frame_sent = 0;
+ s->prev_timestamp_ms = 0;
+ s->infinite_loop = 0;
+ s->first_frame_pts = -1;
+
+ avctx->pix_fmt = AV_PIX_FMT_RGBA;
+ avctx->pkt_timebase = av_make_q(1, 1000);
+
+ return 0;
+}
+
+static int libwebp_decode_frame(AVCodecContext *avctx, AVFrame *p,
+ int *got_frame, AVPacket *avpkt)
+{
+ AnimatedWebPContext *s = avctx->priv_data;
+ WebPAnimInfo anim_info;
+ uint8_t *frame_rgba;
+ int timestamp_ms;
+ int ret;
+
+ if (!s->dec) {
+ if (!avpkt || avpkt->size <= 0)
+ return AVERROR(EINVAL);
+
+ s->file_content = av_buffer_ref(avpkt->buf);
+ if (!s->file_content)
+ return AVERROR(ENOMEM);
+
+ s->webp_data.bytes = s->file_content->data;
+ s->webp_data.size = s->file_content->size;
+
+ s->dec = WebPAnimDecoderNew(&s->webp_data, &s->dec_options);
+ if (!s->dec) {
+ av_log(avctx, AV_LOG_ERROR, "Error creating WebPAnimDecoder.\n");
+ av_buffer_unref(&s->file_content);
+ return AVERROR_EXTERNAL;
+ }
+
+ if (!WebPAnimDecoderGetInfo(s->dec, &anim_info)) {
+ av_log(avctx, AV_LOG_ERROR, "Error getting WebP animation
info.\n");
+ WebPAnimDecoderDelete(s->dec);
+ s->dec = NULL;
+ av_buffer_unref(&s->file_content);
+ return AVERROR_EXTERNAL;
+ }
+
+ av_log(avctx, AV_LOG_DEBUG,
+ "WebP: %ux%u, %u frames, loop_count=%u\n",
+ anim_info.canvas_width, anim_info.canvas_height,
+ anim_info.frame_count, anim_info.loop_count);
+
+ s->loop_count = anim_info.loop_count;
+ s->frame_count = anim_info.frame_count;
+ s->infinite_loop = (anim_info.loop_count == 0) && !s->ignore_loop;
+
+ avctx->width = anim_info.canvas_width;
+ avctx->coded_width = anim_info.canvas_width;
+ avctx->height = anim_info.canvas_height;
+ avctx->coded_height = anim_info.canvas_height;
+
+ if (anim_info.frame_count > 0)
+ avctx->framerate = av_make_q(1000, 1);
+ }
+
+ if (!WebPAnimDecoderHasMoreFrames(s->dec)) {
+ s->loop_sent++;
+ WebPAnimDecoderReset(s->dec);
+ s->frame_sent = 0;
+ s->prev_timestamp_ms = 0;
+ s->first_frame_pts = -1;
+
+ if (!s->infinite_loop && s->loop_sent >= s->loop_count) {
+ *got_frame = 0;
+ return 0;
+ }
+ av_log(avctx, AV_LOG_DEBUG, "Loop %u/%u\n", s->loop_sent + 1,
+ s->infinite_loop ? 0 : s->loop_count);
+ }
+
+ if (!WebPAnimDecoderGetNext(s->dec, &frame_rgba, ×tamp_ms)) {
+ av_log(avctx, AV_LOG_ERROR, "Error getting next frame from
WebPAnimDecoder.\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ if (!frame_rgba || avctx->width <= 0 || avctx->height <= 0) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid frame data: frame_rgba=%p,
width=%d, height=%d\n",
+ frame_rgba, avctx->width, avctx->height);
+ return AVERROR_EXTERNAL;
+ }
+
+ ret = ff_get_buffer(avctx, p, 0);
+ if (ret < 0)
+ return ret;
+
+ p->width = avctx->width;
+ p->height = avctx->height;
+ p->format = AV_PIX_FMT_RGBA;
+ p->pts = timestamp_ms;
+ p->pkt_dts = timestamp_ms;
+ p->pict_type = AV_PICTURE_TYPE_I;
+ p->flags |= AV_FRAME_FLAG_KEY;
+
+ if (s->first_frame_pts < 0)
+ s->first_frame_pts = timestamp_ms;
+
+ if (s->frame_sent > 0)
+ p->duration = timestamp_ms - s->prev_timestamp_ms;
+
+ s->prev_timestamp_ms = timestamp_ms;
+ s->frame_sent++;
+
+ if (p->linesize[0] < avctx->width * 4) {
+ av_log(avctx, AV_LOG_ERROR, "Linesize too small: %d < %d\n",
+ p->linesize[0], avctx->width * 4);
+ return AVERROR_EXTERNAL;
+ }
+
+ av_image_copy_plane(p->data[0], p->linesize[0],
+ frame_rgba, avctx->width * 4,
+ avctx->width * 4, avctx->height);
+
+ *got_frame = 1;
+ return avpkt->size;
+}
+
+static av_cold int libwebp_decode_close(AVCodecContext *avctx)
+{
+ AnimatedWebPContext *s = avctx->priv_data;
+
+ if (s->dec) {
+ WebPAnimDecoderDelete(s->dec);
+ s->dec = NULL;
+ }
+ av_buffer_unref(&s->file_content);
+
+ return 0;
+}
+
+static void libwebp_decode_flush(AVCodecContext *avctx)
+{
+ AnimatedWebPContext *s = avctx->priv_data;
+
+ if (s->dec) {
+ WebPAnimDecoderReset(s->dec);
+ s->loop_sent = 0;
+ s->frame_sent = 0;
+ s->prev_timestamp_ms = 0;
+ s->first_frame_pts = -1;
+ }
+}
+
+static const AVOption options[] = {
+ { "ignore_loop", "ignore loop setting", offsetof(AnimatedWebPContext,
ignore_loop), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM
},
+ { NULL },
+};
+
+static const AVClass libwebp_decoder_class = {
+ .class_name = "libwebp",
+ .item_name = av_default_item_name,
+ .option = options,
+ .version = LIBAVUTIL_VERSION_INT,
+ .category = AV_CLASS_CATEGORY_DECODER,
+};
+
+const FFCodec ff_libwebp_decoder = {
+ .p.name = "libwebp",
+ CODEC_LONG_NAME("libwebp WebP image/animation decoder"),
+ .p.type = AVMEDIA_TYPE_VIDEO,
+ .p.id = AV_CODEC_ID_WEBP,
+ .p.priv_class = &libwebp_decoder_class,
+ .priv_data_size = sizeof(AnimatedWebPContext),
+ .p.wrapper_name = "libwebp",
+ .init = libwebp_decode_init,
+ FF_CODEC_DECODE_CB(libwebp_decode_frame),
+ .close = libwebp_decode_close,
+ .flush = libwebp_decode_flush,
+ .p.capabilities = AV_CODEC_CAP_DR1,
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 2a08e42..0ef6c99 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
#include "version_major.h"
-#define LIBAVCODEC_VERSION_MINOR 33
+#define LIBAVCODEC_VERSION_MINOR 34
#define LIBAVCODEC_VERSION_MICRO 100
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
--
2.43.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]