Libav 0.7.1
|
00001 /* 00002 * BMP image format encoder 00003 * Copyright (c) 2006, 2007 Michel Bardiaux 00004 * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu> 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include "libavutil/imgutils.h" 00024 #include "avcodec.h" 00025 #include "bytestream.h" 00026 #include "bmp.h" 00027 00028 static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF }; 00029 static const uint32_t rgb565_masks[] = { 0xF800, 0x07E0, 0x001F }; 00030 00031 static av_cold int bmp_encode_init(AVCodecContext *avctx){ 00032 BMPContext *s = avctx->priv_data; 00033 00034 avcodec_get_frame_defaults((AVFrame*)&s->picture); 00035 avctx->coded_frame = (AVFrame*)&s->picture; 00036 00037 switch (avctx->pix_fmt) { 00038 case PIX_FMT_BGR24: 00039 avctx->bits_per_coded_sample = 24; 00040 break; 00041 case PIX_FMT_RGB555: 00042 avctx->bits_per_coded_sample = 16; 00043 break; 00044 case PIX_FMT_RGB565: 00045 avctx->bits_per_coded_sample = 16; 00046 break; 00047 case PIX_FMT_RGB8: 00048 case PIX_FMT_BGR8: 00049 case PIX_FMT_RGB4_BYTE: 00050 case PIX_FMT_BGR4_BYTE: 00051 case PIX_FMT_GRAY8: 00052 case PIX_FMT_PAL8: 00053 avctx->bits_per_coded_sample = 8; 00054 break; 00055 case PIX_FMT_MONOBLACK: 00056 avctx->bits_per_coded_sample = 1; 00057 break; 00058 default: 00059 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n"); 00060 return -1; 00061 } 00062 00063 return 0; 00064 } 00065 00066 static int bmp_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ 00067 BMPContext *s = avctx->priv_data; 00068 AVFrame *pict = data; 00069 AVFrame * const p= (AVFrame*)&s->picture; 00070 int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize; 00071 const uint32_t *pal = NULL; 00072 int pad_bytes_per_row, pal_entries = 0, compression = BMP_RGB; 00073 int bit_count = avctx->bits_per_coded_sample; 00074 uint8_t *ptr; 00075 unsigned char* buf0 = buf; 00076 *p = *pict; 00077 p->pict_type= AV_PICTURE_TYPE_I; 00078 p->key_frame= 1; 00079 switch (avctx->pix_fmt) { 00080 case PIX_FMT_RGB565: 00081 compression = BMP_BITFIELDS; 00082 pal = rgb565_masks; // abuse pal to hold color masks 00083 pal_entries = 3; 00084 break; 00085 case PIX_FMT_RGB8: 00086 case PIX_FMT_BGR8: 00087 case PIX_FMT_RGB4_BYTE: 00088 case PIX_FMT_BGR4_BYTE: 00089 case PIX_FMT_GRAY8: 00090 ff_set_systematic_pal2((uint32_t*)p->data[1], avctx->pix_fmt); 00091 case PIX_FMT_PAL8: 00092 pal = (uint32_t *)p->data[1]; 00093 break; 00094 case PIX_FMT_MONOBLACK: 00095 pal = monoblack_pal; 00096 break; 00097 } 00098 if (pal && !pal_entries) pal_entries = 1 << bit_count; 00099 n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL; 00100 pad_bytes_per_row = (4 - n_bytes_per_row) & 3; 00101 n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row); 00102 00103 // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER 00104 // and related pages. 00105 #define SIZE_BITMAPFILEHEADER 14 00106 #define SIZE_BITMAPINFOHEADER 40 00107 hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2); 00108 n_bytes = n_bytes_image + hsize; 00109 if(n_bytes>buf_size) { 00110 av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", n_bytes, buf_size); 00111 return -1; 00112 } 00113 bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType 00114 bytestream_put_byte(&buf, 'M'); // do. 00115 bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize 00116 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1 00117 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2 00118 bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits 00119 bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize 00120 bytestream_put_le32(&buf, avctx->width); // BITMAPINFOHEADER.biWidth 00121 bytestream_put_le32(&buf, avctx->height); // BITMAPINFOHEADER.biHeight 00122 bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes 00123 bytestream_put_le16(&buf, bit_count); // BITMAPINFOHEADER.biBitCount 00124 bytestream_put_le32(&buf, compression); // BITMAPINFOHEADER.biCompression 00125 bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage 00126 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter 00127 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter 00128 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed 00129 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant 00130 for (i = 0; i < pal_entries; i++) 00131 bytestream_put_le32(&buf, pal[i] & 0xFFFFFF); 00132 // BMP files are bottom-to-top so we start from the end... 00133 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0]; 00134 buf = buf0 + hsize; 00135 for(i = 0; i < avctx->height; i++) { 00136 if (bit_count == 16) { 00137 const uint16_t *src = (const uint16_t *) ptr; 00138 uint16_t *dst = (uint16_t *) buf; 00139 for(n = 0; n < avctx->width; n++) 00140 AV_WL16(dst + n, src[n]); 00141 } else { 00142 memcpy(buf, ptr, n_bytes_per_row); 00143 } 00144 buf += n_bytes_per_row; 00145 memset(buf, 0, pad_bytes_per_row); 00146 buf += pad_bytes_per_row; 00147 ptr -= p->linesize[0]; // ... and go back 00148 } 00149 return n_bytes; 00150 } 00151 00152 AVCodec ff_bmp_encoder = { 00153 "bmp", 00154 AVMEDIA_TYPE_VIDEO, 00155 CODEC_ID_BMP, 00156 sizeof(BMPContext), 00157 bmp_encode_init, 00158 bmp_encode_frame, 00159 NULL, //encode_end, 00160 .pix_fmts = (const enum PixelFormat[]){ 00161 PIX_FMT_BGR24, 00162 PIX_FMT_RGB555, PIX_FMT_RGB565, 00163 PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8, 00164 PIX_FMT_MONOBLACK, 00165 PIX_FMT_NONE}, 00166 .long_name = NULL_IF_CONFIG_SMALL("BMP image"), 00167 };