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