/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 | | |
31 | | typedef struct WebPParseContext { |
32 | | ParseContext pc; |
33 | | uint32_t fsize; |
34 | | uint32_t remaining_size; |
35 | | } WebPParseContext; |
36 | | |
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) |
40 | 4.31k | { |
41 | 4.31k | WebPParseContext *ctx = s->priv_data; |
42 | 4.31k | uint64_t state = ctx->pc.state64; |
43 | 4.31k | int next = END_NOT_FOUND; |
44 | 4.31k | int i = 0; |
45 | | |
46 | 4.31k | *poutbuf = NULL; |
47 | 4.31k | *poutbuf_size = 0; |
48 | | |
49 | 7.62k | restart: |
50 | 7.62k | if (ctx->pc.frame_start_found <= 8) { |
51 | 11.5M | for (; i < buf_size; i++) { |
52 | 11.5M | state = (state << 8) | buf[i]; |
53 | 11.5M | if (ctx->pc.frame_start_found == 0) { |
54 | 11.5M | if ((state >> 32) == MKBETAG('R', 'I', 'F', 'F')) { |
55 | 3.72k | ctx->fsize = av_bswap32(state); |
56 | 3.72k | if (ctx->fsize > 15 && ctx->fsize <= UINT32_MAX - 10) { |
57 | 3.51k | ctx->pc.frame_start_found = 1; |
58 | 3.51k | ctx->fsize += 8; |
59 | 3.51k | } |
60 | 3.72k | } |
61 | 11.5M | } else if (ctx->pc.frame_start_found == 8) { |
62 | 3.52k | if ((state >> 32) != MKBETAG('W', 'E', 'B', 'P')) { |
63 | 247 | ctx->pc.frame_start_found = 0; |
64 | 247 | continue; |
65 | 247 | } |
66 | 3.27k | ctx->pc.frame_start_found++; |
67 | 3.27k | ctx->remaining_size = ctx->fsize + i - 15; |
68 | 3.27k | if (ctx->pc.index + i > 15) { |
69 | 1.61k | next = i - 15; |
70 | 1.61k | state = 0; |
71 | 1.61k | break; |
72 | 1.65k | } else { |
73 | 1.65k | ctx->pc.state64 = 0; |
74 | 1.65k | goto restart; |
75 | 1.65k | } |
76 | 24.6k | } else if (ctx->pc.frame_start_found) |
77 | 24.6k | ctx->pc.frame_start_found++; |
78 | 11.5M | } |
79 | 2.16k | ctx->pc.state64 = state; |
80 | 3.79k | } else { |
81 | 3.79k | if (ctx->remaining_size) { |
82 | 3.79k | i = FFMIN(ctx->remaining_size, buf_size); |
83 | 3.79k | ctx->remaining_size -= i; |
84 | 3.79k | if (ctx->remaining_size) |
85 | 2.15k | goto flush; |
86 | | |
87 | 1.64k | ctx->pc.frame_start_found = 0; |
88 | 1.64k | goto restart; |
89 | 3.79k | } |
90 | 3.79k | } |
91 | | |
92 | 4.31k | flush: |
93 | 4.31k | if (ff_combine_frame(&ctx->pc, next, &buf, &buf_size) < 0) |
94 | 2.63k | return buf_size; |
95 | | |
96 | 1.67k | if (next != END_NOT_FOUND && next < 0) |
97 | 10 | ctx->pc.frame_start_found = FFMAX(ctx->pc.frame_start_found - i - 1, 0); |
98 | 1.66k | else |
99 | 1.66k | ctx->pc.frame_start_found = 0; |
100 | | |
101 | 1.67k | *poutbuf = buf; |
102 | 1.67k | *poutbuf_size = buf_size; |
103 | | |
104 | 1.67k | return next; |
105 | 4.31k | } |
106 | | |
107 | | const 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, |
112 | | }; |