FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/targa.c
Date: 2022-01-16 20:33:26
Exec Total Coverage
Lines: 117 172 68.0%
Branches: 61 104 58.7%

Line Branch Exec Source
1 /*
2 * Targa (.tga) image decoder
3 * Copyright (c) 2006 Konstantin Shishkov
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/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "targa.h"
28
29 typedef struct TargaContext {
30 GetByteContext gb;
31 } TargaContext;
32
33 7392 static uint8_t *advance_line(uint8_t *start, uint8_t *line,
34 int stride, int *y, int h, int interleave)
35 {
36 7392 *y += interleave;
37
38
2/2
✓ Branch 0 taken 7355 times.
✓ Branch 1 taken 37 times.
7392 if (*y < h) {
39 7355 return line + interleave * stride;
40 } else {
41 37 *y = (*y + 1) & (interleave - 1);
42
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
37 if (*y && *y < h) {
43 return start + *y * stride;
44 } else {
45 37 return NULL;
46 }
47 }
48 }
49
50 12 static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s,
51 uint8_t *start, int w, int h, int stride,
52 int bpp, int interleave)
53 {
54 int x, y;
55 12 int depth = (bpp + 1) >> 3;
56 int type, count;
57 12 uint8_t *line = start;
58 12 uint8_t *dst = line;
59
60 12 x = y = count = 0;
61
2/2
✓ Branch 0 taken 22044 times.
✓ Branch 1 taken 12 times.
22056 while (dst) {
62
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22044 times.
22044 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
63 av_log(avctx, AV_LOG_ERROR,
64 "Ran ouf of data before end-of-image\n");
65 return AVERROR_INVALIDDATA;
66 }
67 22044 type = bytestream2_get_byteu(&s->gb);
68 22044 count = (type & 0x7F) + 1;
69 22044 type &= 0x80;
70
2/2
✓ Branch 0 taken 1284 times.
✓ Branch 1 taken 20760 times.
22044 if (!type) {
71 do {
72 1284 int n = FFMIN(count, w - x);
73 1284 bytestream2_get_buffer(&s->gb, dst, n * depth);
74 1284 count -= n;
75 1284 dst += n * depth;
76 1284 x += n;
77
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 772 times.
1284 if (x == w) {
78 512 x = 0;
79 512 dst = line = advance_line(start, line, stride, &y, h, interleave);
80 }
81
3/4
✓ Branch 0 taken 1282 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1282 times.
1284 } while (dst && count > 0);
82 } else {
83 uint8_t tmp[4];
84 20760 bytestream2_get_buffer(&s->gb, tmp, depth);
85 do {
86 20760 int n = FFMIN(count, w - x);
87 20760 count -= n;
88 20760 x += n;
89 do {
90 164400 memcpy(dst, tmp, depth);
91 164400 dst += depth;
92
2/2
✓ Branch 0 taken 143640 times.
✓ Branch 1 taken 20760 times.
164400 } while (--n);
93
2/2
✓ Branch 0 taken 1280 times.
✓ Branch 1 taken 19480 times.
20760 if (x == w) {
94 1280 x = 0;
95 1280 dst = line = advance_line(start, line, stride, &y, h, interleave);
96 }
97
3/4
✓ Branch 0 taken 20750 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20750 times.
20760 } while (dst && count > 0);
98 }
99 }
100
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (count) {
102 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds\n");
103 return AVERROR_INVALIDDATA;
104 }
105
106 12 return 0;
107 }
108
109 37 static int decode_frame(AVCodecContext *avctx,
110 void *data, int *got_frame,
111 AVPacket *avpkt)
112 {
113 37 TargaContext * const s = avctx->priv_data;
114 37 AVFrame * const p = data;
115 uint8_t *dst;
116 int stride;
117 int idlen, pal, compr, y, w, h, bpp, flags, ret;
118 int first_clr, colors, csize;
119 int interleave;
120 size_t img_size;
121
122 37 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
123
124 /* parse image header */
125 37 idlen = bytestream2_get_byte(&s->gb);
126 37 pal = bytestream2_get_byte(&s->gb);
127 37 compr = bytestream2_get_byte(&s->gb);
128 37 first_clr = bytestream2_get_le16(&s->gb);
129 37 colors = bytestream2_get_le16(&s->gb);
130 37 csize = bytestream2_get_byte(&s->gb);
131 37 bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
132 37 w = bytestream2_get_le16(&s->gb);
133 37 h = bytestream2_get_le16(&s->gb);
134 37 bpp = bytestream2_get_byte(&s->gb);
135
136 37 flags = bytestream2_get_byte(&s->gb);
137
138
5/8
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 33 times.
37 if (!pal && (first_clr || colors || csize)) {
139 av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
140 // specification says we should ignore those value in this case
141 first_clr = colors = csize = 0;
142 }
143
144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if (bytestream2_get_bytes_left(&s->gb) < idlen + 2*colors) {
145 av_log(avctx, AV_LOG_ERROR,
146 "Not enough data to read header\n");
147 return AVERROR_INVALIDDATA;
148 }
149
150 // skip identifier if any
151 37 bytestream2_skip(&s->gb, idlen);
152
153
4/5
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
37 switch (bpp) {
154 8 case 8:
155
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
156 8 break;
157 4 case 15:
158 case 16:
159 4 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
160 4 break;
161 21 case 24:
162 21 avctx->pix_fmt = AV_PIX_FMT_BGR24;
163 21 break;
164 4 case 32:
165 4 avctx->pix_fmt = AV_PIX_FMT_BGRA;
166 4 break;
167 default:
168 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
169 return AVERROR_INVALIDDATA;
170 }
171
172
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
37 if (colors && (colors + first_clr) > 256) {
173 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
174 return AVERROR_INVALIDDATA;
175 }
176
177
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
178 return ret;
179
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if ((compr & (~TGA_RLE)) == TGA_NODATA) {
181 return avpkt->size;
182 }
183
184
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 12 times.
37 if (!(compr & TGA_RLE)) {
185 25 img_size = w * ((bpp + 1) >> 3);
186
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
25 if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
187 av_log(avctx, AV_LOG_ERROR,
188 "Not enough data available for image\n");
189 return AVERROR_INVALIDDATA;
190 }
191 }
192
193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
194 return ret;
195 37 p->pict_type = AV_PICTURE_TYPE_I;
196
197
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 20 times.
37 if (flags & TGA_TOPTOBOTTOM) {
198 17 dst = p->data[0];
199 17 stride = p->linesize[0];
200 } else { //image is upside-down
201 20 dst = p->data[0] + p->linesize[0] * (h - 1);
202 20 stride = -p->linesize[0];
203 }
204
205
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
74 interleave = flags & TGA_INTERLEAVE2 ? 2 :
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 flags & TGA_INTERLEAVE4 ? 4 : 1;
207
208
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 33 times.
37 if (colors) {
209 int pal_size, pal_sample_size;
210
211
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 switch (csize) {
212 case 32: pal_sample_size = 4; break;
213 case 24: pal_sample_size = 3; break;
214 4 case 16:
215 4 case 15: pal_sample_size = 2; break;
216 default:
217 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
218 return AVERROR_INVALIDDATA;
219 }
220 4 pal_size = colors * pal_sample_size;
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (avctx->pix_fmt != AV_PIX_FMT_PAL8) //should not occur but skip palette anyway
222 bytestream2_skip(&s->gb, pal_size);
223 else {
224 int t;
225 4 uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
226
227
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
228 av_log(avctx, AV_LOG_ERROR,
229 "Not enough data to read palette\n");
230 return AVERROR_INVALIDDATA;
231 }
232
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 switch (pal_sample_size) {
233 case 4:
234 for (t = 0; t < colors; t++)
235 *pal++ = bytestream2_get_le32u(&s->gb);
236 break;
237 case 3:
238 /* RGB24 */
239 for (t = 0; t < colors; t++)
240 *pal++ = (0xffU<<24) | bytestream2_get_le24u(&s->gb);
241 break;
242 4 case 2:
243 /* RGB555 */
244
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 4 times.
1028 for (t = 0; t < colors; t++) {
245 1024 uint32_t v = bytestream2_get_le16u(&s->gb);
246 1024 v = ((v & 0x7C00) << 9) |
247 1024 ((v & 0x03E0) << 6) |
248 1024 ((v & 0x001F) << 3);
249 /* left bit replication */
250 1024 v |= (v & 0xE0E0E0U) >> 5;
251 1024 *pal++ = (0xffU<<24) | v;
252 }
253 4 break;
254 }
255 4 p->palette_has_changed = 1;
256 }
257 }
258
259
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 25 times.
37 if (compr & TGA_RLE) {
260 12 int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp, interleave);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (res < 0)
262 return res;
263 } else {
264 uint8_t *line;
265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
25 if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
266 av_log(avctx, AV_LOG_ERROR,
267 "Not enough data available for image\n");
268 return AVERROR_INVALIDDATA;
269 }
270
271 25 line = dst;
272 25 y = 0;
273 do {
274 5600 bytestream2_get_buffer(&s->gb, line, img_size);
275 5600 line = advance_line(dst, line, stride, &y, h, interleave);
276
2/2
✓ Branch 0 taken 5575 times.
✓ Branch 1 taken 25 times.
5600 } while (line);
277 }
278
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (flags & TGA_RIGHTTOLEFT) { // right-to-left, needs horizontal flip
280 int x;
281 for (y = 0; y < h; y++) {
282 void *line = &p->data[0][y * p->linesize[0]];
283 for (x = 0; x < w >> 1; x++) {
284 switch (bpp) {
285 case 32:
286 FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[w - x - 1]);
287 break;
288 case 24:
289 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * w - 3 * x - 3]);
290 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * w - 3 * x - 2]);
291 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * w - 3 * x - 1]);
292 break;
293 case 16:
294 FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[w - x - 1]);
295 break;
296 case 8:
297 FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[w - x - 1]);
298 }
299 }
300 }
301 }
302
303
304 37 *got_frame = 1;
305
306 37 return avpkt->size;
307 }
308
309 const AVCodec ff_targa_decoder = {
310 .name = "targa",
311 .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
312 .type = AVMEDIA_TYPE_VIDEO,
313 .id = AV_CODEC_ID_TARGA,
314 .priv_data_size = sizeof(TargaContext),
315 .decode = decode_frame,
316 .capabilities = AV_CODEC_CAP_DR1,
317 };
318