| 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 "parser_internal.h" | ||
| 29 | #include "png.h" | ||
| 30 | |||
| 31 | typedef struct PNGParseContext { | ||
| 32 | ParseContext pc; | ||
| 33 | uint32_t chunk_pos; ///< position inside current chunk | ||
| 34 | uint32_t chunk_length; ///< length of the current chunk | ||
| 35 | uint32_t remaining_size; ///< remaining size of the current chunk | ||
| 36 | } PNGParseContext; | ||
| 37 | |||
| 38 | 945 | static int png_parse(AVCodecParserContext *s, AVCodecContext *avctx, | |
| 39 | const uint8_t **poutbuf, int *poutbuf_size, | ||
| 40 | const uint8_t *buf, int buf_size) | ||
| 41 | { | ||
| 42 | 945 | PNGParseContext *ppc = s->priv_data; | |
| 43 | 945 | int next = END_NOT_FOUND; | |
| 44 | 945 | int i = 0; | |
| 45 | |||
| 46 | 945 | s->pict_type = AV_PICTURE_TYPE_NONE; | |
| 47 | |||
| 48 | 945 | *poutbuf_size = 0; | |
| 49 | 945 | *poutbuf = NULL; | |
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 315 times.
✓ Branch 1 taken 630 times.
|
945 | if (!ppc->pc.frame_start_found) { |
| 52 | 315 | uint64_t state64 = ppc->pc.state64; | |
| 53 |
2/2✓ Branch 0 taken 2128 times.
✓ Branch 1 taken 49 times.
|
2177 | for (; i < buf_size; i++) { |
| 54 | 2128 | state64 = (state64 << 8) | buf[i]; | |
| 55 |
3/4✓ Branch 0 taken 1862 times.
✓ Branch 1 taken 266 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1862 times.
|
2128 | if (state64 == PNGSIG || state64 == MNGSIG) { |
| 56 | 266 | i++; | |
| 57 | 266 | ppc->pc.frame_start_found = 1; | |
| 58 | 266 | break; | |
| 59 | } | ||
| 60 | } | ||
| 61 | 315 | ppc->pc.state64 = state64; | |
| 62 |
1/2✓ Branch 0 taken 630 times.
✗ Branch 1 not taken.
|
630 | } else if (ppc->remaining_size) { |
| 63 | 630 | i = FFMIN(ppc->remaining_size, buf_size); | |
| 64 | 630 | ppc->remaining_size -= i; | |
| 65 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 450 times.
|
630 | if (ppc->remaining_size) |
| 66 | 180 | goto flush; | |
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
|
450 | if (ppc->chunk_pos == -1) { |
| 68 | ✗ | next = i; | |
| 69 | ✗ | goto flush; | |
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 |
3/4✓ Branch 0 taken 49 times.
✓ Branch 1 taken 64328 times.
✓ Branch 2 taken 64328 times.
✗ Branch 3 not taken.
|
64377 | for (; ppc->pc.frame_start_found && i < buf_size; i++) { |
| 74 | 64328 | ppc->pc.state = (ppc->pc.state << 8) | buf[i]; | |
| 75 |
2/2✓ Branch 0 taken 8041 times.
✓ Branch 1 taken 56287 times.
|
64328 | if (ppc->chunk_pos == 3) { |
| 76 | 8041 | ppc->chunk_length = ppc->pc.state; | |
| 77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8041 times.
|
8041 | if (ppc->chunk_length > 0x7fffffff) { |
| 78 | ✗ | ppc->chunk_pos = ppc->pc.frame_start_found = 0; | |
| 79 | ✗ | goto flush; | |
| 80 | } | ||
| 81 | 8041 | ppc->chunk_length += 4; | |
| 82 |
2/2✓ Branch 0 taken 8041 times.
✓ Branch 1 taken 48246 times.
|
56287 | } else if (ppc->chunk_pos == 7) { |
| 83 |
2/2✓ Branch 0 taken 451 times.
✓ Branch 1 taken 7590 times.
|
8041 | if (ppc->chunk_length >= buf_size - i) |
| 84 | 451 | ppc->remaining_size = ppc->chunk_length - buf_size + i + 1; | |
| 85 |
2/2✓ Branch 0 taken 265 times.
✓ Branch 1 taken 7776 times.
|
8041 | if (ppc->pc.state == MKBETAG('I', 'E', 'N', 'D')) { |
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 265 times.
|
265 | if (ppc->remaining_size) |
| 87 | ✗ | ppc->chunk_pos = -1; | |
| 88 | else | ||
| 89 | 265 | next = ppc->chunk_length + i + 1; | |
| 90 | 265 | break; | |
| 91 | } else { | ||
| 92 | 7776 | ppc->chunk_pos = 0; | |
| 93 |
2/2✓ Branch 0 taken 451 times.
✓ Branch 1 taken 7325 times.
|
7776 | if (ppc->remaining_size) |
| 94 | 451 | break; | |
| 95 | else | ||
| 96 | 7325 | i += ppc->chunk_length; | |
| 97 | 7325 | continue; | |
| 98 | } | ||
| 99 | } | ||
| 100 | 56287 | ppc->chunk_pos++; | |
| 101 | } | ||
| 102 | |||
| 103 | 49 | flush: | |
| 104 |
2/2✓ Branch 1 taken 630 times.
✓ Branch 2 taken 315 times.
|
945 | if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0) |
| 105 | 630 | return buf_size; | |
| 106 | |||
| 107 | 315 | ppc->chunk_pos = ppc->pc.frame_start_found = 0; | |
| 108 | |||
| 109 | 315 | *poutbuf = buf; | |
| 110 | 315 | *poutbuf_size = buf_size; | |
| 111 | 315 | return next; | |
| 112 | } | ||
| 113 | |||
| 114 | const FFCodecParser ff_png_parser = { | ||
| 115 | PARSER_CODEC_LIST(AV_CODEC_ID_PNG), | ||
| 116 | .priv_data_size = sizeof(PNGParseContext), | ||
| 117 | .parse = png_parse, | ||
| 118 | .close = ff_parse_close, | ||
| 119 | }; | ||
| 120 |