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 #include "internal.h"
00029
00030 static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
00031 static const uint32_t rgb565_masks[] = { 0xF800, 0x07E0, 0x001F };
00032 static const uint32_t rgb444_masks[] = { 0x0F00, 0x00F0, 0x000F };
00033
00034 static av_cold int bmp_encode_init(AVCodecContext *avctx){
00035 BMPContext *s = avctx->priv_data;
00036
00037 avcodec_get_frame_defaults(&s->picture);
00038 avctx->coded_frame = &s->picture;
00039
00040 switch (avctx->pix_fmt) {
00041 case PIX_FMT_BGRA:
00042 avctx->bits_per_coded_sample = 32;
00043 break;
00044 case PIX_FMT_BGR24:
00045 avctx->bits_per_coded_sample = 24;
00046 break;
00047 case PIX_FMT_RGB555:
00048 case PIX_FMT_RGB565:
00049 case PIX_FMT_RGB444:
00050 avctx->bits_per_coded_sample = 16;
00051 break;
00052 case PIX_FMT_RGB8:
00053 case PIX_FMT_BGR8:
00054 case PIX_FMT_RGB4_BYTE:
00055 case PIX_FMT_BGR4_BYTE:
00056 case PIX_FMT_GRAY8:
00057 case PIX_FMT_PAL8:
00058 avctx->bits_per_coded_sample = 8;
00059 break;
00060 case PIX_FMT_MONOBLACK:
00061 avctx->bits_per_coded_sample = 1;
00062 break;
00063 default:
00064 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
00065 return -1;
00066 }
00067
00068 return 0;
00069 }
00070
00071 static int bmp_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00072 const AVFrame *pict, int *got_packet)
00073 {
00074 BMPContext *s = avctx->priv_data;
00075 AVFrame * const p = &s->picture;
00076 int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize, ret;
00077 const uint32_t *pal = NULL;
00078 uint32_t palette256[256];
00079 int pad_bytes_per_row, pal_entries = 0, compression = BMP_RGB;
00080 int bit_count = avctx->bits_per_coded_sample;
00081 uint8_t *ptr, *buf;
00082 *p = *pict;
00083 p->pict_type= AV_PICTURE_TYPE_I;
00084 p->key_frame= 1;
00085 switch (avctx->pix_fmt) {
00086 case PIX_FMT_RGB444:
00087 compression = BMP_BITFIELDS;
00088 pal = rgb444_masks;
00089 pal_entries = 3;
00090 break;
00091 case PIX_FMT_RGB565:
00092 compression = BMP_BITFIELDS;
00093 pal = rgb565_masks;
00094 pal_entries = 3;
00095 break;
00096 case PIX_FMT_RGB8:
00097 case PIX_FMT_BGR8:
00098 case PIX_FMT_RGB4_BYTE:
00099 case PIX_FMT_BGR4_BYTE:
00100 case PIX_FMT_GRAY8:
00101 assert(bit_count == 8);
00102 ff_set_systematic_pal2(palette256, avctx->pix_fmt);
00103 pal = palette256;
00104 break;
00105 case PIX_FMT_PAL8:
00106 pal = (uint32_t *)p->data[1];
00107 break;
00108 case PIX_FMT_MONOBLACK:
00109 pal = monoblack_pal;
00110 break;
00111 }
00112 if (pal && !pal_entries) pal_entries = 1 << bit_count;
00113 n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL;
00114 pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
00115 n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row);
00116
00117
00118
00119 #define SIZE_BITMAPFILEHEADER 14
00120 #define SIZE_BITMAPINFOHEADER 40
00121 hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
00122 n_bytes = n_bytes_image + hsize;
00123 if ((ret = ff_alloc_packet2(avctx, pkt, n_bytes)) < 0)
00124 return ret;
00125 buf = pkt->data;
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 = pkt->data + 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
00163 pkt->flags |= AV_PKT_FLAG_KEY;
00164 *got_packet = 1;
00165 return 0;
00166 }
00167
00168 AVCodec ff_bmp_encoder = {
00169 .name = "bmp",
00170 .type = AVMEDIA_TYPE_VIDEO,
00171 .id = CODEC_ID_BMP,
00172 .priv_data_size = sizeof(BMPContext),
00173 .init = bmp_encode_init,
00174 .encode2 = bmp_encode_frame,
00175 .pix_fmts = (const enum PixelFormat[]){
00176 PIX_FMT_BGRA, PIX_FMT_BGR24,
00177 PIX_FMT_RGB565, PIX_FMT_RGB555, PIX_FMT_RGB444,
00178 PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8,
00179 PIX_FMT_MONOBLACK,
00180 PIX_FMT_NONE
00181 },
00182 .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
00183 };