FFmpeg
Loading...
Searching...
No Matches
bmp.c
Go to the documentation of this file.
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"
31
33 int *got_frame, AVPacket *avpkt)
34{
35 const uint8_t *buf = avpkt->data;
36 int buf_size = avpkt->size;
37 unsigned int fsize, hsize;
38 int width, height;
39 unsigned int depth;
41 unsigned int ihsize;
42 int i, j, n, linesize, ret;
43 uint32_t rgb[3] = {0};
44 uint32_t alpha = 0;
45 uint8_t *ptr;
46 int dsize;
47 const uint8_t *buf0 = buf;
49
50 if (buf_size < 14) {
51 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
53 }
54
55 if (bytestream_get_byte(&buf) != 'B' ||
56 bytestream_get_byte(&buf) != 'M') {
57 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
59 }
60
61 fsize = bytestream_get_le32(&buf);
62 if (buf_size < fsize) {
63 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %u), trying to decode anyway\n",
64 buf_size, fsize);
65 fsize = buf_size;
66 }
67
68 buf += 2; /* reserved1 */
69 buf += 2; /* reserved2 */
70
71 hsize = bytestream_get_le32(&buf); /* header size */
72 ihsize = bytestream_get_le32(&buf); /* more header size */
73 if (ihsize + 14LL > hsize) {
74 av_log(avctx, AV_LOG_ERROR, "invalid header size %u\n", hsize);
76 }
77
78 /* sometimes file size is set to some headers size, set a real size in that case */
79 if (fsize == 14 || fsize == ihsize + 14)
80 fsize = buf_size - 2;
81
82 if (fsize <= hsize) {
83 av_log(avctx, AV_LOG_ERROR,
84 "Declared file size is less than header size (%u < %u)\n",
85 fsize, hsize);
87 }
88
89 switch (ihsize) {
90 case 40: // windib
91 case 56: // windib v3
92 case 64: // OS/2 v2
93 case 108: // windib v4
94 case 124: // windib v5
95 width = bytestream_get_le32(&buf);
96 height = bytestream_get_le32(&buf);
97 break;
98 case 12: // OS/2 v1
99 width = bytestream_get_le16(&buf);
100 height = bytestream_get_le16(&buf);
101 break;
102 default:
103 avpriv_report_missing_feature(avctx, "Information header size %u",
104 ihsize);
106 }
107
108 /* planes */
109 if (bytestream_get_le16(&buf) != 1) {
110 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
111 return AVERROR_INVALIDDATA;
112 }
113
114 depth = bytestream_get_le16(&buf);
115
116 if (ihsize >= 40)
117 comp = bytestream_get_le32(&buf);
118 else
119 comp = BMP_RGB;
120
121 if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
122 comp != BMP_RLE8) {
123 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
124 return AVERROR_INVALIDDATA;
125 }
126
127 if (comp == BMP_BITFIELDS) {
128 buf += 20;
129 rgb[0] = bytestream_get_le32(&buf);
130 rgb[1] = bytestream_get_le32(&buf);
131 rgb[2] = bytestream_get_le32(&buf);
132 if (ihsize > 40)
133 alpha = bytestream_get_le32(&buf);
134 }
135
136 ret = ff_set_dimensions(avctx, width, height > 0 ? height : -(unsigned)height);
137 if (ret < 0) {
138 av_log(avctx, AV_LOG_ERROR, "Failed to set dimensions %d %d\n", width, height);
139 return AVERROR_INVALIDDATA;
140 }
141
142 avctx->pix_fmt = AV_PIX_FMT_NONE;
143
144 switch (depth) {
145 case 32:
146 if (comp == BMP_BITFIELDS) {
147 if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
149 else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
151 else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
153 else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
155 else {
156 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields "
157 "%0"PRIX32" %0"PRIX32" %0"PRIX32"\n", rgb[0], rgb[1], rgb[2]);
158 return AVERROR(EINVAL);
159 }
160 } else {
161 avctx->pix_fmt = AV_PIX_FMT_BGRA;
162 }
163 break;
164 case 24:
165 avctx->pix_fmt = AV_PIX_FMT_BGR24;
166 break;
167 case 16:
168 if (comp == BMP_RGB)
169 avctx->pix_fmt = AV_PIX_FMT_RGB555;
170 else if (comp == BMP_BITFIELDS) {
171 if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
172 avctx->pix_fmt = AV_PIX_FMT_RGB565;
173 else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
174 avctx->pix_fmt = AV_PIX_FMT_RGB555;
175 else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
176 avctx->pix_fmt = AV_PIX_FMT_RGB444;
177 else {
178 av_log(avctx, AV_LOG_ERROR,
179 "Unknown bitfields %0"PRIX32" %0"PRIX32" %0"PRIX32"\n",
180 rgb[0], rgb[1], rgb[2]);
181 return AVERROR(EINVAL);
182 }
183 }
184 break;
185 case 8:
186 if (hsize - ihsize - 14 > 0)
187 avctx->pix_fmt = AV_PIX_FMT_PAL8;
188 else
189 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
190 break;
191 case 1:
192 case 4:
193 if (hsize - ihsize - 14 > 0) {
194 avctx->pix_fmt = AV_PIX_FMT_PAL8;
195 } else {
196 av_log(avctx, AV_LOG_ERROR, "Unknown palette for %u-colour BMP\n",
197 1 << depth);
198 return AVERROR_INVALIDDATA;
199 }
200 break;
201 default:
202 av_log(avctx, AV_LOG_ERROR, "depth %u not supported\n", depth);
203 return AVERROR_INVALIDDATA;
204 }
205
206 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
207 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
208 return AVERROR_INVALIDDATA;
209 }
210
211 buf = buf0 + hsize;
212 dsize = buf_size - hsize;
213
214 /* Line size in file multiple of 4 */
215 n = ((avctx->width * depth + 31) / 8) & ~3;
216
217 if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
218 n = (avctx->width * depth + 7) / 8;
219 if (n * avctx->height > dsize) {
220 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
221 dsize, n * avctx->height);
222 return AVERROR_INVALIDDATA;
223 }
224 av_log(avctx, AV_LOG_ERROR, "data size too small, assuming missing line alignment\n");
225 }
226 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
227 return ret;
228
229 // RLE may skip decoding some picture areas, so blank picture before decoding
230 if (comp == BMP_RLE4 || comp == BMP_RLE8)
231 memset(p->data[0], 0, avctx->height * p->linesize[0]);
232
233 if (height > 0) {
234 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
235 linesize = -p->linesize[0];
236 } else {
237 ptr = p->data[0];
238 linesize = p->linesize[0];
239 }
240
241 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
242 int colors = 1 << depth;
243
244 memset(p->data[1], 0, 1024);
245
246 if (ihsize >= 36) {
247 int t;
248 buf = buf0 + 46;
249 t = bytestream_get_le32(&buf);
250 if (t < 0 || t > (1 << depth)) {
251 av_log(avctx, AV_LOG_ERROR,
252 "Incorrect number of colors - %X for bitdepth %u\n",
253 t, depth);
254 } else if (t) {
255 colors = t;
256 }
257 } else {
258 colors = FFMIN(256, (hsize-ihsize-14) / 3);
259 }
260 buf = buf0 + 14 + ihsize; //palette location
261 // OS/2 bitmap, 3 bytes per palette entry
262 if ((hsize-ihsize-14) < (colors << 2)) {
263 if ((hsize-ihsize-14) < colors * 3) {
264 av_log(avctx, AV_LOG_ERROR, "palette doesn't fit in packet\n");
265 return AVERROR_INVALIDDATA;
266 }
267 for (i = 0; i < colors; i++)
268 ((uint32_t*)p->data[1])[i] = (0xFFU<<24) | bytestream_get_le24(&buf);
269 } else {
270 for (i = 0; i < colors; i++)
271 ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
272 }
273 buf = buf0 + hsize;
274 }
275 if (comp == BMP_RLE4 || comp == BMP_RLE8) {
276 if (comp == BMP_RLE8 && height < 0) {
277 p->data[0] += p->linesize[0] * (avctx->height - 1);
278 p->linesize[0] = -p->linesize[0];
279 }
280 bytestream2_init(&gb, buf, dsize);
281 ff_msrle_decode(avctx, p, depth, &gb);
282 if (height < 0) {
283 p->data[0] += p->linesize[0] * (avctx->height - 1);
284 p->linesize[0] = -p->linesize[0];
285 }
286 } else {
287 switch (depth) {
288 case 1:
289 for (i = 0; i < avctx->height; i++) {
290 int j;
291 for (j = 0; j < avctx->width >> 3; j++) {
292 ptr[j*8+0] = buf[j] >> 7;
293 ptr[j*8+1] = (buf[j] >> 6) & 1;
294 ptr[j*8+2] = (buf[j] >> 5) & 1;
295 ptr[j*8+3] = (buf[j] >> 4) & 1;
296 ptr[j*8+4] = (buf[j] >> 3) & 1;
297 ptr[j*8+5] = (buf[j] >> 2) & 1;
298 ptr[j*8+6] = (buf[j] >> 1) & 1;
299 ptr[j*8+7] = buf[j] & 1;
300 }
301 for (j = 0; j < (avctx->width & 7); j++) {
302 ptr[avctx->width - (avctx->width & 7) + j] = buf[avctx->width >> 3] >> (7 - j) & 1;
303 }
304 buf += n;
305 ptr += linesize;
306 }
307 break;
308 case 8:
309 case 24:
310 case 32:
311 for (i = 0; i < avctx->height; i++) {
312 memcpy(ptr, buf, n);
313 buf += n;
314 ptr += linesize;
315 }
316 break;
317 case 4:
318 for (i = 0; i < avctx->height; i++) {
319 int j;
320 for (j = 0; j < n; j++) {
321 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
322 ptr[j*2+1] = buf[j] & 0xF;
323 }
324 buf += n;
325 ptr += linesize;
326 }
327 break;
328 case 16:
329 for (i = 0; i < avctx->height; i++) {
330 const uint8_t *src = buf;
331 uint16_t *dst = (uint16_t *) ptr;
332
333 for (j = 0; j < avctx->width; j++) {
334 *dst++ = AV_RL16(src);
335 src += 2;
336 }
337
338 buf += n;
339 ptr += linesize;
340 }
341 break;
342 default:
343 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
344 return AVERROR_INVALIDDATA;
345 }
346 }
347 if (avctx->pix_fmt == AV_PIX_FMT_BGRA) {
348 for (i = 0; i < avctx->height; i++) {
349 int j;
350 uint8_t *ptr = p->data[0] + p->linesize[0]*i + 3;
351 for (j = 0; j < avctx->width; j++) {
352 if (ptr[4*j])
353 break;
354 }
355 if (j < avctx->width)
356 break;
357 }
358 if (i == avctx->height)
359 avctx->pix_fmt = p->format = AV_PIX_FMT_BGR0;
360 }
361
362 *got_frame = 1;
363
364 return buf_size;
365}
366
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,
374};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_bmp_decoder
Definition bmp.c:367
static int64_t fsize(FILE *f)
Definition audiomatch.c:29
Libavcodec external API header.
static int bmp_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition bmp.c:32
BiCompression
Definition bmp.h:25
@ BMP_BITFIELDS
Definition bmp.h:29
@ BMP_RLE4
Definition bmp.h:28
@ BMP_RGB
Definition bmp.h:26
@ BMP_RLE8
Definition bmp.h:27
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition eamad.c:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_BMP
Definition codec_id.h:128
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static const int16_t alpha[]
Definition ilbcdata.h:55
#define AV_RL16(p)
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define FFMIN(a, b)
Definition macros.h:49
int ff_msrle_decode(AVCodecContext *avctx, AVFrame *pic, int depth, GetByteContext *gb)
Decode stream in MS RLE format into frame.
Definition msrledec.c:249
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition pixfmt.h:265
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition pixfmt.h:99
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition pixfmt.h:101
@ AV_PIX_FMT_0BGR
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition pixfmt.h:264
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition pixfmt.h:263
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
@ AV_PIX_FMT_0RGB
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition pixfmt.h:262
#define AV_PIX_FMT_RGB565
Definition pixfmt.h:532
#define AV_PIX_FMT_RGB444
Definition pixfmt.h:534
#define AV_PIX_FMT_RGB555
Definition pixfmt.h:533
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
Definition rpzaenc.c:60
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89