FFmpeg
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"
30 
31 static int bmp_decode_frame(AVCodecContext *avctx, AVFrame *p,
32  int *got_frame, AVPacket *avpkt)
33 {
34  const uint8_t *buf = avpkt->data;
35  int buf_size = avpkt->size;
36  unsigned int fsize, hsize;
37  int width, height;
38  unsigned int depth;
40  unsigned int ihsize;
41  int i, j, n, linesize, ret;
42  uint32_t rgb[3] = {0};
43  uint32_t alpha = 0;
44  uint8_t *ptr;
45  int dsize;
46  const uint8_t *buf0 = buf;
47  GetByteContext gb;
48 
49  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  if (bytestream_get_byte(&buf) != 'B' ||
55  bytestream_get_byte(&buf) != 'M') {
56  av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
57  return AVERROR_INVALIDDATA;
58  }
59 
60  fsize = bytestream_get_le32(&buf);
61  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  buf += 2; /* reserved1 */
68  buf += 2; /* reserved2 */
69 
70  hsize = bytestream_get_le32(&buf); /* header size */
71  ihsize = bytestream_get_le32(&buf); /* more header size */
72  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  if (fsize == 14 || fsize == ihsize + 14)
79  fsize = buf_size - 2;
80 
81  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  switch (ihsize) {
89  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  width = bytestream_get_le32(&buf);
95  height = bytestream_get_le32(&buf);
96  break;
97  case 12: // OS/2 v1
98  width = bytestream_get_le16(&buf);
99  height = bytestream_get_le16(&buf);
100  break;
101  default:
102  avpriv_report_missing_feature(avctx, "Information header size %u",
103  ihsize);
104  return AVERROR_PATCHWELCOME;
105  }
106 
107  /* planes */
108  if (bytestream_get_le16(&buf) != 1) {
109  av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
110  return AVERROR_INVALIDDATA;
111  }
112 
113  depth = bytestream_get_le16(&buf);
114 
115  if (ihsize >= 40)
116  comp = bytestream_get_le32(&buf);
117  else
118  comp = BMP_RGB;
119 
120  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  if (comp == BMP_BITFIELDS) {
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 
135  ret = ff_set_dimensions(avctx, width, height > 0 ? height : -(unsigned)height);
136  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  avctx->pix_fmt = AV_PIX_FMT_NONE;
142 
143  switch (depth) {
144  case 32:
145  if (comp == BMP_BITFIELDS) {
146  if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
148  else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
150  else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
152  else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
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  avctx->pix_fmt = AV_PIX_FMT_BGRA;
161  }
162  break;
163  case 24:
164  avctx->pix_fmt = AV_PIX_FMT_BGR24;
165  break;
166  case 16:
167  if (comp == BMP_RGB)
168  avctx->pix_fmt = AV_PIX_FMT_RGB555;
169  else if (comp == BMP_BITFIELDS) {
170  if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
171  avctx->pix_fmt = AV_PIX_FMT_RGB565;
172  else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
173  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  break;
184  case 8:
185  if (hsize - ihsize - 14 > 0)
186  avctx->pix_fmt = AV_PIX_FMT_PAL8;
187  else
188  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
189  break;
190  case 1:
191  case 4:
192  if (hsize - ihsize - 14 > 0) {
193  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  break;
200  default:
201  av_log(avctx, AV_LOG_ERROR, "depth %u not supported\n", depth);
202  return AVERROR_INVALIDDATA;
203  }
204 
205  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  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
211  return ret;
213  p->flags |= AV_FRAME_FLAG_KEY;
214 
215  buf = buf0 + hsize;
216  dsize = buf_size - hsize;
217 
218  /* Line size in file multiple of 4 */
219  n = ((avctx->width * depth + 31) / 8) & ~3;
220 
221  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  if (comp == BMP_RLE4 || comp == BMP_RLE8)
233  memset(p->data[0], 0, avctx->height * p->linesize[0]);
234 
235  if (height > 0) {
236  ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
237  linesize = -p->linesize[0];
238  } else {
239  ptr = p->data[0];
240  linesize = p->linesize[0];
241  }
242 
243  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
244  int colors = 1 << depth;
245 
246  memset(p->data[1], 0, 1024);
247 
248  if (ihsize >= 36) {
249  int t;
250  buf = buf0 + 46;
251  t = bytestream_get_le32(&buf);
252  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  } else if (t) {
257  colors = t;
258  }
259  } else {
260  colors = FFMIN(256, (hsize-ihsize-14) / 3);
261  }
262  buf = buf0 + 14 + ihsize; //palette location
263  // OS/2 bitmap, 3 bytes per palette entry
264  if ((hsize-ihsize-14) < (colors << 2)) {
265  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  for (i = 0; i < colors; i++)
270  ((uint32_t*)p->data[1])[i] = (0xFFU<<24) | bytestream_get_le24(&buf);
271  } else {
272  for (i = 0; i < colors; i++)
273  ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
274  }
275  buf = buf0 + hsize;
276  }
277  if (comp == BMP_RLE4 || comp == BMP_RLE8) {
278  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  bytestream2_init(&gb, buf, dsize);
283  ff_msrle_decode(avctx, p, depth, &gb);
284  if (height < 0) {
285  p->data[0] += p->linesize[0] * (avctx->height - 1);
286  p->linesize[0] = -p->linesize[0];
287  }
288  } else {
289  switch (depth) {
290  case 1:
291  for (i = 0; i < avctx->height; i++) {
292  int j;
293  for (j = 0; j < avctx->width >> 3; j++) {
294  ptr[j*8+0] = buf[j] >> 7;
295  ptr[j*8+1] = (buf[j] >> 6) & 1;
296  ptr[j*8+2] = (buf[j] >> 5) & 1;
297  ptr[j*8+3] = (buf[j] >> 4) & 1;
298  ptr[j*8+4] = (buf[j] >> 3) & 1;
299  ptr[j*8+5] = (buf[j] >> 2) & 1;
300  ptr[j*8+6] = (buf[j] >> 1) & 1;
301  ptr[j*8+7] = buf[j] & 1;
302  }
303  for (j = 0; j < (avctx->width & 7); j++) {
304  ptr[avctx->width - (avctx->width & 7) + j] = buf[avctx->width >> 3] >> (7 - j) & 1;
305  }
306  buf += n;
307  ptr += linesize;
308  }
309  break;
310  case 8:
311  case 24:
312  case 32:
313  for (i = 0; i < avctx->height; i++) {
314  memcpy(ptr, buf, n);
315  buf += n;
316  ptr += linesize;
317  }
318  break;
319  case 4:
320  for (i = 0; i < avctx->height; i++) {
321  int j;
322  for (j = 0; j < n; j++) {
323  ptr[j*2+0] = (buf[j] >> 4) & 0xF;
324  ptr[j*2+1] = buf[j] & 0xF;
325  }
326  buf += n;
327  ptr += linesize;
328  }
329  break;
330  case 16:
331  for (i = 0; i < avctx->height; i++) {
332  const uint16_t *src = (const uint16_t *) buf;
333  uint16_t *dst = (uint16_t *) ptr;
334 
335  for (j = 0; j < avctx->width; j++)
336  *dst++ = av_le2ne16(*src++);
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 };
ff_bmp_decoder
const FFCodec ff_bmp_decoder
Definition: bmp.c:367
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
comp
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:80
GetByteContext
Definition: bytestream.h:33
BMP_RLE4
@ BMP_RLE4
Definition: bmp.h:28
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVPacket::data
uint8_t * data
Definition: packet.h:491
FFCodec
Definition: codec_internal.h:127
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:649
BMP_RGB
@ BMP_RGB
Definition: bmp.h:26
ff_set_dimensions
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:94
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
rgb
Definition: rpzaenc.c:60
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
bmp_decode_frame
static int bmp_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition: bmp.c:31
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:628
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
AV_CODEC_ID_BMP
@ AV_CODEC_ID_BMP
Definition: codec_id.h:130
decode.h
fsize
static int64_t fsize(FILE *f)
Definition: audiomatch.c:29
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
bmp.h
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
msrledec.h
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
AV_PIX_FMT_BGR0
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:258
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
BiCompression
BiCompression
Definition: bmp.h:25
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:442
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1617
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:492
codec_internal.h
BMP_BITFIELDS
@ BMP_BITFIELDS
Definition: bmp.h:29
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:427
av_le2ne16
#define av_le2ne16(x)
Definition: bswap.h:97
height
#define height
AV_PIX_FMT_RGB0
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:256
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
BMP_RLE8
@ BMP_RLE8
Definition: bmp.h:27
AV_PIX_FMT_RGB555
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:456
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:621
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
AV_PIX_FMT_RGB565
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:455
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_0BGR
@ AV_PIX_FMT_0BGR
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:257
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:441
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_msrle_decode
int ff_msrle_decode(AVCodecContext *avctx, AVFrame *pic, int depth, GetByteContext *gb)
Decode stream in MS RLE format into frame.
Definition: msrledec.c:249
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:468
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
AV_PIX_FMT_0RGB
@ AV_PIX_FMT_0RGB
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:255
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_PIX_FMT_RGB444
#define AV_PIX_FMT_RGB444
Definition: pixfmt.h:457