4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/bswap.h"
27 #include "libavutil/common.h"
31 typedef struct WebPParseContext {
34 uint32_t remaining_size;
37 static int webp_parse(AVCodecParserContext *s, AVCodecContext *avctx,
38 const uint8_t **poutbuf, int *poutbuf_size,
39 const uint8_t *buf, int buf_size)
41 WebPParseContext *ctx = s->priv_data;
42 uint64_t state = ctx->pc.state64;
43 int next = END_NOT_FOUND;
50 if (ctx->pc.frame_start_found <= 8) {
51 for (; i < buf_size; i++) {
52 state = (state << 8) | buf[i];
53 if (ctx->pc.frame_start_found == 0) {
54 if ((state >> 32) == MKBETAG('R', 'I', 'F', 'F')) {
55 ctx->fsize = av_bswap32(state);
56 if (ctx->fsize > 15 && ctx->fsize <= UINT32_MAX - 10) {
57 ctx->pc.frame_start_found = 1;
61 } else if (ctx->pc.frame_start_found == 8) {
62 if ((state >> 32) != MKBETAG('W', 'E', 'B', 'P')) {
63 ctx->pc.frame_start_found = 0;
66 ctx->pc.frame_start_found++;
67 ctx->remaining_size = ctx->fsize + i - 15;
68 if (ctx->pc.index + i > 15) {
76 } else if (ctx->pc.frame_start_found)
77 ctx->pc.frame_start_found++;
79 ctx->pc.state64 = state;
81 if (ctx->remaining_size) {
82 i = FFMIN(ctx->remaining_size, buf_size);
83 ctx->remaining_size -= i;
84 if (ctx->remaining_size)
87 ctx->pc.frame_start_found = 0;
93 if (ff_combine_frame(&ctx->pc, next, &buf, &buf_size) < 0)
96 if (next != END_NOT_FOUND && next < 0)
97 ctx->pc.frame_start_found = FFMAX(ctx->pc.frame_start_found - i - 1, 0);
99 ctx->pc.frame_start_found = 0;
102 *poutbuf_size = buf_size;
107 AVCodecParser ff_webp_parser = {
108 .codec_ids = { AV_CODEC_ID_WEBP },
109 .priv_data_size = sizeof(WebPParseContext),
110 .parser_parse = webp_parse,
111 .parser_close = ff_parse_close,