00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "libavutil/imgutils.h"
00024 #include "avcodec.h"
00025 #include "bytestream.h"
00026 #include "bmp.h"
00027 #include <assert.h>
00028
00029 static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
00030 static const uint32_t rgb565_masks[] = { 0xF800, 0x07E0, 0x001F };
00031 static const uint32_t rgb444_masks[] = { 0x0F00, 0x00F0, 0x000F };
00032
00033 static av_cold int bmp_encode_init(AVCodecContext *avctx){
00034 BMPContext *s = avctx->priv_data;
00035
00036 avcodec_get_frame_defaults((AVFrame*)&s->picture);
00037 avctx->coded_frame = (AVFrame*)&s->picture;
00038
00039 switch (avctx->pix_fmt) {
00040 case PIX_FMT_BGRA:
00041 avctx->bits_per_coded_sample = 32;
00042 break;
00043 case PIX_FMT_BGR24:
00044 avctx->bits_per_coded_sample = 24;
00045 break;
00046 case PIX_FMT_RGB555:
00047 case PIX_FMT_RGB565:
00048 case PIX_FMT_RGB444:
00049 avctx->bits_per_coded_sample = 16;
00050 break;
00051 case PIX_FMT_RGB8:
00052 case PIX_FMT_BGR8:
00053 case PIX_FMT_RGB4_BYTE:
00054 case PIX_FMT_BGR4_BYTE:
00055 case PIX_FMT_GRAY8:
00056 case PIX_FMT_PAL8:
00057 avctx->bits_per_coded_sample = 8;
00058 break;
00059 case PIX_FMT_MONOBLACK:
00060 avctx->bits_per_coded_sample = 1;
00061 break;
00062 default:
00063 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
00064 return -1;
00065 }
00066
00067 return 0;
00068 }
00069
00070 static int bmp_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00071 BMPContext *s = avctx->priv_data;
00072 AVFrame *pict = data;
00073 AVFrame * const p= (AVFrame*)&s->picture;
00074 int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize;
00075 const uint32_t *pal = NULL;
00076 uint32_t palette256[256];
00077 int pad_bytes_per_row, pal_entries = 0, compression = BMP_RGB;
00078 int bit_count = avctx->bits_per_coded_sample;
00079 uint8_t *ptr;
00080 unsigned char* buf0 = buf;
00081 *p = *pict;
00082 p->pict_type= AV_PICTURE_TYPE_I;
00083 p->key_frame= 1;
00084 switch (avctx->pix_fmt) {
00085 case PIX_FMT_RGB444:
00086 compression = BMP_BITFIELDS;
00087 pal = rgb444_masks;
00088 pal_entries = 3;
00089 break;
00090 case PIX_FMT_RGB565:
00091 compression = BMP_BITFIELDS;
00092 pal = rgb565_masks;
00093 pal_entries = 3;
00094 break;
00095 case PIX_FMT_RGB8:
00096 case PIX_FMT_BGR8:
00097 case PIX_FMT_RGB4_BYTE:
00098 case PIX_FMT_BGR4_BYTE:
00099 case PIX_FMT_GRAY8:
00100 assert(bit_count == 8);
00101 ff_set_systematic_pal2(palette256, avctx->pix_fmt);
00102 pal = palette256;
00103 break;
00104 case PIX_FMT_PAL8:
00105 pal = (uint32_t *)p->data[1];
00106 break;
00107 case PIX_FMT_MONOBLACK:
00108 pal = monoblack_pal;
00109 break;
00110 }
00111 if (pal && !pal_entries) pal_entries = 1 << bit_count;
00112 n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL;
00113 pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
00114 n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row);
00115
00116
00117
00118 #define SIZE_BITMAPFILEHEADER 14
00119 #define SIZE_BITMAPINFOHEADER 40
00120 hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
00121 n_bytes = n_bytes_image + hsize;
00122 if(n_bytes>buf_size) {
00123 av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", n_bytes, buf_size);
00124 return -1;
00125 }
00126 bytestream_put_byte(&buf, 'B');
00127 bytestream_put_byte(&buf, 'M');
00128 bytestream_put_le32(&buf, n_bytes);
00129 bytestream_put_le16(&buf, 0);
00130 bytestream_put_le16(&buf, 0);
00131 bytestream_put_le32(&buf, hsize);
00132 bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER);
00133 bytestream_put_le32(&buf, avctx->width);
00134 bytestream_put_le32(&buf, avctx->height);
00135 bytestream_put_le16(&buf, 1);
00136 bytestream_put_le16(&buf, bit_count);
00137 bytestream_put_le32(&buf, compression);
00138 bytestream_put_le32(&buf, n_bytes_image);
00139 bytestream_put_le32(&buf, 0);
00140 bytestream_put_le32(&buf, 0);
00141 bytestream_put_le32(&buf, 0);
00142 bytestream_put_le32(&buf, 0);
00143 for (i = 0; i < pal_entries; i++)
00144 bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
00145
00146 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
00147 buf = buf0 + hsize;
00148 for(i = 0; i < avctx->height; i++) {
00149 if (bit_count == 16) {
00150 const uint16_t *src = (const uint16_t *) ptr;
00151 uint16_t *dst = (uint16_t *) buf;
00152 for(n = 0; n < avctx->width; n++)
00153 AV_WL16(dst + n, src[n]);
00154 } else {
00155 memcpy(buf, ptr, n_bytes_per_row);
00156 }
00157 buf += n_bytes_per_row;
00158 memset(buf, 0, pad_bytes_per_row);
00159 buf += pad_bytes_per_row;
00160 ptr -= p->linesize[0];
00161 }
00162 return n_bytes;
00163 }
00164
00165 AVCodec ff_bmp_encoder = {
00166 .name = "bmp",
00167 .type = AVMEDIA_TYPE_VIDEO,
00168 .id = CODEC_ID_BMP,
00169 .priv_data_size = sizeof(BMPContext),
00170 .init = bmp_encode_init,
00171 .encode = bmp_encode_frame,
00172 .pix_fmts = (const enum PixelFormat[]){
00173 PIX_FMT_BGRA, PIX_FMT_BGR24,
00174 PIX_FMT_RGB565, PIX_FMT_RGB555, PIX_FMT_RGB444,
00175 PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8,
00176 PIX_FMT_MONOBLACK,
00177 PIX_FMT_NONE},
00178 .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
00179 };