3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "bytestream.h"
27 * - add 2, 4 and 16 bit depth support
34 typedef struct PNGDecContext
{
37 const uint8_t *bytestream
;
38 const uint8_t *bytestream_start
;
39 const uint8_t *bytestream_end
;
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
];
91 switch(bits_per_pixel
) {
93 /* we must initialize the line to zero before writing to it */
95 memset(dst
, 0, (width
+ 7) >> 3);
97 for(x
= 0; x
< width
; x
++) {
99 if ((dsp_mask
<< j
) & 0x80) {
100 b
= (src
[src_x
>> 3] >> (7 - (src_x
& 7))) & 1;
101 dst
[x
>> 3] |= b
<< (7 - j
);
103 if ((mask
<< j
) & 0x80)
108 bpp
= bits_per_pixel
>> 3;
111 if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
112 for(x
= 0; x
< width
; x
++) {
114 if ((dsp_mask
<< j
) & 0x80) {
115 *(uint32_t *)d
= (s
[3] << 24) | (s
[0] << 16) | (s
[1] << 8) | s
[2];
118 if ((mask
<< j
) & 0x80)
122 for(x
= 0; x
< width
; x
++) {
124 if ((dsp_mask
<< j
) & 0x80) {
128 if ((mask
<< j
) & 0x80)
136 void ff_add_png_paeth_prediction(uint8_t *dst
, uint8_t *src
, uint8_t *top
, int w
, int bpp
)
139 for(i
= 0; i
< w
; i
++) {
140 int a
, b
, c
, p
, pa
, pb
, pc
;
153 if (pa
<= pb
&& pa
<= pc
)
163 #define UNROLL1(bpp, op) {\
165 if(bpp >= 2) g = dst[1];\
166 if(bpp >= 3) b = dst[2];\
167 if(bpp >= 4) a = dst[3];\
168 for(; i < size; i+=bpp) {\
169 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
170 if(bpp == 1) continue;\
171 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
172 if(bpp == 2) continue;\
173 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
174 if(bpp == 3) continue;\
175 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
179 #define UNROLL_FILTER(op)\
180 if(bpp == 1) UNROLL1(1, op)\
181 else if(bpp == 2) UNROLL1(2, op)\
182 else if(bpp == 3) UNROLL1(3, op)\
183 else if(bpp == 4) UNROLL1(4, op)\
185 for (; i < size; i += bpp) {\
187 for (j = 0; j < bpp; j++)\
188 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
192 /* NOTE: 'dst' can be equal to 'last' */
193 static void png_filter_row(DSPContext
*dsp
, uint8_t *dst
, int filter_type
,
194 uint8_t *src
, uint8_t *last
, int size
, int bpp
)
196 int i
, p
, r
, g
, b
, a
;
198 switch(filter_type
) {
199 case PNG_FILTER_VALUE_NONE
:
200 memcpy(dst
, src
, size
);
202 case PNG_FILTER_VALUE_SUB
:
203 for(i
= 0; i
< bpp
; i
++) {
208 for(; i
< size
; i
+=bpp
) {
209 int s
= *(int*)(src
+i
);
210 p
= ((s
&0x7f7f7f7f) + (p
&0x7f7f7f7f)) ^ ((s
^p
)&0x80808080);
214 #define OP_SUB(x,s,l) x+s
215 UNROLL_FILTER(OP_SUB
);
218 case PNG_FILTER_VALUE_UP
:
219 dsp
->add_bytes_l2(dst
, src
, last
, size
);
221 case PNG_FILTER_VALUE_AVG
:
222 for(i
= 0; i
< bpp
; i
++) {
226 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
227 UNROLL_FILTER(OP_AVG
);
229 case PNG_FILTER_VALUE_PAETH
:
230 for(i
= 0; i
< bpp
; i
++) {
234 if(bpp
> 1 && size
> 4) {
235 // would write off the end of the array if we let it process the last pixel with bpp=3
236 int w
= bpp
==4 ? size
: size
-3;
237 dsp
->add_png_paeth_prediction(dst
+i
, src
+i
, last
+i
, w
-i
, bpp
);
240 ff_add_png_paeth_prediction(dst
+i
, src
+i
, last
+i
, size
-i
, bpp
);
245 static av_always_inline
void convert_to_rgb32_loco(uint8_t *dst
, const uint8_t *src
, int width
, int loco
)
248 unsigned int r
, g
, b
, a
;
250 for(j
= 0;j
< width
; j
++) {
259 *(uint32_t *)dst
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
265 static void convert_to_rgb32(uint8_t *dst
, const uint8_t *src
, int width
, int loco
)
268 convert_to_rgb32_loco(dst
, src
, width
, 1);
270 convert_to_rgb32_loco(dst
, src
, width
, 0);
273 static void deloco_rgb24(uint8_t *dst
, int size
)
276 for(i
=0; i
<size
; i
+=3) {
283 /* process exactly one decompressed row */
284 static void png_handle_row(PNGDecContext
*s
)
286 uint8_t *ptr
, *last_row
;
289 if (!s
->interlace_type
) {
290 ptr
= s
->image_buf
+ s
->image_linesize
* s
->y
;
291 /* need to swap bytes correctly for RGB_ALPHA */
292 if (s
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
293 png_filter_row(&s
->dsp
, s
->tmp_row
, s
->crow_buf
[0], s
->crow_buf
+ 1,
294 s
->last_row
, s
->row_size
, s
->bpp
);
295 convert_to_rgb32(ptr
, s
->tmp_row
, s
->width
, s
->filter_type
== PNG_FILTER_TYPE_LOCO
);
296 FFSWAP(uint8_t*, s
->last_row
, s
->tmp_row
);
298 /* in normal case, we avoid one copy */
300 last_row
= s
->last_row
;
302 last_row
= ptr
- s
->image_linesize
;
304 png_filter_row(&s
->dsp
, ptr
, s
->crow_buf
[0], s
->crow_buf
+ 1,
305 last_row
, s
->row_size
, s
->bpp
);
307 /* loco lags by 1 row so that it doesn't interfere with top prediction */
308 if (s
->filter_type
== PNG_FILTER_TYPE_LOCO
&&
309 s
->color_type
== PNG_COLOR_TYPE_RGB
&& s
->y
> 0)
310 deloco_rgb24(ptr
- s
->image_linesize
, s
->row_size
);
312 if (s
->y
== s
->height
) {
313 s
->state
|= PNG_ALLIMAGE
;
314 if (s
->filter_type
== PNG_FILTER_TYPE_LOCO
&&
315 s
->color_type
== PNG_COLOR_TYPE_RGB
)
316 deloco_rgb24(ptr
, s
->row_size
);
321 ptr
= s
->image_buf
+ s
->image_linesize
* s
->y
;
322 if ((ff_png_pass_ymask
[s
->pass
] << (s
->y
& 7)) & 0x80) {
323 /* if we already read one row, it is time to stop to
324 wait for the next one */
327 png_filter_row(&s
->dsp
, s
->tmp_row
, s
->crow_buf
[0], s
->crow_buf
+ 1,
328 s
->last_row
, s
->pass_row_size
, s
->bpp
);
329 FFSWAP(uint8_t*, s
->last_row
, s
->tmp_row
);
332 if ((png_pass_dsp_ymask
[s
->pass
] << (s
->y
& 7)) & 0x80) {
333 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
334 png_put_interlaced_row(ptr
, s
->width
, s
->bits_per_pixel
, s
->pass
,
335 s
->color_type
, s
->last_row
);
338 if (s
->y
== s
->height
) {
340 if (s
->pass
== NB_PASSES
- 1) {
341 s
->state
|= PNG_ALLIMAGE
;
346 s
->pass_row_size
= ff_png_pass_row_size(s
->pass
,
349 s
->crow_size
= s
->pass_row_size
+ 1;
350 if (s
->pass_row_size
!= 0)
352 /* skip pass if empty row */
361 static int png_decode_idat(PNGDecContext
*s
, int length
)
364 s
->zstream
.avail_in
= length
;
365 s
->zstream
.next_in
= s
->bytestream
;
366 s
->bytestream
+= length
;
368 if(s
->bytestream
> s
->bytestream_end
)
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 *data_size
,
392 const uint8_t *buf
= avpkt
->data
;
393 int buf_size
= avpkt
->size
;
394 PNGDecContext
* const s
= avctx
->priv_data
;
395 AVFrame
*picture
= data
;
397 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
;
407 s
->bytestream_end
= buf
+ buf_size
;
409 /* check signature */
410 if (memcmp(s
->bytestream
, ff_pngsig
, 8) != 0 &&
411 memcmp(s
->bytestream
, ff_mngsig
, 8) != 0)
416 // memset(s, 0, sizeof(PNGDecContext));
418 s
->zstream
.zalloc
= ff_png_zalloc
;
419 s
->zstream
.zfree
= ff_png_zfree
;
420 s
->zstream
.opaque
= NULL
;
421 ret
= inflateInit(&s
->zstream
);
426 if (s
->bytestream
>= s
->bytestream_end
)
428 length
= bytestream_get_be32(&s
->bytestream
);
429 if (length
> 0x7fffffff)
431 tag32
= bytestream_get_be32(&s
->bytestream
);
432 tag
= bswap_32(tag32
);
434 av_log(avctx
, AV_LOG_DEBUG
, "png: tag=%c%c%c%c length=%u\n",
437 ((tag
>> 16) & 0xff),
438 ((tag
>> 24) & 0xff), length
);
441 case MKTAG('I', 'H', 'D', 'R'):
444 s
->width
= bytestream_get_be32(&s
->bytestream
);
445 s
->height
= bytestream_get_be32(&s
->bytestream
);
446 if(avcodec_check_dimensions(avctx
, s
->width
, s
->height
)){
447 s
->width
= s
->height
= 0;
450 s
->bit_depth
= *s
->bytestream
++;
451 s
->color_type
= *s
->bytestream
++;
452 s
->compression_type
= *s
->bytestream
++;
453 s
->filter_type
= *s
->bytestream
++;
454 s
->interlace_type
= *s
->bytestream
++;
455 crc
= bytestream_get_be32(&s
->bytestream
);
456 s
->state
|= PNG_IHDR
;
458 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",
459 s
->width
, s
->height
, s
->bit_depth
, s
->color_type
,
460 s
->compression_type
, s
->filter_type
, s
->interlace_type
);
463 case MKTAG('I', 'D', 'A', 'T'):
464 if (!(s
->state
& PNG_IHDR
))
466 if (!(s
->state
& PNG_IDAT
)) {
467 /* init image info */
468 avctx
->width
= s
->width
;
469 avctx
->height
= s
->height
;
471 s
->channels
= ff_png_get_nb_channels(s
->color_type
);
472 s
->bits_per_pixel
= s
->bit_depth
* s
->channels
;
473 s
->bpp
= (s
->bits_per_pixel
+ 7) >> 3;
474 s
->row_size
= (avctx
->width
* s
->bits_per_pixel
+ 7) >> 3;
476 if (s
->bit_depth
== 8 &&
477 s
->color_type
== PNG_COLOR_TYPE_RGB
) {
478 avctx
->pix_fmt
= PIX_FMT_RGB24
;
479 } else if (s
->bit_depth
== 8 &&
480 s
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
481 avctx
->pix_fmt
= PIX_FMT_RGB32
;
482 } else if (s
->bit_depth
== 8 &&
483 s
->color_type
== PNG_COLOR_TYPE_GRAY
) {
484 avctx
->pix_fmt
= PIX_FMT_GRAY8
;
485 } else if (s
->bit_depth
== 16 &&
486 s
->color_type
== PNG_COLOR_TYPE_GRAY
) {
487 avctx
->pix_fmt
= PIX_FMT_GRAY16BE
;
488 } else if (s
->bit_depth
== 16 &&
489 s
->color_type
== PNG_COLOR_TYPE_RGB
) {
490 avctx
->pix_fmt
= PIX_FMT_RGB48BE
;
491 } else if (s
->bit_depth
== 1 &&
492 s
->color_type
== PNG_COLOR_TYPE_GRAY
) {
493 avctx
->pix_fmt
= PIX_FMT_MONOBLACK
;
494 } else if (s
->color_type
== PNG_COLOR_TYPE_PALETTE
) {
495 avctx
->pix_fmt
= PIX_FMT_PAL8
;
500 avctx
->release_buffer(avctx
, p
);
503 if(avctx
->get_buffer(avctx
, p
) < 0){
504 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
507 p
->pict_type
= FF_I_TYPE
;
509 p
->interlaced_frame
= !!s
->interlace_type
;
511 /* compute the compressed row size */
512 if (!s
->interlace_type
) {
513 s
->crow_size
= s
->row_size
+ 1;
516 s
->pass_row_size
= ff_png_pass_row_size(s
->pass
,
519 s
->crow_size
= s
->pass_row_size
+ 1;
522 av_log(avctx
, AV_LOG_DEBUG
, "row_size=%d crow_size =%d\n",
523 s
->row_size
, s
->crow_size
);
525 s
->image_buf
= p
->data
[0];
526 s
->image_linesize
= p
->linesize
[0];
527 /* copy the palette if needed */
528 if (s
->color_type
== PNG_COLOR_TYPE_PALETTE
)
529 memcpy(p
->data
[1], s
->palette
, 256 * sizeof(uint32_t));
530 /* empty row is used if differencing to the first row */
531 s
->last_row
= av_mallocz(s
->row_size
);
534 if (s
->interlace_type
||
535 s
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
536 s
->tmp_row
= av_malloc(s
->row_size
);
541 crow_buf_base
= av_malloc(s
->row_size
+ 16);
545 /* we want crow_buf+1 to be 16-byte aligned */
546 s
->crow_buf
= crow_buf_base
+ 15;
547 s
->zstream
.avail_out
= s
->crow_size
;
548 s
->zstream
.next_out
= s
->crow_buf
;
550 s
->state
|= PNG_IDAT
;
551 if (png_decode_idat(s
, length
) < 0)
554 crc
= bytestream_get_be32(&s
->bytestream
);
556 case MKTAG('P', 'L', 'T', 'E'):
560 if ((length
% 3) != 0 || length
> 256 * 3)
562 /* read the palette */
565 r
= *s
->bytestream
++;
566 g
= *s
->bytestream
++;
567 b
= *s
->bytestream
++;
568 s
->palette
[i
] = (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
571 s
->palette
[i
] = (0xff << 24);
573 s
->state
|= PNG_PLTE
;
574 crc
= bytestream_get_be32(&s
->bytestream
);
577 case MKTAG('t', 'R', 'N', 'S'):
581 /* read the transparency. XXX: Only palette mode supported */
582 if (s
->color_type
!= PNG_COLOR_TYPE_PALETTE
||
584 !(s
->state
& PNG_PLTE
))
586 for(i
=0;i
<length
;i
++) {
587 v
= *s
->bytestream
++;
588 s
->palette
[i
] = (s
->palette
[i
] & 0x00ffffff) | (v
<< 24);
590 crc
= bytestream_get_be32(&s
->bytestream
);
593 case MKTAG('I', 'E', 'N', 'D'):
594 if (!(s
->state
& PNG_ALLIMAGE
))
596 crc
= bytestream_get_be32(&s
->bytestream
);
601 s
->bytestream
+= length
+ 4;
606 /* handle p-frames only if a predecessor frame is available */
607 if(s
->last_picture
->data
[0] != NULL
) {
608 if(!(avpkt
->flags
& PKT_FLAG_KEY
)) {
610 uint8_t *pd
= s
->current_picture
->data
[0];
611 uint8_t *pd_last
= s
->last_picture
->data
[0];
613 for(j
=0; j
< s
->height
; j
++) {
614 for(i
=0; i
< s
->width
* s
->bpp
; i
++) {
617 pd
+= s
->image_linesize
;
618 pd_last
+= s
->image_linesize
;
623 *picture
= *s
->current_picture
;
624 *data_size
= sizeof(AVFrame
);
626 ret
= s
->bytestream
- s
->bytestream_start
;
628 inflateEnd(&s
->zstream
);
629 av_free(crow_buf_base
);
631 av_freep(&s
->last_row
);
632 av_freep(&s
->tmp_row
);
639 static av_cold
int png_dec_init(AVCodecContext
*avctx
){
640 PNGDecContext
*s
= avctx
->priv_data
;
642 s
->current_picture
= &s
->picture1
;
643 s
->last_picture
= &s
->picture2
;
644 avcodec_get_frame_defaults(&s
->picture1
);
645 avcodec_get_frame_defaults(&s
->picture2
);
646 dsputil_init(&s
->dsp
, avctx
);
651 AVCodec png_decoder
= {
655 sizeof(PNGDecContext
),
660 CODEC_CAP_DR1
/*| CODEC_CAP_DRAW_HORIZ_BAND*/,
662 .long_name
= NULL_IF_CONFIG_SMALL("PNG image"),