Merge commit '9f5b77c16f4da6248b57f0601364d9c762c620c2'
authorJames Almer <jamrial@gmail.com>
Sat, 11 Nov 2017 12:58:01 +0000 (09:58 -0300)
committerJames Almer <jamrial@gmail.com>
Sat, 11 Nov 2017 12:58:01 +0000 (09:58 -0300)
* commit '9f5b77c16f4da6248b57f0601364d9c762c620c2':
  png: Report more details regarding unsupported pixel formats

Merged-by: James Almer <jamrial@gmail.com>
1  2 
libavcodec/pngdec.c

index 1d72f9542a730f12858f5d801f19466ea52d9602,7dc5c283d48c91aa76f276e96bca21ab6fd6c30e..f93f200bb152472c24144fc8d849db9f200fb08d
@@@ -431,844 -405,214 +431,844 @@@ static int png_decode_idat(PNGDecContex
      return 0;
  }
  
 -static int decode_frame(AVCodecContext *avctx,
 -                        void *data, int *got_frame,
 -                        AVPacket *avpkt)
 +static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
 +                       const uint8_t *data_end)
  {
 -    PNGDecContext *const s = avctx->priv_data;
 -    const uint8_t *buf     = avpkt->data;
 -    int buf_size           = avpkt->size;
 -    AVFrame *p             = data;
 -    uint8_t *crow_buf_base = NULL;
 -    uint32_t tag, length;
 +    z_stream zstream;
 +    unsigned char *buf;
 +    unsigned buf_size;
      int ret;
  
 -    /* check signature */
 -    if (buf_size < 8) {
 -        av_log(avctx, AV_LOG_ERROR, "Not enough data %d\n",
 -               buf_size);
 +    zstream.zalloc = ff_png_zalloc;
 +    zstream.zfree  = ff_png_zfree;
 +    zstream.opaque = NULL;
 +    if (inflateInit(&zstream) != Z_OK)
 +        return AVERROR_EXTERNAL;
 +    zstream.next_in  = (unsigned char *)data;
 +    zstream.avail_in = data_end - data;
 +    av_bprint_init(bp, 0, -1);
 +
 +    while (zstream.avail_in > 0) {
 +        av_bprint_get_buffer(bp, 2, &buf, &buf_size);
 +        if (buf_size < 2) {
 +            ret = AVERROR(ENOMEM);
 +            goto fail;
 +        }
 +        zstream.next_out  = buf;
 +        zstream.avail_out = buf_size - 1;
 +        ret = inflate(&zstream, Z_PARTIAL_FLUSH);
 +        if (ret != Z_OK && ret != Z_STREAM_END) {
 +            ret = AVERROR_EXTERNAL;
 +            goto fail;
 +        }
 +        bp->len += zstream.next_out - buf;
 +        if (ret == Z_STREAM_END)
 +            break;
 +    }
 +    inflateEnd(&zstream);
 +    bp->str[bp->len] = 0;
 +    return 0;
 +
 +fail:
 +    inflateEnd(&zstream);
 +    av_bprint_finalize(bp, NULL);
 +    return ret;
 +}
 +
 +static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
 +{
 +    size_t extra = 0, i;
 +    uint8_t *out, *q;
 +
 +    for (i = 0; i < size_in; i++)
 +        extra += in[i] >= 0x80;
 +    if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
 +        return NULL;
 +    q = out = av_malloc(size_in + extra + 1);
 +    if (!out)
 +        return NULL;
 +    for (i = 0; i < size_in; i++) {
 +        if (in[i] >= 0x80) {
 +            *(q++) = 0xC0 | (in[i] >> 6);
 +            *(q++) = 0x80 | (in[i] & 0x3F);
 +        } else {
 +            *(q++) = in[i];
 +        }
 +    }
 +    *(q++) = 0;
 +    return out;
 +}
 +
 +static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
 +                             AVDictionary **dict)
 +{
 +    int ret, method;
 +    const uint8_t *data        = s->gb.buffer;
 +    const uint8_t *data_end    = data + length;
 +    const uint8_t *keyword     = data;
 +    const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
 +    uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
 +    unsigned text_len;
 +    AVBPrint bp;
 +
 +    if (!keyword_end)
 +        return AVERROR_INVALIDDATA;
 +    data = keyword_end + 1;
 +
 +    if (compressed) {
 +        if (data == data_end)
 +            return AVERROR_INVALIDDATA;
 +        method = *(data++);
 +        if (method)
 +            return AVERROR_INVALIDDATA;
 +        if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
 +            return ret;
 +        text_len = bp.len;
 +        ret = av_bprint_finalize(&bp, (char **)&text);
 +        if (ret < 0)
 +            return ret;
 +    } else {
 +        text = (uint8_t *)data;
 +        text_len = data_end - text;
 +    }
 +
 +    kw_utf8  = iso88591_to_utf8(keyword, keyword_end - keyword);
 +    txt_utf8 = iso88591_to_utf8(text, text_len);
 +    if (text != data)
 +        av_free(text);
 +    if (!(kw_utf8 && txt_utf8)) {
 +        av_free(kw_utf8);
 +        av_free(txt_utf8);
 +        return AVERROR(ENOMEM);
 +    }
 +
 +    av_dict_set(dict, kw_utf8, txt_utf8,
 +                AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
 +    return 0;
 +}
 +
 +static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,
 +                             uint32_t length)
 +{
 +    if (length != 13)
 +        return AVERROR_INVALIDDATA;
 +
 +    if (s->pic_state & PNG_IDAT) {
 +        av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
 +        return AVERROR_INVALIDDATA;
 +    }
 +
 +    if (s->hdr_state & PNG_IHDR) {
 +        av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");
 +        return AVERROR_INVALIDDATA;
 +    }
 +
 +    s->width  = s->cur_w = bytestream2_get_be32(&s->gb);
 +    s->height = s->cur_h = bytestream2_get_be32(&s->gb);
 +    if (av_image_check_size(s->width, s->height, 0, avctx)) {
 +        s->cur_w = s->cur_h = s->width = s->height = 0;
 +        av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
          return AVERROR_INVALIDDATA;
      }
 -    if (memcmp(buf, ff_pngsig, 8) != 0 &&
 -        memcmp(buf, ff_mngsig, 8) != 0) {
 -        char signature[5 * 8 + 1] = { 0 };
 -        int i;
 -        for (i = 0; i < 8; i++) {
 -            av_strlcatf(signature + i * 5, sizeof(signature) - i * 5,
 -                        " 0x%02x", buf[i]);
 +    s->bit_depth        = bytestream2_get_byte(&s->gb);
 +    if (s->bit_depth != 1 && s->bit_depth != 2 && s->bit_depth != 4 &&
 +        s->bit_depth != 8 && s->bit_depth != 16) {
 +        av_log(avctx, AV_LOG_ERROR, "Invalid bit depth\n");
 +        goto error;
 +    }
 +    s->color_type       = bytestream2_get_byte(&s->gb);
 +    s->compression_type = bytestream2_get_byte(&s->gb);
 +    s->filter_type      = bytestream2_get_byte(&s->gb);
 +    s->interlace_type   = bytestream2_get_byte(&s->gb);
 +    bytestream2_skip(&s->gb, 4); /* crc */
 +    s->hdr_state |= PNG_IHDR;
 +    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",
 +                s->width, s->height, s->bit_depth, s->color_type,
 +                s->compression_type, s->filter_type, s->interlace_type);
 +
 +    return 0;
 +error:
 +    s->cur_w = s->cur_h = s->width = s->height = 0;
 +    s->bit_depth = 8;
 +    return AVERROR_INVALIDDATA;
 +}
 +
 +static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s)
 +{
 +    if (s->pic_state & PNG_IDAT) {
 +        av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
 +        return AVERROR_INVALIDDATA;
 +    }
 +    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 */
 +
 +    return 0;
 +}
 +
 +static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
 +                             uint32_t length, AVFrame *p)
 +{
 +    int ret;
 +    size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
 +
 +    if (!(s->hdr_state & PNG_IHDR)) {
 +        av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
 +        return AVERROR_INVALIDDATA;
 +    }
 +    if (!(s->pic_state & PNG_IDAT)) {
 +        /* init image info */
 +        ret = ff_set_dimensions(avctx, s->width, s->height);
 +        if (ret < 0)
 +            return ret;
 +
 +        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       = (s->cur_w * s->bits_per_pixel + 7) >> 3;
 +
 +        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 == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
 +                s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
 +            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) {
 +            avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
 +        } else if (s->bit_depth == 16 &&
 +                s->color_type == PNG_COLOR_TYPE_RGB) {
 +            avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
 +        } else if (s->bit_depth == 16 &&
 +                s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
 +            avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
 +        } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
 +                s->color_type == PNG_COLOR_TYPE_PALETTE) {
 +            avctx->pix_fmt = AV_PIX_FMT_PAL8;
 +        } else if (s->bit_depth == 1 && s->bits_per_pixel == 1 && avctx->codec_id != AV_CODEC_ID_APNG) {
 +            avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
 +        } else if (s->bit_depth == 8 &&
 +                s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
 +            avctx->pix_fmt = AV_PIX_FMT_YA8;
 +        } else if (s->bit_depth == 16 &&
 +                s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
 +            avctx->pix_fmt = AV_PIX_FMT_YA16BE;
 +        } else {
-             av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
-                     "and color type %d\n",
-                     s->bit_depth, s->color_type);
-             return AVERROR_INVALIDDATA;
++            avpriv_report_missing_feature(avctx,
++                                          "Bit depth %d color type %d",
++                                          s->bit_depth, s->color_type);
++            return AVERROR_PATCHWELCOME;
 +        }
 +
 +        if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
 +            switch (avctx->pix_fmt) {
 +            case AV_PIX_FMT_RGB24:
 +                avctx->pix_fmt = AV_PIX_FMT_RGBA;
 +                break;
 +
 +            case AV_PIX_FMT_RGB48BE:
 +                avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
 +                break;
 +
 +            case AV_PIX_FMT_GRAY8:
 +                avctx->pix_fmt = AV_PIX_FMT_YA8;
 +                break;
 +
 +            case AV_PIX_FMT_GRAY16BE:
 +                avctx->pix_fmt = AV_PIX_FMT_YA16BE;
 +                break;
 +
 +            default:
 +                avpriv_request_sample(avctx, "bit depth %d "
 +                        "and color type %d with TRNS",
 +                        s->bit_depth, s->color_type);
 +                return AVERROR_INVALIDDATA;
 +            }
 +
 +            s->bpp += byte_depth;
 +        }
 +
 +        if ((ret = ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
 +            return ret;
 +        if (avctx->codec_id == AV_CODEC_ID_APNG && s->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
 +            ff_thread_release_buffer(avctx, &s->previous_picture);
 +            if ((ret = ff_thread_get_buffer(avctx, &s->previous_picture, AV_GET_BUFFER_FLAG_REF)) < 0)
 +                return ret;
 +        }
 +        p->pict_type        = AV_PICTURE_TYPE_I;
 +        p->key_frame        = 1;
 +        p->interlaced_frame = !!s->interlace_type;
 +
 +        ff_thread_finish_setup(avctx);
 +
 +        /* compute the compressed row size */
 +        if (!s->interlace_type) {
 +            s->crow_size = s->row_size + 1;
 +        } else {
 +            s->pass          = 0;
 +            s->pass_row_size = ff_png_pass_row_size(s->pass,
 +                    s->bits_per_pixel,
 +                    s->cur_w);
 +            s->crow_size = s->pass_row_size + 1;
          }
 -        av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature %s\n",
 -               signature);
 +        ff_dlog(avctx, "row_size=%d crow_size =%d\n",
 +                s->row_size, s->crow_size);
 +        s->image_buf      = p->data[0];
 +        s->image_linesize = p->linesize[0];
 +        /* copy the palette if needed */
 +        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 */
 +        av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
 +        if (!s->last_row)
 +            return AVERROR_INVALIDDATA;
 +        if (s->interlace_type ||
 +                s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
 +            av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
 +            if (!s->tmp_row)
 +                return AVERROR_INVALIDDATA;
 +        }
 +        /* compressed row */
 +        av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
 +        if (!s->buffer)
 +            return AVERROR(ENOMEM);
 +
 +        /* we want crow_buf+1 to be 16-byte aligned */
 +        s->crow_buf          = s->buffer + 15;
 +        s->zstream.avail_out = s->crow_size;
 +        s->zstream.next_out  = s->crow_buf;
 +    }
 +
 +    s->pic_state |= PNG_IDAT;
 +
 +    /* set image to non-transparent bpp while decompressing */
 +    if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
 +        s->bpp -= byte_depth;
 +
 +    ret = png_decode_idat(s, length);
 +
 +    if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
 +        s->bpp += byte_depth;
 +
 +    if (ret < 0)
 +        return ret;
 +
 +    bytestream2_skip(&s->gb, 4); /* crc */
 +
 +    return 0;
 +}
 +
 +static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s,
 +                             uint32_t length)
 +{
 +    int n, i, r, g, b;
 +
 +    if ((length % 3) != 0 || length > 256 * 3)
          return AVERROR_INVALIDDATA;
 +    /* read the palette */
 +    n = length / 3;
 +    for (i = 0; i < n; i++) {
 +        r = bytestream2_get_byte(&s->gb);
 +        g = bytestream2_get_byte(&s->gb);
 +        b = bytestream2_get_byte(&s->gb);
 +        s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
      }
 +    for (; i < 256; i++)
 +        s->palette[i] = (0xFFU << 24);
 +    s->hdr_state |= PNG_PLTE;
 +    bytestream2_skip(&s->gb, 4);     /* crc */
  
 -    bytestream2_init(&s->gb, buf + 8, buf_size - 8);
 -    s->y = s->state = 0;
 +    return 0;
 +}
 +
 +static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
 +                             uint32_t length)
 +{
 +    int v, i;
 +
 +    if (!(s->hdr_state & PNG_IHDR)) {
 +        av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");
 +        return AVERROR_INVALIDDATA;
 +    }
 +
 +    if (s->pic_state & PNG_IDAT) {
 +        av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");
 +        return AVERROR_INVALIDDATA;
 +    }
 +
 +    if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
 +        if (length > 256 || !(s->hdr_state & PNG_PLTE))
 +            return AVERROR_INVALIDDATA;
 +
 +        for (i = 0; i < length; i++) {
 +            unsigned v = bytestream2_get_byte(&s->gb);
 +            s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
 +        }
 +    } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {
 +        if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||
 +            (s->color_type == PNG_COLOR_TYPE_RGB && length != 6) ||
 +            s->bit_depth == 1)
 +            return AVERROR_INVALIDDATA;
 +
 +        for (i = 0; i < length / 2; i++) {
 +            /* only use the least significant bits */
 +            v = av_mod_uintp2(bytestream2_get_be16(&s->gb), s->bit_depth);
 +
 +            if (s->bit_depth > 8)
 +                AV_WB16(&s->transparent_color_be[2 * i], v);
 +            else
 +                s->transparent_color_be[i] = v;
 +        }
 +    } else {
 +        return AVERROR_INVALIDDATA;
 +    }
 +
 +    bytestream2_skip(&s->gb, 4); /* crc */
 +    s->has_trns = 1;
 +
 +    return 0;
 +}
 +
 +static int decode_iccp_chunk(PNGDecContext *s, int length, AVFrame *f)
 +{
 +    int ret, cnt = 0;
 +    uint8_t *data, profile_name[82];
 +    AVBPrint bp;
 +    AVFrameSideData *sd;
 +
 +    while ((profile_name[cnt++] = bytestream2_get_byte(&s->gb)) && cnt < 81);
 +    if (cnt > 80) {
 +        av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid name!\n");
 +        return AVERROR_INVALIDDATA;
 +    }
 +
 +    length = FFMAX(length - cnt, 0);
 +
 +    if (bytestream2_get_byte(&s->gb) != 0) {
 +        av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid compression!\n");
 +        return AVERROR_INVALIDDATA;
 +    }
 +
 +    length = FFMAX(length - 1, 0);
 +
 +    if ((ret = decode_zbuf(&bp, s->gb.buffer, s->gb.buffer + length)) < 0)
 +        return ret;
 +
 +    ret = av_bprint_finalize(&bp, (char **)&data);
 +    if (ret < 0)
 +        return ret;
 +
 +    sd = av_frame_new_side_data(f, AV_FRAME_DATA_ICC_PROFILE, bp.len);
 +    if (!sd) {
 +        av_free(data);
 +        return AVERROR(ENOMEM);
 +    }
 +
 +    av_dict_set(&sd->metadata, "name", profile_name, 0);
 +    memcpy(sd->data, data, bp.len);
 +    av_free(data);
 +
 +    /* ICC compressed data and CRC */
 +    bytestream2_skip(&s->gb, length + 4);
 +
 +    return 0;
 +}
 +
 +static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
 +{
 +    if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
 +        int i, j, k;
 +        uint8_t *pd = p->data[0];
 +        for (j = 0; j < s->height; j++) {
 +            i = s->width / 8;
 +            for (k = 7; k >= 1; k--)
 +                if ((s->width&7) >= k)
 +                    pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
 +            for (i--; 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;
 +        }
 +    } else if (s->bits_per_pixel == 2) {
 +        int i, j;
 +        uint8_t *pd = p->data[0];
 +        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;
 +                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->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;
 +        }
 +    } else if (s->bits_per_pixel == 4) {
 +        int i, j;
 +        uint8_t *pd = p->data[0];
 +        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;
 +                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;
 +        }
 +    }
 +}
 +
 +static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s,
 +                             uint32_t length)
 +{
 +    uint32_t sequence_number;
 +    int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
 +
 +    if (length != 26)
 +        return AVERROR_INVALIDDATA;
 +
 +    if (!(s->hdr_state & PNG_IHDR)) {
 +        av_log(avctx, AV_LOG_ERROR, "fctl before IHDR\n");
 +        return AVERROR_INVALIDDATA;
 +    }
 +
 +    s->last_w = s->cur_w;
 +    s->last_h = s->cur_h;
 +    s->last_x_offset = s->x_offset;
 +    s->last_y_offset = s->y_offset;
 +    s->last_dispose_op = s->dispose_op;
 +
 +    sequence_number = bytestream2_get_be32(&s->gb);
 +    cur_w           = bytestream2_get_be32(&s->gb);
 +    cur_h           = bytestream2_get_be32(&s->gb);
 +    x_offset        = bytestream2_get_be32(&s->gb);
 +    y_offset        = bytestream2_get_be32(&s->gb);
 +    bytestream2_skip(&s->gb, 4); /* delay_num (2), delay_den (2) */
 +    dispose_op      = bytestream2_get_byte(&s->gb);
 +    blend_op        = bytestream2_get_byte(&s->gb);
 +    bytestream2_skip(&s->gb, 4); /* crc */
 +
 +    if (sequence_number == 0 &&
 +        (cur_w != s->width ||
 +         cur_h != s->height ||
 +         x_offset != 0 ||
 +         y_offset != 0) ||
 +        cur_w <= 0 || cur_h <= 0 ||
 +        x_offset < 0 || y_offset < 0 ||
 +        cur_w > s->width - x_offset|| cur_h > s->height - y_offset)
 +            return AVERROR_INVALIDDATA;
 +
 +    if (blend_op != APNG_BLEND_OP_OVER && blend_op != APNG_BLEND_OP_SOURCE) {
 +        av_log(avctx, AV_LOG_ERROR, "Invalid blend_op %d\n", blend_op);
 +        return AVERROR_INVALIDDATA;
 +    }
 +
 +    if ((sequence_number == 0 || !s->previous_picture.f->data[0]) &&
 +        dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
 +        // No previous frame to revert to for the first frame
 +        // Spec says to just treat it as a APNG_DISPOSE_OP_BACKGROUND
 +        dispose_op = APNG_DISPOSE_OP_BACKGROUND;
 +    }
 +
 +    if (blend_op == APNG_BLEND_OP_OVER && !s->has_trns && (
 +            avctx->pix_fmt == AV_PIX_FMT_RGB24 ||
 +            avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
 +            avctx->pix_fmt == AV_PIX_FMT_PAL8 ||
 +            avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
 +            avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
 +            avctx->pix_fmt == AV_PIX_FMT_MONOBLACK
 +        )) {
 +        // APNG_BLEND_OP_OVER is the same as APNG_BLEND_OP_SOURCE when there is no alpha channel
 +        blend_op = APNG_BLEND_OP_SOURCE;
 +    }
 +
 +    s->cur_w      = cur_w;
 +    s->cur_h      = cur_h;
 +    s->x_offset   = x_offset;
 +    s->y_offset   = y_offset;
 +    s->dispose_op = dispose_op;
 +    s->blend_op   = blend_op;
 +
 +    return 0;
 +}
 +
 +static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
 +{
 +    int i, j;
 +    uint8_t *pd      = p->data[0];
 +    uint8_t *pd_last = s->last_picture.f->data[0];
 +    int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
 +
 +    ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
 +    for (j = 0; j < s->height; j++) {
 +        for (i = 0; i < ls; i++)
 +            pd[i] += pd_last[i];
 +        pd      += s->image_linesize;
 +        pd_last += s->image_linesize;
 +    }
 +}
 +
 +// divide by 255 and round to nearest
 +// apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
 +#define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
 +
 +static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,
 +                               AVFrame *p)
 +{
 +    size_t x, y;
 +    uint8_t *buffer;
 +
 +    if (s->blend_op == APNG_BLEND_OP_OVER &&
 +        avctx->pix_fmt != AV_PIX_FMT_RGBA &&
 +        avctx->pix_fmt != AV_PIX_FMT_GRAY8A &&
 +        avctx->pix_fmt != AV_PIX_FMT_PAL8) {
 +        avpriv_request_sample(avctx, "Blending with pixel format %s",
 +                              av_get_pix_fmt_name(avctx->pix_fmt));
 +        return AVERROR_PATCHWELCOME;
 +    }
 +
 +    buffer = av_malloc_array(s->image_linesize, s->height);
 +    if (!buffer)
 +        return AVERROR(ENOMEM);
 +
 +
 +    // Do the disposal operation specified by the last frame on the frame
 +    if (s->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
 +        ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
 +        memcpy(buffer, s->last_picture.f->data[0], s->image_linesize * s->height);
 +
 +        if (s->last_dispose_op == APNG_DISPOSE_OP_BACKGROUND)
 +            for (y = s->last_y_offset; y < s->last_y_offset + s->last_h; ++y)
 +                memset(buffer + s->image_linesize * y + s->bpp * s->last_x_offset, 0, s->bpp * s->last_w);
 +
 +        memcpy(s->previous_picture.f->data[0], buffer, s->image_linesize * s->height);
 +        ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);
 +    } else {
 +        ff_thread_await_progress(&s->previous_picture, INT_MAX, 0);
 +        memcpy(buffer, s->previous_picture.f->data[0], s->image_linesize * s->height);
 +    }
 +
 +    // Perform blending
 +    if (s->blend_op == APNG_BLEND_OP_SOURCE) {
 +        for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
 +            size_t row_start = s->image_linesize * y + s->bpp * s->x_offset;
 +            memcpy(buffer + row_start, p->data[0] + row_start, s->bpp * s->cur_w);
 +        }
 +    } else { // APNG_BLEND_OP_OVER
 +        for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
 +            uint8_t *foreground = p->data[0] + s->image_linesize * y + s->bpp * s->x_offset;
 +            uint8_t *background = buffer + s->image_linesize * y + s->bpp * s->x_offset;
 +            for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += s->bpp, background += s->bpp) {
 +                size_t b;
 +                uint8_t foreground_alpha, background_alpha, output_alpha;
 +                uint8_t output[10];
 +
 +                // Since we might be blending alpha onto alpha, we use the following equations:
 +                // output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha
 +                // output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha
 +
 +                switch (avctx->pix_fmt) {
 +                case AV_PIX_FMT_RGBA:
 +                    foreground_alpha = foreground[3];
 +                    background_alpha = background[3];
 +                    break;
 +
 +                case AV_PIX_FMT_GRAY8A:
 +                    foreground_alpha = foreground[1];
 +                    background_alpha = background[1];
 +                    break;
 +
 +                case AV_PIX_FMT_PAL8:
 +                    foreground_alpha = s->palette[foreground[0]] >> 24;
 +                    background_alpha = s->palette[background[0]] >> 24;
 +                    break;
 +                }
 +
 +                if (foreground_alpha == 0)
 +                    continue;
 +
 +                if (foreground_alpha == 255) {
 +                    memcpy(background, foreground, s->bpp);
 +                    continue;
 +                }
 +
 +                if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
 +                    // TODO: Alpha blending with PAL8 will likely need the entire image converted over to RGBA first
 +                    avpriv_request_sample(avctx, "Alpha blending palette samples");
 +                    background[0] = foreground[0];
 +                    continue;
 +                }
 +
 +                output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);
 +
 +                av_assert0(s->bpp <= 10);
 +
 +                for (b = 0; b < s->bpp - 1; ++b) {
 +                    if (output_alpha == 0) {
 +                        output[b] = 0;
 +                    } else if (background_alpha == 255) {
 +                        output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);
 +                    } else {
 +                        output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);
 +                    }
 +                }
 +                output[b] = output_alpha;
 +                memcpy(background, output, s->bpp);
 +            }
 +        }
 +    }
 +
 +    // Copy blended buffer into the frame and free
 +    memcpy(p->data[0], buffer, s->image_linesize * s->height);
 +    av_free(buffer);
 +
 +    return 0;
 +}
 +
 +static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
 +                               AVFrame *p, AVPacket *avpkt)
 +{
 +    AVDictionary **metadatap = NULL;
 +    uint32_t tag, length;
 +    int decode_next_dat = 0;
 +    int i, ret;
  
 -    /* init the zlib */
 -    s->zstream.zalloc = ff_png_zalloc;
 -    s->zstream.zfree  = ff_png_zfree;
 -    s->zstream.opaque = NULL;
 -    ret = inflateInit(&s->zstream);
 -    if (ret != Z_OK)
 -        return -1;
      for (;;) {
 -        if (bytestream2_get_bytes_left(&s->gb) <= 0)
 +        length = bytestream2_get_bytes_left(&s->gb);
 +        if (length <= 0) {
 +
 +            if (avctx->codec_id == AV_CODEC_ID_PNG &&
 +                avctx->skip_frame == AVDISCARD_ALL) {
 +                return 0;
 +            }
 +
 +            if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
 +                if (!(s->pic_state & PNG_IDAT))
 +                    return 0;
 +                else
 +                    goto exit_loop;
 +            }
 +            av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
 +            if (   s->pic_state & PNG_ALLIMAGE
 +                && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL)
 +                goto exit_loop;
 +            ret = AVERROR_INVALIDDATA;
              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");
 +            ret = AVERROR_INVALIDDATA;
              goto fail;
 +        }
          tag = bytestream2_get_le32(&s->gb);
 -        ff_dlog(avctx, "png: tag=%c%c%c%c length=%"PRIu32"\n",
 -                (tag & 0xff),
 -                ((tag >> 8) & 0xff),
 -                ((tag >> 16) & 0xff),
 -                ((tag >> 24) & 0xff), length);
 +        if (avctx->debug & FF_DEBUG_STARTCODE)
 +            av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",
 +                   av_fourcc2str(tag), length);
 +
 +        if (avctx->codec_id == AV_CODEC_ID_PNG &&
 +            avctx->skip_frame == AVDISCARD_ALL) {
 +            switch(tag) {
 +            case MKTAG('I', 'H', 'D', 'R'):
 +            case MKTAG('p', 'H', 'Y', 's'):
 +            case MKTAG('t', 'E', 'X', 't'):
 +            case MKTAG('I', 'D', 'A', 'T'):
 +            case MKTAG('t', 'R', 'N', 'S'):
 +                break;
 +            default:
 +                goto skip_tag;
 +            }
 +        }
 +
 +        metadatap = &p->metadata;
          switch (tag) {
          case MKTAG('I', 'H', 'D', 'R'):
 -            if (length != 13)
 +            if ((ret = decode_ihdr_chunk(avctx, s, length)) < 0)
                  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;
 +            break;
 +        case MKTAG('p', 'H', 'Y', 's'):
 +            if ((ret = decode_phys_chunk(avctx, s)) < 0)
                  goto fail;
 -            }
 -            s->bit_depth        = bytestream2_get_byte(&s->gb);
 -            s->color_type       = bytestream2_get_byte(&s->gb);
 -            s->compression_type = bytestream2_get_byte(&s->gb);
 -            s->filter_type      = bytestream2_get_byte(&s->gb);
 -            s->interlace_type   = bytestream2_get_byte(&s->gb);
 -            bytestream2_skip(&s->gb, 4); /* crc */
 -            s->state |= PNG_IHDR;
 -            ff_dlog(avctx, "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('I', 'D', 'A', 'T'):
 -            if (!(s->state & PNG_IHDR))
 +        case MKTAG('f', 'c', 'T', 'L'):
 +            if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
 +                goto skip_tag;
 +            if ((ret = decode_fctl_chunk(avctx, s, length)) < 0)
 +                goto fail;
 +            decode_next_dat = 1;
 +            break;
 +        case MKTAG('f', 'd', 'A', 'T'):
 +            if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
 +                goto skip_tag;
 +            if (!decode_next_dat) {
 +                ret = AVERROR_INVALIDDATA;
                  goto fail;
 -            if (!(s->state & PNG_IDAT)) {
 -                /* init image info */
 -                avctx->width  = s->width;
 -                avctx->height = s->height;
 -
 -                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;
 -
 -                if (s->bit_depth == 8 &&
 -                    s->color_type == PNG_COLOR_TYPE_RGB) {
 -                    avctx->pix_fmt = AV_PIX_FMT_RGB24;
 -                } else if (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 &&
 -                           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) {
 -                    avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
 -                } else if (s->bit_depth == 16 &&
 -                           s->color_type == PNG_COLOR_TYPE_RGB) {
 -                    avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
 -                } else if (s->bit_depth == 1 &&
 -                           s->color_type == PNG_COLOR_TYPE_GRAY) {
 -                    avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
 -                } else if (s->bit_depth == 8 &&
 -                           s->color_type == PNG_COLOR_TYPE_PALETTE) {
 -                    avctx->pix_fmt = AV_PIX_FMT_PAL8;
 -                } else if (s->bit_depth == 8 &&
 -                           s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
 -                    avctx->pix_fmt = AV_PIX_FMT_YA8;
 -                } else if (s->bit_depth == 16 &&
 -                           s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
 -                    avctx->pix_fmt = AV_PIX_FMT_YA16BE;
 -                } else {
 -                    avpriv_report_missing_feature(avctx,
 -                                                  "Bit depth %d color type %d",
 -                                                  s->bit_depth, s->color_type);
 -                    goto fail;
 -                }
 -
 -                if (ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF) < 0) {
 -                    av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
 -                    goto fail;
 -                }
 -                p->pict_type        = AV_PICTURE_TYPE_I;
 -                p->key_frame        = 1;
 -                p->interlaced_frame = !!s->interlace_type;
 -
 -                /* compute the compressed row size */
 -                if (!s->interlace_type) {
 -                    s->crow_size = s->row_size + 1;
 -                } else {
 -                    s->pass          = 0;
 -                    s->pass_row_size = ff_png_pass_row_size(s->pass,
 -                                                            s->bits_per_pixel,
 -                                                            s->width);
 -                    s->crow_size = s->pass_row_size + 1;
 -                }
 -                ff_dlog(avctx, "row_size=%d crow_size =%d\n",
 -                        s->row_size, s->crow_size);
 -                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)
 -                    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);
 -                if (!s->last_row)
 -                    goto fail;
 -                if (s->interlace_type ||
 -                    s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
 -                    s->tmp_row = av_malloc(s->row_size);
 -                    if (!s->tmp_row)
 -                        goto fail;
 -                }
 -                /* compressed row */
 -                crow_buf_base = av_malloc(s->row_size + 16);
 -                if (!crow_buf_base)
 -                    goto fail;
 -
 -                /* we want crow_buf+1 to be 16-byte aligned */
 -                s->crow_buf          = crow_buf_base + 15;
 -                s->zstream.avail_out = s->crow_size;
 -                s->zstream.next_out  = s->crow_buf;
              }
 -            s->state |= PNG_IDAT;
 -            if (png_decode_idat(s, length) < 0)
 +            bytestream2_get_be32(&s->gb);
 +            length -= 4;
 +            /* fallthrough */
 +        case MKTAG('I', 'D', 'A', 'T'):
 +            if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
 +                goto skip_tag;
 +            if ((ret = decode_idat_chunk(avctx, s, length, p)) < 0)
                  goto fail;
 -            bytestream2_skip(&s->gb, 4); /* crc */
              break;
          case MKTAG('P', 'L', 'T', 'E'):
 -        {
 -            int n, i, r, g, b;
 -
 -            if ((length % 3) != 0 || length > 256 * 3)
 +            if (decode_plte_chunk(avctx, s, length) < 0)
                  goto skip_tag;
 -            /* read the palette */
 -            n = length / 3;
 -            for (i = 0; i < n; i++) {
 -                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;
 -            }
 -            for (; i < 256; i++)
 -                s->palette[i] = (0xff << 24);
 -            s->state |= PNG_PLTE;
 -            bytestream2_skip(&s->gb, 4);     /* crc */
 -        }
 -        break;
 +            break;
          case MKTAG('t', 'R', 'N', 'S'):
 -        {
 -            int v, i;
 -
 -            /* read the transparency. XXX: Only palette mode supported */
 -            if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
 -                length > 256 ||
 -                !(s->state & PNG_PLTE))
 +            if (decode_trns_chunk(avctx, s, length) < 0)
                  goto skip_tag;
 -            for (i = 0; i < length; i++) {
 -                v = bytestream2_get_byte(&s->gb);
 -                s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
 -            }
 -            bytestream2_skip(&s->gb, 4);     /* crc */
 -        }
 -        break;
 +            break;
 +        case MKTAG('t', 'E', 'X', 't'):
 +            if (decode_text_chunk(s, length, 0, metadatap) < 0)
 +                av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
 +            bytestream2_skip(&s->gb, length + 4);
 +            break;
 +        case MKTAG('z', 'T', 'X', 't'):
 +            if (decode_text_chunk(s, length, 1, metadatap) < 0)
 +                av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
 +            bytestream2_skip(&s->gb, length + 4);
 +            break;
          case MKTAG('s', 'T', 'E', 'R'): {
              int mode = bytestream2_get_byte(&s->gb);
              AVStereo3D *stereo3d = av_stereo3d_create_side_data(p);