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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "bmp.h"
25 #include "internal.h"
26 #include "msrledec.h"
27 
29 {
30  BMPContext *s = avctx->priv_data;
31 
33  avctx->coded_frame = &s->picture;
34 
35  return 0;
36 }
37 
38 static int bmp_decode_frame(AVCodecContext *avctx,
39  void *data, int *got_frame,
40  AVPacket *avpkt)
41 {
42  const uint8_t *buf = avpkt->data;
43  int buf_size = avpkt->size;
44  BMPContext *s = avctx->priv_data;
45  AVFrame *picture = data;
46  AVFrame *p = &s->picture;
47  unsigned int fsize, hsize;
48  int width, height;
49  unsigned int depth;
51  unsigned int ihsize;
52  int i, j, n, linesize;
53  uint32_t rgb[3];
54  uint8_t *ptr;
55  int dsize;
56  const uint8_t *buf0 = buf;
57  GetByteContext gb;
58 
59  if (buf_size < 14) {
60  av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
61  return -1;
62  }
63 
64  if (bytestream_get_byte(&buf) != 'B' ||
65  bytestream_get_byte(&buf) != 'M') {
66  av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
67  return -1;
68  }
69 
70  fsize = bytestream_get_le32(&buf);
71  if (buf_size < fsize) {
72  av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
73  buf_size, fsize);
74  fsize = buf_size;
75  }
76 
77  buf += 2; /* reserved1 */
78  buf += 2; /* reserved2 */
79 
80  hsize = bytestream_get_le32(&buf); /* header size */
81  ihsize = bytestream_get_le32(&buf); /* more header size */
82  if (ihsize + 14 > hsize) {
83  av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
84  return -1;
85  }
86 
87  /* sometimes file size is set to some headers size, set a real size in that case */
88  if (fsize == 14 || fsize == ihsize + 14)
89  fsize = buf_size - 2;
90 
91  if (fsize <= hsize) {
92  av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
93  fsize, hsize);
94  return -1;
95  }
96 
97  switch (ihsize) {
98  case 40: // windib v3
99  case 64: // OS/2 v2
100  case 108: // windib v4
101  case 124: // windib v5
102  width = bytestream_get_le32(&buf);
103  height = bytestream_get_le32(&buf);
104  break;
105  case 12: // OS/2 v1
106  width = bytestream_get_le16(&buf);
107  height = bytestream_get_le16(&buf);
108  break;
109  default:
110  av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
111  return -1;
112  }
113 
114  /* planes */
115  if (bytestream_get_le16(&buf) != 1) {
116  av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
117  return -1;
118  }
119 
120  depth = bytestream_get_le16(&buf);
121 
122  if (ihsize == 40)
123  comp = bytestream_get_le32(&buf);
124  else
125  comp = BMP_RGB;
126 
127  if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
128  comp != BMP_RLE8) {
129  av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
130  return -1;
131  }
132 
133  if (comp == BMP_BITFIELDS) {
134  buf += 20;
135  rgb[0] = bytestream_get_le32(&buf);
136  rgb[1] = bytestream_get_le32(&buf);
137  rgb[2] = bytestream_get_le32(&buf);
138  }
139 
140  avctx->width = width;
141  avctx->height = height > 0 ? height : -height;
142 
143  avctx->pix_fmt = AV_PIX_FMT_NONE;
144 
145  switch (depth) {
146  case 32:
147  if (comp == BMP_BITFIELDS) {
148  rgb[0] = (rgb[0] >> 15) & 3;
149  rgb[1] = (rgb[1] >> 15) & 3;
150  rgb[2] = (rgb[2] >> 15) & 3;
151 
152  if (rgb[0] + rgb[1] + rgb[2] != 3 ||
153  rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]) {
154  break;
155  }
156  } else {
157  rgb[0] = 2;
158  rgb[1] = 1;
159  rgb[2] = 0;
160  }
161 
162  avctx->pix_fmt = AV_PIX_FMT_BGR24;
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, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
179  return AVERROR(EINVAL);
180  }
181  }
182  break;
183  case 8:
184  if (hsize - ihsize - 14 > 0)
185  avctx->pix_fmt = AV_PIX_FMT_PAL8;
186  else
187  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
188  break;
189  case 1:
190  case 4:
191  if (hsize - ihsize - 14 > 0) {
192  avctx->pix_fmt = AV_PIX_FMT_PAL8;
193  } else {
194  av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
195  return -1;
196  }
197  break;
198  default:
199  av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
200  return -1;
201  }
202 
203  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
204  av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
205  return -1;
206  }
207 
208  if (p->data[0])
209  avctx->release_buffer(avctx, p);
210 
211  p->reference = 0;
212  if (ff_get_buffer(avctx, p) < 0) {
213  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
214  return -1;
215  }
217  p->key_frame = 1;
218 
219  buf = buf0 + hsize;
220  dsize = buf_size - hsize;
221 
222  /* Line size in file multiple of 4 */
223  n = ((avctx->width * depth) / 8 + 3) & ~3;
224 
225  if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
226  av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
227  dsize, n * avctx->height);
228  return -1;
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, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
254  } else if (t) {
255  colors = t;
256  }
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  for (i = 0; i < colors; i++)
262  ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
263  } else {
264  for (i = 0; i < colors; i++)
265  ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
266  }
267  buf = buf0 + hsize;
268  }
269  if (comp == BMP_RLE4 || comp == BMP_RLE8) {
270  if (height < 0) {
271  p->data[0] += p->linesize[0] * (avctx->height - 1);
272  p->linesize[0] = -p->linesize[0];
273  }
274  bytestream2_init(&gb, buf, dsize);
275  ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
276  if (height < 0) {
277  p->data[0] += p->linesize[0] * (avctx->height - 1);
278  p->linesize[0] = -p->linesize[0];
279  }
280  } else {
281  switch (depth) {
282  case 1:
283  for (i = 0; i < avctx->height; i++) {
284  int j;
285  for (j = 0; j < n; j++) {
286  ptr[j*8+0] = buf[j] >> 7;
287  ptr[j*8+1] = (buf[j] >> 6) & 1;
288  ptr[j*8+2] = (buf[j] >> 5) & 1;
289  ptr[j*8+3] = (buf[j] >> 4) & 1;
290  ptr[j*8+4] = (buf[j] >> 3) & 1;
291  ptr[j*8+5] = (buf[j] >> 2) & 1;
292  ptr[j*8+6] = (buf[j] >> 1) & 1;
293  ptr[j*8+7] = buf[j] & 1;
294  }
295  buf += n;
296  ptr += linesize;
297  }
298  break;
299  case 8:
300  case 24:
301  for (i = 0; i < avctx->height; i++) {
302  memcpy(ptr, buf, n);
303  buf += n;
304  ptr += linesize;
305  }
306  break;
307  case 4:
308  for (i = 0; i < avctx->height; i++) {
309  int j;
310  for (j = 0; j < n; j++) {
311  ptr[j*2+0] = (buf[j] >> 4) & 0xF;
312  ptr[j*2+1] = buf[j] & 0xF;
313  }
314  buf += n;
315  ptr += linesize;
316  }
317  break;
318  case 16:
319  for (i = 0; i < avctx->height; i++) {
320  const uint16_t *src = (const uint16_t *) buf;
321  uint16_t *dst = (uint16_t *) ptr;
322 
323  for (j = 0; j < avctx->width; j++)
324  *dst++ = av_le2ne16(*src++);
325 
326  buf += n;
327  ptr += linesize;
328  }
329  break;
330  case 32:
331  for (i = 0; i < avctx->height; i++) {
332  const uint8_t *src = buf;
333  uint8_t *dst = ptr;
334 
335  for (j = 0; j < avctx->width; j++) {
336  dst[0] = src[rgb[2]];
337  dst[1] = src[rgb[1]];
338  dst[2] = src[rgb[0]];
339  dst += 3;
340  src += 4;
341  }
342 
343  buf += n;
344  ptr += linesize;
345  }
346  break;
347  default:
348  av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
349  return -1;
350  }
351  }
352 
353  *picture = s->picture;
354  *got_frame = 1;
355 
356  return buf_size;
357 }
358 
360 {
361  BMPContext* c = avctx->priv_data;
362 
363  if (c->picture.data[0])
364  avctx->release_buffer(avctx, &c->picture);
365 
366  return 0;
367 }
368 
370  .name = "bmp",
371  .type = AVMEDIA_TYPE_VIDEO,
372  .id = AV_CODEC_ID_BMP,
373  .priv_data_size = sizeof(BMPContext),
377  .capabilities = CODEC_CAP_DR1,
378  .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
379 };
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
AVFrame picture
Definition: bmp.h:28
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
four components are given, that's all.
Definition: avcodec.h:3153
AVCodec.
Definition: avcodec.h:2960
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
Definition: bmp.h:27
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
Definition: bmp.h:33
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
struct BMPContext BMPContext
static int bmp_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: bmp.c:38
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
Definition: bmp.h:34
static AVFrame * picture
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int width
picture width / height.
Definition: avcodec.h:1508
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
static av_cold int bmp_decode_end(AVCodecContext *avctx)
Definition: bmp.c:359
static int width
Definition: utils.c:156
external API header
#define av_le2ne16(x)
Definition: bswap.h:95
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static av_cold int bmp_decode_init(AVCodecContext *avctx)
Definition: bmp.c:28
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:604
Definition: bmp.h:32
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:72
Y , 8bpp.
Definition: pixfmt.h:73
common internal api header.
BiCompression
Definition: bmp.h:31
void * priv_data
Definition: avcodec.h:1382
int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth, GetByteContext *gb)
Decode stream in MS RLE format into frame.
Definition: msrledec.c:246
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
AVCodec ff_bmp_decoder
Definition: bmp.c:369
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:74
This structure stores compressed data.
Definition: avcodec.h:898