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 "libavutil/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 int ret;
00071
00072 left = bytestream_get_le16(&s->bytestream);
00073 top = bytestream_get_le16(&s->bytestream);
00074 width = bytestream_get_le16(&s->bytestream);
00075 height = bytestream_get_le16(&s->bytestream);
00076 flags = bytestream_get_byte(&s->bytestream);
00077 is_interleaved = flags & 0x40;
00078 has_local_palette = flags & 0x80;
00079 bits_per_pixel = (flags & 0x07) + 1;
00080
00081 av_dlog(s->avctx, "image x=%d y=%d w=%d h=%d\n", left, top, width, height);
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] = (0xffu << 24) | AV_RB24(spal);
00101 spal += 3;
00102 }
00103 for(; i < 256; i++)
00104 s->image_palette[i] = (0xffu << 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 if ((ret = ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
00112 s->bytestream_end - s->bytestream, FF_LZW_GIF)) < 0) {
00113 av_log(s->avctx, AV_LOG_ERROR, "LZW init failed\n");
00114 return ret;
00115 }
00116
00117
00118 linesize = s->picture.linesize[0];
00119 ptr1 = s->picture.data[0] + top * linesize + left;
00120 ptr = ptr1;
00121 pass = 0;
00122 y1 = 0;
00123 for (y = 0; y < height; y++) {
00124 ff_lzw_decode(s->lzw, ptr, width);
00125 if (is_interleaved) {
00126 switch(pass) {
00127 default:
00128 case 0:
00129 case 1:
00130 y1 += 8;
00131 ptr += linesize * 8;
00132 if (y1 >= height) {
00133 y1 = pass ? 2 : 4;
00134 ptr = ptr1 + linesize * y1;
00135 pass++;
00136 }
00137 break;
00138 case 2:
00139 y1 += 4;
00140 ptr += linesize * 4;
00141 if (y1 >= height) {
00142 y1 = 1;
00143 ptr = ptr1 + linesize;
00144 pass++;
00145 }
00146 break;
00147 case 3:
00148 y1 += 2;
00149 ptr += linesize * 2;
00150 break;
00151 }
00152 } else {
00153 ptr += linesize;
00154 }
00155 }
00156
00157 ff_lzw_decode_tail(s->lzw);
00158 s->bytestream = ff_lzw_cur_ptr(s->lzw);
00159 return 0;
00160 }
00161
00162 static int gif_read_extension(GifState *s)
00163 {
00164 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
00165
00166
00167 ext_code = bytestream_get_byte(&s->bytestream);
00168 ext_len = bytestream_get_byte(&s->bytestream);
00169
00170 av_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len);
00171
00172 switch(ext_code) {
00173 case 0xf9:
00174 if (ext_len != 4)
00175 goto discard_ext;
00176 s->transparent_color_index = -1;
00177 gce_flags = bytestream_get_byte(&s->bytestream);
00178 s->gce_delay = bytestream_get_le16(&s->bytestream);
00179 gce_transparent_index = bytestream_get_byte(&s->bytestream);
00180 if (gce_flags & 0x01)
00181 s->transparent_color_index = gce_transparent_index;
00182 else
00183 s->transparent_color_index = -1;
00184 s->gce_disposal = (gce_flags >> 2) & 0x7;
00185
00186 av_dlog(s->avctx, "gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
00187 gce_flags, s->gce_delay,
00188 s->transparent_color_index, s->gce_disposal);
00189
00190 ext_len = bytestream_get_byte(&s->bytestream);
00191 break;
00192 }
00193
00194
00195 discard_ext:
00196 while (ext_len != 0) {
00197 for (i = 0; i < ext_len; i++)
00198 bytestream_get_byte(&s->bytestream);
00199 ext_len = bytestream_get_byte(&s->bytestream);
00200
00201 av_dlog(s->avctx, "ext_len1=%d\n", ext_len);
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 AVERROR_INVALIDDATA;
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 AVERROR_INVALIDDATA;
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(s->avctx, AV_LOG_ERROR, "picture size too large\n");
00228 return AVERROR_INVALIDDATA;
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
00238 av_dlog(s->avctx, "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
00242 if (has_global_palette) {
00243 n = 1 << s->bits_per_pixel;
00244 if (s->bytestream_end < s->bytestream + n * 3)
00245 return AVERROR_INVALIDDATA;
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
00256 av_dlog(s->avctx, "code=%02x '%c'\n", code, code);
00257
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 av_cold 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, AVPacket *avpkt)
00289 {
00290 const uint8_t *buf = avpkt->data;
00291 int buf_size = avpkt->size;
00292 GifState *s = avctx->priv_data;
00293 AVFrame *picture = data;
00294 int ret;
00295
00296 s->bytestream = buf;
00297 s->bytestream_end = buf + buf_size;
00298 if ((ret = gif_read_header1(s)) < 0)
00299 return ret;
00300
00301 avctx->pix_fmt = AV_PIX_FMT_PAL8;
00302 if (av_image_check_size(s->screen_width, s->screen_height, 0, avctx))
00303 return -1;
00304 avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
00305
00306 if (s->picture.data[0])
00307 avctx->release_buffer(avctx, &s->picture);
00308 if ((ret = avctx->get_buffer(avctx, &s->picture)) < 0) {
00309 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00310 return ret;
00311 }
00312 s->image_palette = (uint32_t *)s->picture.data[1];
00313 ret = gif_parse_next_image(s);
00314 if (ret < 0)
00315 return ret;
00316
00317 *picture = s->picture;
00318 *data_size = sizeof(AVPicture);
00319 return s->bytestream - buf;
00320 }
00321
00322 static av_cold int gif_decode_close(AVCodecContext *avctx)
00323 {
00324 GifState *s = avctx->priv_data;
00325
00326 ff_lzw_decode_close(&s->lzw);
00327 if(s->picture.data[0])
00328 avctx->release_buffer(avctx, &s->picture);
00329 return 0;
00330 }
00331
00332 AVCodec ff_gif_decoder = {
00333 .name = "gif",
00334 .type = AVMEDIA_TYPE_VIDEO,
00335 .id = AV_CODEC_ID_GIF,
00336 .priv_data_size = sizeof(GifState),
00337 .init = gif_decode_init,
00338 .close = gif_decode_close,
00339 .decode = gif_decode_frame,
00340 .capabilities = CODEC_CAP_DR1,
00341 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
00342 };