1- From 21d88e28b8834fae08fcd3a337faf1ac209a4a4f Mon Sep 17 00:00:00 2001
1+ From c00008ca039260f7dca9d8c06f599197a96a75cf Mon Sep 17 00:00:00 2001
22From: qianlongxu <
[email protected] >
3- Date: Mon, 8 Sep 2025 13:59:17 +0800
3+ Date: Mon, 8 Sep 2025 15:24:55 +0800
44Subject: [PATCH] add webp demuxer and libwebp decoder
55
66---
@@ -9,11 +9,11 @@ Subject: [PATCH] add webp demuxer and libwebp decoder
99 libavcodec/allcodecs.c | 2 +-
1010 libavcodec/codec_desc.c | 11 +-
1111 libavcodec/codec_id.h | 2 +-
12- libavcodec/libwebpdec.c | 350 +++++++++++++++++++++++++++++++++++++++
12+ libavcodec/libwebpdec.c | 376 +++++++++++++++++++++++++++++++++++++++
1313 libavformat/Makefile | 1 +
1414 libavformat/allformats.c | 3 +
15- libavformat/webpdec.c | 290 ++ ++++++++++++++++++++++++++++++
16- 9 files changed, 663 insertions(+), 4 deletions(-)
15+ libavformat/webpdec.c | 296 ++++++++++++++++++++++++++++++
16+ 9 files changed, 695 insertions(+), 4 deletions(-)
1717 create mode 100644 libavcodec/libwebpdec.c
1818 create mode 100644 libavformat/webpdec.c
1919
@@ -111,15 +111,30 @@ index 8c724a0..041015e 100644
111111 AV_CODEC_ID_MPEG2TS = 0x20000, /**< _FAKE_ codec to indicate a raw MPEG-2 TS
112112diff --git a/libavcodec/libwebpdec.c b/libavcodec/libwebpdec.c
113113new file mode 100644
114- index 0000000..c2884c0
114+ index 0000000..17b69d0
115115--- /dev/null
116116+++ b/libavcodec/libwebpdec.c
117- @@ -0,0 +1,350 @@
117+ @@ -0,0 +1,376 @@
118118+ /*
119- + * WebP decoder using libwebp for FFmpeg 7.x
120- + * Copyright (c) 2024
119+ + * libwebpdec.c
121120+ *
122- + * This file is part of FFmpeg.
121+ + * Copyright (c) 2025 debugly <[email protected] >122+ + *
123+ + * This file is part of FSPlayer.
124+ + *
125+ + * FSPlayer is free software; you can redistribute it and/or
126+ + * modify it under the terms of the GNU Lesser General Public
127+ + * License as published by the Free Software Foundation; either
128+ + * version 3 of the License, or (at your option) any later version.
129+ + *
130+ + * FSPlayer is distributed in the hope that it will be useful,
131+ + * but WITHOUT ANY WARRANTY; without even the implied warranty of
132+ + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
133+ + * Lesser General Public License for more details.
134+ + *
135+ + * You should have received a copy of the GNU Lesser General Public
136+ + * License along with FSPlayer; if not, write to the Free Software
137+ + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
123138+ */
124139+
125140+ #include "codec_internal.h"
@@ -431,6 +446,16 @@ index 0000000..c2884c0
431446+ return ret;
432447+ }
433448+
449+ + static int libwebp_decode_flush(AVCodecContext *avctx)
450+ + {
451+ + LibWebPDecoderContext *s = avctx->priv_data;
452+ + if (s->canvas && s->canvas->data) {
453+ + memset(s->canvas->data, 0, s->canvas->width * s->canvas->height * 4);
454+ + }
455+ + return 0;
456+ + }
457+ +
458+ + //when seek or seek to zero for loop,clear then canvas
434459+ static av_cold int libwebp_decode_close(AVCodecContext *avctx)
435460+ {
436461+ LibWebPDecoderContext *s = avctx->priv_data;
@@ -459,6 +484,7 @@ index 0000000..c2884c0
459484+ .priv_data_size = sizeof(LibWebPDecoderContext),
460485+ .init = libwebp_decode_init,
461486+ .cb.decode = libwebp_decode_frame,
487+ + .flush = libwebp_decode_flush,
462488+ .close = libwebp_decode_close,
463489+ .p.capabilities = AV_CODEC_CAP_DR1,
464490+ .p.priv_class = &libwebp_decoder_class,
@@ -500,41 +526,45 @@ index dbae42e..d736732 100644
500526
501527diff --git a/libavformat/webpdec.c b/libavformat/webpdec.c
502528new file mode 100644
503- index 0000000..4fbe410
529+ index 0000000..df4a3af
504530--- /dev/null
505531+++ b/libavformat/webpdec.c
506- @@ -0,0 +1,290 @@
532+ @@ -0,0 +1,296 @@
507533+ /*
508- + * WebP demuxer for FFmpeg 7.x and libwebp 1.5.0+
509- + * Copyright (c) 2024
534+ + * webpdec.c
535+ + *
536+ + * Copyright (c) 2025 debugly <[email protected] >510537+ *
511- + * This file is part of FFmpeg .
538+ + * This file is part of FSPlayer .
512539+ *
513- + * FFmpeg is free software; you can redistribute it and/or
540+ + * FSPlayer is free software; you can redistribute it and/or
514541+ * modify it under the terms of the GNU Lesser General Public
515542+ * License as published by the Free Software Foundation; either
516- + * version 2.1 of the License, or (at your option) any later version.
543+ + * version 3 of the License, or (at your option) any later version.
544+ + *
545+ + * FSPlayer is distributed in the hope that it will be useful,
546+ + * but WITHOUT ANY WARRANTY; without even the implied warranty of
547+ + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
548+ + * Lesser General Public License for more details.
549+ + *
550+ + * You should have received a copy of the GNU Lesser General Public
551+ + * License along with FSPlayer; if not, write to the Free Software
552+ + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
517553+ */
518554+
519555+ #include "internal.h"
520556+ #include "demux.h"
521557+ #include "libavutil/intreadwrite.h"
522- + #include "libavutil/mem.h" // 内存管理函数
523- + #include "libavutil/common.h" // 其他工具函数
558+ + #include "libavutil/mem.h"
559+ + #include "libavutil/common.h"
524560+ #include <webp/demux.h>
525- + #include <webp/mux_types.h>
526561+
527562+ typedef struct WebPDemuxContext {
528563+ const AVClass *class;
529564+ WebPDemuxer *demuxer;
530565+ WebPIterator iter; // 帧迭代器
531566+ int current_frame; // 当前帧索引
532567+ int frame_count; // 总帧数
533- + int loop_count; //
534- + int has_animation;
535- + int has_alpha;
536- + uint32_t canvas_width; // 画布宽度
537- + uint32_t canvas_height; // 画布高度
538568+ int64_t duration; // 总时长(ms)
539569+ uint8_t *data; // 文件数据缓冲区
540570+ size_t data_size; // 数据大小
@@ -602,15 +632,16 @@ index 0000000..4fbe410
602632+ goto fail;
603633+ }
604634+
605- + // 获取画布尺寸
606- + wp->canvas_width = WebPDemuxGetI(wp->demuxer, WEBP_FF_CANVAS_WIDTH);
607- + wp->canvas_height = WebPDemuxGetI(wp->demuxer, WEBP_FF_CANVAS_HEIGHT);
608635+ wp->frame_count = WebPDemuxGetI(wp->demuxer, WEBP_FF_FRAME_COUNT);
609- + wp->loop_count = WebPDemuxGetI(wp->demuxer, WEBP_FF_LOOP_COUNT);
610- + uint32_t flags = WebPDemuxGetI(wp->demuxer, WEBP_FF_FORMAT_FLAGS);
636+ + // 获取画布尺寸
637+ + int canvas_width = WebPDemuxGetI(wp->demuxer, WEBP_FF_CANVAS_WIDTH);
638+ + int canvas_height = WebPDemuxGetI(wp->demuxer, WEBP_FF_CANVAS_HEIGHT);
639+ +
640+ + // int loop_count = WebPDemuxGetI(wp->demuxer, WEBP_FF_LOOP_COUNT);
641+ + // uint32_t flags = WebPDemuxGetI(wp->demuxer, WEBP_FF_FORMAT_FLAGS);
611642+
612- + wp-> has_animation = flags & ANIMATION_FLAG;
613- + wp-> has_alpha = flags & ALPHA_FLAG;
643+ + // int has_animation = flags & ANIMATION_FLAG;
644+ + // int has_alpha = flags & ALPHA_FLAG;
614645+
615646+ // 分配帧时间戳和时长数组
616647+ wp->frame_timestamps = av_malloc_array(wp->frame_count, sizeof(int64_t));
@@ -660,9 +691,10 @@ index 0000000..4fbe410
660691+
661692+ // 设置流参数
662693+ st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
694+ + // 使用自定义解码器
663695+ st->codecpar->codec_id = AV_CODEC_ID_LIBWEBP;
664- + st->codecpar->width = wp-> canvas_width;
665- + st->codecpar->height = wp-> canvas_height;
696+ + st->codecpar->width = canvas_width;
697+ + st->codecpar->height = canvas_height;
666698+ st->duration = wp->duration;
667699+ avpriv_set_pts_info(st, 64, 1, 1000); // 时基:1ms
668700+
@@ -786,7 +818,7 @@ index 0000000..4fbe410
786818+ .p.long_name = NULL_IF_CONFIG_SMALL("WebP image format (libwebp 1.5.0+)"),
787819+ .p.flags = AVFMT_GENERIC_INDEX,
788820+ .p.extensions = "webp",
789- + .read_probe = webp_read_probe, // 新增的探测函数
821+ + .read_probe = webp_read_probe,
790822+ .read_header = webp_read_header,
791823+ .read_packet = webp_read_packet,
792824+ .read_seek = webp_read_seek,
0 commit comments