FFmpeg
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 
31 typedef struct WebPParseContext {
33  uint32_t fsize;
34  uint32_t remaining_size;
36 
38  const uint8_t **poutbuf, int *poutbuf_size,
39  const uint8_t *buf, int buf_size)
40 {
42  uint64_t state = ctx->pc.state64;
43  int next = END_NOT_FOUND;
44  int i = 0;
45 
46  *poutbuf = NULL;
47  *poutbuf_size = 0;
48 
49 restart:
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;
58  ctx->fsize += 8;
59  }
60  }
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;
64  continue;
65  }
66  ctx->pc.frame_start_found++;
67  ctx->remaining_size = ctx->fsize + i - 15;
68  if (ctx->pc.index + i > 15) {
69  next = i - 15;
70  state = 0;
71  break;
72  } else {
73  ctx->pc.state64 = 0;
74  goto restart;
75  }
76  } else if (ctx->pc.frame_start_found)
77  ctx->pc.frame_start_found++;
78  }
79  ctx->pc.state64 = state;
80  } else {
81  if (ctx->remaining_size) {
82  i = FFMIN(ctx->remaining_size, buf_size);
83  ctx->remaining_size -= i;
84  if (ctx->remaining_size)
85  goto flush;
86 
87  ctx->pc.frame_start_found = 0;
88  goto restart;
89  }
90  }
91 
92 flush:
93  if (ff_combine_frame(&ctx->pc, next, &buf, &buf_size) < 0)
94  return buf_size;
95 
96  if (next != END_NOT_FOUND && next < 0)
97  ctx->pc.frame_start_found = FFMAX(ctx->pc.frame_start_found - i - 1, 0);
98  else
99  ctx->pc.frame_start_found = 0;
100 
101  *poutbuf = buf;
102  *poutbuf_size = buf_size;
103 
104  return next;
105 }
106 
109  .priv_data_size = sizeof(WebPParseContext),
110  .parser_parse = webp_parse,
111  .parser_close = ff_parse_close,
112 };
#define NULL
Definition: coverity.c:32
uint32_t remaining_size
Definition: webp_parser.c:34
static void flush(AVCodecContext *avctx)
int codec_ids[5]
Definition: avcodec.h:3533
int frame_start_found
Definition: parser.h:34
uint8_t
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:234
#define FFMAX(a, b)
Definition: common.h:94
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:319
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:37
#define FFMIN(a, b)
Definition: common.h:96
uint32_t fsize
Definition: webp_parser.c:33
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
#define av_bswap32
Definition: bswap.h:33
AVCodecParser ff_webp_parser
Definition: webp_parser.c:107
main external API structure.
Definition: avcodec.h:531
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
byte swapping routines
#define END_NOT_FOUND
Definition: parser.h:40
common internal and external API header
static struct @323 state
int index
Definition: parser.h:30
#define MKBETAG(a, b, c, d)
Definition: common.h:406
ParseContext pc
Definition: webp_parser.c:32
int i
Definition: input.c:407