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
00026 #include "libavutil/imgutils.h"
00027 #include "libavutil/opt.h"
00028 #include "avcodec.h"
00029 #include "bytestream.h"
00030 #include "internal.h"
00031 #include "lzw.h"
00032 #include "gif.h"
00033
00034
00035
00036
00037
00038 #define GIF_TRANSPARENT_COLOR 0x00ffffff
00039
00040 typedef struct GifState {
00041 const AVClass *class;
00042 AVFrame picture;
00043 int screen_width;
00044 int screen_height;
00045 int has_global_palette;
00046 int bits_per_pixel;
00047 uint32_t bg_color;
00048 int background_color_index;
00049 int transparent_color_index;
00050 int color_resolution;
00051
00052
00053 uint8_t *idx_line;
00054 int idx_line_size;
00055
00056
00057 int gce_prev_disposal;
00058 int gce_disposal;
00059
00060 int gce_l, gce_t, gce_w, gce_h;
00061
00062
00063
00064 uint32_t * stored_img;
00065 int stored_img_size;
00066 int stored_bg_color;
00067
00068 GetByteContext gb;
00069
00070 LZWState *lzw;
00071
00072
00073 uint32_t global_palette[256];
00074 uint32_t local_palette[256];
00075
00076 AVCodecContext *avctx;
00077 int keyframe;
00078 int trans_color;
00079 } GifState;
00080
00081 static void gif_read_palette(GifState *s, uint32_t *pal, int nb)
00082 {
00083 int i;
00084
00085 for (i = 0; i < nb; i++, pal++)
00086 *pal = (0xffu << 24) | bytestream2_get_be24u(&s->gb);
00087 }
00088
00089 static void gif_fill(AVFrame *picture, uint32_t color)
00090 {
00091 uint32_t *p = (uint32_t *)picture->data[0];
00092 uint32_t *p_end = p + (picture->linesize[0] / sizeof(uint32_t)) * picture->height;
00093
00094 for (; p < p_end; p++)
00095 *p = color;
00096 }
00097
00098 static void gif_fill_rect(AVFrame *picture, uint32_t color, int l, int t, int w, int h)
00099 {
00100 const int linesize = picture->linesize[0] / sizeof(uint32_t);
00101 const uint32_t *py = (uint32_t *)picture->data[0] + t * linesize;
00102 const uint32_t *pr, *pb = py + (t + h) * linesize;
00103 uint32_t *px;
00104
00105 for (; py < pb; py += linesize) {
00106 px = (uint32_t *)py + l;
00107 pr = px + w;
00108
00109 for (; px < pr; px++)
00110 *px = color;
00111 }
00112 }
00113
00114 static void gif_copy_img_rect(const uint32_t *src, uint32_t *dst,
00115 int linesize, int l, int t, int w, int h)
00116 {
00117 const int y_start = t * linesize;
00118 const uint32_t *src_px, *src_pr,
00119 *src_py = src + y_start,
00120 *dst_py = dst + y_start;
00121 const uint32_t *src_pb = src_py + (t + h) * linesize;
00122 uint32_t *dst_px;
00123
00124 for (; src_py < src_pb; src_py += linesize, dst_py += linesize) {
00125 src_px = src_py + l;
00126 dst_px = (uint32_t *)dst_py + l;
00127 src_pr = src_px + w;
00128
00129 for (; src_px < src_pr; src_px++, dst_px++)
00130 *dst_px = *src_px;
00131 }
00132 }
00133
00134 static int gif_read_image(GifState *s)
00135 {
00136 int left, top, width, height, bits_per_pixel, code_size, flags;
00137 int is_interleaved, has_local_palette, y, pass, y1, linesize, pal_size;
00138 uint32_t *ptr, *pal, *px, *pr, *ptr1;
00139 int ret;
00140 uint8_t *idx;
00141
00142
00143 if (bytestream2_get_bytes_left(&s->gb) < 9)
00144 return AVERROR_INVALIDDATA;
00145
00146 left = bytestream2_get_le16u(&s->gb);
00147 top = bytestream2_get_le16u(&s->gb);
00148 width = bytestream2_get_le16u(&s->gb);
00149 height = bytestream2_get_le16u(&s->gb);
00150 flags = bytestream2_get_byteu(&s->gb);
00151 is_interleaved = flags & 0x40;
00152 has_local_palette = flags & 0x80;
00153 bits_per_pixel = (flags & 0x07) + 1;
00154
00155 av_dlog(s->avctx, "image x=%d y=%d w=%d h=%d\n", left, top, width, height);
00156
00157 if (has_local_palette) {
00158 pal_size = 1 << bits_per_pixel;
00159
00160 if (bytestream2_get_bytes_left(&s->gb) < pal_size * 3)
00161 return AVERROR_INVALIDDATA;
00162
00163 gif_read_palette(s, s->local_palette, pal_size);
00164 pal = s->local_palette;
00165 } else {
00166 if (!s->has_global_palette) {
00167 av_log(s->avctx, AV_LOG_FATAL, "picture doesn't have either global or local palette.\n");
00168 return AVERROR_INVALIDDATA;
00169 }
00170
00171 pal = s->global_palette;
00172 }
00173
00174 if (s->keyframe) {
00175 if (s->transparent_color_index == -1 && s->has_global_palette) {
00176
00177 gif_fill(&s->picture, s->bg_color);
00178 } else {
00179
00180
00181 gif_fill(&s->picture, s->trans_color);
00182 }
00183 }
00184
00185
00186 if (left + width > s->screen_width ||
00187 top + height > s->screen_height)
00188 return AVERROR(EINVAL);
00189
00190
00191 if (s->gce_prev_disposal == GCE_DISPOSAL_BACKGROUND) {
00192 gif_fill_rect(&s->picture, s->stored_bg_color, s->gce_l, s->gce_t, s->gce_w, s->gce_h);
00193 } else if (s->gce_prev_disposal == GCE_DISPOSAL_RESTORE) {
00194 gif_copy_img_rect(s->stored_img, (uint32_t *)s->picture.data[0],
00195 s->picture.linesize[0] / sizeof(uint32_t), s->gce_l, s->gce_t, s->gce_w, s->gce_h);
00196 }
00197
00198 s->gce_prev_disposal = s->gce_disposal;
00199
00200 if (s->gce_disposal != GCE_DISPOSAL_NONE) {
00201 s->gce_l = left; s->gce_t = top;
00202 s->gce_w = width; s->gce_h = height;
00203
00204 if (s->gce_disposal == GCE_DISPOSAL_BACKGROUND) {
00205 if (s->background_color_index == s->transparent_color_index)
00206 s->stored_bg_color = s->trans_color;
00207 else
00208 s->stored_bg_color = s->bg_color;
00209 } else if (s->gce_disposal == GCE_DISPOSAL_RESTORE) {
00210 av_fast_malloc(&s->stored_img, &s->stored_img_size, s->picture.linesize[0] * s->picture.height);
00211 if (!s->stored_img)
00212 return AVERROR(ENOMEM);
00213
00214 gif_copy_img_rect((uint32_t *)s->picture.data[0], s->stored_img,
00215 s->picture.linesize[0] / sizeof(uint32_t), left, top, width, height);
00216 }
00217 }
00218
00219
00220 if (bytestream2_get_bytes_left(&s->gb) < 2)
00221 return AVERROR_INVALIDDATA;
00222
00223
00224 code_size = bytestream2_get_byteu(&s->gb);
00225 if ((ret = ff_lzw_decode_init(s->lzw, code_size, s->gb.buffer,
00226 bytestream2_get_bytes_left(&s->gb), FF_LZW_GIF)) < 0) {
00227 av_log(s->avctx, AV_LOG_ERROR, "LZW init failed\n");
00228 return ret;
00229 }
00230
00231
00232 linesize = s->picture.linesize[0] / sizeof(uint32_t);
00233 ptr1 = (uint32_t *)s->picture.data[0] + top * linesize + left;
00234 ptr = ptr1;
00235 pass = 0;
00236 y1 = 0;
00237 for (y = 0; y < height; y++) {
00238 if (ff_lzw_decode(s->lzw, s->idx_line, width) == 0)
00239 goto decode_tail;
00240
00241 pr = ptr + width;
00242
00243 for (px = ptr, idx = s->idx_line; px < pr; px++, idx++) {
00244 if (*idx != s->transparent_color_index)
00245 *px = pal[*idx];
00246 }
00247
00248 if (is_interleaved) {
00249 switch(pass) {
00250 default:
00251 case 0:
00252 case 1:
00253 y1 += 8;
00254 ptr += linesize * 8;
00255 if (y1 >= height) {
00256 y1 = pass ? 2 : 4;
00257 ptr = ptr1 + linesize * y1;
00258 pass++;
00259 }
00260 break;
00261 case 2:
00262 y1 += 4;
00263 ptr += linesize * 4;
00264 if (y1 >= height) {
00265 y1 = 1;
00266 ptr = ptr1 + linesize;
00267 pass++;
00268 }
00269 break;
00270 case 3:
00271 y1 += 2;
00272 ptr += linesize * 2;
00273 break;
00274 }
00275 } else {
00276 ptr += linesize;
00277 }
00278 }
00279
00280 decode_tail:
00281
00282 ff_lzw_decode_tail(s->lzw);
00283
00284
00285
00286 s->transparent_color_index = -1;
00287 s->gce_disposal = GCE_DISPOSAL_NONE;
00288
00289 return 0;
00290 }
00291
00292 static int gif_read_extension(GifState *s)
00293 {
00294 int ext_code, ext_len, gce_flags, gce_transparent_index;
00295
00296
00297
00298 if (bytestream2_get_bytes_left(&s->gb) < 2)
00299 return AVERROR_INVALIDDATA;
00300
00301 ext_code = bytestream2_get_byteu(&s->gb);
00302 ext_len = bytestream2_get_byteu(&s->gb);
00303
00304 av_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len);
00305
00306 switch(ext_code) {
00307 case GIF_GCE_EXT_LABEL:
00308 if (ext_len != 4)
00309 goto discard_ext;
00310
00311
00312
00313 if (bytestream2_get_bytes_left(&s->gb) < 5)
00314 return AVERROR_INVALIDDATA;
00315
00316 s->transparent_color_index = -1;
00317 gce_flags = bytestream2_get_byteu(&s->gb);
00318 bytestream2_skipu(&s->gb, 2);
00319 gce_transparent_index = bytestream2_get_byteu(&s->gb);
00320 if (gce_flags & 0x01)
00321 s->transparent_color_index = gce_transparent_index;
00322 else
00323 s->transparent_color_index = -1;
00324 s->gce_disposal = (gce_flags >> 2) & 0x7;
00325
00326 av_dlog(s->avctx, "gce_flags=%x tcolor=%d disposal=%d\n",
00327 gce_flags,
00328 s->transparent_color_index, s->gce_disposal);
00329
00330 if (s->gce_disposal > 3) {
00331 s->gce_disposal = GCE_DISPOSAL_NONE;
00332 av_dlog(s->avctx, "invalid value in gce_disposal (%d). Using default value of 0.\n", ext_len);
00333 }
00334
00335 ext_len = bytestream2_get_byteu(&s->gb);
00336 break;
00337 }
00338
00339
00340 discard_ext:
00341 while (ext_len != 0) {
00342
00343 if (bytestream2_get_bytes_left(&s->gb) < ext_len + 1)
00344 return AVERROR_INVALIDDATA;
00345
00346 bytestream2_skipu(&s->gb, ext_len);
00347 ext_len = bytestream2_get_byteu(&s->gb);
00348
00349 av_dlog(s->avctx, "ext_len1=%d\n", ext_len);
00350 }
00351 return 0;
00352 }
00353
00354 static int gif_read_header1(GifState *s)
00355 {
00356 uint8_t sig[6];
00357 int v, n;
00358 int background_color_index;
00359
00360 if (bytestream2_get_bytes_left(&s->gb) < 13)
00361 return AVERROR_INVALIDDATA;
00362
00363
00364 bytestream2_get_bufferu(&s->gb, sig, 6);
00365 if (memcmp(sig, gif87a_sig, 6) != 0 &&
00366 memcmp(sig, gif89a_sig, 6) != 0)
00367 return AVERROR_INVALIDDATA;
00368
00369
00370 s->transparent_color_index = -1;
00371 s->screen_width = bytestream2_get_le16u(&s->gb);
00372 s->screen_height = bytestream2_get_le16u(&s->gb);
00373 if( (unsigned)s->screen_width > 32767
00374 || (unsigned)s->screen_height > 32767){
00375 av_log(s->avctx, AV_LOG_ERROR, "picture size too large\n");
00376 return AVERROR_INVALIDDATA;
00377 }
00378
00379 av_fast_malloc(&s->idx_line, &s->idx_line_size, s->screen_width);
00380 if (!s->idx_line)
00381 return AVERROR(ENOMEM);
00382
00383 v = bytestream2_get_byteu(&s->gb);
00384 s->color_resolution = ((v & 0x70) >> 4) + 1;
00385 s->has_global_palette = (v & 0x80);
00386 s->bits_per_pixel = (v & 0x07) + 1;
00387 background_color_index = bytestream2_get_byteu(&s->gb);
00388 n = bytestream2_get_byteu(&s->gb);
00389 if (n) {
00390 s->avctx->sample_aspect_ratio.num = n + 15;
00391 s->avctx->sample_aspect_ratio.den = 64;
00392 }
00393
00394 av_dlog(s->avctx, "screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
00395 s->screen_width, s->screen_height, s->bits_per_pixel,
00396 s->has_global_palette);
00397
00398 if (s->has_global_palette) {
00399 s->background_color_index = background_color_index;
00400 n = 1 << s->bits_per_pixel;
00401 if (bytestream2_get_bytes_left(&s->gb) < n * 3)
00402 return AVERROR_INVALIDDATA;
00403
00404 gif_read_palette(s, s->global_palette, n);
00405 s->bg_color = s->global_palette[s->background_color_index];
00406 } else
00407 s->background_color_index = -1;
00408
00409 return 0;
00410 }
00411
00412 static int gif_parse_next_image(GifState *s, int *got_picture)
00413 {
00414 int ret;
00415 *got_picture = 1;
00416 while (bytestream2_get_bytes_left(&s->gb)) {
00417 int code = bytestream2_get_byte(&s->gb);
00418
00419 av_dlog(s->avctx, "code=%02x '%c'\n", code, code);
00420
00421 switch (code) {
00422 case GIF_IMAGE_SEPARATOR:
00423 return gif_read_image(s);
00424 case GIF_EXTENSION_INTRODUCER:
00425 if ((ret = gif_read_extension(s)) < 0)
00426 return ret;
00427 break;
00428 case GIF_TRAILER:
00429
00430 *got_picture = 0;
00431 return 0;
00432 default:
00433
00434 return AVERROR_INVALIDDATA;
00435 }
00436 }
00437 return AVERROR_EOF;
00438 }
00439
00440 static av_cold int gif_decode_init(AVCodecContext *avctx)
00441 {
00442 GifState *s = avctx->priv_data;
00443
00444 s->avctx = avctx;
00445
00446 avctx->pix_fmt = AV_PIX_FMT_RGB32;
00447 avcodec_get_frame_defaults(&s->picture);
00448 avctx->coded_frame= &s->picture;
00449 s->picture.data[0] = NULL;
00450 ff_lzw_decode_open(&s->lzw);
00451 return 0;
00452 }
00453
00454 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
00455 {
00456 GifState *s = avctx->priv_data;
00457 AVFrame *picture = data;
00458 int ret;
00459
00460 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
00461
00462 s->picture.pts = avpkt->pts;
00463 s->picture.pkt_pts = avpkt->pts;
00464 s->picture.pkt_dts = avpkt->dts;
00465 s->picture.pkt_duration = avpkt->duration;
00466
00467 if (avpkt->size >= 6) {
00468 s->keyframe = memcmp(avpkt->data, gif87a_sig, 6) == 0 ||
00469 memcmp(avpkt->data, gif89a_sig, 6) == 0;
00470 } else {
00471 s->keyframe = 0;
00472 }
00473
00474 if (s->keyframe) {
00475 if ((ret = gif_read_header1(s)) < 0)
00476 return ret;
00477
00478 if ((ret = av_image_check_size(s->screen_width, s->screen_height, 0, avctx)) < 0)
00479 return ret;
00480 avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
00481
00482 if (s->picture.data[0])
00483 avctx->release_buffer(avctx, &s->picture);
00484
00485 if ((ret = ff_get_buffer(avctx, &s->picture)) < 0) {
00486 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00487 return ret;
00488 }
00489
00490 s->picture.pict_type = AV_PICTURE_TYPE_I;
00491 s->picture.key_frame = 1;
00492 } else {
00493 if ((ret = avctx->reget_buffer(avctx, &s->picture)) < 0) {
00494 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00495 return ret;
00496 }
00497
00498 s->picture.pict_type = AV_PICTURE_TYPE_P;
00499 s->picture.key_frame = 0;
00500 }
00501
00502 ret = gif_parse_next_image(s, got_frame);
00503 if (ret < 0)
00504 return ret;
00505 else if (*got_frame)
00506 *picture = s->picture;
00507
00508 return avpkt->size;
00509 }
00510
00511 static av_cold int gif_decode_close(AVCodecContext *avctx)
00512 {
00513 GifState *s = avctx->priv_data;
00514
00515 ff_lzw_decode_close(&s->lzw);
00516 if(s->picture.data[0])
00517 avctx->release_buffer(avctx, &s->picture);
00518
00519 av_freep(&s->idx_line);
00520 av_freep(&s->stored_img);
00521
00522 return 0;
00523 }
00524
00525 static const AVOption options[] = {
00526 { "trans_color", "color value (ARGB) that is used instead of transparent color",
00527 offsetof(GifState, trans_color), AV_OPT_TYPE_INT,
00528 {.i64 = GIF_TRANSPARENT_COLOR}, 0, 0xffffffff,
00529 AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM },
00530 { NULL },
00531 };
00532
00533 static const AVClass decoder_class = {
00534 .class_name = "gif decoder",
00535 .item_name = av_default_item_name,
00536 .option = options,
00537 .version = LIBAVUTIL_VERSION_INT,
00538 .category = AV_CLASS_CATEGORY_DECODER,
00539 };
00540
00541 AVCodec ff_gif_decoder = {
00542 .name = "gif",
00543 .type = AVMEDIA_TYPE_VIDEO,
00544 .id = AV_CODEC_ID_GIF,
00545 .priv_data_size = sizeof(GifState),
00546 .init = gif_decode_init,
00547 .close = gif_decode_close,
00548 .decode = gif_decode_frame,
00549 .capabilities = CODEC_CAP_DR1,
00550 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
00551 .priv_class = &decoder_class,
00552 };