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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 "avcodec.h"
25 #include "bytestream.h"
26 #include "bmp.h"
27 
28 static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
29 static const uint32_t rgb565_masks[] = { 0xF800, 0x07E0, 0x001F };
30 static const uint32_t rgb444_masks[] = { 0x0F00, 0x00F0, 0x000F };
31 
33  BMPContext *s = avctx->priv_data;
34 
36  avctx->coded_frame = (AVFrame*)&s->picture;
37 
38  switch (avctx->pix_fmt) {
39  case PIX_FMT_BGR24:
40  avctx->bits_per_coded_sample = 24;
41  break;
42  case PIX_FMT_RGB555:
43  case PIX_FMT_RGB565:
44  case PIX_FMT_RGB444:
45  avctx->bits_per_coded_sample = 16;
46  break;
47  case PIX_FMT_RGB8:
48  case PIX_FMT_BGR8:
49  case PIX_FMT_RGB4_BYTE:
50  case PIX_FMT_BGR4_BYTE:
51  case PIX_FMT_GRAY8:
52  case PIX_FMT_PAL8:
53  avctx->bits_per_coded_sample = 8;
54  break;
55  case PIX_FMT_MONOBLACK:
56  avctx->bits_per_coded_sample = 1;
57  break;
58  default:
59  av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
60  return -1;
61  }
62 
63  return 0;
64 }
65 
66 static int bmp_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
67  BMPContext *s = avctx->priv_data;
68  AVFrame *pict = data;
69  AVFrame * const p= (AVFrame*)&s->picture;
70  int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize;
71  const uint32_t *pal = NULL;
72  int pad_bytes_per_row, pal_entries = 0, compression = BMP_RGB;
73  int bit_count = avctx->bits_per_coded_sample;
74  uint8_t *ptr;
75  unsigned char* buf0 = buf;
76  *p = *pict;
78  p->key_frame= 1;
79  switch (avctx->pix_fmt) {
80  case PIX_FMT_RGB444:
81  compression = BMP_BITFIELDS;
82  pal = rgb444_masks; // abuse pal to hold color masks
83  pal_entries = 3;
84  break;
85  case PIX_FMT_RGB565:
86  compression = BMP_BITFIELDS;
87  pal = rgb565_masks; // abuse pal to hold color masks
88  pal_entries = 3;
89  break;
90  case PIX_FMT_RGB8:
91  case PIX_FMT_BGR8:
92  case PIX_FMT_RGB4_BYTE:
93  case PIX_FMT_BGR4_BYTE:
94  case PIX_FMT_GRAY8:
95  ff_set_systematic_pal2((uint32_t*)p->data[1], avctx->pix_fmt);
96  case PIX_FMT_PAL8:
97  pal = (uint32_t *)p->data[1];
98  break;
99  case PIX_FMT_MONOBLACK:
100  pal = monoblack_pal;
101  break;
102  }
103  if (pal && !pal_entries) pal_entries = 1 << bit_count;
104  n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL;
105  pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
106  n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row);
107 
108  // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
109  // and related pages.
110 #define SIZE_BITMAPFILEHEADER 14
111 #define SIZE_BITMAPINFOHEADER 40
112  hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
113  n_bytes = n_bytes_image + hsize;
114  if(n_bytes>buf_size) {
115  av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", n_bytes, buf_size);
116  return -1;
117  }
118  bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType
119  bytestream_put_byte(&buf, 'M'); // do.
120  bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize
121  bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1
122  bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2
123  bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits
124  bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize
125  bytestream_put_le32(&buf, avctx->width); // BITMAPINFOHEADER.biWidth
126  bytestream_put_le32(&buf, avctx->height); // BITMAPINFOHEADER.biHeight
127  bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes
128  bytestream_put_le16(&buf, bit_count); // BITMAPINFOHEADER.biBitCount
129  bytestream_put_le32(&buf, compression); // BITMAPINFOHEADER.biCompression
130  bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage
131  bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter
132  bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter
133  bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed
134  bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant
135  for (i = 0; i < pal_entries; i++)
136  bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
137  // BMP files are bottom-to-top so we start from the end...
138  ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
139  buf = buf0 + hsize;
140  for(i = 0; i < avctx->height; i++) {
141  if (bit_count == 16) {
142  const uint16_t *src = (const uint16_t *) ptr;
143  uint16_t *dst = (uint16_t *) buf;
144  for(n = 0; n < avctx->width; n++)
145  AV_WL16(dst + n, src[n]);
146  } else {
147  memcpy(buf, ptr, n_bytes_per_row);
148  }
149  buf += n_bytes_per_row;
150  memset(buf, 0, pad_bytes_per_row);
151  buf += pad_bytes_per_row;
152  ptr -= p->linesize[0]; // ... and go back
153  }
154  return n_bytes;
155 }
156 
158  .name = "bmp",
159  .type = AVMEDIA_TYPE_VIDEO,
160  .id = CODEC_ID_BMP,
161  .priv_data_size = sizeof(BMPContext),
163  .encode = bmp_encode_frame,
164  .pix_fmts = (const enum PixelFormat[]){
169  PIX_FMT_NONE},
170  .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
171 };
AVCodec ff_bmp_encoder
Definition: bmpenc.c:157
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
Audio Video Frame.
Definition: avcodec.h:985
misc image utilities
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
AVFrame picture
Definition: bmp.h:28
#define SIZE_BITMAPINFOHEADER
AVCodec.
Definition: avcodec.h:3189
static const uint32_t rgb565_masks[]
Definition: bmpenc.c:29
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:86
Definition: bmp.h:27
#define av_cold
Definition: attributes.h:71
#define PIX_FMT_RGB555
Definition: pixfmt.h:177
const char data[16]
Definition: mxf.c:60
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:74
int ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt)
Definition: imgutils.c:136
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1974
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
struct BMPContext BMPContext
#define SIZE_BITMAPFILEHEADER
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
static const uint32_t monoblack_pal[]
Definition: bmpenc.c:28
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:85
#define PIX_FMT_RGB565
Definition: pixfmt.h:176
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1029
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:88
int width
picture width / height.
Definition: avcodec.h:1408
NULL
Definition: eval.c:50
external API header
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:1329
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static av_cold int bmp_encode_init(AVCodecContext *avctx)
Definition: bmpenc.c:32
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:83
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:67
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:73
PixelFormat
Pixel format.
Definition: pixfmt.h:62
static int bmp_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
Definition: bmpenc.c:66
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:75
#define PIX_FMT_RGB444
Definition: pixfmt.h:178
void * priv_data
Definition: avcodec.h:1531
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1022
#define AV_LOG_INFO
Definition: log.h:119
void avcodec_get_frame_defaults(AVFrame *pic)
Set the fields of the given AVFrame to default values.
Definition: utils.c:609
Y , 8bpp.
Definition: pixfmt.h:72
#define AV_WL16(p, d)
Definition: intreadwrite.h:225
static const uint32_t rgb444_masks[]
Definition: bmpenc.c:30