Mercurial > libavcodec.hg
annotate gifdec.c @ 4209:ec6d49d9c19d libavcodec
Overlap filtering was done in reverse order
| author | kostya |
|---|---|
| date | Fri, 17 Nov 2006 06:09:32 +0000 |
| parents | dc52a253f51b |
| children | 9bf957e669f0 |
| rev | line source |
|---|---|
| 4054 | 1 /* |
| 2 * GIF decoder | |
| 3 * Copyright (c) 2003 Fabrice Bellard. | |
| 4 * Copyright (c) 2006 Baptiste Coudurier. | |
| 5 * | |
| 6 * This file is part of FFmpeg. | |
| 7 * | |
| 8 * FFmpeg is free software; you can redistribute it and/or | |
| 9 * modify it under the terms of the GNU Lesser General Public | |
| 10 * License as published by the Free Software Foundation; either | |
| 11 * version 2.1 of the License, or (at your option) any later version. | |
| 12 * | |
| 13 * FFmpeg is distributed in the hope that it will be useful, | |
| 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 16 * Lesser General Public License for more details. | |
| 17 * | |
| 18 * You should have received a copy of the GNU Lesser General Public | |
| 19 * License along with FFmpeg; if not, write to the Free Software | |
| 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 21 */ | |
| 22 | |
| 23 //#define DEBUG | |
| 24 | |
| 25 #include "avcodec.h" | |
| 26 #include "bytestream.h" | |
|
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4060
diff
changeset
|
27 #include "lzw.h" |
| 4054 | 28 |
| 29 #define GCE_DISPOSAL_NONE 0 | |
| 30 #define GCE_DISPOSAL_INPLACE 1 | |
| 31 #define GCE_DISPOSAL_BACKGROUND 2 | |
| 32 #define GCE_DISPOSAL_RESTORE 3 | |
| 33 | |
| 34 typedef struct GifState { | |
| 4058 | 35 AVFrame picture; |
| 4054 | 36 int screen_width; |
| 37 int screen_height; | |
| 38 int bits_per_pixel; | |
| 39 int background_color_index; | |
| 40 int transparent_color_index; | |
| 41 int color_resolution; | |
| 4058 | 42 uint32_t *image_palette; |
| 4054 | 43 |
| 44 /* after the frame is displayed, the disposal method is used */ | |
| 45 int gce_disposal; | |
| 46 /* delay during which the frame is shown */ | |
| 47 int gce_delay; | |
| 48 | |
| 49 /* LZW compatible decoder */ | |
| 50 uint8_t *bytestream; | |
|
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4060
diff
changeset
|
51 LZWState *lzw; |
| 4054 | 52 |
| 53 /* aux buffers */ | |
| 54 uint8_t global_palette[256 * 3]; | |
| 55 uint8_t local_palette[256 * 3]; | |
| 56 } GifState; | |
| 57 | |
| 58 static const uint8_t gif87a_sig[6] = "GIF87a"; | |
| 59 static const uint8_t gif89a_sig[6] = "GIF89a"; | |
| 60 | |
| 61 static int gif_read_image(GifState *s) | |
| 62 { | |
| 63 int left, top, width, height, bits_per_pixel, code_size, flags; | |
| 4057 | 64 int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i; |
| 4144 | 65 uint8_t *ptr, *spal, *palette, *ptr1; |
| 4054 | 66 |
| 67 left = bytestream_get_le16(&s->bytestream); | |
| 68 top = bytestream_get_le16(&s->bytestream); | |
| 69 width = bytestream_get_le16(&s->bytestream); | |
| 70 height = bytestream_get_le16(&s->bytestream); | |
| 71 flags = bytestream_get_byte(&s->bytestream); | |
| 72 is_interleaved = flags & 0x40; | |
| 73 has_local_palette = flags & 0x80; | |
| 74 bits_per_pixel = (flags & 0x07) + 1; | |
| 75 #ifdef DEBUG | |
| 4056 | 76 dprintf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height); |
| 4054 | 77 #endif |
| 78 | |
| 79 if (has_local_palette) { | |
| 80 bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel)); | |
| 81 palette = s->local_palette; | |
| 82 } else { | |
| 83 palette = s->global_palette; | |
| 84 bits_per_pixel = s->bits_per_pixel; | |
| 85 } | |
| 86 | |
| 87 /* verify that all the image is inside the screen dimensions */ | |
| 88 if (left + width > s->screen_width || | |
| 89 top + height > s->screen_height) | |
| 90 return -EINVAL; | |
| 91 | |
| 92 /* build the palette */ | |
| 4060 | 93 n = (1 << bits_per_pixel); |
| 94 spal = palette; | |
| 95 for(i = 0; i < n; i++) { | |
| 96 s->image_palette[i] = (0xff << 24) | | |
| 97 (spal[0] << 16) | (spal[1] << 8) | (spal[2]); | |
| 98 spal += 3; | |
| 99 } | |
| 100 for(; i < 256; i++) | |
| 101 s->image_palette[i] = (0xff << 24); | |
| 102 /* handle transparency */ | |
| 103 if (s->transparent_color_index >= 0) | |
| 104 s->image_palette[s->transparent_color_index] = 0; | |
| 4054 | 105 |
| 106 /* now get the image data */ | |
| 107 code_size = bytestream_get_byte(&s->bytestream); | |
|
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4060
diff
changeset
|
108 //TODO: add proper data size |
|
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4060
diff
changeset
|
109 ff_lzw_decode_init(s->lzw, code_size, s->bytestream, 0, FF_LZW_GIF); |
| 4054 | 110 |
| 111 /* read all the image */ | |
| 4058 | 112 linesize = s->picture.linesize[0]; |
| 4143 | 113 ptr1 = s->picture.data[0] + top * linesize + left; |
| 4054 | 114 ptr = ptr1; |
| 115 pass = 0; | |
| 116 y1 = 0; | |
| 117 for (y = 0; y < height; y++) { | |
|
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4060
diff
changeset
|
118 ff_lzw_decode(s->lzw, ptr, width); |
| 4054 | 119 if (is_interleaved) { |
| 120 switch(pass) { | |
| 121 default: | |
| 122 case 0: | |
| 123 case 1: | |
| 124 y1 += 8; | |
| 125 ptr += linesize * 8; | |
| 126 if (y1 >= height) { | |
| 127 y1 = 4; | |
| 128 if (pass == 0) | |
| 129 ptr = ptr1 + linesize * 4; | |
| 130 else | |
| 131 ptr = ptr1 + linesize * 2; | |
| 132 pass++; | |
| 133 } | |
| 134 break; | |
| 135 case 2: | |
| 136 y1 += 4; | |
| 137 ptr += linesize * 4; | |
| 138 if (y1 >= height) { | |
| 139 y1 = 1; | |
| 140 ptr = ptr1 + linesize; | |
| 141 pass++; | |
| 142 } | |
| 143 break; | |
| 144 case 3: | |
| 145 y1 += 2; | |
| 146 ptr += linesize * 2; | |
| 147 break; | |
| 148 } | |
| 149 } else { | |
| 150 ptr += linesize; | |
| 151 } | |
| 152 } | |
| 153 /* read the garbage data until end marker is found */ | |
|
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4060
diff
changeset
|
154 ff_lzw_decode_tail(s->lzw); |
|
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4060
diff
changeset
|
155 s->bytestream = ff_lzw_cur_ptr(s->lzw); |
| 4054 | 156 return 0; |
| 157 } | |
| 158 | |
| 159 static int gif_read_extension(GifState *s) | |
| 160 { | |
| 161 int ext_code, ext_len, i, gce_flags, gce_transparent_index; | |
| 162 | |
| 163 /* extension */ | |
| 164 ext_code = bytestream_get_byte(&s->bytestream); | |
| 165 ext_len = bytestream_get_byte(&s->bytestream); | |
| 166 #ifdef DEBUG | |
| 4056 | 167 dprintf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len); |
| 4054 | 168 #endif |
| 169 switch(ext_code) { | |
| 170 case 0xf9: | |
| 171 if (ext_len != 4) | |
| 172 goto discard_ext; | |
| 173 s->transparent_color_index = -1; | |
| 174 gce_flags = bytestream_get_byte(&s->bytestream); | |
| 175 s->gce_delay = bytestream_get_le16(&s->bytestream); | |
| 176 gce_transparent_index = bytestream_get_byte(&s->bytestream); | |
| 177 if (gce_flags & 0x01) | |
| 178 s->transparent_color_index = gce_transparent_index; | |
| 179 else | |
| 180 s->transparent_color_index = -1; | |
| 181 s->gce_disposal = (gce_flags >> 2) & 0x7; | |
| 182 #ifdef DEBUG | |
| 4056 | 183 dprintf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n", |
| 4054 | 184 gce_flags, s->gce_delay, |
| 185 s->transparent_color_index, s->gce_disposal); | |
| 186 #endif | |
| 187 ext_len = bytestream_get_byte(&s->bytestream); | |
| 188 break; | |
| 189 } | |
| 190 | |
| 191 /* NOTE: many extension blocks can come after */ | |
| 192 discard_ext: | |
| 193 while (ext_len != 0) { | |
| 194 for (i = 0; i < ext_len; i++) | |
| 195 bytestream_get_byte(&s->bytestream); | |
| 196 ext_len = bytestream_get_byte(&s->bytestream); | |
| 197 #ifdef DEBUG | |
| 4056 | 198 dprintf("gif: ext_len1=%d\n", ext_len); |
| 4054 | 199 #endif |
| 200 } | |
| 201 return 0; | |
| 202 } | |
| 203 | |
| 204 static int gif_read_header1(GifState *s) | |
| 205 { | |
| 206 uint8_t sig[6]; | |
| 4057 | 207 int v, n; |
| 4054 | 208 int has_global_palette; |
| 209 | |
| 210 /* read gif signature */ | |
| 211 bytestream_get_buffer(&s->bytestream, sig, 6); | |
| 212 if (memcmp(sig, gif87a_sig, 6) != 0 && | |
| 213 memcmp(sig, gif89a_sig, 6) != 0) | |
| 214 return -1; | |
| 215 | |
| 216 /* read screen header */ | |
| 217 s->transparent_color_index = -1; | |
| 218 s->screen_width = bytestream_get_le16(&s->bytestream); | |
| 219 s->screen_height = bytestream_get_le16(&s->bytestream); | |
| 220 if( (unsigned)s->screen_width > 32767 | |
| 221 || (unsigned)s->screen_height > 32767){ | |
| 222 av_log(NULL, AV_LOG_ERROR, "picture size too large\n"); | |
| 223 return -1; | |
| 224 } | |
| 225 | |
| 226 v = bytestream_get_byte(&s->bytestream); | |
| 227 s->color_resolution = ((v & 0x70) >> 4) + 1; | |
| 228 has_global_palette = (v & 0x80); | |
| 229 s->bits_per_pixel = (v & 0x07) + 1; | |
| 230 s->background_color_index = bytestream_get_byte(&s->bytestream); | |
| 231 bytestream_get_byte(&s->bytestream); /* ignored */ | |
| 232 #ifdef DEBUG | |
| 4056 | 233 dprintf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n", |
| 4054 | 234 s->screen_width, s->screen_height, s->bits_per_pixel, |
| 235 has_global_palette); | |
| 236 #endif | |
| 237 if (has_global_palette) { | |
| 238 n = 1 << s->bits_per_pixel; | |
| 239 bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3); | |
| 240 } | |
| 241 return 0; | |
| 242 } | |
| 243 | |
| 244 static int gif_parse_next_image(GifState *s) | |
| 245 { | |
| 246 int ret, code; | |
| 247 | |
| 248 for (;;) { | |
| 249 code = bytestream_get_byte(&s->bytestream); | |
| 250 #ifdef DEBUG | |
| 4056 | 251 dprintf("gif: code=%02x '%c'\n", code, code); |
| 4054 | 252 #endif |
| 253 switch (code) { | |
| 254 case ',': | |
| 255 if (gif_read_image(s) < 0) | |
| 256 return -1; | |
| 257 ret = 0; | |
| 258 goto the_end; | |
| 259 case ';': | |
| 260 /* end of image */ | |
| 261 ret = -1; | |
| 262 goto the_end; | |
| 263 case '!': | |
| 264 if (gif_read_extension(s) < 0) | |
| 265 return -1; | |
| 266 break; | |
| 267 case EOF: | |
| 268 default: | |
| 269 /* error or errneous EOF */ | |
| 270 ret = -1; | |
| 271 goto the_end; | |
| 272 } | |
| 273 } | |
| 274 the_end: | |
| 275 return ret; | |
| 276 } | |
| 277 | |
| 278 static int gif_decode_init(AVCodecContext *avctx) | |
| 279 { | |
| 280 GifState *s = avctx->priv_data; | |
| 281 | |
| 4058 | 282 avcodec_get_frame_defaults(&s->picture); |
| 283 avctx->coded_frame= &s->picture; | |
| 284 s->picture.data[0] = NULL; | |
|
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4060
diff
changeset
|
285 ff_lzw_decode_open(&s->lzw); |
| 4054 | 286 return 0; |
| 287 } | |
| 288 | |
| 289 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size) | |
| 290 { | |
| 291 GifState *s = avctx->priv_data; | |
| 292 AVFrame *picture = data; | |
| 293 int ret; | |
| 294 | |
| 295 s->bytestream = buf; | |
| 296 if (gif_read_header1(s) < 0) | |
| 297 return -1; | |
| 298 | |
| 299 avctx->pix_fmt = PIX_FMT_PAL8; | |
| 4058 | 300 if (avcodec_check_dimensions(avctx, s->screen_width, s->screen_height)) |
| 301 return -1; | |
| 302 avcodec_set_dimensions(avctx, s->screen_width, s->screen_height); | |
| 4054 | 303 |
| 4058 | 304 if (s->picture.data[0]) |
| 305 avctx->release_buffer(avctx, &s->picture); | |
| 306 if (avctx->get_buffer(avctx, &s->picture) < 0) { | |
| 307 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
| 308 return -1; | |
| 309 } | |
| 310 s->image_palette = (uint32_t *)s->picture.data[1]; | |
| 4054 | 311 ret = gif_parse_next_image(s); |
| 312 if (ret < 0) | |
| 313 return ret; | |
| 314 | |
| 4058 | 315 *picture = s->picture; |
| 316 *data_size = sizeof(AVPicture); | |
| 317 return 0; | |
| 318 } | |
| 4054 | 319 |
| 4058 | 320 static int gif_decode_close(AVCodecContext *avctx) |
| 321 { | |
| 322 GifState *s = avctx->priv_data; | |
| 323 | |
|
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4060
diff
changeset
|
324 ff_lzw_decode_close(&s->lzw); |
| 4058 | 325 if(s->picture.data[0]) |
| 326 avctx->release_buffer(avctx, &s->picture); | |
| 4054 | 327 return 0; |
| 328 } | |
| 329 | |
| 330 AVCodec gif_decoder = { | |
| 331 "gif", | |
| 332 CODEC_TYPE_VIDEO, | |
| 333 CODEC_ID_GIF, | |
| 334 sizeof(GifState), | |
| 335 gif_decode_init, | |
| 336 NULL, | |
| 4058 | 337 gif_decode_close, |
| 4054 | 338 gif_decode_frame, |
| 339 }; |
