author Michael Niedermayer Mon, 17 Mar 2014 14:24:05 +0000 (15:24 +0100) committer Michael Niedermayer Mon, 17 Mar 2014 14:29:42 +0000 (15:29 +0100)
* commit 'c598b569fb3d1f4b6c4868fe64f6989254df5186':
png: K&R formatting cosmetics

Conflicts:
libavcodec/png.c
libavcodec/pngdec.c
libavcodec/pngenc.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2
libavcodec/png.c
libavcodec/pngdec.c
libavcodec/pngdsp.c
libavcodec/pngenc.c

Simple merge
@@@ -184,30 -159,45 +185,36 @@@ void ff_add_png_paeth_prediction(uint8_
}
}

- #define UNROLL1(bpp, op) {\
-                  r = dst[0];\
-     if(bpp >= 2) g = dst[1];\
-     if(bpp >= 3) b = dst[2];\
-     if(bpp >= 4) a = dst[3];\
-     for(; i <= size - bpp; i+=bpp) {\
-         dst[i+0] = r = op(r, src[i+0], last[i+0]);\
-         if(bpp == 1) continue;\
-         dst[i+1] = g = op(g, src[i+1], last[i+1]);\
-         if(bpp == 2) continue;\
-         dst[i+2] = b = op(b, src[i+2], last[i+2]);\
-         if(bpp == 3) continue;\
-         dst[i+3] = a = op(a, src[i+3], last[i+3]);\
-     }\
+ #define UNROLL1(bpp, op) {                                                    \
+         r = dst[0];                                                           \
+         if (bpp >= 2)                                                         \
+             g = dst[1];                                                       \
+         if (bpp >= 3)                                                         \
+             b = dst[2];                                                       \
+         if (bpp >= 4)                                                         \
+             a = dst[3];                                                       \
-        for (; i < size; i += bpp) {                                          \
++        for (; i <= size - bpp; i += bpp) {                                   \
+             dst[i + 0] = r = op(r, src[i + 0], last[i + 0]);                  \
+             if (bpp == 1)                                                     \
+                 continue;                                                     \
+             dst[i + 1] = g = op(g, src[i + 1], last[i + 1]);                  \
+             if (bpp == 2)                                                     \
+                 continue;                                                     \
+             dst[i + 2] = b = op(b, src[i + 2], last[i + 2]);                  \
+             if (bpp == 3)                                                     \
+                 continue;                                                     \
+             dst[i + 3] = a = op(a, src[i + 3], last[i + 3]);                  \
+         }                                                                     \
}

-#define UNROLL_FILTER(op)                                                     \
-    if (bpp == 1)                                                             \
-        UNROLL1(1, op)                                                        \
-        else if (bpp == 2)                                                    \
-            UNROLL1(2, op)                                                    \
-            else if (bpp == 3)                                                \
-                UNROLL1(3, op)                                                \
-                else if (bpp == 4)                                            \
-                    UNROLL1(4, op)                                            \
-                    else {                                                    \
-                        for (; i < size; i += bpp) {                          \
-                            int j;                                            \
-                            for (j = 0; j < bpp; j++)                         \
-                                dst[i + j] = op(dst[i + j - bpp],             \
-                                                src[i + j], last[i + j]);     \
-                        }                                                     \
-                    }
+#define UNROLL_FILTER(op)\
-          if(bpp == 1) UNROLL1(1, op)\
-     else if(bpp == 2) UNROLL1(2, op)\
-     else if(bpp == 3) UNROLL1(3, op)\
-     else if(bpp == 4) UNROLL1(4, op)\
++         if (bpp == 1) UNROLL1(1, op)\
++    else if (bpp == 2) UNROLL1(2, op)\
++    else if (bpp == 3) UNROLL1(3, op)\
++    else if (bpp == 4) UNROLL1(4, op)\
+    for (; i < size; i++) {\
-         dst[i] = op(dst[i-bpp], src[i], last[i]);\
++        dst[i] = op(dst[i - bpp], src[i], last[i]);\
+    }\

