3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/imgutils.h"
23 #include "bytestream.h"
29 * - add 2, 4 and 16 bit depth support
36 typedef struct PNGDecContext
{
40 AVFrame picture1
, picture2
;
41 AVFrame
*current_picture
, *last_picture
;
56 uint32_t palette
[256];
61 int crow_size
; /* compressed row size (include filter type) */
62 int row_size
; /* decompressed row size */
63 int pass_row_size
; /* decompress row size of the current pass */
68 /* Mask to determine which y pixels can be written in a pass */
69 static const uint8_t png_pass_dsp_ymask
[NB_PASSES
] = {
70 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
73 /* Mask to determine which pixels to overwrite while displaying */
74 static const uint8_t png_pass_dsp_mask
[NB_PASSES
] = {
75 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
78 /* NOTE: we try to construct a good looking image at each pass. width
79 is the original image width. We also do pixel format conversion at
81 static void png_put_interlaced_row(uint8_t *dst
, int width
,
82 int bits_per_pixel
, int pass
,
83 int color_type
, const uint8_t *src
)
85 int x
, mask
, dsp_mask
, j
, src_x
, b
, bpp
;
89 mask
= ff_png_pass_mask
[pass
];
90 dsp_mask
= png_pass_dsp_mask
[pass
];
92 switch (bits_per_pixel
) {
94 /* we must initialize the line to zero before writing to it */
96 memset(dst
, 0, (width
+ 7) >> 3);
98 for (x
= 0; x
< width
; x
++) {
100 if ((dsp_mask
<< j
) & 0x80) {
101 b
= (src
[src_x
>> 3] >> (7 - (src_x
& 7))) & 1;
102 dst
[x
>> 3] |= b
<< (7 - j
);
104 if ((mask
<< j
) & 0x80)
109 bpp
= bits_per_pixel
>> 3;
112 if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
113 for (x
= 0; x
< width
; x
++) {
115 if ((dsp_mask
<< j
) & 0x80) {
116 *(uint32_t *)d
= (s
[3] << 24) | (s
[0] << 16) | (s
[1] << 8) | s
[2];
119 if ((mask
<< j
) & 0x80)
123 for(x
= 0; x
< width
; x
++) {
125 if ((dsp_mask
<< j
) & 0x80) {
129 if ((mask
<< j
) & 0x80)
137 void ff_add_png_paeth_prediction(uint8_t *dst
, uint8_t *src
, uint8_t *top
, int w
, int bpp
)
140 for (i
= 0; i
< w
; i
++) {
141 int a
, b
, c
, p
, pa
, pb
, pc
;
154 if (pa
<= pb
&& pa
<= pc
)
164 #define UNROLL1(bpp, op) {\
166 if(bpp >= 2) g = dst[1];\
167 if(bpp >= 3) b = dst[2];\
168 if(bpp >= 4) a = dst[3];\
169 for(; i < size; i+=bpp) {\
170 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
171 if(bpp == 1) continue;\
172 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
173 if(bpp == 2) continue;\
174 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
175 if(bpp == 3) continue;\
176 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
180 #define UNROLL_FILTER(op)\
181 if(bpp == 1) UNROLL1(1, op)\
182 else if(bpp == 2) UNROLL1(2, op)\
183 else if(bpp == 3) UNROLL1(3, op)\
184 else if(bpp == 4) UNROLL1(4, op)\
186 for (; i < size; i += bpp) {\
188 for (j = 0; j < bpp; j++)\
189 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
193 /* NOTE: 'dst' can be equal to 'last' */
194 static void png_filter_row(PNGDSPContext
*dsp
, uint8_t *dst
, int filter_type
,
195 uint8_t *src
, uint8_t *last
, int size
, int bpp
)
197 int i
, p
, r
, g
, b
, a
;
199 switch (filter_type
) {
200 case PNG_FILTER_VALUE_NONE
:
201 memcpy(dst
, src
, size
);
203 case PNG_FILTER_VALUE_SUB
:
204 for (i
= 0; i
< bpp
; i
++) {
209 for (; i
< size
; i
+= bpp
) {
210 int s
= *(int*)(src
+ i
);
211 p
= ((s
& 0x7f7f7f7f) + (p
& 0x7f7f7f7f)) ^ ((s
^ p
) & 0x80808080);
212 *(int*)(dst
+ i
) = p
;
215 #define OP_SUB(x,s,l) x+s
216 UNROLL_FILTER(OP_SUB
);
219 case PNG_FILTER_VALUE_UP
:
220 dsp
->add_bytes_l2(dst
, src
, last
, size
);
222 case PNG_FILTER_VALUE_AVG
:
223 for (i
= 0; i
< bpp
; i
++) {
227 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
228 UNROLL_FILTER(OP_AVG
);
230 case PNG_FILTER_VALUE_PAETH
:
231 for (i
= 0; i
< bpp
; i
++) {
235 if (bpp
> 1 && size
> 4) {
236 // would write off the end of the array if we let it process the last pixel with bpp=3
237 int w
= bpp
== 4 ? size
: size
- 3;
238 dsp
->add_paeth_prediction(dst
+ i
, src
+ i
, last
+ i
, w
- i
, bpp
);
241 ff_add_png_paeth_prediction(dst
+ i
, src
+ i
, last
+ i
, size
- i
, bpp
);
246 static av_always_inline
void convert_to_rgb32_loco(uint8_t *dst
,
251 unsigned int r
, g
, b
, a
;
253 for (j
= 0; j
< width
; j
++) {
262 *(uint32_t *)dst
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
268 static void convert_to_rgb32(uint8_t *dst
, const uint8_t *src
, int width
, int loco
)
271 convert_to_rgb32_loco(dst
, src
, width
, 1);
273 convert_to_rgb32_loco(dst
, src
, width
, 0);
276 static void deloco_rgb24(uint8_t *dst
, int size
)
279 for (i
= 0; i
< size
; i
+= 3) {
286 /* process exactly one decompressed row */
287 static void png_handle_row(PNGDecContext
*s
)
289 uint8_t *ptr
, *last_row
;
292 if (!s
->interlace_type
) {
293 ptr
= s
->image_buf
+ s
->image_linesize
* s
->y
;
294 /* need to swap bytes correctly for RGB_ALPHA */
295 if (s
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
296 png_filter_row(&s
->dsp
, s
->tmp_row
, s
->crow_buf
[0], s
->crow_buf
+ 1,
297 s
->last_row
, s
->row_size
, s
->bpp
);
298 convert_to_rgb32(ptr
, s
->tmp_row
, s
->width
, s
->filter_type
== PNG_FILTER_TYPE_LOCO
);
299 FFSWAP(uint8_t*, s
->last_row
, s
->tmp_row
);
301 /* in normal case, we avoid one copy */
303 last_row
= s
->last_row
;
305 last_row
= ptr
- s
->image_linesize
;
307 png_filter_row(&s
->dsp
, ptr
, s
->crow_buf
[0], s
->crow_buf
+ 1,
308 last_row
, s
->row_size
, s
->bpp
);
310 /* loco lags by 1 row so that it doesn't interfere with top prediction */
311 if (s
->filter_type
== PNG_FILTER_TYPE_LOCO
&&
312 s
->color_type
== PNG_COLOR_TYPE_RGB
&& s
->y
> 0)
313 deloco_rgb24(ptr
- s
->image_linesize
, s
->row_size
);
315 if (s
->y
== s
->height
) {
316 s
->state
|= PNG_ALLIMAGE
;
317 if (s
->filter_type
== PNG_FILTER_TYPE_LOCO
&&
318 s
->color_type
== PNG_COLOR_TYPE_RGB
)
319 deloco_rgb24(ptr
, s
->row_size
);
324 ptr
= s
->image_buf
+ s
->image_linesize
* s
->y
;
325 if ((ff_png_pass_ymask
[s
->pass
] << (s
->y
& 7)) & 0x80) {
326 /* if we already read one row, it is time to stop to
327 wait for the next one */
330 png_filter_row(&s
->dsp
, s
->tmp_row
, s
->crow_buf
[0], s
->crow_buf
+ 1,
331 s
->last_row
, s
->pass_row_size
, s
->bpp
);
332 FFSWAP(uint8_t*, s
->last_row
, s
->tmp_row
);
335 if ((png_pass_dsp_ymask
[s
->pass
] << (s
->y
& 7)) & 0x80) {
336 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
337 png_put_interlaced_row(ptr
, s
->width
, s
->bits_per_pixel
, s
->pass
,
338 s
->color_type
, s
->last_row
);
341 if (s
->y
== s
->height
) {
343 if (s
->pass
== NB_PASSES
- 1) {
344 s
->state
|= PNG_ALLIMAGE
;
349 s
->pass_row_size
= ff_png_pass_row_size(s
->pass
,
352 s
->crow_size
= s
->pass_row_size
+ 1;
353 if (s
->pass_row_size
!= 0)
355 /* skip pass if empty row */
364 static int png_decode_idat(PNGDecContext
*s
, int length
)
367 s
->zstream
.avail_in
= FFMIN(length
, bytestream2_get_bytes_left(&s
->gb
));
368 s
->zstream
.next_in
= s
->gb
.buffer
;
369 bytestream2_skip(&s
->gb
, length
);
371 /* decode one line if possible */
372 while (s
->zstream
.avail_in
> 0) {
373 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
374 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
377 if (s
->zstream
.avail_out
== 0) {
378 if (!(s
->state
& PNG_ALLIMAGE
)) {
381 s
->zstream
.avail_out
= s
->crow_size
;
382 s
->zstream
.next_out
= s
->crow_buf
;
388 static int decode_frame(AVCodecContext
*avctx
,
389 void *data
, int *got_frame
,
392 PNGDecContext
* const s
= avctx
->priv_data
;
393 const uint8_t *buf
= avpkt
->data
;
394 int buf_size
= avpkt
->size
;
395 AVFrame
*picture
= data
;
396 uint8_t *crow_buf_base
= NULL
;
398 uint32_t tag
, length
;
401 FFSWAP(AVFrame
*, s
->current_picture
, s
->last_picture
);
402 avctx
->coded_frame
= s
->current_picture
;
403 p
= s
->current_picture
;
405 /* check signature */
407 memcmp(buf
, ff_pngsig
, 8) != 0 &&
408 memcmp(buf
, ff_mngsig
, 8) != 0)
411 bytestream2_init(&s
->gb
, buf
+ 8, buf_size
- 8);
415 s
->zstream
.zalloc
= ff_png_zalloc
;
416 s
->zstream
.zfree
= ff_png_zfree
;
417 s
->zstream
.opaque
= NULL
;
418 ret
= inflateInit(&s
->zstream
);
422 if (bytestream2_get_bytes_left(&s
->gb
) <= 0)
424 length
= bytestream2_get_be32(&s
->gb
);
425 if (length
> 0x7fffffff)
427 tag
= bytestream2_get_le32(&s
->gb
);
428 av_dlog(avctx
, "png: tag=%c%c%c%c length=%u\n",
431 ((tag
>> 16) & 0xff),
432 ((tag
>> 24) & 0xff), length
);
434 case MKTAG('I', 'H', 'D', 'R'):
437 s
->width
= bytestream2_get_be32(&s
->gb
);
438 s
->height
= bytestream2_get_be32(&s
->gb
);
439 if (av_image_check_size(s
->width
, s
->height
, 0, avctx
)) {
440 s
->width
= s
->height
= 0;
443 s
->bit_depth
= bytestream2_get_byte(&s
->gb
);
444 s
->color_type
= bytestream2_get_byte(&s
->gb
);
445 s
->compression_type
= bytestream2_get_byte(&s
->gb
);
446 s
->filter_type
= bytestream2_get_byte(&s
->gb
);
447 s
->interlace_type
= bytestream2_get_byte(&s
->gb
);
448 bytestream2_skip(&s
->gb
, 4); /* crc */
449 s
->state
|= PNG_IHDR
;
450 av_dlog(avctx
, "width=%d height=%d depth=%d color_type=%d "
451 "compression_type=%d filter_type=%d interlace_type=%d\n",
452 s
->width
, s
->height
, s
->bit_depth
, s
->color_type
,
453 s
->compression_type
, s
->filter_type
, s
->interlace_type
);
455 case MKTAG('I', 'D', 'A', 'T'):
456 if (!(s
->state
& PNG_IHDR
))
458 if (!(s
->state
& PNG_IDAT
)) {
459 /* init image info */
460 avctx
->width
= s
->width
;
461 avctx
->height
= s
->height
;
463 s
->channels
= ff_png_get_nb_channels(s
->color_type
);
464 s
->bits_per_pixel
= s
->bit_depth
* s
->channels
;
465 s
->bpp
= (s
->bits_per_pixel
+ 7) >> 3;
466 s
->row_size
= (avctx
->width
* s
->bits_per_pixel
+ 7) >> 3;
468 if (s
->bit_depth
== 8 &&
469 s
->color_type
== PNG_COLOR_TYPE_RGB
) {
470 avctx
->pix_fmt
= AV_PIX_FMT_RGB24
;
471 } else if (s
->bit_depth
== 8 &&
472 s
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
473 avctx
->pix_fmt
= AV_PIX_FMT_RGB32
;
474 } else if (s
->bit_depth
== 8 &&
475 s
->color_type
== PNG_COLOR_TYPE_GRAY
) {
476 avctx
->pix_fmt
= AV_PIX_FMT_GRAY8
;
477 } else if (s
->bit_depth
== 16 &&
478 s
->color_type
== PNG_COLOR_TYPE_GRAY
) {
479 avctx
->pix_fmt
= AV_PIX_FMT_GRAY16BE
;
480 } else if (s
->bit_depth
== 16 &&
481 s
->color_type
== PNG_COLOR_TYPE_RGB
) {
482 avctx
->pix_fmt
= AV_PIX_FMT_RGB48BE
;
483 } else if (s
->bit_depth
== 1 &&
484 s
->color_type
== PNG_COLOR_TYPE_GRAY
) {
485 avctx
->pix_fmt
= AV_PIX_FMT_MONOBLACK
;
486 } else if (s
->bit_depth
== 8 &&
487 s
->color_type
== PNG_COLOR_TYPE_PALETTE
) {
488 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
489 } else if (s
->bit_depth
== 8 &&
490 s
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
) {
491 avctx
->pix_fmt
= AV_PIX_FMT_Y400A
;
496 avctx
->release_buffer(avctx
, p
);
499 if (ff_get_buffer(avctx
, p
) < 0) {
500 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
503 p
->pict_type
= AV_PICTURE_TYPE_I
;
505 p
->interlaced_frame
= !!s
->interlace_type
;
507 /* compute the compressed row size */
508 if (!s
->interlace_type
) {
509 s
->crow_size
= s
->row_size
+ 1;
512 s
->pass_row_size
= ff_png_pass_row_size(s
->pass
,
515 s
->crow_size
= s
->pass_row_size
+ 1;
517 av_dlog(avctx
, "row_size=%d crow_size =%d\n",
518 s
->row_size
, s
->crow_size
);
519 s
->image_buf
= p
->data
[0];
520 s
->image_linesize
= p
->linesize
[0];
521 /* copy the palette if needed */
522 if (s
->color_type
== PNG_COLOR_TYPE_PALETTE
)
523 memcpy(p
->data
[1], s
->palette
, 256 * sizeof(uint32_t));
524 /* empty row is used if differencing to the first row */
525 s
->last_row
= av_mallocz(s
->row_size
);
528 if (s
->interlace_type
||
529 s
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
530 s
->tmp_row
= av_malloc(s
->row_size
);
535 crow_buf_base
= av_malloc(s
->row_size
+ 16);
539 /* we want crow_buf+1 to be 16-byte aligned */
540 s
->crow_buf
= crow_buf_base
+ 15;
541 s
->zstream
.avail_out
= s
->crow_size
;
542 s
->zstream
.next_out
= s
->crow_buf
;
544 s
->state
|= PNG_IDAT
;
545 if (png_decode_idat(s
, length
) < 0)
547 bytestream2_skip(&s
->gb
, 4); /* crc */
549 case MKTAG('P', 'L', 'T', 'E'):
553 if ((length
% 3) != 0 || length
> 256 * 3)
555 /* read the palette */
557 for (i
= 0; i
< n
; i
++) {
558 r
= bytestream2_get_byte(&s
->gb
);
559 g
= bytestream2_get_byte(&s
->gb
);
560 b
= bytestream2_get_byte(&s
->gb
);
561 s
->palette
[i
] = (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
563 for (; i
< 256; i
++) {
564 s
->palette
[i
] = (0xff << 24);
566 s
->state
|= PNG_PLTE
;
567 bytestream2_skip(&s
->gb
, 4); /* crc */
570 case MKTAG('t', 'R', 'N', 'S'):
574 /* read the transparency. XXX: Only palette mode supported */
575 if (s
->color_type
!= PNG_COLOR_TYPE_PALETTE
||
577 !(s
->state
& PNG_PLTE
))
579 for (i
= 0; i
< length
; i
++) {
580 v
= bytestream2_get_byte(&s
->gb
);
581 s
->palette
[i
] = (s
->palette
[i
] & 0x00ffffff) | (v
<< 24);
583 bytestream2_skip(&s
->gb
, 4); /* crc */
586 case MKTAG('I', 'E', 'N', 'D'):
587 if (!(s
->state
& PNG_ALLIMAGE
))
589 bytestream2_skip(&s
->gb
, 4); /* crc */
594 bytestream2_skip(&s
->gb
, length
+ 4);
599 /* handle p-frames only if a predecessor frame is available */
600 if (s
->last_picture
->data
[0] != NULL
) {
601 if (!(avpkt
->flags
& AV_PKT_FLAG_KEY
)) {
603 uint8_t *pd
= s
->current_picture
->data
[0];
604 uint8_t *pd_last
= s
->last_picture
->data
[0];
606 for (j
= 0; j
< s
->height
; j
++) {
607 for (i
= 0; i
< s
->width
* s
->bpp
; i
++) {
610 pd
+= s
->image_linesize
;
611 pd_last
+= s
->image_linesize
;
616 *picture
= *s
->current_picture
;
619 ret
= bytestream2_tell(&s
->gb
);
621 inflateEnd(&s
->zstream
);
622 av_free(crow_buf_base
);
624 av_freep(&s
->last_row
);
625 av_freep(&s
->tmp_row
);
632 static av_cold
int png_dec_init(AVCodecContext
*avctx
)
634 PNGDecContext
*s
= avctx
->priv_data
;
636 s
->current_picture
= &s
->picture1
;
637 s
->last_picture
= &s
->picture2
;
638 avcodec_get_frame_defaults(&s
->picture1
);
639 avcodec_get_frame_defaults(&s
->picture2
);
640 ff_pngdsp_init(&s
->dsp
);
645 static av_cold
int png_dec_end(AVCodecContext
*avctx
)
647 PNGDecContext
*s
= avctx
->priv_data
;
649 if (s
->picture1
.data
[0])
650 avctx
->release_buffer(avctx
, &s
->picture1
);
651 if (s
->picture2
.data
[0])
652 avctx
->release_buffer(avctx
, &s
->picture2
);
657 AVCodec ff_png_decoder
= {
659 .type
= AVMEDIA_TYPE_VIDEO
,
660 .id
= AV_CODEC_ID_PNG
,
661 .priv_data_size
= sizeof(PNGDecContext
),
662 .init
= png_dec_init
,
663 .close
= png_dec_end
,
664 .decode
= decode_frame
,
665 .capabilities
= CODEC_CAP_DR1
/*| CODEC_CAP_DRAW_HORIZ_BAND*/,
666 .long_name
= NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),