• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 FFmpeg.
00007  *
00008  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "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     /* after the frame is displayed, the disposal method is used */
00045     int gce_disposal;
00046     /* delay during which the frame is shown */
00047     int gce_delay;
00048 
00049     /* LZW compatible decoder */
00050     const uint8_t *bytestream;
00051     const uint8_t *bytestream_end;
00052     LZWState *lzw;
00053 
00054     /* aux buffers */
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     /* 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] = (0xff << 24) | AV_RB24(spal);
00100         spal += 3;
00101     }
00102     for(; i < 256; i++)
00103         s->image_palette[i] = (0xff << 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                 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     /* read the garbage data until end marker is found */
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     /* extension */
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     /* NOTE: many extension blocks can come after */
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     /* read gif signature */
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     /* read screen header */
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);                /* ignored */
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             /* end of image */
00264         default:
00265             /* error or erroneous EOF */
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, AVPacket *avpkt)
00286 {
00287     const uint8_t *buf = avpkt->data;
00288     int buf_size = avpkt->size;
00289     GifState *s = avctx->priv_data;
00290     AVFrame *picture = data;
00291     int ret;
00292 
00293     s->bytestream = buf;
00294     s->bytestream_end = buf + buf_size;
00295     if (gif_read_header1(s) < 0)
00296         return -1;
00297 
00298     avctx->pix_fmt = PIX_FMT_PAL8;
00299     if (avcodec_check_dimensions(avctx, s->screen_width, s->screen_height))
00300         return -1;
00301     avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
00302 
00303     if (s->picture.data[0])
00304         avctx->release_buffer(avctx, &s->picture);
00305     if (avctx->get_buffer(avctx, &s->picture) < 0) {
00306         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00307         return -1;
00308     }
00309     s->image_palette = (uint32_t *)s->picture.data[1];
00310     ret = gif_parse_next_image(s);
00311     if (ret < 0)
00312         return ret;
00313 
00314     *picture = s->picture;
00315     *data_size = sizeof(AVPicture);
00316     return s->bytestream - buf;
00317 }
00318 
00319 static av_cold int gif_decode_close(AVCodecContext *avctx)
00320 {
00321     GifState *s = avctx->priv_data;
00322 
00323     ff_lzw_decode_close(&s->lzw);
00324     if(s->picture.data[0])
00325         avctx->release_buffer(avctx, &s->picture);
00326     return 0;
00327 }
00328 
00329 AVCodec gif_decoder = {
00330     "gif",
00331     AVMEDIA_TYPE_VIDEO,
00332     CODEC_ID_GIF,
00333     sizeof(GifState),
00334     gif_decode_init,
00335     NULL,
00336     gif_decode_close,
00337     gif_decode_frame,
00338     CODEC_CAP_DR1,
00339     .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
00340 };

Generated on Fri Sep 16 2011 17:17:36 for FFmpeg by  doxygen 1.7.1