FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "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, ret;
53  uint32_t rgb[3] = {0};
54  uint32_t alpha = 0;
55  uint8_t *ptr;
56  int dsize;
57  const uint8_t *buf0 = buf;
58  GetByteContext gb;
59 
60  if (buf_size < 14) {
61  av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
62  return AVERROR_INVALIDDATA;
63  }
64 
65  if (bytestream_get_byte(&buf) != 'B' ||
66  bytestream_get_byte(&buf) != 'M') {
67  av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
68  return AVERROR_INVALIDDATA;
69  }
70 
71  fsize = bytestream_get_le32(&buf);
72  if (buf_size < fsize) {
73  av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
74  buf_size, fsize);
75  fsize = buf_size;
76  }
77 
78  buf += 2; /* reserved1 */
79  buf += 2; /* reserved2 */
80 
81  hsize = bytestream_get_le32(&buf); /* header size */
82  ihsize = bytestream_get_le32(&buf); /* more header size */
83  if (ihsize + 14 > hsize) {
84  av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
85  return AVERROR_INVALIDDATA;
86  }
87 
88  /* sometimes file size is set to some headers size, set a real size in that case */
89  if (fsize == 14 || fsize == ihsize + 14)
90  fsize = buf_size - 2;
91 
92  if (fsize <= hsize) {
93  av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
94  fsize, hsize);
95  return AVERROR_INVALIDDATA;
96  }
97 
98  switch (ihsize) {
99  case 40: // windib
100  case 56: // windib v3
101  case 64: // OS/2 v2
102  case 108: // windib v4
103  case 124: // windib v5
104  width = bytestream_get_le32(&buf);
105  height = bytestream_get_le32(&buf);
106  break;
107  case 12: // OS/2 v1
108  width = bytestream_get_le16(&buf);
109  height = bytestream_get_le16(&buf);
110  break;
111  default:
112  av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
113  return AVERROR_PATCHWELCOME;
114  }
115 
116  /* planes */
117  if (bytestream_get_le16(&buf) != 1) {
118  av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
119  return AVERROR_INVALIDDATA;
120  }
121 
122  depth = bytestream_get_le16(&buf);
123 
124  if (ihsize >= 40)
125  comp = bytestream_get_le32(&buf);
126  else
127  comp = BMP_RGB;
128 
129  if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
130  comp != BMP_RLE8) {
131  av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
132  return AVERROR_INVALIDDATA;
133  }
134 
135  if (comp == BMP_BITFIELDS) {
136  buf += 20;
137  rgb[0] = bytestream_get_le32(&buf);
138  rgb[1] = bytestream_get_le32(&buf);
139  rgb[2] = bytestream_get_le32(&buf);
140  alpha = bytestream_get_le32(&buf);
141  }
142 
143  avctx->width = width;
144  avctx->height = height > 0 ? height : -height;
145 
146  avctx->pix_fmt = AV_PIX_FMT_NONE;
147 
148  switch (depth) {
149  case 32:
150  if (comp == BMP_BITFIELDS) {
151  if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
152  avctx->pix_fmt = alpha ? AV_PIX_FMT_ABGR : AV_PIX_FMT_0BGR;
153  else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
154  avctx->pix_fmt = alpha ? AV_PIX_FMT_BGRA : AV_PIX_FMT_BGR0;
155  else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
156  avctx->pix_fmt = alpha ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
157  else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
158  avctx->pix_fmt = alpha ? AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB0;
159  else {
160  av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
161  return AVERROR(EINVAL);
162  }
163  } else {
164  avctx->pix_fmt = AV_PIX_FMT_BGRA;
165  }
166  break;
167  case 24:
168  avctx->pix_fmt = AV_PIX_FMT_BGR24;
169  break;
170  case 16:
171  if (comp == BMP_RGB)
172  avctx->pix_fmt = AV_PIX_FMT_RGB555;
173  else if (comp == BMP_BITFIELDS) {
174  if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
175  avctx->pix_fmt = AV_PIX_FMT_RGB565;
176  else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
177  avctx->pix_fmt = AV_PIX_FMT_RGB555;
178  else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
179  avctx->pix_fmt = AV_PIX_FMT_RGB444;
180  else {
181  av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", 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 %d-colour BMP\n", 1<<depth);
198  return AVERROR_INVALIDDATA;
199  }
200  break;
201  default:
202  av_log(avctx, AV_LOG_ERROR, "depth %d 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  if (p->data[0])
212  avctx->release_buffer(avctx, p);
213 
214  p->reference = 0;
215  if ((ret = ff_get_buffer(avctx, p)) < 0) {
216  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
217  return ret;
218  }
220  p->key_frame = 1;
221 
222  buf = buf0 + hsize;
223  dsize = buf_size - hsize;
224 
225  /* Line size in file multiple of 4 */
226  n = ((avctx->width * depth + 31) / 8) & ~3;
227 
228  if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
229  av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
230  dsize, n * avctx->height);
231  return AVERROR_INVALIDDATA;
232  }
233 
234  // RLE may skip decoding some picture areas, so blank picture before decoding
235  if (comp == BMP_RLE4 || comp == BMP_RLE8)
236  memset(p->data[0], 0, avctx->height * p->linesize[0]);
237 
238  if (height > 0) {
239  ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
240  linesize = -p->linesize[0];
241  } else {
242  ptr = p->data[0];
243  linesize = p->linesize[0];
244  }
245 
246  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
247  int colors = 1 << depth;
248 
249  memset(p->data[1], 0, 1024);
250 
251  if (ihsize >= 36) {
252  int t;
253  buf = buf0 + 46;
254  t = bytestream_get_le32(&buf);
255  if (t < 0 || t > (1 << depth)) {
256  av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
257  } else if (t) {
258  colors = t;
259  }
260  }
261  buf = buf0 + 14 + ihsize; //palette location
262  // OS/2 bitmap, 3 bytes per palette entry
263  if ((hsize-ihsize-14) < (colors << 2)) {
264  if ((hsize-ihsize-14) < colors * 3) {
265  av_log(avctx, AV_LOG_ERROR, "palette doesnt fit in packet\n");
266  return AVERROR_INVALIDDATA;
267  }
268  for (i = 0; i < colors; i++)
269  ((uint32_t*)p->data[1])[i] = (0xFFU<<24) | bytestream_get_le24(&buf);
270  } else {
271  for (i = 0; i < colors; i++)
272  ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
273  }
274  buf = buf0 + hsize;
275  }
276  if (comp == BMP_RLE4 || comp == BMP_RLE8) {
277  if (height < 0) {
278  p->data[0] += p->linesize[0] * (avctx->height - 1);
279  p->linesize[0] = -p->linesize[0];
280  }
281  bytestream2_init(&gb, buf, dsize);
282  ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
283  if (height < 0) {
284  p->data[0] += p->linesize[0] * (avctx->height - 1);
285  p->linesize[0] = -p->linesize[0];
286  }
287  } else {
288  switch (depth) {
289  case 1:
290  for (i = 0; i < avctx->height; i++) {
291  int j;
292  for (j = 0; j < n; j++) {
293  ptr[j*8+0] = buf[j] >> 7;
294  ptr[j*8+1] = (buf[j] >> 6) & 1;
295  ptr[j*8+2] = (buf[j] >> 5) & 1;
296  ptr[j*8+3] = (buf[j] >> 4) & 1;
297  ptr[j*8+4] = (buf[j] >> 3) & 1;
298  ptr[j*8+5] = (buf[j] >> 2) & 1;
299  ptr[j*8+6] = (buf[j] >> 1) & 1;
300  ptr[j*8+7] = buf[j] & 1;
301  }
302  buf += n;
303  ptr += linesize;
304  }
305  break;
306  case 8:
307  case 24:
308  case 32:
309  for (i = 0; i < avctx->height; i++) {
310  memcpy(ptr, buf, n);
311  buf += n;
312  ptr += linesize;
313  }
314  break;
315  case 4:
316  for (i = 0; i < avctx->height; i++) {
317  int j;
318  for (j = 0; j < n; j++) {
319  ptr[j*2+0] = (buf[j] >> 4) & 0xF;
320  ptr[j*2+1] = buf[j] & 0xF;
321  }
322  buf += n;
323  ptr += linesize;
324  }
325  break;
326  case 16:
327  for (i = 0; i < avctx->height; i++) {
328  const uint16_t *src = (const uint16_t *) buf;
329  uint16_t *dst = (uint16_t *) ptr;
330 
331  for (j = 0; j < avctx->width; j++)
332  *dst++ = av_le2ne16(*src++);
333 
334  buf += n;
335  ptr += linesize;
336  }
337  break;
338  default:
339  av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
340  return AVERROR_INVALIDDATA;
341  }
342  }
343 
344  *picture = s->picture;
345  *got_frame = 1;
346 
347  return buf_size;
348 }
349 
351 {
352  BMPContext* c = avctx->priv_data;
353 
354  if (c->picture.data[0])
355  avctx->release_buffer(avctx, &c->picture);
356 
357  return 0;
358 }
359 
361  .name = "bmp",
362  .type = AVMEDIA_TYPE_VIDEO,
363  .id = AV_CODEC_ID_BMP,
364  .priv_data_size = sizeof(BMPContext),
368  .capabilities = CODEC_CAP_DR1,
369  .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
370 };