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
29
31 void *
data,
int *got_frame,
33 {
35 int buf_size = avpkt->
size;
37 unsigned int fsize, hsize;
41 unsigned int ihsize;
42 int i, j,
n, linesize, ret;
43 uint32_t rgb[3] = {0};
46 int dsize;
49
50 if (buf_size < 14) {
53 }
54
55 if (bytestream_get_byte(&buf) != 'B' ||
56 bytestream_get_byte(&buf) != 'M') {
59 }
60
61 fsize = bytestream_get_le32(&buf);
62 if (buf_size < fsize) {
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) {
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) {
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:
105 }
106
107 /* planes */
108 if (bytestream_get_le16(&buf) != 1) {
111 }
112
113 depth = bytestream_get_le16(&buf);
114
115 if (ihsize >= 40)
116 comp = bytestream_get_le32(&buf);
117 else
119
124 }
125
127 buf += 20;
128 rgb[0] = bytestream_get_le32(&buf);
129 rgb[1] = bytestream_get_le32(&buf);
130 rgb[2] = bytestream_get_le32(&buf);
131 if (ihsize > 40)
132 alpha = bytestream_get_le32(&buf);
133 }
134
137
139
140 switch (depth) {
141 case 32:
143 if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
145 else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
147 else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
149 else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
151 else {
152 av_log(avctx,
AV_LOG_ERROR,
"Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
154 }
155 } else {
157 }
158 break;
159 case 24:
161 break;
162 case 16:
166 if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
168 else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
170 else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
172 else {
174 "Unknown bitfields %0"PRIX32" %0"PRIX32" %0"PRIX32"\n",
175 rgb[0], rgb[1], rgb[2]);
177 }
178 }
179 break;
180 case 8:
181 if (hsize - ihsize - 14 > 0)
183 else
185 break;
186 case 1:
187 case 4:
188 if (hsize - ihsize - 14 > 0) {
190 } else {
192 1 << depth);
194 }
195 break;
196 default:
199 }
200
204 }
205
207 return ret;
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
218 n = (avctx->
width * depth + 7) / 8;
219 if (n * avctx->
height > dsize) {
221 dsize, n * avctx->
height);
223 }
224 av_log(avctx,
AV_LOG_ERROR,
"data size too small, assuming missing line alignment\n");
225 }
226
227 // RLE may skip decoding some picture areas, so blank picture before decoding
230
231 if (height > 0) {
234 } else {
237 }
238
240 int colors = 1 <<
depth;
241
242 memset(p->
data[1], 0, 1024);
243
244 if (ihsize >= 36) {
245 int t;
246 buf = buf0 + 46;
247 t = bytestream_get_le32(&buf);
248 if (t < 0 || t > (1 << depth)) {
250 "Incorrect number of colors - %X for bitdepth %u\n",
251 t, depth);
252 } else if (t) {
253 colors = t;
254 }
255 } else {
256 colors =
FFMIN(256, (hsize-ihsize-14) / 3);
257 }
258 buf = buf0 + 14 + ihsize; //palette location
259 // OS/2 bitmap, 3 bytes per palette entry
260 if ((hsize-ihsize-14) < (colors << 2)) {
261 if ((hsize-ihsize-14) < colors * 3) {
264 }
265 for (i = 0; i < colors; i++)
266 ((uint32_t*)p->
data[1])[i] = (0xFF
U<<24) | bytestream_get_le24(&buf);
267 } else {
268 for (i = 0; i < colors; i++)
269 ((uint32_t*)p->
data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
270 }
271 buf = buf0 + hsize;
272 }
274 if (comp ==
BMP_RLE8 && height < 0) {
277 }
280 if (height < 0) {
283 }
284 } else {
285 switch (depth) {
286 case 1:
287 for (i = 0; i < avctx->
height; i++) {
288 int j;
289 for (j = 0; j <
n; j++) {
290 ptr[j*8+0] = buf[j] >> 7;
291 ptr[j*8+1] = (buf[j] >> 6) & 1;
292 ptr[j*8+2] = (buf[j] >> 5) & 1;
293 ptr[j*8+3] = (buf[j] >> 4) & 1;
294 ptr[j*8+4] = (buf[j] >> 3) & 1;
295 ptr[j*8+5] = (buf[j] >> 2) & 1;
296 ptr[j*8+6] = (buf[j] >> 1) & 1;
297 ptr[j*8+7] = buf[j] & 1;
298 }
300 ptr += linesize;
301 }
302 break;
303 case 8:
304 case 24:
305 case 32:
306 for (i = 0; i < avctx->
height; i++) {
307 memcpy(ptr, buf, n);
309 ptr += linesize;
310 }
311 break;
312 case 4:
313 for (i = 0; i < avctx->
height; i++) {
314 int j;
315 for (j = 0; j <
n; j++) {
316 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
317 ptr[j*2+1] = buf[j] & 0xF;
318 }
320 ptr += linesize;
321 }
322 break;
323 case 16:
324 for (i = 0; i < avctx->
height; i++) {
325 const uint16_t *
src = (
const uint16_t *) buf;
326 uint16_t *dst = (uint16_t *) ptr;
327
328 for (j = 0; j < avctx->
width; j++)
330
332 ptr += linesize;
333 }
334 break;
335 default:
338 }
339 }
341 for (i = 0; i < avctx->
height; i++) {
342 int j;
344 for (j = 0; j < avctx->
width; j++) {
345 if (ptr[4*j])
346 break;
347 }
348 if (j < avctx->width)
349 break;
350 }
353 }
354
355 *got_frame = 1;
356
357 return buf_size;
358 }
359
367 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
int ff_msrle_decode(AVCodecContext *avctx, AVFrame *pic, int depth, GetByteContext *gb)
Decode stream in MS RLE format into frame.
ptrdiff_t const GLvoid * data
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
#define AV_PIX_FMT_RGB444
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
8 bit with AV_PIX_FMT_RGB32 palette
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
static double alpha(void *priv, double x, double y)
static int bmp_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
const char * name
Name of the codec implementation.
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
enum AVPictureType pict_type
Picture type of the frame.
int width
picture width / height.
packed RGB 8:8:8, 24bpp, BGRBGR...
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
BYTE int const BYTE int int int height
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
common internal api header.
#define AV_PIX_FMT_RGB555
int key_frame
1 -> keyframe, 0-> not
#define AV_PIX_FMT_RGB565
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
This structure stores compressed data.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.