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