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 = pass ? 2 : 4;
00130 ptr = ptr1 + linesize * y1;
00131 pass++;
00132 }
00133 break;
00134 case 2:
00135 y1 += 4;
00136 ptr += linesize * 4;
00137 if (y1 >= height) {
00138 y1 = 1;
00139 ptr = ptr1 + linesize;
00140 pass++;
00141 }
00142 break;
00143 case 3:
00144 y1 += 2;
00145 ptr += linesize * 2;
00146 break;
00147 }
00148 } else {
00149 ptr += linesize;
00150 }
00151 }
00152
00153 ff_lzw_decode_tail(s->lzw);
00154 s->bytestream = ff_lzw_cur_ptr(s->lzw);
00155 return 0;
00156 }
00157
00158 static int gif_read_extension(GifState *s)
00159 {
00160 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
00161
00162
00163 ext_code = bytestream_get_byte(&s->bytestream);
00164 ext_len = bytestream_get_byte(&s->bytestream);
00165 #ifdef DEBUG
00166 dprintf(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
00167 #endif
00168 switch(ext_code) {
00169 case 0xf9:
00170 if (ext_len != 4)
00171 goto discard_ext;
00172 s->transparent_color_index = -1;
00173 gce_flags = bytestream_get_byte(&s->bytestream);
00174 s->gce_delay = bytestream_get_le16(&s->bytestream);
00175 gce_transparent_index = bytestream_get_byte(&s->bytestream);
00176 if (gce_flags & 0x01)
00177 s->transparent_color_index = gce_transparent_index;
00178 else
00179 s->transparent_color_index = -1;
00180 s->gce_disposal = (gce_flags >> 2) & 0x7;
00181 #ifdef DEBUG
00182 dprintf(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
00183 gce_flags, s->gce_delay,
00184 s->transparent_color_index, s->gce_disposal);
00185 #endif
00186 ext_len = bytestream_get_byte(&s->bytestream);
00187 break;
00188 }
00189
00190
00191 discard_ext:
00192 while (ext_len != 0) {
00193 for (i = 0; i < ext_len; i++)
00194 bytestream_get_byte(&s->bytestream);
00195 ext_len = bytestream_get_byte(&s->bytestream);
00196 #ifdef DEBUG
00197 dprintf(s->avctx, "gif: ext_len1=%d\n", ext_len);
00198 #endif
00199 }
00200 return 0;
00201 }
00202
00203 static int gif_read_header1(GifState *s)
00204 {
00205 uint8_t sig[6];
00206 int v, n;
00207 int has_global_palette;
00208
00209 if (s->bytestream_end < s->bytestream + 13)
00210 return -1;
00211
00212
00213 bytestream_get_buffer(&s->bytestream, sig, 6);
00214 if (memcmp(sig, gif87a_sig, 6) != 0 &&
00215 memcmp(sig, gif89a_sig, 6) != 0)
00216 return -1;
00217
00218
00219 s->transparent_color_index = -1;
00220 s->screen_width = bytestream_get_le16(&s->bytestream);
00221 s->screen_height = bytestream_get_le16(&s->bytestream);
00222 if( (unsigned)s->screen_width > 32767
00223 || (unsigned)s->screen_height > 32767){
00224 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
00225 return -1;
00226 }
00227
00228 v = bytestream_get_byte(&s->bytestream);
00229 s->color_resolution = ((v & 0x70) >> 4) + 1;
00230 has_global_palette = (v & 0x80);
00231 s->bits_per_pixel = (v & 0x07) + 1;
00232 s->background_color_index = bytestream_get_byte(&s->bytestream);
00233 bytestream_get_byte(&s->bytestream);
00234 #ifdef DEBUG
00235 dprintf(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
00236 s->screen_width, s->screen_height, s->bits_per_pixel,
00237 has_global_palette);
00238 #endif
00239 if (has_global_palette) {
00240 n = 1 << s->bits_per_pixel;
00241 if (s->bytestream_end < s->bytestream + n * 3)
00242 return -1;
00243 bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
00244 }
00245 return 0;
00246 }
00247
00248 static int gif_parse_next_image(GifState *s)
00249 {
00250 while (s->bytestream < s->bytestream_end) {
00251 int code = bytestream_get_byte(&s->bytestream);
00252 #ifdef DEBUG
00253 dprintf(s->avctx, "gif: code=%02x '%c'\n", code, code);
00254 #endif
00255 switch (code) {
00256 case ',':
00257 return gif_read_image(s);
00258 case '!':
00259 if (gif_read_extension(s) < 0)
00260 return -1;
00261 break;
00262 case ';':
00263
00264 default:
00265
00266 return -1;
00267 }
00268 }
00269 return -1;
00270 }
00271
00272 static av_cold int gif_decode_init(AVCodecContext *avctx)
00273 {
00274 GifState *s = avctx->priv_data;
00275
00276 s->avctx = avctx;
00277
00278 avcodec_get_frame_defaults(&s->picture);
00279 avctx->coded_frame= &s->picture;
00280 s->picture.data[0] = NULL;
00281 ff_lzw_decode_open(&s->lzw);
00282 return 0;
00283 }
00284
00285 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
00286 {
00287 GifState *s = avctx->priv_data;
00288 AVFrame *picture = data;
00289 int ret;
00290
00291 s->bytestream = buf;
00292 s->bytestream_end = buf + buf_size;
00293 if (gif_read_header1(s) < 0)
00294 return -1;
00295
00296 avctx->pix_fmt = PIX_FMT_PAL8;
00297 if (avcodec_check_dimensions(avctx, s->screen_width, s->screen_height))
00298 return -1;
00299 avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
00300
00301 if (s->picture.data[0])
00302 avctx->release_buffer(avctx, &s->picture);
00303 if (avctx->get_buffer(avctx, &s->picture) < 0) {
00304 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00305 return -1;
00306 }
00307 s->image_palette = (uint32_t *)s->picture.data[1];
00308 ret = gif_parse_next_image(s);
00309 if (ret < 0)
00310 return ret;
00311
00312 *picture = s->picture;
00313 *data_size = sizeof(AVPicture);
00314 return s->bytestream - buf;
00315 }
00316
00317 static av_cold int gif_decode_close(AVCodecContext *avctx)
00318 {
00319 GifState *s = avctx->priv_data;
00320
00321 ff_lzw_decode_close(&s->lzw);
00322 if(s->picture.data[0])
00323 avctx->release_buffer(avctx, &s->picture);
00324 return 0;
00325 }
00326
00327 AVCodec gif_decoder = {
00328 "gif",
00329 CODEC_TYPE_VIDEO,
00330 CODEC_ID_GIF,
00331 sizeof(GifState),
00332 gif_decode_init,
00333 NULL,
00334 gif_decode_close,
00335 gif_decode_frame,
00336 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
00337 };