FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
bmpenc.c
Go to the documentation of this file.
1 /*
2  * BMP image format encoder
3  * Copyright (c) 2006, 2007 Michel Bardiaux
4  * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/imgutils.h"
24 #include "libavutil/avassert.h"
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "bmp.h"
28 #include "internal.h"
29 
30 static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
31 static const uint32_t rgb565_masks[] = { 0xF800, 0x07E0, 0x001F };
32 static const uint32_t rgb444_masks[] = { 0x0F00, 0x00F0, 0x000F };
33 
35  BMPContext *s = avctx->priv_data;
36 
38  avctx->coded_frame = &s->picture;
39 
40  switch (avctx->pix_fmt) {
41  case AV_PIX_FMT_BGRA:
42  avctx->bits_per_coded_sample = 32;
43  break;
44  case AV_PIX_FMT_BGR24:
45  avctx->bits_per_coded_sample = 24;
46  break;
47  case AV_PIX_FMT_RGB555:
48  case AV_PIX_FMT_RGB565:
49  case AV_PIX_FMT_RGB444:
50  avctx->bits_per_coded_sample = 16;
51  break;
52  case AV_PIX_FMT_RGB8:
53  case AV_PIX_FMT_BGR8:
56  case AV_PIX_FMT_GRAY8:
57  case AV_PIX_FMT_PAL8:
58  avctx->bits_per_coded_sample = 8;
59  break;
61  avctx->bits_per_coded_sample = 1;
62  break;
63  default:
64  av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
65  return -1;
66  }
67 
68  return 0;
69 }
70 
72  const AVFrame *pict, int *got_packet)
73 {
74  BMPContext *s = avctx->priv_data;
75  AVFrame * const p = &s->picture;
76  int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize, ret;
77  const uint32_t *pal = NULL;
78  uint32_t palette256[256];
79  int pad_bytes_per_row, pal_entries = 0, compression = BMP_RGB;
80  int bit_count = avctx->bits_per_coded_sample;
81  uint8_t *ptr, *buf;
82  *p = *pict;
84  p->key_frame= 1;
85  switch (avctx->pix_fmt) {
86  case AV_PIX_FMT_RGB444:
87  compression = BMP_BITFIELDS;
88  pal = rgb444_masks; // abuse pal to hold color masks
89  pal_entries = 3;
90  break;
91  case AV_PIX_FMT_RGB565:
92  compression = BMP_BITFIELDS;
93  pal = rgb565_masks; // abuse pal to hold color masks
94  pal_entries = 3;
95  break;
96  case AV_PIX_FMT_RGB8:
97  case AV_PIX_FMT_BGR8:
100  case AV_PIX_FMT_GRAY8:
101  av_assert1(bit_count == 8);
102  avpriv_set_systematic_pal2(palette256, avctx->pix_fmt);
103  pal = palette256;
104  break;
105  case AV_PIX_FMT_PAL8:
106  pal = (uint32_t *)p->data[1];
107  break;
109  pal = monoblack_pal;
110  break;
111  }
112  if (pal && !pal_entries) pal_entries = 1 << bit_count;
113  n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL;
114  pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
115  n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row);
116 
117  // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
118  // and related pages.
119 #define SIZE_BITMAPFILEHEADER 14
120 #define SIZE_BITMAPINFOHEADER 40
121  hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
122  n_bytes = n_bytes_image + hsize;
123  if ((ret = ff_alloc_packet2(avctx, pkt, n_bytes)) < 0)
124  return ret;
125  buf = pkt->data;
126  bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType
127  bytestream_put_byte(&buf, 'M'); // do.
128  bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize
129  bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1
130  bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2
131  bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits
132  bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize
133  bytestream_put_le32(&buf, avctx->width); // BITMAPINFOHEADER.biWidth
134  bytestream_put_le32(&buf, avctx->height); // BITMAPINFOHEADER.biHeight
135  bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes
136  bytestream_put_le16(&buf, bit_count); // BITMAPINFOHEADER.biBitCount
137  bytestream_put_le32(&buf, compression); // BITMAPINFOHEADER.biCompression
138  bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage
139  bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter
140  bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter
141  bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed
142  bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant
143  for (i = 0; i < pal_entries; i++)
144  bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
145  // BMP files are bottom-to-top so we start from the end...
146  ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
147  buf = pkt->data + hsize;
148  for(i = 0; i < avctx->height; i++) {
149  if (bit_count == 16) {
150  const uint16_t *src = (const uint16_t *) ptr;
151  uint16_t *dst = (uint16_t *) buf;
152  for(n = 0; n < avctx->width; n++)
153  AV_WL16(dst + n, src[n]);
154  } else {
155  memcpy(buf, ptr, n_bytes_per_row);
156  }
157  buf += n_bytes_per_row;
158  memset(buf, 0, pad_bytes_per_row);
159  buf += pad_bytes_per_row;
160  ptr -= p->linesize[0]; // ... and go back
161  }
162 
163  pkt->flags |= AV_PKT_FLAG_KEY;
164  *got_packet = 1;
165  return 0;
166 }
167 
169  .name = "bmp",
170  .type = AVMEDIA_TYPE_VIDEO,
171  .id = AV_CODEC_ID_BMP,
172  .priv_data_size = sizeof(BMPContext),
174  .encode2 = bmp_encode_frame,
175  .pix_fmts = (const enum AVPixelFormat[]){
181  },
182  .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
183 };