Merge commit '1f3e56b6dcc163a705704e98569d4850a31d651c'
authorMichael Niedermayer <michaelni@gmx.at>
Fri, 22 Nov 2013 10:39:00 +0000 (11:39 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Fri, 22 Nov 2013 10:46:08 +0000 (11:46 +0100)
* commit '1f3e56b6dcc163a705704e98569d4850a31d651c':
  gifdec: convert to bytestream2

Conflicts:
libavcodec/gifdec.c
libavcodec/lzw.c
libavcodec/lzw.h

See: 3fd60d804996031ceaba9cad0b38652b92551eb0
Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavcodec/gifdec.c

@@@ -46,25 -38,15 +46,24 @@@ typedef struct GifState 
      int background_color_index;
      int transparent_color_index;
      int color_resolution;
 -    uint32_t *image_palette;
 +    /* intermediate buffer for storing color indices
 +     * obtained from lzw-encoded data stream */
 +    uint8_t *idx_line;
 +    int idx_line_size;
  
      /* after the frame is displayed, the disposal method is used */
 +    int gce_prev_disposal;
      int gce_disposal;
 -    /* delay during which the frame is shown */
 -    int gce_delay;
 +    /* rectangle describing area that must be disposed */
 +    int gce_l, gce_t, gce_w, gce_h;
 +    /* depending on disposal method we store either part of the image
 +     * drawn on the canvas or background color that
 +     * should be used upon disposal */
 +    uint32_t * stored_img;
 +    int stored_img_size;
 +    int stored_bg_color;
  
 -    /* LZW compatible decoder */
      GetByteContext gb;
-     /* LZW compatible decoder */
      LZWState *lzw;
  
      /* aux buffers */
@@@ -131,20 -62,14 +130,20 @@@ static void gif_copy_img_rect(const uin
  static int gif_read_image(GifState *s, AVFrame *frame)
  {
      int left, top, width, height, bits_per_pixel, code_size, flags;
 -    int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
 -    uint8_t *ptr, *spal, *palette, *ptr1;
 -
 -    left   = bytestream2_get_le16(&s->gb);
 -    top    = bytestream2_get_le16(&s->gb);
 -    width  = bytestream2_get_le16(&s->gb);
 -    height = bytestream2_get_le16(&s->gb);
 -    flags  = bytestream2_get_byte(&s->gb);
 +    int is_interleaved, has_local_palette, y, pass, y1, linesize, pal_size;
 +    uint32_t *ptr, *pal, *px, *pr, *ptr1;
 +    int ret;
 +    uint8_t *idx;
 +
 +    /* At least 9 bytes of Image Descriptor. */
 +    if (bytestream2_get_bytes_left(&s->gb) < 9)
 +        return AVERROR_INVALIDDATA;
 +
-     left = bytestream2_get_le16u(&s->gb);
-     top = bytestream2_get_le16u(&s->gb);
-     width = bytestream2_get_le16u(&s->gb);
++    left   = bytestream2_get_le16u(&s->gb);
++    top    = bytestream2_get_le16u(&s->gb);
++    width  = bytestream2_get_le16u(&s->gb);
 +    height = bytestream2_get_le16u(&s->gb);
-     flags = bytestream2_get_byteu(&s->gb);
++    flags  = bytestream2_get_byteu(&s->gb);
      is_interleaved = flags & 0x40;
      has_local_palette = flags & 0x80;
      bits_per_pixel = (flags & 0x07) + 1;
  
  static int gif_read_extension(GifState *s)
  {
 -    int ext_code, ext_len, i, gce_flags, gce_transparent_index;
 +    int ext_code, ext_len, gce_flags, gce_transparent_index;
  
 -    /* extension */
 -    ext_code = bytestream2_get_byte(&s->gb);
 -    ext_len  = bytestream2_get_byte(&s->gb);
 +    /* There must be at least 2 bytes:
 +     * 1 for extension label and 1 for extension length. */
 +    if (bytestream2_get_bytes_left(&s->gb) < 2)
 +        return AVERROR_INVALIDDATA;
 +
 +    ext_code = bytestream2_get_byteu(&s->gb);
-     ext_len = bytestream2_get_byteu(&s->gb);
++    ext_len  = bytestream2_get_byteu(&s->gb);
  
 -    av_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
 +    av_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len);
  
      switch(ext_code) {
 -    case 0xf9:
 +    case GIF_GCE_EXT_LABEL:
          if (ext_len != 4)
              goto discard_ext;
 -        s->transparent_color_index = -1;
 -        gce_flags    = bytestream2_get_byte(&s->gb);
 -        s->gce_delay = bytestream2_get_le16(&s->gb);
 -        gce_transparent_index = bytestream2_get_byte(&s->gb);
 +
 +        /* We need at least 5 bytes more: 4 is for extension body
 +         * and 1 for next block size. */
 +        if (bytestream2_get_bytes_left(&s->gb) < 5)
 +            return AVERROR_INVALIDDATA;
 +
-         gce_flags = bytestream2_get_byteu(&s->gb);
++        gce_flags    = bytestream2_get_byteu(&s->gb);
 +        bytestream2_skipu(&s->gb, 2);    // delay during which the frame is shown
 +        gce_transparent_index = bytestream2_get_byteu(&s->gb);
          if (gce_flags & 0x01)
              s->transparent_color_index = gce_transparent_index;
          else
@@@ -370,26 -217,26 +369,26 @@@ static int gif_read_header1(GifState *s
  
      /* read screen header */
      s->transparent_color_index = -1;
-     s->screen_width = bytestream2_get_le16u(&s->gb);
 -    s->screen_width  = bytestream2_get_le16(&s->gb);
 -    s->screen_height = bytestream2_get_le16(&s->gb);
 -    if(   (unsigned)s->screen_width  > 32767
 -       || (unsigned)s->screen_height > 32767){
 -        av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
 -        return AVERROR_INVALIDDATA;
 -    }
++    s->screen_width  = bytestream2_get_le16u(&s->gb);
 +    s->screen_height = bytestream2_get_le16u(&s->gb);
  
 -    v = bytestream2_get_byte(&s->gb);
 +    v = bytestream2_get_byteu(&s->gb);
      s->color_resolution = ((v & 0x70) >> 4) + 1;
 -    has_global_palette = (v & 0x80);
 +    s->has_global_palette = (v & 0x80);
      s->bits_per_pixel = (v & 0x07) + 1;
 -    s->background_color_index = bytestream2_get_byte(&s->gb);
 -    bytestream2_get_byte(&s->gb);                /* ignored */
 +    background_color_index = bytestream2_get_byteu(&s->gb);
 +    n = bytestream2_get_byteu(&s->gb);
 +    if (n) {
 +        s->avctx->sample_aspect_ratio.num = n + 15;
 +        s->avctx->sample_aspect_ratio.den = 64;
 +    }
  
 -    av_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
 +    av_dlog(s->avctx, "screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
             s->screen_width, s->screen_height, s->bits_per_pixel,
 -           has_global_palette);
 +           s->has_global_palette);
  
 -    if (has_global_palette) {
 +    if (s->has_global_palette) {
 +        s->background_color_index = background_color_index;
          n = 1 << s->bits_per_pixel;
          if (bytestream2_get_bytes_left(&s->gb) < n * 3)
              return AVERROR_INVALIDDATA;
@@@ -498,11 -307,8 +497,11 @@@ static int gif_decode_frame(AVCodecCont
      if (ret < 0)
          return ret;
  
 +    if ((ret = av_frame_ref(data, s->frame)) < 0)
 +        return ret;
      *got_frame = 1;
-     return avpkt->size;
 +
+     return bytestream2_tell(&s->gb);
  }
  
  static av_cold int gif_decode_close(AVCodecContext *avctx)