00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "avcodec.h"
00026 #include "bytestream.h"
00027 #include "lzw.h"
00028
00029 #define GCE_DISPOSAL_NONE 0
00030 #define GCE_DISPOSAL_INPLACE 1
00031 #define GCE_DISPOSAL_BACKGROUND 2
00032 #define GCE_DISPOSAL_RESTORE 3
00033
00034 typedef struct GifState {
00035 AVFrame picture;
00036 int screen_width;
00037 int screen_height;
00038 int bits_per_pixel;
00039 int background_color_index;
00040 int transparent_color_index;
00041 int color_resolution;
00042 uint32_t *image_palette;
00043
00044
00045 int gce_disposal;
00046
00047 int gce_delay;
00048
00049
00050 const uint8_t *bytestream;
00051 const uint8_t *bytestream_end;
00052 LZWState *lzw;
00053
00054
00055 uint8_t global_palette[256 * 3];
00056 uint8_t local_palette[256 * 3];
00057
00058 AVCodecContext* avctx;
00059 } GifState;
00060
00061 static const uint8_t gif87a_sig[6] = "GIF87a";
00062 static const uint8_t gif89a_sig[6] = "GIF89a";
00063
00064 static int gif_read_image(GifState *s)
00065 {
00066 int left, top, width, height, bits_per_pixel, code_size, flags;
00067 int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
00068 uint8_t *ptr, *spal, *palette, *ptr1;
00069
00070 left = bytestream_get_le16(&s->bytestream);
00071 top = bytestream_get_le16(&s->bytestream);
00072 width = bytestream_get_le16(&s->bytestream);
00073 height = bytestream_get_le16(&s->bytestream);
00074 flags = bytestream_get_byte(&s->bytestream);
00075 is_interleaved = flags & 0x40;
00076 has_local_palette = flags & 0x80;
00077 bits_per_pixel = (flags & 0x07) + 1;
00078 #ifdef DEBUG
00079 dprintf(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
00080 #endif
00081
00082 if (has_local_palette) {
00083 bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
00084 palette = s->local_palette;
00085 } else {
00086 palette = s->global_palette;
00087 bits_per_pixel = s->bits_per_pixel;
00088 }
00089
00090
00091 if (left + width > s->screen_width ||
00092 top + height > s->screen_height)
00093 return AVERROR(EINVAL);
00094
00095
00096 n = (1 << bits_per_pixel);
00097 spal = palette;
00098 for(i = 0; i < n; i++) {
00099 s->image_palette[i] = (0xff << 24) | AV_RB24(spal);
00100 spal += 3;
00101 }
00102 for(; i < 256; i++)
00103 s->image_palette[i] = (0xff << 24);
00104
00105 if (s->transparent_color_index >= 0)
00106 s->image_palette[s->transparent_color_index] = 0;
00107
00108
00109 code_size = bytestream_get_byte(&s->bytestream);
00110 ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
00111 s->bytestream_end - s->bytestream, FF_LZW_GIF);
00112
00113
00114 linesize = s->picture.linesize[0];
00115 ptr1 = s->picture.data[0] + top * linesize + left;
00116 ptr = ptr1;
00117 pass = 0;
00118 y1 = 0;
00119 for (y = 0; y < height; y++) {
00120 ff_lzw_decode(s->lzw, ptr, width);
00121 if (is_interleaved) {
00122 switch(pass) {
00123 default:
00124 case 0:
00125 case 1:
00126 y1 += 8;
00127 ptr += linesize * 8;
00128 if (y1 >= height) {
00129 y1 = 4;
00130 if (pass == 0)
00131 ptr = ptr1 + linesize * 4;
00132 else
00133 ptr = ptr1 + linesize * 2;
00134 pass++;
00135 }
00136 break;
00137 case 2:
00138 y1 += 4;
00139 ptr += linesize * 4;
00140 if (y1 >= height) {
00141 y1 = 1;
00142 ptr = ptr1 + linesize;
00143 pass++;
00144 }
00145 break;
00146 case 3:
00147 y1 += 2;
00148 ptr += linesize * 2;
00149 break;
00150 }
00151 } else {
00152 ptr += linesize;
00153 }
00154 }
00155
00156 ff_lzw_decode_tail(s->lzw);
00157 s->bytestream = ff_lzw_cur_ptr(s->lzw);
00158 return 0;
00159 }
00160
00161 static int gif_read_extension(GifState *s)
00162 {
00163 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
00164
00165
00166 ext_code = bytestream_get_byte(&s->bytestream);
00167 ext_len = bytestream_get_byte(&s->bytestream);
00168 #ifdef DEBUG
00169 dprintf(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
00170 #endif
00171 switch(ext_code) {
00172 case 0xf9:
00173 if (ext_len != 4)
00174 goto discard_ext;
00175 s->transparent_color_index = -1;
00176 gce_flags = bytestream_get_byte(&s->bytestream);
00177 s->gce_delay = bytestream_get_le16(&s->bytestream);
00178 gce_transparent_index = bytestream_get_byte(&s->bytestream);
00179 if (gce_flags & 0x01)
00180 s->transparent_color_index = gce_transparent_index;
00181 else
00182 s->transparent_color_index = -1;
00183 s->gce_disposal = (gce_flags >> 2) & 0x7;
00184 #ifdef DEBUG
00185 dprintf(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
00186 gce_flags, s->gce_delay,
00187 s->transparent_color_index, s->gce_disposal);
00188 #endif
00189 ext_len = bytestream_get_byte(&s->bytestream);
00190 break;
00191 }
00192
00193
00194 discard_ext:
00195 while (ext_len != 0) {
00196 for (i = 0; i < ext_len; i++)
00197 bytestream_get_byte(&s->bytestream);
00198 ext_len = bytestream_get_byte(&s->bytestream);
00199 #ifdef DEBUG
00200 dprintf(s->avctx, "gif: ext_len1=%d\n", ext_len);
00201 #endif
00202 }
00203 return 0;
00204 }
00205
00206 static int gif_read_header1(GifState *s)
00207 {
00208 uint8_t sig[6];
00209 int v, n;
00210 int has_global_palette;
00211
00212 if (s->bytestream_end < s->bytestream + 13)
00213 return -1;
00214
00215
00216 bytestream_get_buffer(&s->bytestream, sig, 6);
00217 if (memcmp(sig, gif87a_sig, 6) != 0 &&
00218 memcmp(sig, gif89a_sig, 6) != 0)
00219 return -1;
00220
00221
00222 s->transparent_color_index = -1;
00223 s->screen_width = bytestream_get_le16(&s->bytestream);
00224 s->screen_height = bytestream_get_le16(&s->bytestream);
00225 if( (unsigned)s->screen_width > 32767
00226 || (unsigned)s->screen_height > 32767){
00227 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
00228 return -1;
00229 }
00230
00231 v = bytestream_get_byte(&s->bytestream);
00232 s->color_resolution = ((v & 0x70) >> 4) + 1;
00233 has_global_palette = (v & 0x80);
00234 s->bits_per_pixel = (v & 0x07) + 1;
00235 s->background_color_index = bytestream_get_byte(&s->bytestream);
00236 bytestream_get_byte(&s->bytestream);
00237 #ifdef DEBUG
00238 dprintf(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
00239 s->screen_width, s->screen_height, s->bits_per_pixel,
00240 has_global_palette);
00241 #endif
00242 if (has_global_palette) {
00243 n = 1 << s->bits_per_pixel;
00244 if (s->bytestream_end < s->bytestream + n * 3)
00245 return -1;
00246 bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
00247 }
00248 return 0;
00249 }
00250
00251 static int gif_parse_next_image(GifState *s)
00252 {
00253 while (s->bytestream < s->bytestream_end) {
00254 int code = bytestream_get_byte(&s->bytestream);
00255 #ifdef DEBUG
00256 dprintf(s->avctx, "gif: code=%02x '%c'\n", code, code);
00257 #endif
00258 switch (code) {
00259 case ',':
00260 return gif_read_image(s);
00261 case '!':
00262 if (gif_read_extension(s) < 0)
00263 return -1;
00264 break;
00265 case ';':
00266
00267 default:
00268
00269 return -1;
00270 }
00271 }
00272 return -1;
00273 }
00274
00275 static int gif_decode_init(AVCodecContext *avctx)
00276 {
00277 GifState *s = avctx->priv_data;
00278
00279 s->avctx = avctx;
00280
00281 avcodec_get_frame_defaults(&s->picture);
00282 avctx->coded_frame= &s->picture;
00283 s->picture.data[0] = NULL;
00284 ff_lzw_decode_open(&s->lzw);
00285 return 0;
00286 }
00287
00288 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
00289 {
00290 GifState *s = avctx->priv_data;
00291 AVFrame *picture = data;
00292 int ret;
00293
00294 s->bytestream = buf;
00295 s->bytestream_end = buf + buf_size;
00296 if (gif_read_header1(s) < 0)
00297 return -1;
00298
00299 avctx->pix_fmt = PIX_FMT_PAL8;
00300 if (avcodec_check_dimensions(avctx, s->screen_width, s->screen_height))
00301 return -1;
00302 avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
00303
00304 if (s->picture.data[0])
00305 avctx->release_buffer(avctx, &s->picture);
00306 if (avctx->get_buffer(avctx, &s->picture) < 0) {
00307 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00308 return -1;
00309 }
00310 s->image_palette = (uint32_t *)s->picture.data[1];
00311 ret = gif_parse_next_image(s);
00312 if (ret < 0)
00313 return ret;
00314
00315 *picture = s->picture;
00316 *data_size = sizeof(AVPicture);
00317 return s->bytestream - buf;
00318 }
00319
00320 static int gif_decode_close(AVCodecContext *avctx)
00321 {
00322 GifState *s = avctx->priv_data;
00323
00324 ff_lzw_decode_close(&s->lzw);
00325 if(s->picture.data[0])
00326 avctx->release_buffer(avctx, &s->picture);
00327 return 0;
00328 }
00329
00330 AVCodec gif_decoder = {
00331 "gif",
00332 CODEC_TYPE_VIDEO,
00333 CODEC_ID_GIF,
00334 sizeof(GifState),
00335 gif_decode_init,
00336 NULL,
00337 gif_decode_close,
00338 gif_decode_frame,
00339 };