2 * BMP image format encoder
3 * Copyright (c) 2006, 2007 Michel Bardiaux
4 * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
6 * This file is part of Libav.
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.
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.
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
23 #include "libavutil/imgutils.h"
25 #include "bytestream.h"
28 static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
29 static const uint32_t rgb565_masks[] = { 0xF800, 0x07E0, 0x001F };
31 static av_cold int bmp_encode_init(AVCodecContext *avctx){
32 BMPContext *s = avctx->priv_data;
34 avcodec_get_frame_defaults((AVFrame*)&s->picture);
35 avctx->coded_frame = (AVFrame*)&s->picture;
37 switch (avctx->pix_fmt) {
39 avctx->bits_per_coded_sample = 24;
42 avctx->bits_per_coded_sample = 16;
45 avctx->bits_per_coded_sample = 16;
49 case PIX_FMT_RGB4_BYTE:
50 case PIX_FMT_BGR4_BYTE:
53 avctx->bits_per_coded_sample = 8;
55 case PIX_FMT_MONOBLACK:
56 avctx->bits_per_coded_sample = 1;
59 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
66 static int bmp_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
67 BMPContext *s = avctx->priv_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;
75 unsigned char* buf0 = buf;
77 p->pict_type= AV_PICTURE_TYPE_I;
79 switch (avctx->pix_fmt) {
81 compression = BMP_BITFIELDS;
82 pal = rgb565_masks; // abuse pal to hold color masks
87 case PIX_FMT_RGB4_BYTE:
88 case PIX_FMT_BGR4_BYTE:
90 ff_set_systematic_pal2((uint32_t*)p->data[1], avctx->pix_fmt);
92 pal = (uint32_t *)p->data[1];
94 case PIX_FMT_MONOBLACK:
98 if (pal && !pal_entries) pal_entries = 1 << bit_count;
99 n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL;
100 pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
101 n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row);
103 // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
104 // and related pages.
105 #define SIZE_BITMAPFILEHEADER 14
106 #define SIZE_BITMAPINFOHEADER 40
107 hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
108 n_bytes = n_bytes_image + hsize;
109 if(n_bytes>buf_size) {
110 av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", n_bytes, buf_size);
113 bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType
114 bytestream_put_byte(&buf, 'M'); // do.
115 bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize
116 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1
117 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2
118 bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits
119 bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize
120 bytestream_put_le32(&buf, avctx->width); // BITMAPINFOHEADER.biWidth
121 bytestream_put_le32(&buf, avctx->height); // BITMAPINFOHEADER.biHeight
122 bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes
123 bytestream_put_le16(&buf, bit_count); // BITMAPINFOHEADER.biBitCount
124 bytestream_put_le32(&buf, compression); // BITMAPINFOHEADER.biCompression
125 bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage
126 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter
127 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter
128 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed
129 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant
130 for (i = 0; i < pal_entries; i++)
131 bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
132 // BMP files are bottom-to-top so we start from the end...
133 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
135 for(i = 0; i < avctx->height; i++) {
136 if (bit_count == 16) {
137 const uint16_t *src = (const uint16_t *) ptr;
138 uint16_t *dst = (uint16_t *) buf;
139 for(n = 0; n < avctx->width; n++)
140 AV_WL16(dst + n, src[n]);
142 memcpy(buf, ptr, n_bytes_per_row);
144 buf += n_bytes_per_row;
145 memset(buf, 0, pad_bytes_per_row);
146 buf += pad_bytes_per_row;
147 ptr -= p->linesize[0]; // ... and go back
152 AVCodec ff_bmp_encoder = {
154 .type = AVMEDIA_TYPE_VIDEO,
156 .priv_data_size = sizeof(BMPContext),
157 .init = bmp_encode_init,
158 .encode = bmp_encode_frame,
159 .pix_fmts = (const enum PixelFormat[]){
161 PIX_FMT_RGB555, PIX_FMT_RGB565,
162 PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8,
165 .long_name = NULL_IF_CONFIG_SMALL("BMP image"),