• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 FFmpeg.
00007  *
00008  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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; // abuse pal to hold color 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     // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
00086     // and related pages.
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');                   // BITMAPFILEHEADER.bfType
00096     bytestream_put_byte(&buf, 'M');                   // do.
00097     bytestream_put_le32(&buf, n_bytes);               // BITMAPFILEHEADER.bfSize
00098     bytestream_put_le16(&buf, 0);                     // BITMAPFILEHEADER.bfReserved1
00099     bytestream_put_le16(&buf, 0);                     // BITMAPFILEHEADER.bfReserved2
00100     bytestream_put_le32(&buf, hsize);                 // BITMAPFILEHEADER.bfOffBits
00101     bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize
00102     bytestream_put_le32(&buf, avctx->width);          // BITMAPINFOHEADER.biWidth
00103     bytestream_put_le32(&buf, avctx->height);         // BITMAPINFOHEADER.biHeight
00104     bytestream_put_le16(&buf, 1);                     // BITMAPINFOHEADER.biPlanes
00105     bytestream_put_le16(&buf, bit_count);             // BITMAPINFOHEADER.biBitCount
00106     bytestream_put_le32(&buf, compression);           // BITMAPINFOHEADER.biCompression
00107     bytestream_put_le32(&buf, n_bytes_image);         // BITMAPINFOHEADER.biSizeImage
00108     bytestream_put_le32(&buf, 0);                     // BITMAPINFOHEADER.biXPelsPerMeter
00109     bytestream_put_le32(&buf, 0);                     // BITMAPINFOHEADER.biYPelsPerMeter
00110     bytestream_put_le32(&buf, 0);                     // BITMAPINFOHEADER.biClrUsed
00111     bytestream_put_le32(&buf, 0);                     // BITMAPINFOHEADER.biClrImportant
00112     for (i = 0; i < pal_entries; i++)
00113         bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
00114     // BMP files are bottom-to-top so we start from the end...
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]; // ... and go back
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, //encode_end,
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 };

Generated on Fri Sep 16 2011 17:17:34 for FFmpeg by  doxygen 1.7.1