Merge commit 'a0cabd0a27587525e90a44660c795d40d2f44fe2'
authorMichael Niedermayer <michaelni@gmx.at>
Sat, 26 Jan 2013 14:19:48 +0000 (15:19 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Sat, 26 Jan 2013 14:19:48 +0000 (15:19 +0100)
* commit 'a0cabd0a27587525e90a44660c795d40d2f44fe2':
  mimic: cosmetics, reformat
  iff: drop ff_ prefix from a static function.
  pngdec: cosmetics, reformat.
  dxa: return meaningful error codes.
  eatgq: cosmetics, reformat.

Conflicts:
libavcodec/dxa.c
libavcodec/eatgq.c
libavcodec/iff.c
libavcodec/mimic.c
libavcodec/pngdec.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavcodec/dxa.c
libavcodec/eatgq.c
libavcodec/iff.c
libavcodec/mimic.c
libavcodec/pngdec.c

@@@ -197,21 -198,25 +197,21 @@@ static int decode_frame(AVCodecContext 
      unsigned long dsize;
      int i, j, compr, ret;
      int stride;
 -    int orig_buf_size = buf_size;
      int pc = 0;
 +    GetByteContext gb;
  
 -    /* make the palette available on the way out */
 -    if(buf[0]=='C' && buf[1]=='M' && buf[2]=='A' && buf[3]=='P'){
 -        int r, g, b;
 +    bytestream2_init(&gb, avpkt->data, avpkt->size);
  
 -        buf += 4;
 +    /* make the palette available on the way out */
 +    if (bytestream2_peek_le32(&gb) == MKTAG('C','M','A','P')) {
 +        bytestream2_skip(&gb, 4);
          for(i = 0; i < 256; i++){
 -            r = *buf++;
 -            g = *buf++;
 -            b = *buf++;
 -            c->pal[i] = (r << 16) | (g << 8) | b;
 +            c->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
          }
          pc = 1;
 -        buf_size -= 768+4;
      }
  
-     if ((ret = ff_get_buffer(avctx, &c->pic)) < 0){
+     if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
          av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
          return ret;
      }
      tmpptr = c->prev.data[0];
      stride = c->pic.linesize[0];
  
 -    if(buf[0]=='N' && buf[1]=='U' && buf[2]=='L' && buf[3]=='L')
 +    if (bytestream2_get_le32(&gb) == MKTAG('N','U','L','L'))
          compr = -1;
      else
 -        compr = buf[4];
 +        compr = bytestream2_get_byte(&gb);
  
      dsize = c->dsize;
 -    if((compr != 4 && compr != -1) && uncompress(c->decomp_buf, &dsize, buf + 9, buf_size - 9) != Z_OK){
 -        av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
 -        return AVERROR_UNKNOWN;
 +    if (compr != 4 && compr != -1) {
 +        bytestream2_skip(&gb, 4);
 +        if (uncompress(c->decomp_buf, &dsize, avpkt->data + bytestream2_tell(&gb),
 +                       bytestream2_get_bytes_left(&gb)) != Z_OK) {
 +            av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
-             return AVERROR_INVALIDDATA;
++            return AVERROR_UNKNOWN;
 +        }
      }
      switch(compr){
      case -1:
@@@ -142,19 -148,17 +148,17 @@@ static void tgq_idct_put_mb_dconly(TgqC
      }
  }
  
- /**
-  * @return <0 on error
-  */
- static int tgq_decode_mb(TgqContext *s, int mb_y, int mb_x){
 -static void tgq_decode_mb(TgqContext *s, int mb_y, int mb_x)
++static int tgq_decode_mb(TgqContext *s, int mb_y, int mb_x)
+ {
      int mode;
      int i;
      int8_t dc[6];
  
      mode = bytestream2_get_byte(&s->gb);
-     if (mode>12) {
+     if (mode > 12) {
          GetBitContext gb;
 -        init_get_bits(&gb, s->gb.buffer, FFMIN(s->gb.buffer_end - s->gb.buffer, mode) * 8);
 +        init_get_bits(&gb, s->gb.buffer, FFMIN(bytestream2_get_bytes_left(&s->gb), mode) * 8);
-         for(i=0; i<6; i++)
+         for (i = 0; i < 6; i++)
              tgq_decode_block(s, s->block[i], &gb);
          tgq_idct_put_mb(s, s->block, mb_x, mb_y);
          bytestream2_skip(&s->gb, mode);
                  dc[i] = bytestream2_get_byte(&s->gb);
                  bytestream2_skip(&s->gb, 1);
              }
-         }else{
+         } else {
              av_log(s->avctx, AV_LOG_ERROR, "unsupported mb mode %i\n", mode);
 +            return -1;
          }
          tgq_idct_put_mb_dconly(s, mb_x, mb_y, dc);
      }
 +    return 0;
  }
  
- static void tgq_calculate_qtable(TgqContext *s, int quant){
-     int i,j;
-     const int a = (14*(100-quant))/100 + 1;
-     const int b = (11*(100-quant))/100 + 4;
-     for(j=0;j<8;j++)
-     for(i=0;i<8;i++)
-         s->qtable[j*8+i] = ((a*(j+i)/(7+7) + b)*ff_inv_aanscales[j*8+i])>>(14-4);
+ static void tgq_calculate_qtable(TgqContext *s, int quant)
+ {
+     int i, j;
+     const int a = (14 * (100 - quant)) / 100 + 1;
+     const int b = (11 * (100 - quant)) / 100 + 4;
+     for (j = 0; j < 8; j++)
+         for (i = 0; i < 8; i++)
+             s->qtable[j * 8 + i] = ((a * (j + i) / (7 + 7) + b) *
+                                     ff_inv_aanscales[j * 8 + i]) >> (14 - 4);
  }
  
  static int tgq_decode_frame(AVCodecContext *avctx,
                              void *data, int *got_frame,
-                             AVPacket *avpkt){
+                             AVPacket *avpkt)
+ {
      const uint8_t *buf = avpkt->data;
-     int buf_size = avpkt->size;
-     TgqContext *s = avctx->priv_data;
+     int buf_size       = avpkt->size;
+     TgqContext *s      = avctx->priv_data;
      int x, y, ret;
 -    int big_endian = AV_RL32(&buf[4]) > 0x000FFFFF;
 +    int big_endian;
  
      if (buf_size < 16) {
          av_log(avctx, AV_LOG_WARNING, "truncated header\n");
@@@ -139,15 -120,12 +139,15 @@@ static av_always_inline uint32_t gray2r
  /**
   * Convert CMAP buffer (stored in extradata) to lavc palette format
   */
- static int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
+ static int cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
  {
 +    IffContext *s = avctx->priv_data;
      int count, i;
 +    const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);
 +    int palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  
      if (avctx->bits_per_coded_sample > 8) {
 -        av_log(avctx, AV_LOG_ERROR, "bit_per_coded_sample > 8 not supported\n");
 +        av_log(avctx, AV_LOG_ERROR, "bits_per_coded_sample > 8 not supported\n");
          return AVERROR_INVALIDDATA;
      }
  
@@@ -670,12 -321,9 +670,12 @@@ static int decode_frame(AVCodecContext 
      } else if ((res = ff_get_buffer(avctx, &s->frame)) < 0) {
          av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
          return res;
 -    } else if (avctx->bits_per_coded_sample <= 8 && avctx->pix_fmt != AV_PIX_FMT_GRAY8) {
 +    } else if (avctx->bits_per_coded_sample <= 8 && avctx->pix_fmt == AV_PIX_FMT_PAL8) {
-         if ((res = ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1])) < 0)
+         if ((res = cmap_read_palette(avctx, (uint32_t*)s->frame.data[1])) < 0)
              return res;
-         if ((res = ff_cmap_read_palette(avctx, s->mask_palbuf)) < 0)
 +    } else if (avctx->pix_fmt == AV_PIX_FMT_RGB32 && avctx->bits_per_coded_sample <= 8) {
++        if ((res = cmap_read_palette(avctx, s->mask_palbuf)) < 0)
 +            return res;
      }
      s->init = 1;
  
@@@ -355,9 -357,10 +357,10 @@@ static int mimic_decode_frame(AVCodecCo
          return -1;
      }
  
 -    ctx->buf_ptrs[ctx->cur_index].reference = 1;
 +    ctx->buf_ptrs[ctx->cur_index].reference = 3;
-     ctx->buf_ptrs[ctx->cur_index].pict_type = is_pframe ? AV_PICTURE_TYPE_P:AV_PICTURE_TYPE_I;
-     if(ff_thread_get_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index])) {
+     ctx->buf_ptrs[ctx->cur_index].pict_type = is_pframe ? AV_PICTURE_TYPE_P :
+                                                           AV_PICTURE_TYPE_I;
+     if (ff_thread_get_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index])) {
          av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
          return -1;
      }
@@@ -96,12 -86,16 +96,13 @@@ static void png_put_interlaced_row(uint
      uint8_t *d;
      const uint8_t *s;
  
-     mask = png_pass_mask[pass];
 -    mask     = ff_png_pass_mask[pass];
++    mask     = png_pass_mask[pass];
      dsp_mask = png_pass_dsp_mask[pass];
-     switch(bits_per_pixel) {
+     switch (bits_per_pixel) {
      case 1:
 -        /* we must initialize the line to zero before writing to it */
 -        if (pass == 0)
 -            memset(dst, 0, (width + 7) >> 3);
          src_x = 0;
-         for(x = 0; x < width; x++) {
+         for (x = 0; x < width; x++) {
              j = (x & 7);
              if ((dsp_mask << j) & 0x80) {
                  b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
                  src_x++;
          }
          break;
-         for(x = 0; x < width; x++) {
-             int j2 = 2*(x&3);
 +    case 2:
 +        src_x = 0;
-         for(x = 0; x < width; x++) {
++        for (x = 0; x < width; x++) {
++            int j2 = 2 * (x & 3);
 +            j = (x & 7);
 +            if ((dsp_mask << j) & 0x80) {
 +                b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
 +                dst[x >> 2] &= 0xFF3F>>j2;
 +                dst[x >> 2] |= b << (6 - j2);
 +            }
 +            if ((mask << j) & 0x80)
 +                src_x++;
 +        }
 +        break;
 +    case 4:
 +        src_x = 0;
++        for (x = 0; x < width; x++) {
 +            int j2 = 4*(x&1);
 +            j = (x & 7);
 +            if ((dsp_mask << j) & 0x80) {
 +                b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
 +                dst[x >> 1] &= 0xFF0F>>j2;
 +                dst[x >> 1] |= b << (4 - j2);
 +            }
 +            if ((mask << j) & 0x80)
 +                src_x++;
 +        }
 +        break;
      default:
          bpp = bits_per_pixel >> 3;
-         d = dst;
-         s = src;
-             for(x = 0; x < width; x++) {
+         d   = dst;
+         s   = src;
 -        if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
+             for (x = 0; x < width; x++) {
                  j = x & 7;
                  if ((dsp_mask << j) & 0x80) {
 -                    *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
 -                }
 -                d += bpp;
 -                if ((mask << j) & 0x80)
 -                    s += bpp;
 -            }
 -        } else {
 -            for(x = 0; x < width; x++) {
 -                j = x & 7;
 -                if ((dsp_mask << j) & 0x80) {
                      memcpy(d, s, bpp);
                  }
                  d += bpp;
@@@ -255,13 -232,13 +256,13 @@@ static void png_filter_row(PNGDSPContex
              p = last[i];
              dst[i] = p + src[i];
          }
-         if(bpp > 2 && size > 4) {
 -        if (bpp > 1 && size > 4) {
++        if (bpp > 2 && size > 4) {
              // would write off the end of the array if we let it process the last pixel with bpp=3
-             int w = bpp==4 ? size : size-3;
-             dsp->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
+             int w = bpp == 4 ? size : size - 3;
+             dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
              i = w;
          }
-         ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
+         ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
          break;
      }
  }
@@@ -340,8 -339,7 +341,8 @@@ static void png_handle_row(PNGDecContex
              }
              s->y++;
              if (s->y == s->height) {
-                 for(;;) {
 +                memset(s->last_row, 0, s->row_size);
+                 for (;;) {
                      if (s->pass == NB_PASSES - 1) {
                          s->state |= PNG_ALLIMAGE;
                          goto the_end;
@@@ -367,7 -365,7 +368,7 @@@ static int png_decode_idat(PNGDecContex
  {
      int ret;
      s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
-     s->zstream.next_in = (unsigned char *)s->gb.buffer;
 -    s->zstream.next_in  = s->gb.buffer;
++    s->zstream.next_in  = (unsigned char *)s->gb.buffer;
      bytestream2_skip(&s->gb, length);
  
      /* decode one line if possible */
@@@ -508,57 -389,43 +509,56 @@@ static int decode_frame(AVCodecContext 
                          void *data, int *got_frame,
                          AVPacket *avpkt)
  {
-     const uint8_t *buf = avpkt->data;
-     int buf_size = avpkt->size;
      PNGDecContext * const s = avctx->priv_data;
-     AVFrame *picture = data;
+     const uint8_t *buf      = avpkt->data;
+     int buf_size            = avpkt->size;
+     AVFrame *picture        = data;
++    AVDictionary *metadata  = NULL;
+     uint8_t *crow_buf_base  = NULL;
      AVFrame *p;
-     AVDictionary *metadata = NULL;
-     uint8_t *crow_buf_base = NULL;
      uint32_t tag, length;
 +    int64_t sig;
      int ret;
  
      FFSWAP(AVFrame *, s->current_picture, s->last_picture);
-     avctx->coded_frame= s->current_picture;
+     avctx->coded_frame = s->current_picture;
      p = s->current_picture;
  
 +    bytestream2_init(&s->gb, buf, buf_size);
 +
      /* check signature */
 -    if (buf_size < 8 ||
 -        memcmp(buf, ff_pngsig, 8) != 0 &&
 -        memcmp(buf, ff_mngsig, 8) != 0)
 +    sig = bytestream2_get_be64(&s->gb);
 +    if (sig != PNGSIG &&
 +        sig != MNGSIG) {
 +        av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
          return -1;
 +    }
  
-     s->y=
-     s->state=0;
- //    memset(s, 0, sizeof(PNGDecContext));
 -    bytestream2_init(&s->gb, buf + 8, buf_size - 8);
+     s->y = s->state = 0;
      /* init the zlib */
      s->zstream.zalloc = ff_png_zalloc;
-     s->zstream.zfree = ff_png_zfree;
+     s->zstream.zfree  = ff_png_zfree;
      s->zstream.opaque = NULL;
      ret = inflateInit(&s->zstream);
 -    if (ret != Z_OK)
 +    if (ret != Z_OK) {
 +        av_log(avctx, AV_LOG_ERROR, "inflateInit returned %d\n", ret);
          return -1;
-     for(;;) {
 +    }
 -        if (bytestream2_get_bytes_left(&s->gb) <= 0)
+     for (;;) {
 +        if (bytestream2_get_bytes_left(&s->gb) <= 0) {
 +            av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
              goto fail;
 +        }
 +
          length = bytestream2_get_be32(&s->gb);
 -        if (length > 0x7fffffff)
 +        if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb))  {
 +            av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
              goto fail;
 +        }
          tag = bytestream2_get_le32(&s->gb);
 -        av_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
 +        if (avctx->debug & FF_DEBUG_STARTCODE)
 +            av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
                  (tag & 0xff),
                  ((tag >> 8) & 0xff),
                  ((tag >> 16) & 0xff),
                  goto fail;
              s->width  = bytestream2_get_be32(&s->gb);
              s->height = bytestream2_get_be32(&s->gb);
-             if(av_image_check_size(s->width, s->height, 0, avctx)){
-                 s->width= s->height= 0;
+             if (av_image_check_size(s->width, s->height, 0, avctx)) {
+                 s->width = s->height = 0;
 +                av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
                  goto fail;
              }
              s->bit_depth        = bytestream2_get_byte(&s->gb);
              s->interlace_type   = bytestream2_get_byte(&s->gb);
              bytestream2_skip(&s->gb, 4); /* crc */
              s->state |= PNG_IHDR;
 -            av_dlog(avctx, "width=%d height=%d depth=%d color_type=%d "
 +            if (avctx->debug & FF_DEBUG_PICT_INFO)
-                 av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
++                av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
+                     "compression_type=%d filter_type=%d interlace_type=%d\n",
                      s->width, s->height, s->bit_depth, s->color_type,
                      s->compression_type, s->filter_type, s->interlace_type);
              break;
 +        case MKTAG('p', 'H', 'Y', 's'):
 +            if (s->state & PNG_IDAT) {
 +                av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
 +                goto fail;
 +            }
 +            avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
 +            avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
 +            if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
 +                avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
 +            bytestream2_skip(&s->gb, 1); /* unit specifier */
 +            bytestream2_skip(&s->gb, 4); /* crc */
 +            break;
          case MKTAG('I', 'D', 'A', 'T'):
 -            if (!(s->state & PNG_IHDR))
 +            if (!(s->state & PNG_IHDR)) {
 +                av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
                  goto fail;
 +            }
              if (!(s->state & PNG_IDAT)) {
                  /* init image info */
-                 avctx->width = s->width;
+                 avctx->width  = s->width;
                  avctx->height = s->height;
  
-                 s->channels = ff_png_get_nb_channels(s->color_type);
+                 s->channels       = ff_png_get_nb_channels(s->color_type);
                  s->bits_per_pixel = s->bit_depth * s->channels;
-                 s->bpp = (s->bits_per_pixel + 7) >> 3;
-                 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
+                 s->bpp            = (s->bits_per_pixel + 7) >> 3;
+                 s->row_size       = (avctx->width * s->bits_per_pixel + 7) >> 3;
  
 -                if (s->bit_depth == 8 &&
 +                if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
                      s->color_type == PNG_COLOR_TYPE_RGB) {
                      avctx->pix_fmt = AV_PIX_FMT_RGB24;
 -                } else if (s->bit_depth == 8 &&
 +                } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
                             s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
 -                    avctx->pix_fmt = AV_PIX_FMT_RGB32;
 -                } else if (s->bit_depth == 8 &&
 +                    avctx->pix_fmt = AV_PIX_FMT_RGBA;
 +                } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
                             s->color_type == PNG_COLOR_TYPE_GRAY) {
                      avctx->pix_fmt = AV_PIX_FMT_GRAY8;
                  } else if (s->bit_depth == 16 &&
                             s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
                      avctx->pix_fmt = AV_PIX_FMT_Y400A;
                  } else {
 +                    av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
 +                                                "and color type %d\n",
 +                                                 s->bit_depth, s->color_type);
                      goto fail;
                  }
-                 if(p->data[0])
+                 if (p->data[0])
                      avctx->release_buffer(avctx, p);
  
-                 p->reference= 3;
-                 if(ff_get_buffer(avctx, p) < 0){
 -                p->reference = 0;
++                p->reference = 3;
+                 if (ff_get_buffer(avctx, p) < 0) {
                      av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
                      goto fail;
                  }
                  }
                  av_dlog(avctx, "row_size=%d crow_size =%d\n",
                          s->row_size, s->crow_size);
-                 s->image_buf = p->data[0];
+                 s->image_buf      = p->data[0];
                  s->image_linesize = p->linesize[0];
                  /* copy the palette if needed */
 -                if (s->color_type == PNG_COLOR_TYPE_PALETTE)
 +                if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
                      memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
                  /* empty row is used if differencing to the first row */
                  s->last_row = av_mallocz(s->row_size);
                      r = bytestream2_get_byte(&s->gb);
                      g = bytestream2_get_byte(&s->gb);
                      b = bytestream2_get_byte(&s->gb);
 -                    s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
 +                    s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
                  }
-                 for(;i<256;i++) {
+                 for (; i < 256; i++) {
 -                    s->palette[i] = (0xff << 24);
 +                    s->palette[i] = (0xFFU << 24);
                  }
                  s->state |= PNG_PLTE;
                  bytestream2_skip(&s->gb, 4); /* crc */
          }
      }
   exit_loop:
-     if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
 +
-         for(j=0; j < s->height; j++) {
-             for(i=s->width/8-1; i>=0; i--) {
-                 pd[8*i+7]=  pd[i]    &1;
-                 pd[8*i+6]= (pd[i]>>1)&1;
-                 pd[8*i+5]= (pd[i]>>2)&1;
-                 pd[8*i+4]= (pd[i]>>3)&1;
-                 pd[8*i+3]= (pd[i]>>4)&1;
-                 pd[8*i+2]= (pd[i]>>5)&1;
-                 pd[8*i+1]= (pd[i]>>6)&1;
-                 pd[8*i+0]=  pd[i]>>7;
++    if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
 +        int i, j;
 +        uint8_t *pd = s->current_picture->data[0];
-     if(s->bits_per_pixel == 2){
++        for (j = 0; j < s->height; j++) {
++            for (i = s->width/8-1; i >= 0; i--) {
++                pd[8*i + 7]=  pd[i]     & 1;
++                pd[8*i + 6]= (pd[i]>>1) & 1;
++                pd[8*i + 5]= (pd[i]>>2) & 1;
++                pd[8*i + 4]= (pd[i]>>3) & 1;
++                pd[8*i + 3]= (pd[i]>>4) & 1;
++                pd[8*i + 2]= (pd[i]>>5) & 1;
++                pd[8*i + 1]= (pd[i]>>6) & 1;
++                pd[8*i + 0]=  pd[i]>>7;
 +            }
 +            pd += s->image_linesize;
 +        }
 +    }
-         for(j=0; j < s->height; j++) {
-             i = s->width/4;
++    if (s->bits_per_pixel == 2){
 +        int i, j;
 +        uint8_t *pd = s->current_picture->data[0];
-                 if ((s->width&3) >= 3) pd[4*i+2]= (pd[i]>>2)&3;
-                 if ((s->width&3) >= 2) pd[4*i+1]= (pd[i]>>4)&3;
-                 if ((s->width&3) >= 1) pd[4*i+0]=  pd[i]>>6;
-                 for(i--; i>=0; i--) {
-                 pd[4*i+3]=  pd[i]    &3;
-                 pd[4*i+2]= (pd[i]>>2)&3;
-                 pd[4*i+1]= (pd[i]>>4)&3;
-                 pd[4*i+0]=  pd[i]>>6;
-             }
++        for (j = 0; j < s->height; j++) {
++            i = s->width / 4;
 +            if (s->color_type == PNG_COLOR_TYPE_PALETTE){
-                 if ((s->width&3) >= 3) pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
-                 if ((s->width&3) >= 2) pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
-                 if ((s->width&3) >= 1) pd[4*i+0]= ( pd[i]>>6   )*0x55;
-                 for(i--; i>=0; i--) {
-                     pd[4*i+3]= ( pd[i]    &3)*0x55;
-                     pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
-                     pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
-                     pd[4*i+0]= ( pd[i]>>6   )*0x55;
++                if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
++                if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
++                if ((s->width&3) >= 1) pd[4*i + 0]=  pd[i] >> 6;
++                for (i--; i >= 0; i--) {
++                    pd[4*i + 3]=  pd[i]     & 3;
++                    pd[4*i + 2]= (pd[i]>>2) & 3;
++                    pd[4*i + 1]= (pd[i]>>4) & 3;
++                    pd[4*i + 0]=  pd[i]>>6;
++                }
 +            } else {
-     if(s->bits_per_pixel == 4){
++                if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
++                if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
++                if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6     )*0x55;
++                for (i--; i >= 0; i--) {
++                    pd[4*i + 3]= ( pd[i]     & 3)*0x55;
++                    pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
++                    pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
++                    pd[4*i + 0]= ( pd[i]>>6     )*0x55;
 +                }
 +            }
 +            pd += s->image_linesize;
 +        }
 +    }
-         for(j=0; j < s->height; j++) {
-             i=s->width/2;
++    if (s->bits_per_pixel == 4){
 +        int i, j;
 +        uint8_t *pd = s->current_picture->data[0];
-                 for(i--; i>=0; i--) {
-                 pd[2*i+1]= pd[i]&15;
-                 pd[2*i+0]= pd[i]>>4;
++        for (j = 0; j < s->height; j++) {
++            i = s->width/2;
 +            if (s->color_type == PNG_COLOR_TYPE_PALETTE){
 +                if (s->width&1) pd[2*i+0]= pd[i]>>4;
-                 if (s->width&1) pd[2*i+0]= (pd[i]>>4)*0x11;
-                 for(i--; i>=0; i--) {
-                     pd[2*i+1]= (pd[i]&15)*0x11;
-                     pd[2*i+0]= (pd[i]>>4)*0x11;
++                for (i--; i >= 0; i--) {
++                pd[2*i + 1] = pd[i] & 15;
++                pd[2*i + 0] = pd[i] >> 4;
 +            }
 +            } else {
++                if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
++                for (i--; i >= 0; i--) {
++                    pd[2*i + 1] = (pd[i] & 15) * 0x11;
++                    pd[2*i + 0] = (pd[i] >> 4) * 0x11;
 +                }
 +            }
 +            pd += s->image_linesize;
 +        }
 +    }
 +
       /* handle p-frames only if a predecessor frame is available */
-      if(s->last_picture->data[0] != NULL) {
-          if(   !(avpkt->flags & AV_PKT_FLAG_KEY)
+      if (s->last_picture->data[0] != NULL) {
 -         if (!(avpkt->flags & AV_PKT_FLAG_KEY)) {
++         if (   !(avpkt->flags & AV_PKT_FLAG_KEY)
 +            && s->last_picture->width == s->current_picture->width
 +            && s->last_picture->height== s->current_picture->height
 +            && s->last_picture->format== s->current_picture->format
 +         ) {
              int i, j;
-             uint8_t *pd = s->current_picture->data[0];
+             uint8_t *pd      = s->current_picture->data[0];
              uint8_t *pd_last = s->last_picture->data[0];
  
-             for(j=0; j < s->height; j++) {
-                 for(i=0; i < s->width * s->bpp; i++) {
+             for (j = 0; j < s->height; j++) {
+                 for (i = 0; i < s->width * s->bpp; i++) {
                      pd[i] += pd_last[i];
                  }
-                 pd += s->image_linesize;
+                 pd      += s->image_linesize;
                  pd_last += s->image_linesize;
              }
          }
      }
  
-     metadata = NULL;
-     *picture= *s->current_picture;
 +    s->current_picture->metadata = metadata;
++    metadata   = NULL;
+     *picture   = *s->current_picture;
      *got_frame = 1;
  
      ret = bytestream2_tell(&s->gb);
@@@ -876,14 -634,11 +877,14 @@@ static av_cold int png_dec_init(AVCodec
      PNGDecContext *s = avctx->priv_data;
  
      s->current_picture = &s->picture1;
-     s->last_picture = &s->picture2;
+     s->last_picture    = &s->picture2;
      avcodec_get_frame_defaults(&s->picture1);
      avcodec_get_frame_defaults(&s->picture2);
 +
      ff_pngdsp_init(&s->dsp);
  
 +    s->avctx = avctx;
 +
      return 0;
  }