/* NOTE: 'dst' can be equal to 'last' */
static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
memcpy(dst, src, size);
break;
case PNG_FILTER_VALUE_SUB:
-         for (i = 0; i < bpp; i++) {
+         for (i = 0; i < bpp; i++)
dst[i] = src[i];
-         }
if (bpp == 4) {
-             p = *(int*)dst;
+             p = *(int *)dst;
for (; i < size; i += bpp) {
-                 unsigned s = *(int*)(src + i);
-                int s = *(int *)(src + i);
++                unsigned s = *(int *)(src + i);
p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
-                 *(int*)(dst + i) = p;
+                 *(int *)(dst + i) = p;
}
} else {
- #define OP_SUB(x,s,l) x+s
+ #define OP_SUB(x, s, l) x + s
UNROLL_FILTER(OP_SUB);
}
break;
break;
case PNG_FILTER_VALUE_PAETH:
for (i = 0; i < bpp; i++) {
-             p = last[i];
+             p      = last[i];
dst[i] = p + src[i];
}
-        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
+             /* 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);
i = w;
}
}

-static av_always_inline void convert_to_rgb32_loco(uint8_t *dst,
-                                                   const uint8_t *src,
-                                                   int width, int loco)
-{
-    int j;
-    unsigned int r, g, b, a;
-
-    for (j = 0; j < width; j++) {
-        r = src[0];
-        g = src[1];
-        b = src[2];
-        a = src[3];
-        if (loco) {
-            r = (r + g) & 0xff;
-            b = (b + g) & 0xff;
-        }
-        *(uint32_t *) dst = (a << 24) | (r << 16) | (g << 8) | b;
-        dst += 4;
-        src += 4;
-    }
+/* This used to be called "deloco" in FFmpeg
+ * and is actually an inverse reversible colorspace transformation */
+#define YUV2RGB(NAME, TYPE) \
+static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
+{ \
+    int i; \
+    for (i = 0; i < size; i += 3 + alpha) { \
-         int g = dst [i+1]; \
-         dst[i+0] += g; \
-         dst[i+2] += g; \
++        int g = dst [i + 1]; \
++        dst[i + 0] += g; \
++        dst[i + 2] += g; \
+    } \
}

-static void convert_to_rgb32(uint8_t *dst, const uint8_t *src,
-                             int width, int loco)
-{
-    if (loco)
-        convert_to_rgb32_loco(dst, src, width, 1);
-    else
-        convert_to_rgb32_loco(dst, src, width, 0);
-}
-
-static void deloco_rgb24(uint8_t *dst, int size)
-{
-    int i;
-    for (i = 0; i < size; i += 3) {
-        int g = dst[i + 1];
-        dst[i + 0] += g;
-        dst[i + 2] += g;
-    }
-}
+YUV2RGB(rgb8, uint8_t)
+YUV2RGB(rgb16, uint16_t)

/* process exactly one decompressed row */
static void png_handle_row(PNGDecContext *s)
break;
png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
s->last_row, s->pass_row_size, s->bpp);
-                 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
+                 FFSWAP(uint8_t *, s->last_row, s->tmp_row);
+                FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
got_line = 1;
}
if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
@@@ -509,29 -405,21 +517,29 @@@ static int decode_frame(AVCodecContext
void *data, int *got_frame,
AVPacket *avpkt)
{
-     PNGDecContext * const s = avctx->priv_data;
-     const uint8_t *buf      = avpkt->data;
-     int buf_size            = avpkt->size;
+     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;
+    AVFrame *p;
uint32_t tag, length;
+    int64_t sig;
int ret;

+    p = s->picture.f;
+
+    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)
-        return -1;
+    sig = bytestream2_get_be64(&s->gb);
+    if (sig != PNGSIG &&
+        sig != MNGSIG) {
+        av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
+        return AVERROR_INVALIDDATA;
+    }

-    bytestream2_init(&s->gb, buf + 8, buf_size - 8);
s->y = s->state = 0;

