Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * PNG parser | ||
3 | * Copyright (c) 2009 Peter Holik | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | /** | ||
23 | * @file | ||
24 | * PNG parser | ||
25 | */ | ||
26 | |||
27 | #include "parser.h" | ||
28 | #include "png.h" | ||
29 | |||
30 | typedef struct PNGParseContext { | ||
31 | ParseContext pc; | ||
32 | uint32_t chunk_pos; ///< position inside current chunk | ||
33 | uint32_t chunk_length; ///< length of the current chunk | ||
34 | uint32_t remaining_size; ///< remaining size of the current chunk | ||
35 | } PNGParseContext; | ||
36 | |||
37 | 899 | static int png_parse(AVCodecParserContext *s, AVCodecContext *avctx, | |
38 | const uint8_t **poutbuf, int *poutbuf_size, | ||
39 | const uint8_t *buf, int buf_size) | ||
40 | { | ||
41 | 899 | PNGParseContext *ppc = s->priv_data; | |
42 | 899 | int next = END_NOT_FOUND; | |
43 | 899 | int i = 0; | |
44 | |||
45 | 899 | s->pict_type = AV_PICTURE_TYPE_NONE; | |
46 | |||
47 | 899 | *poutbuf_size = 0; | |
48 | 899 | *poutbuf = NULL; | |
49 | |||
50 |
2/2✓ Branch 0 taken 311 times.
✓ Branch 1 taken 588 times.
|
899 | if (!ppc->pc.frame_start_found) { |
51 | 311 | uint64_t state64 = ppc->pc.state64; | |
52 |
2/2✓ Branch 0 taken 2112 times.
✓ Branch 1 taken 47 times.
|
2159 | for (; i < buf_size; i++) { |
53 | 2112 | state64 = (state64 << 8) | buf[i]; | |
54 |
3/4✓ Branch 0 taken 1848 times.
✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1848 times.
|
2112 | if (state64 == PNGSIG || state64 == MNGSIG) { |
55 | 264 | i++; | |
56 | 264 | ppc->pc.frame_start_found = 1; | |
57 | 264 | break; | |
58 | } | ||
59 | } | ||
60 | 311 | ppc->pc.state64 = state64; | |
61 |
1/2✓ Branch 0 taken 588 times.
✗ Branch 1 not taken.
|
588 | } else if (ppc->remaining_size) { |
62 | 588 | i = FFMIN(ppc->remaining_size, buf_size); | |
63 | 588 | ppc->remaining_size -= i; | |
64 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 408 times.
|
588 | if (ppc->remaining_size) |
65 | 180 | goto flush; | |
66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 408 times.
|
408 | if (ppc->chunk_pos == -1) { |
67 | ✗ | next = i; | |
68 | ✗ | goto flush; | |
69 | } | ||
70 | } | ||
71 | |||
72 |
3/4✓ Branch 0 taken 47 times.
✓ Branch 1 taken 82728 times.
✓ Branch 2 taken 82728 times.
✗ Branch 3 not taken.
|
82775 | for (; ppc->pc.frame_start_found && i < buf_size; i++) { |
73 | 82728 | ppc->pc.state = (ppc->pc.state << 8) | buf[i]; | |
74 |
2/2✓ Branch 0 taken 10341 times.
✓ Branch 1 taken 72387 times.
|
82728 | if (ppc->chunk_pos == 3) { |
75 | 10341 | ppc->chunk_length = ppc->pc.state; | |
76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10341 times.
|
10341 | if (ppc->chunk_length > 0x7fffffff) { |
77 | ✗ | ppc->chunk_pos = ppc->pc.frame_start_found = 0; | |
78 | ✗ | goto flush; | |
79 | } | ||
80 | 10341 | ppc->chunk_length += 4; | |
81 |
2/2✓ Branch 0 taken 10341 times.
✓ Branch 1 taken 62046 times.
|
72387 | } else if (ppc->chunk_pos == 7) { |
82 |
2/2✓ Branch 0 taken 409 times.
✓ Branch 1 taken 9932 times.
|
10341 | if (ppc->chunk_length >= buf_size - i) |
83 | 409 | ppc->remaining_size = ppc->chunk_length - buf_size + i + 1; | |
84 |
2/2✓ Branch 0 taken 263 times.
✓ Branch 1 taken 10078 times.
|
10341 | if (ppc->pc.state == MKBETAG('I', 'E', 'N', 'D')) { |
85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 263 times.
|
263 | if (ppc->remaining_size) |
86 | ✗ | ppc->chunk_pos = -1; | |
87 | else | ||
88 | 263 | next = ppc->chunk_length + i + 1; | |
89 | 263 | break; | |
90 | } else { | ||
91 | 10078 | ppc->chunk_pos = 0; | |
92 |
2/2✓ Branch 0 taken 409 times.
✓ Branch 1 taken 9669 times.
|
10078 | if (ppc->remaining_size) |
93 | 409 | break; | |
94 | else | ||
95 | 9669 | i += ppc->chunk_length; | |
96 | 9669 | continue; | |
97 | } | ||
98 | } | ||
99 | 72387 | ppc->chunk_pos++; | |
100 | } | ||
101 | |||
102 | 47 | flush: | |
103 |
2/2✓ Branch 1 taken 588 times.
✓ Branch 2 taken 311 times.
|
899 | if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0) |
104 | 588 | return buf_size; | |
105 | |||
106 | 311 | ppc->chunk_pos = ppc->pc.frame_start_found = 0; | |
107 | |||
108 | 311 | *poutbuf = buf; | |
109 | 311 | *poutbuf_size = buf_size; | |
110 | 311 | return next; | |
111 | } | ||
112 | |||
113 | const AVCodecParser ff_png_parser = { | ||
114 | .codec_ids = { AV_CODEC_ID_PNG }, | ||
115 | .priv_data_size = sizeof(PNGParseContext), | ||
116 | .parser_parse = png_parse, | ||
117 | .parser_close = ff_parse_close, | ||
118 | }; | ||
119 |