Mercurial > libavcodec.hg
annotate bmp.c @ 4393:5187883df562 libavcodec
enum
| author | michael |
|---|---|
| date | Wed, 24 Jan 2007 10:41:03 +0000 |
| parents | a90490a13ac4 |
| children | 182949fbc53e |
| rev | line source |
|---|---|
| 2949 | 1 /* |
| 2 * BMP image format | |
| 3 * Copyright (c) 2005 Mans Rullgard | |
| 4 * | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
5 * This file is part of FFmpeg. |
|
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
6 * |
|
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
| 2949 | 8 * modify it under the terms of the GNU Lesser General Public |
| 9 * License as published by the Free Software Foundation; either | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
| 2949 | 11 * |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
| 2949 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 15 * Lesser General Public License for more details. | |
| 16 * | |
| 17 * You should have received a copy of the GNU Lesser General Public | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
|
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2967
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 2949 | 20 */ |
| 21 | |
| 22 #include "avcodec.h" | |
| 23 #include "bitstream.h" | |
| 24 #include "bswap.h" | |
| 25 | |
| 26 typedef struct BMPContext { | |
| 27 AVFrame picture; | |
| 28 } BMPContext; | |
| 29 | |
| 4393 | 30 typedef enum { |
| 31 BMP_RGB=0, | |
| 32 BMP_RLE8, | |
| 33 BMP_RLE4, | |
| 34 BMP_BITFIELDS, | |
| 35 } BiCompression; | |
| 2949 | 36 |
| 37 #define read16(bits) bswap_16(get_bits(bits, 16)) | |
| 38 #define read32(bits) bswap_32(get_bits_long(bits, 32)) | |
| 39 | |
| 40 static int bmp_decode_init(AVCodecContext *avctx){ | |
| 41 BMPContext *s = avctx->priv_data; | |
| 42 | |
| 43 avcodec_get_frame_defaults((AVFrame*)&s->picture); | |
| 44 avctx->coded_frame = (AVFrame*)&s->picture; | |
| 45 | |
| 46 return 0; | |
| 47 } | |
| 48 | |
| 2967 | 49 static int bmp_decode_frame(AVCodecContext *avctx, |
| 2949 | 50 void *data, int *data_size, |
| 51 uint8_t *buf, int buf_size) | |
| 52 { | |
| 53 BMPContext *s = avctx->priv_data; | |
| 54 AVFrame *picture = data; | |
| 55 AVFrame *p = &s->picture; | |
| 56 GetBitContext bits; | |
| 57 unsigned int fsize, hsize; | |
| 58 int width, height; | |
| 59 unsigned int depth; | |
| 4393 | 60 BiCompression comp; |
| 2949 | 61 unsigned int ihsize; |
| 62 int i, j, n, linesize; | |
| 63 uint32_t rgb[3]; | |
| 64 uint8_t *ptr; | |
| 65 int dsize; | |
| 66 | |
| 67 if(buf_size < 14){ | |
| 68 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size); | |
| 69 return -1; | |
| 70 } | |
| 71 | |
| 72 init_get_bits(&bits, buf, buf_size); | |
| 73 | |
| 74 if(get_bits(&bits, 16) != 0x424d){ /* 'BM' */ | |
| 75 av_log(avctx, AV_LOG_ERROR, "bad magic number\n"); | |
| 76 return -1; | |
| 77 } | |
| 78 | |
| 79 fsize = read32(&bits); | |
| 80 if(buf_size < fsize){ | |
| 81 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n", | |
| 82 buf_size, fsize); | |
| 83 return -1; | |
| 84 } | |
| 85 | |
| 86 skip_bits(&bits, 16); /* reserved1 */ | |
| 87 skip_bits(&bits, 16); /* reserved2 */ | |
| 88 | |
| 89 hsize = read32(&bits); /* header size */ | |
| 90 if(fsize <= hsize){ | |
| 91 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n", | |
| 92 fsize, hsize); | |
| 93 return -1; | |
| 94 } | |
| 95 | |
| 96 ihsize = read32(&bits); /* more header size */ | |
| 97 if(ihsize + 14 > hsize){ | |
| 98 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize); | |
| 99 return -1; | |
| 100 } | |
| 101 | |
| 102 width = read32(&bits); | |
| 103 height = read32(&bits); | |
| 104 | |
| 105 if(read16(&bits) != 1){ /* planes */ | |
| 106 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n"); | |
| 107 return -1; | |
| 108 } | |
| 109 | |
| 110 depth = read16(&bits); | |
| 111 | |
| 112 if(ihsize > 16) | |
| 113 comp = read32(&bits); | |
| 114 else | |
| 115 comp = BMP_RGB; | |
| 116 | |
| 117 if(comp != BMP_RGB && comp != BMP_BITFIELDS){ | |
| 118 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp); | |
| 119 return -1; | |
| 120 } | |
| 121 | |
| 122 if(comp == BMP_BITFIELDS){ | |
| 123 skip_bits(&bits, 20 * 8); | |
| 124 rgb[0] = read32(&bits); | |
| 125 rgb[1] = read32(&bits); | |
| 126 rgb[2] = read32(&bits); | |
| 127 } | |
| 128 | |
| 129 avctx->codec_id = CODEC_ID_BMP; | |
| 130 avctx->width = width; | |
| 131 avctx->height = height > 0? height: -height; | |
| 132 | |
| 133 avctx->pix_fmt = PIX_FMT_NONE; | |
| 134 | |
| 135 switch(depth){ | |
| 136 case 32: | |
| 137 if(comp == BMP_BITFIELDS){ | |
| 138 rgb[0] = (rgb[0] >> 15) & 3; | |
| 139 rgb[1] = (rgb[1] >> 15) & 3; | |
| 140 rgb[2] = (rgb[2] >> 15) & 3; | |
| 141 | |
| 142 if(rgb[0] + rgb[1] + rgb[2] != 3 || | |
| 143 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){ | |
| 144 break; | |
| 145 } | |
| 146 } else { | |
| 147 rgb[0] = 2; | |
| 148 rgb[1] = 1; | |
| 149 rgb[2] = 0; | |
| 150 } | |
| 151 | |
| 152 avctx->pix_fmt = PIX_FMT_BGR24; | |
| 153 break; | |
| 154 case 24: | |
| 155 avctx->pix_fmt = PIX_FMT_BGR24; | |
| 156 break; | |
| 157 case 16: | |
| 158 if(comp == BMP_RGB) | |
| 159 avctx->pix_fmt = PIX_FMT_RGB555; | |
| 160 break; | |
| 161 default: | |
| 162 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth); | |
| 163 return -1; | |
| 164 } | |
| 165 | |
| 166 if(avctx->pix_fmt == PIX_FMT_NONE){ | |
| 167 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n"); | |
| 168 return -1; | |
| 169 } | |
| 170 | |
| 171 p->reference = 0; | |
| 172 if(avctx->get_buffer(avctx, p) < 0){ | |
| 173 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
| 174 return -1; | |
| 175 } | |
| 176 p->pict_type = FF_I_TYPE; | |
| 177 p->key_frame = 1; | |
| 178 | |
| 179 buf += hsize; | |
| 180 dsize = buf_size - hsize; | |
| 181 | |
| 4109 | 182 /* Line size in file multiple of 4 */ |
| 183 n = (avctx->width * (depth / 8) + 3) & ~3; | |
| 2949 | 184 |
| 185 if(n * avctx->height > dsize){ | |
| 186 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n", | |
| 187 dsize, n * avctx->height); | |
| 188 return -1; | |
| 189 } | |
| 190 | |
| 191 if(height > 0){ | |
| 192 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0]; | |
| 193 linesize = -p->linesize[0]; | |
| 194 } else { | |
| 195 ptr = p->data[0]; | |
| 196 linesize = p->linesize[0]; | |
| 197 } | |
| 198 | |
| 199 switch(depth){ | |
| 200 case 24: | |
| 201 for(i = 0; i < avctx->height; i++){ | |
| 202 memcpy(ptr, buf, n); | |
| 203 buf += n; | |
| 204 ptr += linesize; | |
| 205 } | |
| 206 break; | |
| 207 case 16: | |
| 208 for(i = 0; i < avctx->height; i++){ | |
| 209 uint16_t *src = (uint16_t *) buf; | |
| 210 uint16_t *dst = (uint16_t *) ptr; | |
| 211 | |
| 212 for(j = 0; j < avctx->width; j++) | |
| 213 *dst++ = le2me_16(*src++); | |
| 214 | |
| 215 buf += n; | |
| 216 ptr += linesize; | |
| 217 } | |
| 218 break; | |
| 219 case 32: | |
| 220 for(i = 0; i < avctx->height; i++){ | |
| 221 uint8_t *src = buf; | |
| 222 uint8_t *dst = ptr; | |
| 223 | |
| 224 for(j = 0; j < avctx->width; j++){ | |
| 225 dst[0] = src[rgb[2]]; | |
| 226 dst[1] = src[rgb[1]]; | |
| 227 dst[2] = src[rgb[0]]; | |
| 228 dst += 3; | |
| 229 src += 4; | |
| 230 } | |
| 231 | |
| 232 buf += n; | |
| 233 ptr += linesize; | |
| 234 } | |
| 235 break; | |
| 236 default: | |
| 237 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n"); | |
| 238 return -1; | |
| 239 } | |
| 240 | |
| 241 *picture = s->picture; | |
| 242 *data_size = sizeof(AVPicture); | |
| 243 | |
| 244 return buf_size; | |
| 245 } | |
| 246 | |
| 247 AVCodec bmp_decoder = { | |
| 248 "bmp", | |
| 249 CODEC_TYPE_VIDEO, | |
| 250 CODEC_ID_BMP, | |
| 251 sizeof(BMPContext), | |
| 252 bmp_decode_init, | |
| 253 NULL, | |
| 254 NULL, | |
| 255 bmp_decode_frame | |
| 256 }; |