/* init the zlib */
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",
+                            "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;
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)
-                     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] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
-                 }
-                 for (; i < 256; i++) {
-                     s->palette[i] = (0xFFU << 24);
-                 }
-                 s->state |= PNG_PLTE;
-                 bytestream2_skip(&s->gb, 4); /* crc */
+         {
+             int n, i, r, g, b;
+             if ((length % 3) != 0 || length > 256 * 3)
+                 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;
++                s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
}
-             break;
+             for (; i < 256; i++)
-                s->palette[i] = (0xff << 24);
++                s->palette[i] = (0xFFU << 24);
+             s->state |= PNG_PLTE;
+             bytestream2_skip(&s->gb, 4);     /* crc */
+         }
+         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))
-                     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 */
+         {
+             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))
+                 goto skip_tag;
+             for (i = 0; i < length; i++) {
+                 v = bytestream2_get_byte(&s->gb);
+                 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
}
-             break;
+             bytestream2_skip(&s->gb, 4);     /* crc */
+         }
+         break;
+        case MKTAG('t', 'E', 'X', 't'):
+            if (decode_text_chunk(s, length, 0, &metadata) < 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, &metadata) < 0)
+                av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
+            bytestream2_skip(&s->gb, length + 4);
+            break;
case MKTAG('I', 'E', 'N', 'D'):
if (!(s->state & PNG_ALLIMAGE))
+                av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
+            if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
goto fail;
+            }
bytestream2_skip(&s->gb, 4); /* crc */
goto exit_loop;
default:
@@@ -760,96 -602,17 +767,95 @@@ skip_tag
break;
}
}
-  exit_loop:
+ exit_loop:
+
+    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;
+        }
+    }
+    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;
+        }
+    }
+    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;
+        }
+    }
+
-      /* handle p-frames only if a predecessor frame is available */
-      if (s->last_picture.f->data[0]) {
-          if (   !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
+     /* handle p-frames only if a predecessor frame is available */
-    if (s->prev->data[0]) {
-        if (!(avpkt->flags & AV_PKT_FLAG_KEY)) {
++    if (s->last_picture.f->data[0]) {
++        if (   !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
+            && s->last_picture.f->width == p->width
+            && s->last_picture.f->height== p->height
+            && s->last_picture.f->format== p->format
+         ) {
int i, j;
uint8_t *pd      = p->data[0];
-            uint8_t *pd_last = s->prev->data[0];
+            uint8_t *pd_last = s->last_picture.f->data[0];

for (j = 0; j < s->height; j++) {
-                 for (i = 0; i < s->width * s->bpp; i++) {
+                 for (i = 0; i < s->width * s->bpp; i++)
pd[i] += pd_last[i];
-                 }
pd      += s->image_linesize;
pd_last += s->image_linesize;
}
*got_frame = 1;

ret = bytestream2_tell(&s->gb);
the_end:
+ the_end:
inflateEnd(&s->zstream);
-    av_free(crow_buf_base);
s->crow_buf = NULL;
-    av_freep(&s->last_row);
-    av_freep(&s->tmp_row);
return ret;
fail:
+ fail:
-    ret = -1;
+    ret = AVERROR_INVALIDDATA;
goto the_end;
}

Simple merge
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
+#include "internal.h"
#include "bytestream.h"
#include "dsputil.h"
#include "png.h"
@@@ -54,10 -52,9 +55,10 @@@ static void png_get_interlaced_row(uint
int x, mask, dst_x, j, b, bpp;
uint8_t *d;
const uint8_t *s;
+    static const int masks[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};

-     switch(bits_per_pixel) {
+     switch (bits_per_pixel) {
case 1:
memset(dst, 0, row_size);
dst_x = 0;
@@@ -162,10 -145,10 +164,10 @@@ static uint8_t *png_choose_filter(PNGEn
uint8_t *src, uint8_t *top, int size, int bpp)
{
int pred = s->filter_type;
-    assert(bpp || !pred);
+    av_assert0(bpp || !pred);
-     if(!top && pred)
+     if (!top && pred)
pred = PNG_FILTER_VALUE_SUB;
-     if(pred == PNG_FILTER_VALUE_MIXED) {
+     if (pred == PNG_FILTER_VALUE_MIXED) {
int i;
int cost, bcost = INT_MAX;
uint8_t *buf1 = dst, *buf2 = dst + size + 16;
@@@ -231,29 -232,21 +234,29 @@@ static int png_write_row(PNGEncContext
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *pict, int *got_packet)
{
-     PNGEncContext *s = avctx->priv_data;
-     const AVFrame * const p = pict;
+     PNGEncContext *s       = avctx->priv_data;
+     const AVFrame *const p = pict;
int bit_depth, color_type, y, len, row_size, ret, is_progressive;
-    int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
+    int bits_per_pixel, pass_row_size, enc_row_size;
+    int64_t max_packet_size;
int compression_level;
-     uint8_t *ptr, *top;
-     uint8_t *crow_base = NULL, *crow_buf, *crow;
+     uint8_t *ptr, *top, *crow_buf, *crow;
+     uint8_t *crow_base       = NULL;
uint8_t *progressive_buf = NULL;
-     uint8_t *top_buf = NULL;
-    uint8_t *rgba_buf        = NULL;
+     uint8_t *top_buf         = NULL;

is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
-     switch(avctx->pix_fmt) {
+     switch (avctx->pix_fmt) {
-    case AV_PIX_FMT_RGB32:
+    case AV_PIX_FMT_RGBA64BE:
-         bit_depth = 16;
++        bit_depth  = 16;
+        color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+        break;
+    case AV_PIX_FMT_RGB48BE:
-         bit_depth = 16;
++        bit_depth  = 16;
+        color_type = PNG_COLOR_TYPE_RGB;
+        break;
+    case AV_PIX_FMT_RGBA:
-         bit_depth = 8;
+         bit_depth  = 8;
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
break;
case AV_PIX_FMT_RGB24:
color_type = PNG_COLOR_TYPE_GRAY;
break;
case AV_PIX_FMT_GRAY8:
-         bit_depth = 8;
+         bit_depth  = 8;
color_type = PNG_COLOR_TYPE_GRAY;
break;
+    case AV_PIX_FMT_GRAY8A:
+        bit_depth = 8;
+        color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
+        break;
case AV_PIX_FMT_MONOBLACK:
-         bit_depth = 1;
+         bit_depth  = 1;
color_type = PNG_COLOR_TYPE_GRAY;
break;
case AV_PIX_FMT_PAL8:
uint32_t *palette;
uint8_t *alpha_ptr;

-         palette = (uint32_t *)p->data[1];
-         ptr = s->buf;
+         palette   = (uint32_t *)p->data[1];
+         ptr       = s->buf;
alpha_ptr = s->buf + 256 * 3;
has_alpha = 0;
-         for(i = 0; i < 256; i++) {
-             v = palette[i];
+         for (i = 0; i < 256; i++) {
+             v     = palette[i];
alpha = v >> 24;
-            if (alpha && alpha != 0xff)
+            if (alpha != 0xff)
has_alpha = 1;
*alpha_ptr++ = alpha;
bytestream_put_be24(&ptr, v);
pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
if (pass_row_size > 0) {
top = NULL;
-                 for(y = 0; y < avctx->height; y++) {
+                 for (y = 0; y < avctx->height; y++)
if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
ptr = p->data[0] + y * p->linesize[0];
-                         FFSWAP(uint8_t*, progressive_buf, top_buf);
+                         FFSWAP(uint8_t *, progressive_buf, top_buf);
-                        if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
-                            convert_from_rgb32(rgba_buf, ptr, avctx->width);
-                            ptr = rgba_buf;
-                        }
png_get_interlaced_row(progressive_buf, pass_row_size,
bits_per_pixel, pass,
ptr, avctx->width);
}
} else {
top = NULL;
-         for(y = 0; y < avctx->height; y++) {
+         for (y = 0; y < avctx->height; y++) {
ptr = p->data[0] + y * p->linesize[0];
-             crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
-            if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
-                FFSWAP(uint8_t *, rgba_buf, top_buf);
-                convert_from_rgb32(rgba_buf, ptr, avctx->width);
-                ptr = rgba_buf;
-            }
+             crow = png_choose_filter(s, crow_buf, ptr, top,
+                                      row_size, bits_per_pixel >> 3);
png_write_row(s, crow, row_size + 1);
top = ptr;
}
*got_packet = 1;
ret         = 0;

the_end:
+ the_end:
av_free(crow_base);
av_free(progressive_buf);
-    av_free(rgba_buf);
av_free(top_buf);
deflateEnd(&s->zstream);
return ret;
@@@ -444,26 -443,10 +451,27 @@@ fail
goto the_end;
}

- static av_cold int png_enc_init(AVCodecContext *avctx){
+ static av_cold int png_enc_init(AVCodecContext *avctx)
+ {
PNGEncContext *s = avctx->priv_data;

-     switch(avctx->pix_fmt) {
++    switch (avctx->pix_fmt) {
+    case AV_PIX_FMT_RGBA:
+        avctx->bits_per_coded_sample = 32;
+        break;
+    case AV_PIX_FMT_RGB24:
+        avctx->bits_per_coded_sample = 24;
+        break;
+    case AV_PIX_FMT_GRAY8:
+        avctx->bits_per_coded_sample = 0x28;
+        break;
+    case AV_PIX_FMT_MONOBLACK:
+        avctx->bits_per_coded_sample = 1;
+        break;
+    case AV_PIX_FMT_PAL8:
+        avctx->bits_per_coded_sample = 8;
+    }
+
avctx->coded_frame = av_frame_alloc();
if (!avctx->coded_frame)
return AVERROR(ENOMEM);

ff_dsputil_init(&s->dsp, avctx);

-     s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
-     if(avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
+     s->filter_type = av_clip(avctx->prediction_method,
+                              PNG_FILTER_VALUE_NONE,
+                              PNG_FILTER_VALUE_MIXED);
+     if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
s->filter_type = PNG_FILTER_VALUE_NONE;

+    if (s->dpi && s->dpm) {
+      av_log(avctx, AV_LOG_ERROR, "Only one of 'dpi' or 'dpm' options should be set\n");
+      return AVERROR(EINVAL);
+    } else if (s->dpi) {
+      s->dpm = s->dpi * 10000 / 254;
+    }
+
return 0;
}

@@@ -517,12 -480,8 +527,12 @@@ AVCodec ff_png_encoder =
.init           = png_enc_init,
.close          = png_enc_close,
.encode2        = encode_frame,
-     .pix_fmts       = (const enum AVPixelFormat[]){
+    .capabilities   = CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
-        AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
+     .pix_fmts       = (const enum AVPixelFormat[]) {
+        AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
+        AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE,
+        AV_PIX_FMT_PAL8,
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
AV_PIX_FMT_GRAY16BE,
AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE
},