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 FFmpeg.
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.
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.
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
23 #include "libavutil/imgutils.h"
25 #include "bytestream.h"
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 };
34 static av_cold int bmp_encode_init(AVCodecContext *avctx){
35 BMPContext *s = avctx->priv_data;
37 avcodec_get_frame_defaults(&s->picture);
38 avctx->coded_frame = &s->picture;
40 switch (avctx->pix_fmt) {
42 avctx->bits_per_coded_sample = 32;
45 avctx->bits_per_coded_sample = 24;
50 avctx->bits_per_coded_sample = 16;
54 case PIX_FMT_RGB4_BYTE:
55 case PIX_FMT_BGR4_BYTE:
58 avctx->bits_per_coded_sample = 8;
60 case PIX_FMT_MONOBLACK:
61 avctx->bits_per_coded_sample = 1;
64 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
71 static int bmp_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
72 const AVFrame *pict, int *got_packet)
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;
83 p->pict_type= AV_PICTURE_TYPE_I;
85 switch (avctx->pix_fmt) {
87 compression = BMP_BITFIELDS;
88 pal = rgb444_masks; // abuse pal to hold color masks
92 compression = BMP_BITFIELDS;
93 pal = rgb565_masks; // abuse pal to hold color masks
98 case PIX_FMT_RGB4_BYTE:
99 case PIX_FMT_BGR4_BYTE:
101 assert(bit_count == 8);
102 ff_set_systematic_pal2(palette256, avctx->pix_fmt);
106 pal = (uint32_t *)p->data[1];
108 case PIX_FMT_MONOBLACK:
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);
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)
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]);
155 memcpy(buf, ptr, n_bytes_per_row);
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
163 pkt->flags |= AV_PKT_FLAG_KEY;
168 AVCodec ff_bmp_encoder = {
170 .type = AVMEDIA_TYPE_VIDEO,
172 .priv_data_size = sizeof(BMPContext),
173 .init = bmp_encode_init,
174 .encode2 = bmp_encode_frame,
175 .pix_fmts = (const enum PixelFormat[]){
176 PIX_FMT_BGRA, PIX_FMT_BGR24,
177 PIX_FMT_RGB565, PIX_FMT_RGB555, PIX_FMT_RGB444,
178 PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8,
182 .long_name = NULL_IF_CONFIG_SMALL("BMP image"),