Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder | ||
3 | * Copyright (c) 2007, 2008 Ivo van Poorten | ||
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 | #include "libavutil/avassert.h" | ||
23 | #include "libavutil/common.h" | ||
24 | #include "libavutil/intreadwrite.h" | ||
25 | #include "libavutil/mem.h" | ||
26 | #include "avcodec.h" | ||
27 | #include "codec_internal.h" | ||
28 | #include "decode.h" | ||
29 | #include "sunrast.h" | ||
30 | |||
31 | 28 | static int sunrast_decode_frame(AVCodecContext *avctx, AVFrame *p, | |
32 | int *got_frame, AVPacket *avpkt) | ||
33 | { | ||
34 | 28 | const uint8_t *buf = avpkt->data; | |
35 | 28 | const uint8_t *buf_end = avpkt->data + avpkt->size; | |
36 | unsigned int w, h, depth, type, maptype, maplength, x, y, len, alen; | ||
37 | ptrdiff_t stride; | ||
38 | 28 | uint8_t *ptr, *ptr2 = NULL; | |
39 | 28 | const uint8_t *bufstart = buf; | |
40 | int ret; | ||
41 | |||
42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (avpkt->size < 32) |
43 | ✗ | return AVERROR_INVALIDDATA; | |
44 | |||
45 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (AV_RB32(buf) != RAS_MAGIC) { |
46 | ✗ | av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n"); | |
47 | ✗ | return AVERROR_INVALIDDATA; | |
48 | } | ||
49 | |||
50 | 28 | w = AV_RB32(buf + 4); | |
51 | 28 | h = AV_RB32(buf + 8); | |
52 | 28 | depth = AV_RB32(buf + 12); | |
53 | 28 | type = AV_RB32(buf + 20); | |
54 | 28 | maptype = AV_RB32(buf + 24); | |
55 | 28 | maplength = AV_RB32(buf + 28); | |
56 | 28 | buf += 32; | |
57 | |||
58 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (type == RT_EXPERIMENTAL) { |
59 | ✗ | avpriv_request_sample(avctx, "TIFF/IFF/EXPERIMENTAL (compression) type"); | |
60 | ✗ | return AVERROR_PATCHWELCOME; | |
61 | } | ||
62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (type > RT_FORMAT_IFF) { |
63 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n"); | |
64 | ✗ | return AVERROR_INVALIDDATA; | |
65 | } | ||
66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (maptype == RMT_RAW) { |
67 | ✗ | avpriv_request_sample(avctx, "Unknown colormap type"); | |
68 | ✗ | return AVERROR_PATCHWELCOME; | |
69 | } | ||
70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (maptype > RMT_RAW) { |
71 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n"); | |
72 | ✗ | return AVERROR_INVALIDDATA; | |
73 | } | ||
74 | |||
75 |
2/4✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
|
28 | if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) { |
76 | ✗ | av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n"); | |
77 | ✗ | return AVERROR_PATCHWELCOME; | |
78 | } | ||
79 | |||
80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (maplength > 768) { |
81 | ✗ | av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n"); | |
82 | ✗ | return AVERROR_INVALIDDATA; | |
83 | } | ||
84 | |||
85 | // This also checks depth to be valid | ||
86 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
28 | switch (depth) { |
87 | 4 | case 1: | |
88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_MONOWHITE; |
89 | 4 | break; | |
90 | ✗ | case 4: | |
91 | ✗ | avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_NONE; | |
92 | ✗ | break; | |
93 | 6 | case 8: | |
94 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8; |
95 | 6 | break; | |
96 | 18 | case 24: | |
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24; |
98 | 18 | break; | |
99 | ✗ | case 32: | |
100 | ✗ | avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_0RGB : AV_PIX_FMT_0BGR; | |
101 | ✗ | break; | |
102 | ✗ | default: | |
103 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid depth\n"); | |
104 | ✗ | return AVERROR_INVALIDDATA; | |
105 | } | ||
106 | |||
107 | // This checks w and h to be valid in the sense that bytes of a padded bitmap are addressable with 32bit int | ||
108 | 28 | ret = ff_set_dimensions(avctx, w, h); | |
109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (ret < 0) |
110 | ✗ | return ret; | |
111 | |||
112 | // ensured by ff_set_dimensions() | ||
113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | av_assert0(w <= (INT32_MAX - 7) / depth); |
114 | |||
115 | /* scanlines are aligned on 16 bit boundaries */ | ||
116 | 28 | len = (depth * w + 7) >> 3; | |
117 | 28 | alen = len + (len & 1); | |
118 | |||
119 | // ensured by ff_set_dimensions() | ||
120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | av_assert0(h <= INT32_MAX / (3 * len)); |
121 | |||
122 | // maplength is limited to 768 and the right term is limited to INT32_MAX / 256 so the add needs no check | ||
123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (buf_end - buf < (uint64_t)maplength + (len * h) * 3 / 256) |
124 | ✗ | return AVERROR_INVALIDDATA; | |
125 | |||
126 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if ((ret = ff_get_buffer(avctx, p, 0)) < 0) |
127 | ✗ | return ret; | |
128 | |||
129 | 28 | p->pict_type = AV_PICTURE_TYPE_I; | |
130 | |||
131 |
3/4✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
|
28 | if (depth > 8 && maplength) { |
132 | ✗ | av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n"); | |
133 | |||
134 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
|
28 | } else if (maplength) { |
135 | 4 | unsigned int len = maplength / 3; | |
136 | |||
137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (maplength % 3) { |
138 | ✗ | av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n"); | |
139 | ✗ | return AVERROR_INVALIDDATA; | |
140 | } | ||
141 | |||
142 | 4 | ptr = p->data[1]; | |
143 |
2/2✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 4 times.
|
1028 | for (x = 0; x < len; x++, ptr += 4) |
144 | 1024 | *(uint32_t *)ptr = (0xFFU<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x]; | |
145 | } | ||
146 | |||
147 | 28 | buf += maplength; | |
148 | |||
149 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
28 | if (maplength && depth < 8) { |
150 | ✗ | ptr = ptr2 = av_malloc_array((w + 15), h); | |
151 | ✗ | if (!ptr) | |
152 | ✗ | return AVERROR(ENOMEM); | |
153 | ✗ | stride = (w + 15 >> 3) * depth; | |
154 | } else { | ||
155 | 28 | ptr = p->data[0]; | |
156 | 28 | stride = p->linesize[0]; | |
157 | } | ||
158 | |||
159 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8 times.
|
28 | if (type == RT_BYTE_ENCODED) { |
160 | int value, run; | ||
161 | 20 | uint8_t *end = ptr + (ptrdiff_t)h * stride; | |
162 | |||
163 | 20 | x = 0; | |
164 |
3/4✓ Branch 0 taken 6339750 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 6339750 times.
✗ Branch 3 not taken.
|
6339770 | while (ptr != end && buf < buf_end) { |
165 | 6339750 | run = 1; | |
166 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6339750 times.
|
6339750 | if (buf_end - buf < 1) |
167 | ✗ | return AVERROR_INVALIDDATA; | |
168 | |||
169 |
2/2✓ Branch 0 taken 37867 times.
✓ Branch 1 taken 6301883 times.
|
6339750 | if ((value = *buf++) == RLE_TRIGGER) { |
170 | 37867 | run = *buf++ + 1; | |
171 |
2/2✓ Branch 0 taken 27727 times.
✓ Branch 1 taken 10140 times.
|
37867 | if (run != 1) |
172 | 27727 | value = *buf++; | |
173 | } | ||
174 |
2/2✓ Branch 0 taken 6420480 times.
✓ Branch 1 taken 6339730 times.
|
12760210 | while (run--) { |
175 |
1/2✓ Branch 0 taken 6420480 times.
✗ Branch 1 not taken.
|
6420480 | if (x < len) |
176 | 6420480 | ptr[x] = value; | |
177 |
2/2✓ Branch 0 taken 7104 times.
✓ Branch 1 taken 6413376 times.
|
6420480 | if (++x >= alen) { |
178 | 7104 | x = 0; | |
179 | 7104 | ptr += stride; | |
180 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 7084 times.
|
7104 | if (ptr == end) |
181 | 20 | break; | |
182 | } | ||
183 | } | ||
184 | } | ||
185 | } else { | ||
186 |
2/2✓ Branch 0 taken 4608 times.
✓ Branch 1 taken 8 times.
|
4616 | for (y = 0; y < h; y++) { |
187 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4608 times.
|
4608 | if (buf_end - buf < alen) |
188 | ✗ | break; | |
189 | 4608 | memcpy(ptr, buf, len); | |
190 | 4608 | ptr += stride; | |
191 | 4608 | buf += alen; | |
192 | } | ||
193 | } | ||
194 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
28 | if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && depth < 8) { |
195 | ✗ | uint8_t *ptr_free = ptr2; | |
196 | ✗ | ptr = p->data[0]; | |
197 | ✗ | for (y=0; y<h; y++) { | |
198 | ✗ | for (x = 0; x < (w + 7 >> 3) * depth; x++) { | |
199 | ✗ | if (depth == 1) { | |
200 | ✗ | ptr[8*x] = ptr2[x] >> 7; | |
201 | ✗ | ptr[8*x+1] = ptr2[x] >> 6 & 1; | |
202 | ✗ | ptr[8*x+2] = ptr2[x] >> 5 & 1; | |
203 | ✗ | ptr[8*x+3] = ptr2[x] >> 4 & 1; | |
204 | ✗ | ptr[8*x+4] = ptr2[x] >> 3 & 1; | |
205 | ✗ | ptr[8*x+5] = ptr2[x] >> 2 & 1; | |
206 | ✗ | ptr[8*x+6] = ptr2[x] >> 1 & 1; | |
207 | ✗ | ptr[8*x+7] = ptr2[x] & 1; | |
208 | } else { | ||
209 | ✗ | ptr[2*x] = ptr2[x] >> 4; | |
210 | ✗ | ptr[2*x+1] = ptr2[x] & 0xF; | |
211 | } | ||
212 | } | ||
213 | ✗ | ptr += p->linesize[0]; | |
214 | ✗ | ptr2 += (w + 15 >> 3) * depth; | |
215 | } | ||
216 | ✗ | av_freep(&ptr_free); | |
217 | } | ||
218 | |||
219 | 28 | *got_frame = 1; | |
220 | |||
221 | 28 | return buf - bufstart; | |
222 | } | ||
223 | |||
224 | const FFCodec ff_sunrast_decoder = { | ||
225 | .p.name = "sunrast", | ||
226 | CODEC_LONG_NAME("Sun Rasterfile image"), | ||
227 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
228 | .p.id = AV_CODEC_ID_SUNRAST, | ||
229 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
230 | FF_CODEC_DECODE_CB(sunrast_decode_frame), | ||
231 | }; | ||
232 |