FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sgidec.c
Date: 2021-09-22 23:55:11
Exec Total Coverage
Lines: 105 132 79.5%
Branches: 64 86 74.4%

Line Branch Exec Source
1 /*
2 * SGI image decoder
3 * Todd Kirby <doubleshot@pacbell.net>
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/imgutils.h"
23 #include "avcodec.h"
24 #include "bytestream.h"
25 #include "internal.h"
26 #include "sgi.h"
27
28 typedef struct SgiState {
29 AVCodecContext *avctx;
30 unsigned int width;
31 unsigned int height;
32 unsigned int depth;
33 unsigned int bytes_per_channel;
34 int linesize;
35 GetByteContext g;
36 } SgiState;
37
38 /**
39 * Expand an RLE row into a channel.
40 * @param s the current image state
41 * @param out_buf Points to one line after the output buffer.
42 * @param len length of out_buf in bytes
43 * @param pixelstride pixel stride of input buffer
44 * @return size of output in bytes, else return error code.
45 */
46 21152 static int expand_rle_row8(SgiState *s, uint8_t *out_buf,
47 int len, int pixelstride)
48 {
49 unsigned char pixel, count;
50 21152 unsigned char *orig = out_buf;
51 21152 uint8_t *out_end = out_buf + len;
52
53
2/2
✓ Branch 0 taken 525269 times.
✓ Branch 1 taken 21152 times.
546421 while (out_buf < out_end) {
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 525269 times.
525269 if (bytestream2_get_bytes_left(&s->g) < 1)
55 return AVERROR_INVALIDDATA;
56 525269 pixel = bytestream2_get_byteu(&s->g);
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 525269 times.
525269 if (!(count = (pixel & 0x7f))) {
58 break;
59 }
60
61 /* Check for buffer overflow. */
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 525269 times.
525269 if (out_end - out_buf <= pixelstride * (count - 1)) {
63 av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");
64 return AVERROR_INVALIDDATA;
65 }
66
67
2/2
✓ Branch 0 taken 146105 times.
✓ Branch 1 taken 379164 times.
525269 if (pixel & 0x80) {
68
2/2
✓ Branch 0 taken 4780725 times.
✓ Branch 1 taken 146105 times.
4926830 while (count--) {
69 4780725 *out_buf = bytestream2_get_byte(&s->g);
70 4780725 out_buf += pixelstride;
71 }
72 } else {
73 379164 pixel = bytestream2_get_byte(&s->g);
74
75
2/2
✓ Branch 0 taken 3975499 times.
✓ Branch 1 taken 379164 times.
4354663 while (count--) {
76 3975499 *out_buf = pixel;
77 3975499 out_buf += pixelstride;
78 }
79 }
80 }
81 21152 return (out_buf - orig) / pixelstride;
82 }
83
84 4096 static int expand_rle_row16(SgiState *s, uint16_t *out_buf,
85 int len, int pixelstride)
86 {
87 unsigned short pixel;
88 unsigned char count;
89 4096 unsigned short *orig = out_buf;
90 4096 uint16_t *out_end = out_buf + len;
91
92
2/2
✓ Branch 0 taken 144880 times.
✓ Branch 1 taken 4096 times.
148976 while (out_buf < out_end) {
93
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 144880 times.
144880 if (bytestream2_get_bytes_left(&s->g) < 2)
94 return AVERROR_INVALIDDATA;
95 144880 pixel = bytestream2_get_be16u(&s->g);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 144880 times.
144880 if (!(count = (pixel & 0x7f)))
97 break;
98
99 /* Check for buffer overflow. */
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 144880 times.
144880 if (out_end - out_buf <= pixelstride * (count - 1)) {
101 av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");
102 return AVERROR_INVALIDDATA;
103 }
104
105
2/2
✓ Branch 0 taken 18988 times.
✓ Branch 1 taken 125892 times.
144880 if (pixel & 0x80) {
106
2/2
✓ Branch 0 taken 289372 times.
✓ Branch 1 taken 18988 times.
308360 while (count--) {
107 289372 pixel = bytestream2_get_ne16(&s->g);
108 289372 AV_WN16A(out_buf, pixel);
109 289372 out_buf += pixelstride;
110 }
111 } else {
112 125892 pixel = bytestream2_get_ne16(&s->g);
113
114
2/2
✓ Branch 0 taken 1807780 times.
✓ Branch 1 taken 125892 times.
1933672 while (count--) {
115 1807780 AV_WN16A(out_buf, pixel);
116 1807780 out_buf += pixelstride;
117 }
118 }
119 }
120 4096 return (out_buf - orig) / pixelstride;
121 }
122
123
124 /**
125 * Read a run length encoded SGI image.
126 * @param out_buf output buffer
127 * @param s the current image state
128 * @return 0 if no error, else return error code.
129 */
130 33 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
131 {
132 uint8_t *dest_row;
133 33 unsigned int len = s->height * s->depth * 4;
134 33 GetByteContext g_table = s->g;
135 unsigned int y, z;
136 unsigned int start_offset;
137 int linesize, ret;
138
139 /* size of RLE offset and length tables */
140
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
141 return AVERROR_INVALIDDATA;
142 }
143
144
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 33 times.
126 for (z = 0; z < s->depth; z++) {
145 93 dest_row = out_buf;
146
2/2
✓ Branch 0 taken 25248 times.
✓ Branch 1 taken 93 times.
25341 for (y = 0; y < s->height; y++) {
147 25248 linesize = s->width * s->depth;
148 25248 dest_row -= s->linesize;
149 25248 start_offset = bytestream2_get_be32(&g_table);
150 25248 bytestream2_seek(&s->g, start_offset, SEEK_SET);
151
2/2
✓ Branch 0 taken 21152 times.
✓ Branch 1 taken 4096 times.
25248 if (s->bytes_per_channel == 1)
152 21152 ret = expand_rle_row8(s, dest_row + z, linesize, s->depth);
153 else
154 4096 ret = expand_rle_row16(s, (uint16_t *)dest_row + z, linesize, s->depth);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25248 times.
25248 if (ret != s->width)
156 return AVERROR_INVALIDDATA;
157 }
158 }
159 33 return 0;
160 }
161
162 /**
163 * Read an uncompressed SGI image.
164 * @param out_buf output buffer
165 * @param s the current image state
166 * @return 0 if read success, else return error code.
167 */
168 6 static int read_uncompressed_sgi(unsigned char *out_buf, SgiState *s)
169 {
170 int x, y, z;
171 6 unsigned int offset = s->height * s->width * s->bytes_per_channel;
172 GetByteContext gp[4];
173 uint8_t *out_end;
174
175 /* Test buffer size. */
176
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
177 return AVERROR_INVALIDDATA;
178
179 /* Create a reader for each plane */
180
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6 times.
22 for (z = 0; z < s->depth; z++) {
181 16 gp[z] = s->g;
182 16 bytestream2_skip(&gp[z], z * offset);
183 }
184
185
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 6 times.
1542 for (y = s->height - 1; y >= 0; y--) {
186 1536 out_end = out_buf + (y * s->linesize);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1536 times.
1536 if (s->bytes_per_channel == 1) {
188 for (x = s->width; x > 0; x--)
189 for (z = 0; z < s->depth; z++)
190 *out_end++ = bytestream2_get_byteu(&gp[z]);
191 } else {
192 1536 uint16_t *out16 = (uint16_t *)out_end;
193
2/2
✓ Branch 0 taken 786432 times.
✓ Branch 1 taken 1536 times.
787968 for (x = s->width; x > 0; x--)
194
2/2
✓ Branch 0 taken 2097152 times.
✓ Branch 1 taken 786432 times.
2883584 for (z = 0; z < s->depth; z++)
195 2097152 *out16++ = bytestream2_get_ne16u(&gp[z]);
196 }
197 }
198 6 return 0;
199 }
200
201 39 static int decode_frame(AVCodecContext *avctx,
202 void *data, int *got_frame,
203 AVPacket *avpkt)
204 {
205 39 SgiState *s = avctx->priv_data;
206 39 AVFrame *p = data;
207 unsigned int dimension, rle;
208 39 int ret = 0;
209 uint8_t *out_buf, *out_end;
210
211 39 bytestream2_init(&s->g, avpkt->data, avpkt->size);
212
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) {
213 av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
214 return AVERROR_INVALIDDATA;
215 }
216
217 /* Test for SGI magic. */
218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if (bytestream2_get_be16u(&s->g) != SGI_MAGIC) {
219 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
220 return AVERROR_INVALIDDATA;
221 }
222
223 39 rle = bytestream2_get_byteu(&s->g);
224 39 s->bytes_per_channel = bytestream2_get_byteu(&s->g);
225 39 dimension = bytestream2_get_be16u(&s->g);
226 39 s->width = bytestream2_get_be16u(&s->g);
227 39 s->height = bytestream2_get_be16u(&s->g);
228 39 s->depth = bytestream2_get_be16u(&s->g);
229
230
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
39 if (s->bytes_per_channel != 1 && s->bytes_per_channel != 2) {
231 av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
232 return AVERROR_INVALIDDATA;
233 }
234
235 /* Check for supported image dimensions. */
236
3/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
39 if (dimension != 2 && dimension != 3) {
237 av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
238 return AVERROR_INVALIDDATA;
239 }
240
241
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 31 times.
39 if (s->depth == SGI_GRAYSCALE) {
242
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
243
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 8 times.
31 } else if (s->depth == SGI_RGB) {
244
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 19 times.
23 avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGB48BE : AV_PIX_FMT_RGB24;
245
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 } else if (s->depth == SGI_RGBA) {
246
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGBA;
247 } else {
248 av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
249 return AVERROR_INVALIDDATA;
250 }
251
252 39 ret = ff_set_dimensions(avctx, s->width, s->height);
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (ret < 0)
254 return ret;
255
256
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
257 return ret;
258
259 39 p->pict_type = AV_PICTURE_TYPE_I;
260 39 p->key_frame = 1;
261 39 out_buf = p->data[0];
262
263 39 out_end = out_buf + p->linesize[0] * s->height;
264
265 39 s->linesize = p->linesize[0];
266
267 /* Skip header. */
268 39 bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
269
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 6 times.
39 if (rle) {
270 33 ret = read_rle_sgi(out_end, s);
271 } else {
272 6 ret = read_uncompressed_sgi(out_buf, s);
273 }
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (ret)
275 return ret;
276
277 39 *got_frame = 1;
278 39 return avpkt->size;
279 }
280
281 27 static av_cold int sgi_decode_init(AVCodecContext *avctx)
282 {
283 27 SgiState *s = avctx->priv_data;
284
285 27 s->avctx = avctx;
286
287 27 return 0;
288 }
289
290 const AVCodec ff_sgi_decoder = {
291 .name = "sgi",
292 .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
293 .type = AVMEDIA_TYPE_VIDEO,
294 .id = AV_CODEC_ID_SGI,
295 .priv_data_size = sizeof(SgiState),
296 .decode = decode_frame,
297 .init = sgi_decode_init,
298 .capabilities = AV_CODEC_CAP_DR1,
299 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
300 };
301