libavcodec/gifdec.c
Go to the documentation of this file.
00001 /*
00002  * GIF decoder
00003  * Copyright (c) 2003 Fabrice Bellard
00004  * Copyright (c) 2006 Baptiste Coudurier
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 //#define DEBUG
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     /* after the frame is displayed, the disposal method is used */
00046     int gce_disposal;
00047     /* delay during which the frame is shown */
00048     int gce_delay;
00049 
00050     /* LZW compatible decoder */
00051     const uint8_t *bytestream;
00052     const uint8_t *bytestream_end;
00053     LZWState *lzw;
00054 
00055     /* aux buffers */
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 
00080     av_dlog(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
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     /* verify that all the image is inside the screen dimensions */
00091     if (left + width > s->screen_width ||
00092         top + height > s->screen_height)
00093         return AVERROR(EINVAL);
00094 
00095     /* build the palette */
00096     n = (1 << bits_per_pixel);
00097     spal = palette;
00098     for(i = 0; i < n; i++) {
00099         s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);
00100         spal += 3;
00101     }
00102     for(; i < 256; i++)
00103         s->image_palette[i] = (0xffu << 24);
00104     /* handle transparency */
00105     if (s->transparent_color_index >= 0)
00106         s->image_palette[s->transparent_color_index] = 0;
00107 
00108     /* now get the image data */
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     /* read all the image */
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                 break;
00129             case 2:
00130                 y1 += 4;
00131                 ptr += linesize * 4;
00132                 break;
00133             case 3:
00134                 y1 += 2;
00135                 ptr += linesize * 2;
00136                 break;
00137             }
00138             while (y1 >= height) {
00139                 y1  = 4 >> pass;
00140                 ptr = ptr1 + linesize * y1;
00141                 pass++;
00142             }
00143         } else {
00144             ptr += linesize;
00145         }
00146     }
00147     /* read the garbage data until end marker is found */
00148     ff_lzw_decode_tail(s->lzw);
00149     s->bytestream = ff_lzw_cur_ptr(s->lzw);
00150     return 0;
00151 }
00152 
00153 static int gif_read_extension(GifState *s)
00154 {
00155     int ext_code, ext_len, i, gce_flags, gce_transparent_index;
00156 
00157     /* extension */
00158     ext_code = bytestream_get_byte(&s->bytestream);
00159     ext_len = bytestream_get_byte(&s->bytestream);
00160 
00161     av_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
00162 
00163     switch(ext_code) {
00164     case 0xf9:
00165         if (ext_len != 4)
00166             goto discard_ext;
00167         s->transparent_color_index = -1;
00168         gce_flags = bytestream_get_byte(&s->bytestream);
00169         s->gce_delay = bytestream_get_le16(&s->bytestream);
00170         gce_transparent_index = bytestream_get_byte(&s->bytestream);
00171         if (gce_flags & 0x01)
00172             s->transparent_color_index = gce_transparent_index;
00173         else
00174             s->transparent_color_index = -1;
00175         s->gce_disposal = (gce_flags >> 2) & 0x7;
00176 
00177         av_dlog(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
00178                gce_flags, s->gce_delay,
00179                s->transparent_color_index, s->gce_disposal);
00180 
00181         ext_len = bytestream_get_byte(&s->bytestream);
00182         break;
00183     }
00184 
00185     /* NOTE: many extension blocks can come after */
00186  discard_ext:
00187     while (ext_len != 0) {
00188         for (i = 0; i < ext_len; i++)
00189             bytestream_get_byte(&s->bytestream);
00190         ext_len = bytestream_get_byte(&s->bytestream);
00191 
00192         av_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
00193     }
00194     return 0;
00195 }
00196 
00197 static int gif_read_header1(GifState *s)
00198 {
00199     uint8_t sig[6];
00200     int v, n;
00201     int has_global_palette;
00202 
00203     if (s->bytestream_end < s->bytestream + 13)
00204         return -1;
00205 
00206     /* read gif signature */
00207     bytestream_get_buffer(&s->bytestream, sig, 6);
00208     if (memcmp(sig, gif87a_sig, 6) != 0 &&
00209         memcmp(sig, gif89a_sig, 6) != 0)
00210         return -1;
00211 
00212     /* read screen header */
00213     s->transparent_color_index = -1;
00214     s->screen_width = bytestream_get_le16(&s->bytestream);
00215     s->screen_height = bytestream_get_le16(&s->bytestream);
00216     if(   (unsigned)s->screen_width  > 32767
00217        || (unsigned)s->screen_height > 32767){
00218         av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
00219         return -1;
00220     }
00221 
00222     v = bytestream_get_byte(&s->bytestream);
00223     s->color_resolution = ((v & 0x70) >> 4) + 1;
00224     has_global_palette = (v & 0x80);
00225     s->bits_per_pixel = (v & 0x07) + 1;
00226     s->background_color_index = bytestream_get_byte(&s->bytestream);
00227     bytestream_get_byte(&s->bytestream);                /* ignored */
00228 
00229     av_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
00230            s->screen_width, s->screen_height, s->bits_per_pixel,
00231            has_global_palette);
00232 
00233     if (has_global_palette) {
00234         n = 1 << s->bits_per_pixel;
00235         if (s->bytestream_end < s->bytestream + n * 3)
00236             return -1;
00237         bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
00238     }
00239     return 0;
00240 }
00241 
00242 static int gif_parse_next_image(GifState *s)
00243 {
00244     while (s->bytestream < s->bytestream_end) {
00245         int code = bytestream_get_byte(&s->bytestream);
00246 
00247         av_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
00248 
00249         switch (code) {
00250         case ',':
00251             return gif_read_image(s);
00252         case '!':
00253             if (gif_read_extension(s) < 0)
00254                 return -1;
00255             break;
00256         case ';':
00257             /* end of image */
00258         default:
00259             /* error or erroneous EOF */
00260             return -1;
00261         }
00262     }
00263     return -1;
00264 }
00265 
00266 static av_cold int gif_decode_init(AVCodecContext *avctx)
00267 {
00268     GifState *s = avctx->priv_data;
00269 
00270     s->avctx = avctx;
00271 
00272     avcodec_get_frame_defaults(&s->picture);
00273     avctx->coded_frame= &s->picture;
00274     s->picture.data[0] = NULL;
00275     ff_lzw_decode_open(&s->lzw);
00276     return 0;
00277 }
00278 
00279 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00280 {
00281     const uint8_t *buf = avpkt->data;
00282     int buf_size = avpkt->size;
00283     GifState *s = avctx->priv_data;
00284     AVFrame *picture = data;
00285     int ret;
00286 
00287     s->bytestream = buf;
00288     s->bytestream_end = buf + buf_size;
00289     if (gif_read_header1(s) < 0)
00290         return -1;
00291 
00292     avctx->pix_fmt = PIX_FMT_PAL8;
00293     if (av_image_check_size(s->screen_width, s->screen_height, 0, avctx))
00294         return -1;
00295     avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
00296 
00297     if (s->picture.data[0])
00298         avctx->release_buffer(avctx, &s->picture);
00299     if (avctx->get_buffer(avctx, &s->picture) < 0) {
00300         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00301         return -1;
00302     }
00303     s->image_palette = (uint32_t *)s->picture.data[1];
00304     ret = gif_parse_next_image(s);
00305     if (ret < 0)
00306         return ret;
00307 
00308     *picture = s->picture;
00309     *data_size = sizeof(AVPicture);
00310     return s->bytestream - buf;
00311 }
00312 
00313 static av_cold int gif_decode_close(AVCodecContext *avctx)
00314 {
00315     GifState *s = avctx->priv_data;
00316 
00317     ff_lzw_decode_close(&s->lzw);
00318     if(s->picture.data[0])
00319         avctx->release_buffer(avctx, &s->picture);
00320     return 0;
00321 }
00322 
00323 AVCodec ff_gif_decoder = {
00324     .name           = "gif",
00325     .type           = AVMEDIA_TYPE_VIDEO,
00326     .id             = CODEC_ID_GIF,
00327     .priv_data_size = sizeof(GifState),
00328     .init           = gif_decode_init,
00329     .close          = gif_decode_close,
00330     .decode         = gif_decode_frame,
00331     .capabilities   = CODEC_CAP_DR1,
00332     .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
00333 };