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