FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sunrast.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 74 126 58.7%
Branches: 53 90 58.9%

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