FFmpeg
Loading...
Searching...
No Matches
webp_parser.c
Go to the documentation of this file.
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
37
39 const uint8_t **poutbuf, int *poutbuf_size,
40 const uint8_t *buf, int buf_size)
41{
42 WebPParseContext *ctx = s->priv_data;
43 uint64_t state = ctx->pc.state64;
44 int next = END_NOT_FOUND;
45 int i = 0;
46
47 *poutbuf = NULL;
48 *poutbuf_size = 0;
49
50restart:
51 if (ctx->pc.frame_start_found <= 8) {
52 for (; i < buf_size; i++) {
53 state = (state << 8) | buf[i];
54 if (ctx->pc.frame_start_found == 0) {
55 if ((state >> 32) == MKBETAG('R', 'I', 'F', 'F')) {
56 ctx->fsize = av_bswap32(state);
57 if (ctx->fsize > 15 && ctx->fsize <= UINT32_MAX - 10) {
58 ctx->pc.frame_start_found = 1;
59 ctx->fsize += 8;
60 }
61 }
62 } else if (ctx->pc.frame_start_found == 8) {
63 if ((state >> 32) != MKBETAG('W', 'E', 'B', 'P')) {
64 ctx->pc.frame_start_found = 0;
65 continue;
66 }
67 ctx->pc.frame_start_found++;
68 ctx->remaining_size = ctx->fsize + i - 15;
69 if (ctx->pc.index + i > 15) {
70 next = i - 15;
71 state = 0;
72 break;
73 } else {
74 ctx->pc.state64 = 0;
75 goto restart;
76 }
77 } else if (ctx->pc.frame_start_found)
78 ctx->pc.frame_start_found++;
79 }
80 ctx->pc.state64 = state;
81 } else {
82 if (ctx->remaining_size) {
83 i = FFMIN(ctx->remaining_size, buf_size);
84 ctx->remaining_size -= i;
85 if (ctx->remaining_size)
86 goto flush;
87
88 ctx->pc.frame_start_found = 0;
89 goto restart;
90 }
91 }
92
93flush:
94 if (ff_combine_frame(&ctx->pc, next, &buf, &buf_size) < 0)
95 return buf_size;
96
97 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 ctx->pc.frame_start_found = 0;
101
102 *poutbuf = buf;
103 *poutbuf_size = buf_size;
104
105 return next;
106}
107
110 .priv_data_size = sizeof(WebPParseContext),
111 .parse = webp_parse,
113};
static AVFormatContext * ctx
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
byte swapping routines
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
common internal and external API header
#define NULL
Definition coverity.c:32
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:610
static struct @346255127015250356166251341105367306144006377143 state
@ AV_CODEC_ID_WEBP
Definition codec_id.h:221
#define FFMIN(a, b)
Definition macros.h:49
#define MKBETAG(a, b, c, d)
Definition macros.h:56
#define FFMAX(a, b)
Definition macros.h:47
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition parser.c:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
const FFCodecParser ff_webp_parser
#define av_bswap32
Definition bswap.h:47
main external API structure.
Definition avcodec.h:443
uint32_t remaining_size
Definition webp_parser.c:35
ParseContext pc
Definition webp_parser.c:33
static int webp_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition webp_parser.c:38