FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bmp.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 157 217 72.4%
Functions: 1 1 100.0%
Branches: 115 186 61.8%

Line Branch Exec Source
1 /*
2 * BMP image format decoder
3 * Copyright (c) 2005 Mans Rullgard
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 <inttypes.h>
23
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "bmp.h"
27 #include "codec_internal.h"
28 #include "decode.h"
29 #include "msrledec.h"
30
31 53 static int bmp_decode_frame(AVCodecContext *avctx, AVFrame *p,
32 int *got_frame, AVPacket *avpkt)
33 {
34 53 const uint8_t *buf = avpkt->data;
35 53 int buf_size = avpkt->size;
36 unsigned int fsize, hsize;
37 int width, height;
38 unsigned int depth;
39 BiCompression comp;
40 unsigned int ihsize;
41 int i, j, n, linesize, ret;
42 53 uint32_t rgb[3] = {0};
43 53 uint32_t alpha = 0;
44 uint8_t *ptr;
45 int dsize;
46 53 const uint8_t *buf0 = buf;
47 GetByteContext gb;
48
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (buf_size < 14) {
50 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
51 return AVERROR_INVALIDDATA;
52 }
53
54
2/4
✓ Branch 1 taken 53 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
106 if (bytestream_get_byte(&buf) != 'B' ||
55 53 bytestream_get_byte(&buf) != 'M') {
56 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
57 return AVERROR_INVALIDDATA;
58 }
59
60 53 fsize = bytestream_get_le32(&buf);
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (buf_size < fsize) {
62 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %u), trying to decode anyway\n",
63 buf_size, fsize);
64 fsize = buf_size;
65 }
66
67 53 buf += 2; /* reserved1 */
68 53 buf += 2; /* reserved2 */
69
70 53 hsize = bytestream_get_le32(&buf); /* header size */
71 53 ihsize = bytestream_get_le32(&buf); /* more header size */
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (ihsize + 14LL > hsize) {
73 av_log(avctx, AV_LOG_ERROR, "invalid header size %u\n", hsize);
74 return AVERROR_INVALIDDATA;
75 }
76
77 /* sometimes file size is set to some headers size, set a real size in that case */
78
3/4
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 51 times.
53 if (fsize == 14 || fsize == ihsize + 14)
79 2 fsize = buf_size - 2;
80
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (fsize <= hsize) {
82 av_log(avctx, AV_LOG_ERROR,
83 "Declared file size is less than header size (%u < %u)\n",
84 fsize, hsize);
85 return AVERROR_INVALIDDATA;
86 }
87
88
2/3
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
53 switch (ihsize) {
89 51 case 40: // windib
90 case 56: // windib v3
91 case 64: // OS/2 v2
92 case 108: // windib v4
93 case 124: // windib v5
94 51 width = bytestream_get_le32(&buf);
95 51 height = bytestream_get_le32(&buf);
96 51 break;
97 2 case 12: // OS/2 v1
98 2 width = bytestream_get_le16(&buf);
99 2 height = bytestream_get_le16(&buf);
100 2 break;
101 default:
102 avpriv_report_missing_feature(avctx, "Information header size %u",
103 ihsize);
104 return AVERROR_PATCHWELCOME;
105 }
106
107 /* planes */
108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
53 if (bytestream_get_le16(&buf) != 1) {
109 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
110 return AVERROR_INVALIDDATA;
111 }
112
113 53 depth = bytestream_get_le16(&buf);
114
115
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 2 times.
53 if (ihsize >= 40)
116 51 comp = bytestream_get_le32(&buf);
117 else
118 2 comp = BMP_RGB;
119
120
7/8
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
53 if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
121 comp != BMP_RLE8) {
122 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
123 return AVERROR_INVALIDDATA;
124 }
125
126
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 46 times.
53 if (comp == BMP_BITFIELDS) {
127 7 buf += 20;
128 7 rgb[0] = bytestream_get_le32(&buf);
129 7 rgb[1] = bytestream_get_le32(&buf);
130 7 rgb[2] = bytestream_get_le32(&buf);
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ihsize > 40)
132 alpha = bytestream_get_le32(&buf);
133 }
134
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 ret = ff_set_dimensions(avctx, width, height > 0 ? height : -(unsigned)height);
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (ret < 0) {
137 av_log(avctx, AV_LOG_ERROR, "Failed to set dimensions %d %d\n", width, height);
138 return AVERROR_INVALIDDATA;
139 }
140
141 53 avctx->pix_fmt = AV_PIX_FMT_NONE;
142
143
5/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
53 switch (depth) {
144 5 case 32:
145
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (comp == BMP_BITFIELDS) {
146
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3 if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
147 avctx->pix_fmt = alpha ? AV_PIX_FMT_ABGR : AV_PIX_FMT_0BGR;
148
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 avctx->pix_fmt = alpha ? AV_PIX_FMT_BGRA : AV_PIX_FMT_BGR0;
150 else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
151 avctx->pix_fmt = alpha ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
152 else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
153 avctx->pix_fmt = alpha ? AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB0;
154 else {
155 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields "
156 "%0"PRIX32" %0"PRIX32" %0"PRIX32"\n", rgb[0], rgb[1], rgb[2]);
157 return AVERROR(EINVAL);
158 }
159 } else {
160 2 avctx->pix_fmt = AV_PIX_FMT_BGRA;
161 }
162 5 break;
163 28 case 24:
164 28 avctx->pix_fmt = AV_PIX_FMT_BGR24;
165 28 break;
166 6 case 16:
167
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (comp == BMP_RGB)
168 2 avctx->pix_fmt = AV_PIX_FMT_RGB555;
169
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 else if (comp == BMP_BITFIELDS) {
170
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
4 if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
171 2 avctx->pix_fmt = AV_PIX_FMT_RGB565;
172
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
173 2 avctx->pix_fmt = AV_PIX_FMT_RGB555;
174 else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
175 avctx->pix_fmt = AV_PIX_FMT_RGB444;
176 else {
177 av_log(avctx, AV_LOG_ERROR,
178 "Unknown bitfields %0"PRIX32" %0"PRIX32" %0"PRIX32"\n",
179 rgb[0], rgb[1], rgb[2]);
180 return AVERROR(EINVAL);
181 }
182 }
183 6 break;
184 6 case 8:
185
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (hsize - ihsize - 14 > 0)
186 6 avctx->pix_fmt = AV_PIX_FMT_PAL8;
187 else
188 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
189 6 break;
190 8 case 1:
191 case 4:
192
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (hsize - ihsize - 14 > 0) {
193 8 avctx->pix_fmt = AV_PIX_FMT_PAL8;
194 } else {
195 av_log(avctx, AV_LOG_ERROR, "Unknown palette for %u-colour BMP\n",
196 1 << depth);
197 return AVERROR_INVALIDDATA;
198 }
199 8 break;
200 default:
201 av_log(avctx, AV_LOG_ERROR, "depth %u not supported\n", depth);
202 return AVERROR_INVALIDDATA;
203 }
204
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
206 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
207 return AVERROR_INVALIDDATA;
208 }
209
210
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
53 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
211 return ret;
212 53 p->pict_type = AV_PICTURE_TYPE_I;
213 53 p->flags |= AV_FRAME_FLAG_KEY;
214
215 53 buf = buf0 + hsize;
216 53 dsize = buf_size - hsize;
217
218 /* Line size in file multiple of 4 */
219 53 n = ((avctx->width * depth + 31) / 8) & ~3;
220
221
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
53 if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
222 n = (avctx->width * depth + 7) / 8;
223 if (n * avctx->height > dsize) {
224 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
225 dsize, n * avctx->height);
226 return AVERROR_INVALIDDATA;
227 }
228 av_log(avctx, AV_LOG_ERROR, "data size too small, assuming missing line alignment\n");
229 }
230
231 // RLE may skip decoding some picture areas, so blank picture before decoding
232
4/4
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 49 times.
53 if (comp == BMP_RLE4 || comp == BMP_RLE8)
233 4 memset(p->data[0], 0, avctx->height * p->linesize[0]);
234
235
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 if (height > 0) {
236 53 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
237 53 linesize = -p->linesize[0];
238 } else {
239 ptr = p->data[0];
240 linesize = p->linesize[0];
241 }
242
243
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 39 times.
53 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
244 14 int colors = 1 << depth;
245
246 14 memset(p->data[1], 0, 1024);
247
248
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 if (ihsize >= 36) {
249 int t;
250 12 buf = buf0 + 46;
251 12 t = bytestream_get_le32(&buf);
252
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
12 if (t < 0 || t > (1 << depth)) {
253 av_log(avctx, AV_LOG_ERROR,
254 "Incorrect number of colors - %X for bitdepth %u\n",
255 t, depth);
256
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 } else if (t) {
257 2 colors = t;
258 }
259 } else {
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 colors = FFMIN(256, (hsize-ihsize-14) / 3);
261 }
262 14 buf = buf0 + 14 + ihsize; //palette location
263 // OS/2 bitmap, 3 bytes per palette entry
264
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 if ((hsize-ihsize-14) < (colors << 2)) {
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ((hsize-ihsize-14) < colors * 3) {
266 av_log(avctx, AV_LOG_ERROR, "palette doesn't fit in packet\n");
267 return AVERROR_INVALIDDATA;
268 }
269
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 for (i = 0; i < colors; i++)
270 512 ((uint32_t*)p->data[1])[i] = (0xFFU<<24) | bytestream_get_le24(&buf);
271 } else {
272
2/2
✓ Branch 0 taken 1124 times.
✓ Branch 1 taken 12 times.
1136 for (i = 0; i < colors; i++)
273 1124 ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
274 }
275 14 buf = buf0 + hsize;
276 }
277
4/4
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 49 times.
53 if (comp == BMP_RLE4 || comp == BMP_RLE8) {
278
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
4 if (comp == BMP_RLE8 && height < 0) {
279 p->data[0] += p->linesize[0] * (avctx->height - 1);
280 p->linesize[0] = -p->linesize[0];
281 }
282 4 bytestream2_init(&gb, buf, dsize);
283 4 ff_msrle_decode(avctx, p, depth, &gb);
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (height < 0) {
285 p->data[0] += p->linesize[0] * (avctx->height - 1);
286 p->linesize[0] = -p->linesize[0];
287 }
288 } else {
289
4/5
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
49 switch (depth) {
290 2 case 1:
291
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2 times.
130 for (i = 0; i < avctx->height; i++) {
292 int j;
293
2/2
✓ Branch 0 taken 1920 times.
✓ Branch 1 taken 128 times.
2048 for (j = 0; j < avctx->width >> 3; j++) {
294 1920 ptr[j*8+0] = buf[j] >> 7;
295 1920 ptr[j*8+1] = (buf[j] >> 6) & 1;
296 1920 ptr[j*8+2] = (buf[j] >> 5) & 1;
297 1920 ptr[j*8+3] = (buf[j] >> 4) & 1;
298 1920 ptr[j*8+4] = (buf[j] >> 3) & 1;
299 1920 ptr[j*8+5] = (buf[j] >> 2) & 1;
300 1920 ptr[j*8+6] = (buf[j] >> 1) & 1;
301 1920 ptr[j*8+7] = buf[j] & 1;
302 }
303
2/2
✓ Branch 0 taken 896 times.
✓ Branch 1 taken 128 times.
1024 for (j = 0; j < (avctx->width & 7); j++) {
304 896 ptr[avctx->width - (avctx->width & 7) + j] = buf[avctx->width >> 3] >> (7 - j) & 1;
305 }
306 128 buf += n;
307 128 ptr += linesize;
308 }
309 2 break;
310 37 case 8:
311 case 24:
312 case 32:
313
2/2
✓ Branch 0 taken 7118 times.
✓ Branch 1 taken 37 times.
7155 for (i = 0; i < avctx->height; i++) {
314 7118 memcpy(ptr, buf, n);
315 7118 buf += n;
316 7118 ptr += linesize;
317 }
318 37 break;
319 4 case 4:
320
2/2
✓ Branch 0 taken 172 times.
✓ Branch 1 taken 4 times.
176 for (i = 0; i < avctx->height; i++) {
321 int j;
322
2/2
✓ Branch 0 taken 14880 times.
✓ Branch 1 taken 172 times.
15052 for (j = 0; j < n; j++) {
323 14880 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
324 14880 ptr[j*2+1] = buf[j] & 0xF;
325 }
326 172 buf += n;
327 172 ptr += linesize;
328 }
329 4 break;
330 6 case 16:
331
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 6 times.
390 for (i = 0; i < avctx->height; i++) {
332 384 const uint16_t *src = (const uint16_t *) buf;
333 384 uint16_t *dst = (uint16_t *) ptr;
334
335
2/2
✓ Branch 0 taken 48768 times.
✓ Branch 1 taken 384 times.
49152 for (j = 0; j < avctx->width; j++)
336 48768 *dst++ = av_le2ne16(*src++);
337
338 384 buf += n;
339 384 ptr += linesize;
340 }
341 6 break;
342 default:
343 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
344 return AVERROR_INVALIDDATA;
345 }
346 }
347
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 51 times.
53 if (avctx->pix_fmt == AV_PIX_FMT_BGRA) {
348
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2 times.
130 for (i = 0; i < avctx->height; i++) {
349 int j;
350 128 uint8_t *ptr = p->data[0] + p->linesize[0]*i + 3;
351
2/2
✓ Branch 0 taken 16256 times.
✓ Branch 1 taken 128 times.
16384 for (j = 0; j < avctx->width; j++) {
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16256 times.
16256 if (ptr[4*j])
353 break;
354 }
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (j < avctx->width)
356 break;
357 }
358
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (i == avctx->height)
359 2 avctx->pix_fmt = p->format = AV_PIX_FMT_BGR0;
360 }
361
362 53 *got_frame = 1;
363
364 53 return buf_size;
365 }
366
367 const FFCodec ff_bmp_decoder = {
368 .p.name = "bmp",
369 CODEC_LONG_NAME("BMP (Windows and OS/2 bitmap)"),
370 .p.type = AVMEDIA_TYPE_VIDEO,
371 .p.id = AV_CODEC_ID_BMP,
372 .p.capabilities = AV_CODEC_CAP_DR1,
373 FF_CODEC_DECODE_CB(bmp_decode_frame),
374 };
375