/src/ffmpeg/libavcodec/webp_parser.c
Line | Count | 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 | | 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 | 164k | { |
42 | 164k | WebPParseContext *ctx = s->priv_data; |
43 | 164k | uint64_t state = ctx->pc.state64; |
44 | 164k | int next = END_NOT_FOUND; |
45 | 164k | int i = 0; |
46 | | |
47 | 164k | *poutbuf = NULL; |
48 | 164k | *poutbuf_size = 0; |
49 | | |
50 | 412k | restart: |
51 | 412k | if (ctx->pc.frame_start_found <= 8) { |
52 | 29.3M | for (; i < buf_size; i++) { |
53 | 29.3M | state = (state << 8) | buf[i]; |
54 | 29.3M | if (ctx->pc.frame_start_found == 0) { |
55 | 27.2M | if ((state >> 32) == MKBETAG('R', 'I', 'F', 'F')) { |
56 | 276k | ctx->fsize = av_bswap32(state); |
57 | 276k | if (ctx->fsize > 15 && ctx->fsize <= UINT32_MAX - 10) { |
58 | 265k | ctx->pc.frame_start_found = 1; |
59 | 265k | ctx->fsize += 8; |
60 | 265k | } |
61 | 276k | } |
62 | 27.2M | } else if (ctx->pc.frame_start_found == 8) { |
63 | 266k | if ((state >> 32) != MKBETAG('W', 'E', 'B', 'P')) { |
64 | 18.8k | ctx->pc.frame_start_found = 0; |
65 | 18.8k | continue; |
66 | 18.8k | } |
67 | 247k | ctx->pc.frame_start_found++; |
68 | 247k | ctx->remaining_size = ctx->fsize + i - 15; |
69 | 247k | if (ctx->pc.index + i > 15) { |
70 | 123k | next = i - 15; |
71 | 123k | state = 0; |
72 | 123k | break; |
73 | 123k | } else { |
74 | 123k | ctx->pc.state64 = 0; |
75 | 123k | goto restart; |
76 | 123k | } |
77 | 1.86M | } else if (ctx->pc.frame_start_found) |
78 | 1.86M | ctx->pc.frame_start_found++; |
79 | 29.3M | } |
80 | 151k | ctx->pc.state64 = state; |
81 | 151k | } else { |
82 | 136k | if (ctx->remaining_size) { |
83 | 136k | i = FFMIN(ctx->remaining_size, buf_size); |
84 | 136k | ctx->remaining_size -= i; |
85 | 136k | if (ctx->remaining_size) |
86 | 13.0k | goto flush; |
87 | | |
88 | 123k | ctx->pc.frame_start_found = 0; |
89 | 123k | goto restart; |
90 | 136k | } |
91 | 136k | } |
92 | | |
93 | 164k | flush: |
94 | 164k | if (ff_combine_frame(&ctx->pc, next, &buf, &buf_size) < 0) |
95 | 40.8k | return buf_size; |
96 | | |
97 | 123k | if (next != END_NOT_FOUND && next < 0) |
98 | 713 | ctx->pc.frame_start_found = FFMAX(ctx->pc.frame_start_found - i - 1, 0); |
99 | 123k | else |
100 | 123k | ctx->pc.frame_start_found = 0; |
101 | | |
102 | 123k | *poutbuf = buf; |
103 | 123k | *poutbuf_size = buf_size; |
104 | | |
105 | 123k | return next; |
106 | 164k | } |
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 | | }; |