Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * XBM image format | ||
3 | * | ||
4 | * Copyright (c) 2012 Paul B Mahol | ||
5 | * | ||
6 | * This file is part of FFmpeg. | ||
7 | * | ||
8 | * FFmpeg is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * FFmpeg is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with FFmpeg; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | #include "libavutil/reverse.h" | ||
24 | |||
25 | #include "avcodec.h" | ||
26 | #include "codec_internal.h" | ||
27 | #include "decode.h" | ||
28 | |||
29 | 1458922 | static int get_nibble(uint8_t x) | |
30 | { | ||
31 | #define TIMES256(idx) \ | ||
32 | TIMES64(4 * (idx)) TIMES64(4 * (idx) + 1) TIMES64(4 * (idx) + 2) TIMES64(4 * (idx) + 3) | ||
33 | #define TIMES64(idx) \ | ||
34 | TIMES16(4 * (idx)) TIMES16(4 * (idx) + 1) TIMES16(4 * (idx) + 2) TIMES16(4 * (idx) + 3) | ||
35 | #define TIMES16(idx) \ | ||
36 | TIMES4(4 * (idx)) TIMES4(4 * (idx) + 1) TIMES4(4 * (idx) + 2) TIMES4(4 * (idx) + 3) | ||
37 | #define TIMES4(idx) \ | ||
38 | ENTRY(4 * (idx)) ENTRY(4 * (idx) + 1) ENTRY(4 * (idx) + 2) ENTRY(4 * (idx) + 3) | ||
39 | #define ENTRY(x) [x] = ((x) >= 'a' && (x) <= 'f') ? (x) - ('a' - 10) : \ | ||
40 | ((x) >= 'A' && (x) <= 'F') ? (x) - ('A' - 10) : \ | ||
41 | ((x) >= '0' && (x) <= '9') ? (x) - '0' : 255, | ||
42 | |||
43 | static const uint8_t lut[] = { | ||
44 | TIMES256(0) | ||
45 | }; | ||
46 | 1458922 | return lut[x]; | |
47 | } | ||
48 | |||
49 | 90 | static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key) | |
50 | { | ||
51 | 90 | int keylen = strlen(key); | |
52 | 90 | const uint8_t *e = end - keylen; | |
53 | |||
54 |
2/2✓ Branch 0 taken 2382 times.
✓ Branch 1 taken 2 times.
|
2384 | for(; p < e; p++) { |
55 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 2294 times.
|
2382 | if (!memcmp(p, key, keylen)) |
56 | 88 | break; | |
57 | } | ||
58 | 90 | p += keylen; | |
59 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 88 times.
|
90 | if (p >= end) |
60 | 2 | return INT_MIN; | |
61 | |||
62 |
1/2✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
|
88 | for(; p<end; p++) { |
63 | char *eptr; | ||
64 | 88 | int64_t ret = strtol(p, &eptr, 10); | |
65 |
1/2✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
|
88 | if ((const uint8_t *)eptr != p) |
66 | 88 | return ret; | |
67 | } | ||
68 | ✗ | return INT_MIN; | |
69 | } | ||
70 | |||
71 | 45 | static int xbm_decode_frame(AVCodecContext *avctx, AVFrame *p, | |
72 | int *got_frame, AVPacket *avpkt) | ||
73 | { | ||
74 | int ret, linesize, i, j; | ||
75 | 45 | int width = 0; | |
76 | 45 | int height = 0; | |
77 | 45 | const uint8_t *end, *ptr = avpkt->data; | |
78 | const uint8_t *next; | ||
79 | uint8_t *dst; | ||
80 | |||
81 | 45 | avctx->pix_fmt = AV_PIX_FMT_MONOWHITE; | |
82 | 45 | end = avpkt->data + avpkt->size; | |
83 | |||
84 | 45 | width = parse_str_int(avpkt->data, end, "_width"); | |
85 | 45 | height = parse_str_int(avpkt->data, end, "_height"); | |
86 | |||
87 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 44 times.
|
45 | if ((ret = ff_set_dimensions(avctx, width, height)) < 0) |
88 | 1 | return ret; | |
89 | |||
90 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 40 times.
|
44 | if (avctx->skip_frame >= AVDISCARD_ALL) |
91 | 4 | return avpkt->size; | |
92 | |||
93 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
|
40 | if ((ret = ff_get_buffer(avctx, p, 0)) < 0) |
94 | ✗ | return ret; | |
95 | |||
96 | // goto start of image data | ||
97 | 40 | next = memchr(ptr, '{', avpkt->size); | |
98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (!next) |
99 | ✗ | next = memchr(ptr, '(', avpkt->size); | |
100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (!next) |
101 | ✗ | return AVERROR_INVALIDDATA; | |
102 | 40 | ptr = next + 1; | |
103 | |||
104 | 40 | linesize = (avctx->width + 7) / 8; | |
105 |
2/2✓ Branch 0 taken 11150 times.
✓ Branch 1 taken 40 times.
|
11190 | for (i = 0; i < avctx->height; i++) { |
106 | 11150 | dst = p->data[0] + i * p->linesize[0]; | |
107 |
2/2✓ Branch 0 taken 486302 times.
✓ Branch 1 taken 11150 times.
|
497452 | for (j = 0; j < linesize; j++) { |
108 | uint8_t nib, val; | ||
109 | |||
110 |
4/6✓ Branch 0 taken 1956306 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1470004 times.
✓ Branch 3 taken 486302 times.
✓ Branch 4 taken 1470004 times.
✗ Branch 5 not taken.
|
1956306 | while (ptr < end && *ptr != 'x' && *ptr != '$') |
111 | 1470004 | ptr++; | |
112 | |||
113 | 486302 | ptr ++; | |
114 |
2/4✓ Branch 0 taken 486302 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 486302 times.
✗ Branch 4 not taken.
|
486302 | if (ptr < end && (val = get_nibble(*ptr)) <= 15) { |
115 | 486302 | ptr++; | |
116 |
1/2✓ Branch 1 taken 486302 times.
✗ Branch 2 not taken.
|
486302 | if ((nib = get_nibble(*ptr)) <= 15) { |
117 | 486302 | val = (val << 4) + nib; | |
118 | 486302 | ptr++; | |
119 | } | ||
120 | 486302 | *dst++ = ff_reverse[val]; | |
121 |
3/4✓ Branch 1 taken 16 times.
✓ Branch 2 taken 486286 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
|
486318 | if ((val = get_nibble(*ptr)) <= 15 && j+1 < linesize) { |
122 | 16 | j++; | |
123 | 16 | ptr++; | |
124 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | if ((nib = get_nibble(*ptr)) <= 15) { |
125 | 16 | val = (val << 4) + nib; | |
126 | 16 | ptr++; | |
127 | } | ||
128 | 16 | *dst++ = ff_reverse[val]; | |
129 | } | ||
130 | } else { | ||
131 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
132 | "Unexpected data at %.8s.\n", ptr); | ||
133 | ✗ | return AVERROR_INVALIDDATA; | |
134 | } | ||
135 | } | ||
136 | } | ||
137 | |||
138 | 40 | *got_frame = 1; | |
139 | |||
140 | 40 | return avpkt->size; | |
141 | } | ||
142 | |||
143 | const FFCodec ff_xbm_decoder = { | ||
144 | .p.name = "xbm", | ||
145 | CODEC_LONG_NAME("XBM (X BitMap) image"), | ||
146 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
147 | .p.id = AV_CODEC_ID_XBM, | ||
148 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
149 | .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
150 | FF_CODEC_DECODE_CB(xbm_decode_frame), | ||
151 | }; | ||
152 |