FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/webp_parser.c
Date: 2026-01-23 19:11:46
Exec Total Coverage
Lines: 41 48 85.4%
Functions: 1 1 100.0%
Branches: 21 30 70.0%

Line Branch Exec Source
1 /*
2 * WebP parser
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * @file
23 * WebP parser
24 */
25
26 #include "libavutil/bswap.h"
27 #include "libavutil/common.h"
28
29 #include "parser.h"
30 #include "parser_internal.h"
31
32 typedef struct WebPParseContext {
33 ParseContext pc;
34 uint32_t fsize;
35 uint32_t remaining_size;
36 } WebPParseContext;
37
38 45 static int webp_parse(AVCodecParserContext *s, AVCodecContext *avctx,
39 const uint8_t **poutbuf, int *poutbuf_size,
40 const uint8_t *buf, int buf_size)
41 {
42 45 WebPParseContext *ctx = s->priv_data;
43 45 uint64_t state = ctx->pc.state64;
44 45 int next = END_NOT_FOUND;
45 45 int i = 0;
46
47 45 *poutbuf = NULL;
48 45 *poutbuf_size = 0;
49
50 16 restart:
51
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 29 times.
61 if (ctx->pc.frame_start_found <= 8) {
52
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 24 times.
152 for (; i < buf_size; i++) {
53 128 state = (state << 8) | buf[i];
54
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 64 times.
128 if (ctx->pc.frame_start_found == 0) {
55
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 56 times.
64 if ((state >> 32) == MKBETAG('R', 'I', 'F', 'F')) {
56 8 ctx->fsize = av_bswap32(state);
57
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 if (ctx->fsize > 15 && ctx->fsize <= UINT32_MAX - 10) {
58 8 ctx->pc.frame_start_found = 1;
59 8 ctx->fsize += 8;
60 }
61 }
62
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 56 times.
64 } else if (ctx->pc.frame_start_found == 8) {
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if ((state >> 32) != MKBETAG('W', 'E', 'B', 'P')) {
64 ctx->pc.frame_start_found = 0;
65 continue;
66 }
67 8 ctx->pc.frame_start_found++;
68 8 ctx->remaining_size = ctx->fsize + i - 15;
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ctx->pc.index + i > 15) {
70 next = i - 15;
71 state = 0;
72 break;
73 } else {
74 8 ctx->pc.state64 = 0;
75 8 goto restart;
76 }
77
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 } else if (ctx->pc.frame_start_found)
78 56 ctx->pc.frame_start_found++;
79 }
80 24 ctx->pc.state64 = state;
81 } else {
82
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 if (ctx->remaining_size) {
83 29 i = FFMIN(ctx->remaining_size, buf_size);
84 29 ctx->remaining_size -= i;
85
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 8 times.
29 if (ctx->remaining_size)
86 21 goto flush;
87
88 8 ctx->pc.frame_start_found = 0;
89 8 goto restart;
90 }
91 }
92
93 flush:
94
2/2
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 16 times.
45 if (ff_combine_frame(&ctx->pc, next, &buf, &buf_size) < 0)
95 29 return buf_size;
96
97
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 if (next != END_NOT_FOUND && next < 0)
98 ctx->pc.frame_start_found = FFMAX(ctx->pc.frame_start_found - i - 1, 0);
99 else
100 16 ctx->pc.frame_start_found = 0;
101
102 16 *poutbuf = buf;
103 16 *poutbuf_size = buf_size;
104
105 16 return next;
106 }
107
108 const FFCodecParser ff_webp_parser = {
109 PARSER_CODEC_LIST(AV_CODEC_ID_WEBP),
110 .priv_data_size = sizeof(WebPParseContext),
111 .parse = webp_parse,
112 .close = ff_parse_close,
113 };
114