FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sgidec.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 101 125 80.8%
Functions: 5 5 100.0%
Branches: 62 80 77.5%

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