00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "bytestream.h"
00024 #include "bmp.h"
00025
00026 static int bmp_decode_init(AVCodecContext *avctx){
00027 BMPContext *s = avctx->priv_data;
00028
00029 avcodec_get_frame_defaults((AVFrame*)&s->picture);
00030 avctx->coded_frame = (AVFrame*)&s->picture;
00031
00032 return 0;
00033 }
00034
00035 static int bmp_decode_frame(AVCodecContext *avctx,
00036 void *data, int *data_size,
00037 const uint8_t *buf, int buf_size)
00038 {
00039 BMPContext *s = avctx->priv_data;
00040 AVFrame *picture = data;
00041 AVFrame *p = &s->picture;
00042 unsigned int fsize, hsize;
00043 int width, height;
00044 unsigned int depth;
00045 BiCompression comp;
00046 unsigned int ihsize;
00047 int i, j, n, linesize;
00048 uint32_t rgb[3];
00049 uint8_t *ptr;
00050 int dsize;
00051 const uint8_t *buf0 = buf;
00052
00053 if(buf_size < 14){
00054 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
00055 return -1;
00056 }
00057
00058 if(bytestream_get_byte(&buf) != 'B' ||
00059 bytestream_get_byte(&buf) != 'M') {
00060 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
00061 return -1;
00062 }
00063
00064 fsize = bytestream_get_le32(&buf);
00065 if(buf_size < fsize){
00066 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
00067 buf_size, fsize);
00068 return -1;
00069 }
00070
00071 buf += 2;
00072 buf += 2;
00073
00074 hsize = bytestream_get_le32(&buf);
00075 if(fsize <= hsize){
00076 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
00077 fsize, hsize);
00078 return -1;
00079 }
00080
00081 ihsize = bytestream_get_le32(&buf);
00082 if(ihsize + 14 > hsize){
00083 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
00084 return -1;
00085 }
00086
00087 width = bytestream_get_le32(&buf);
00088 height = bytestream_get_le32(&buf);
00089
00090 if(bytestream_get_le16(&buf) != 1){
00091 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
00092 return -1;
00093 }
00094
00095 depth = bytestream_get_le16(&buf);
00096
00097 if(ihsize > 16)
00098 comp = bytestream_get_le32(&buf);
00099 else
00100 comp = BMP_RGB;
00101
00102 if(comp != BMP_RGB && comp != BMP_BITFIELDS){
00103 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
00104 return -1;
00105 }
00106
00107 if(comp == BMP_BITFIELDS){
00108 buf += 20;
00109 rgb[0] = bytestream_get_le32(&buf);
00110 rgb[1] = bytestream_get_le32(&buf);
00111 rgb[2] = bytestream_get_le32(&buf);
00112 }
00113
00114 avctx->width = width;
00115 avctx->height = height > 0? height: -height;
00116
00117 avctx->pix_fmt = PIX_FMT_NONE;
00118
00119 switch(depth){
00120 case 32:
00121 if(comp == BMP_BITFIELDS){
00122 rgb[0] = (rgb[0] >> 15) & 3;
00123 rgb[1] = (rgb[1] >> 15) & 3;
00124 rgb[2] = (rgb[2] >> 15) & 3;
00125
00126 if(rgb[0] + rgb[1] + rgb[2] != 3 ||
00127 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
00128 break;
00129 }
00130 } else {
00131 rgb[0] = 2;
00132 rgb[1] = 1;
00133 rgb[2] = 0;
00134 }
00135
00136 avctx->pix_fmt = PIX_FMT_BGR24;
00137 break;
00138 case 24:
00139 avctx->pix_fmt = PIX_FMT_BGR24;
00140 break;
00141 case 16:
00142 if(comp == BMP_RGB)
00143 avctx->pix_fmt = PIX_FMT_RGB555;
00144 break;
00145 default:
00146 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
00147 return -1;
00148 }
00149
00150 if(avctx->pix_fmt == PIX_FMT_NONE){
00151 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
00152 return -1;
00153 }
00154
00155 if(p->data[0])
00156 avctx->release_buffer(avctx, p);
00157
00158 p->reference = 0;
00159 if(avctx->get_buffer(avctx, p) < 0){
00160 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00161 return -1;
00162 }
00163 p->pict_type = FF_I_TYPE;
00164 p->key_frame = 1;
00165
00166 buf = buf0 + hsize;
00167 dsize = buf_size - hsize;
00168
00169
00170 n = (avctx->width * (depth / 8) + 3) & ~3;
00171
00172 if(n * avctx->height > dsize){
00173 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
00174 dsize, n * avctx->height);
00175 return -1;
00176 }
00177
00178 if(height > 0){
00179 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
00180 linesize = -p->linesize[0];
00181 } else {
00182 ptr = p->data[0];
00183 linesize = p->linesize[0];
00184 }
00185
00186 switch(depth){
00187 case 24:
00188 for(i = 0; i < avctx->height; i++){
00189 memcpy(ptr, buf, avctx->width*(depth>>3));
00190 buf += n;
00191 ptr += linesize;
00192 }
00193 break;
00194 case 16:
00195 for(i = 0; i < avctx->height; i++){
00196 const uint16_t *src = (const uint16_t *) buf;
00197 uint16_t *dst = (uint16_t *) ptr;
00198
00199 for(j = 0; j < avctx->width; j++)
00200 *dst++ = le2me_16(*src++);
00201
00202 buf += n;
00203 ptr += linesize;
00204 }
00205 break;
00206 case 32:
00207 for(i = 0; i < avctx->height; i++){
00208 const uint8_t *src = buf;
00209 uint8_t *dst = ptr;
00210
00211 for(j = 0; j < avctx->width; j++){
00212 dst[0] = src[rgb[2]];
00213 dst[1] = src[rgb[1]];
00214 dst[2] = src[rgb[0]];
00215 dst += 3;
00216 src += 4;
00217 }
00218
00219 buf += n;
00220 ptr += linesize;
00221 }
00222 break;
00223 default:
00224 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
00225 return -1;
00226 }
00227
00228 *picture = s->picture;
00229 *data_size = sizeof(AVPicture);
00230
00231 return buf_size;
00232 }
00233
00234 static int bmp_decode_end(AVCodecContext *avctx)
00235 {
00236 BMPContext* c = avctx->priv_data;
00237
00238 if (c->picture.data[0])
00239 avctx->release_buffer(avctx, &c->picture);
00240
00241 return 0;
00242 }
00243
00244 AVCodec bmp_decoder = {
00245 "bmp",
00246 CODEC_TYPE_VIDEO,
00247 CODEC_ID_BMP,
00248 sizeof(BMPContext),
00249 bmp_decode_init,
00250 NULL,
00251 bmp_decode_end,
00252 bmp_decode_frame
00253 };