libavcodec/bmpenc.c
Go to the documentation of this file.
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 static const uint32_t rgb444_masks[]  = { 0x0F00, 0x00F0, 0x000F };
00031 
00032 static av_cold int bmp_encode_init(AVCodecContext *avctx){
00033     BMPContext *s = avctx->priv_data;
00034 
00035     avcodec_get_frame_defaults((AVFrame*)&s->picture);
00036     avctx->coded_frame = (AVFrame*)&s->picture;
00037 
00038     switch (avctx->pix_fmt) {
00039     case PIX_FMT_BGR24:
00040         avctx->bits_per_coded_sample = 24;
00041         break;
00042     case PIX_FMT_RGB555:
00043     case PIX_FMT_RGB565:
00044     case PIX_FMT_RGB444:
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_RGB444:
00081         compression = BMP_BITFIELDS;
00082         pal = rgb444_masks; // abuse pal to hold color masks
00083         pal_entries = 3;
00084         break;
00085     case PIX_FMT_RGB565:
00086         compression = BMP_BITFIELDS;
00087         pal = rgb565_masks; // abuse pal to hold color masks
00088         pal_entries = 3;
00089         break;
00090     case PIX_FMT_RGB8:
00091     case PIX_FMT_BGR8:
00092     case PIX_FMT_RGB4_BYTE:
00093     case PIX_FMT_BGR4_BYTE:
00094     case PIX_FMT_GRAY8:
00095         ff_set_systematic_pal2((uint32_t*)p->data[1], avctx->pix_fmt);
00096     case PIX_FMT_PAL8:
00097         pal = (uint32_t *)p->data[1];
00098         break;
00099     case PIX_FMT_MONOBLACK:
00100         pal = monoblack_pal;
00101         break;
00102     }
00103     if (pal && !pal_entries) pal_entries = 1 << bit_count;
00104     n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL;
00105     pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
00106     n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row);
00107 
00108     // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
00109     // and related pages.
00110 #define SIZE_BITMAPFILEHEADER 14
00111 #define SIZE_BITMAPINFOHEADER 40
00112     hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
00113     n_bytes = n_bytes_image + hsize;
00114     if(n_bytes>buf_size) {
00115         av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", n_bytes, buf_size);
00116         return -1;
00117     }
00118     bytestream_put_byte(&buf, 'B');                   // BITMAPFILEHEADER.bfType
00119     bytestream_put_byte(&buf, 'M');                   // do.
00120     bytestream_put_le32(&buf, n_bytes);               // BITMAPFILEHEADER.bfSize
00121     bytestream_put_le16(&buf, 0);                     // BITMAPFILEHEADER.bfReserved1
00122     bytestream_put_le16(&buf, 0);                     // BITMAPFILEHEADER.bfReserved2
00123     bytestream_put_le32(&buf, hsize);                 // BITMAPFILEHEADER.bfOffBits
00124     bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize
00125     bytestream_put_le32(&buf, avctx->width);          // BITMAPINFOHEADER.biWidth
00126     bytestream_put_le32(&buf, avctx->height);         // BITMAPINFOHEADER.biHeight
00127     bytestream_put_le16(&buf, 1);                     // BITMAPINFOHEADER.biPlanes
00128     bytestream_put_le16(&buf, bit_count);             // BITMAPINFOHEADER.biBitCount
00129     bytestream_put_le32(&buf, compression);           // BITMAPINFOHEADER.biCompression
00130     bytestream_put_le32(&buf, n_bytes_image);         // BITMAPINFOHEADER.biSizeImage
00131     bytestream_put_le32(&buf, 0);                     // BITMAPINFOHEADER.biXPelsPerMeter
00132     bytestream_put_le32(&buf, 0);                     // BITMAPINFOHEADER.biYPelsPerMeter
00133     bytestream_put_le32(&buf, 0);                     // BITMAPINFOHEADER.biClrUsed
00134     bytestream_put_le32(&buf, 0);                     // BITMAPINFOHEADER.biClrImportant
00135     for (i = 0; i < pal_entries; i++)
00136         bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
00137     // BMP files are bottom-to-top so we start from the end...
00138     ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
00139     buf = buf0 + hsize;
00140     for(i = 0; i < avctx->height; i++) {
00141         if (bit_count == 16) {
00142             const uint16_t *src = (const uint16_t *) ptr;
00143             uint16_t *dst = (uint16_t *) buf;
00144             for(n = 0; n < avctx->width; n++)
00145                 AV_WL16(dst + n, src[n]);
00146         } else {
00147             memcpy(buf, ptr, n_bytes_per_row);
00148         }
00149         buf += n_bytes_per_row;
00150         memset(buf, 0, pad_bytes_per_row);
00151         buf += pad_bytes_per_row;
00152         ptr -= p->linesize[0]; // ... and go back
00153     }
00154     return n_bytes;
00155 }
00156 
00157 AVCodec ff_bmp_encoder = {
00158     .name           = "bmp",
00159     .type           = AVMEDIA_TYPE_VIDEO,
00160     .id             = CODEC_ID_BMP,
00161     .priv_data_size = sizeof(BMPContext),
00162     .init           = bmp_encode_init,
00163     .encode         = bmp_encode_frame,
00164     .pix_fmts = (const enum PixelFormat[]){
00165         PIX_FMT_BGR24,
00166         PIX_FMT_RGB555, PIX_FMT_RGB444, PIX_FMT_RGB565,
00167         PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8,
00168         PIX_FMT_MONOBLACK,
00169         PIX_FMT_NONE},
00170     .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
00171 };