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;
55 uint32_t palette[256];
60 int crow_size; /* compressed row size (include filter type) */
61 int row_size; /* decompressed row size */
62 int pass_row_size; /* decompress row size of the current pass */
67 /* Mask to determine which y pixels can be written in a pass */
68 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
69 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
72 /* Mask to determine which pixels to overwrite while displaying */
73 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
74 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
77 /* NOTE: we try to construct a good looking image at each pass. width
78 is the original image width. We also do pixel format conversion at
80 static void png_put_interlaced_row(uint8_t *dst, int width,
81 int bits_per_pixel, int pass,
82 int color_type, const uint8_t *src)
84 int x, mask, dsp_mask, j, src_x, b, bpp;
88 mask = ff_png_pass_mask[pass];
89 dsp_mask = png_pass_dsp_mask[pass];
90 switch(bits_per_pixel) {
92 /* we must initialize the line to zero before writing to it */
94 memset(dst, 0, (width + 7) >> 3);
96 for(x = 0; x < width; x++) {
98 if ((dsp_mask << j) & 0x80) {
99 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
100 dst[x >> 3] |= b << (7 - j);
102 if ((mask << j) & 0x80)
107 bpp = bits_per_pixel >> 3;
110 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
111 for(x = 0; x < width; x++) {
113 if ((dsp_mask << j) & 0x80) {
114 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
117 if ((mask << j) & 0x80)
121 for(x = 0; x < width; x++) {
123 if ((dsp_mask << j) & 0x80) {
127 if ((mask << j) & 0x80)
135 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
138 for(i = 0; i < w; i++) {
139 int a, b, c, p, pa, pb, pc;
152 if (pa <= pb && pa <= pc)
162 #define UNROLL1(bpp, op) {\
164 if(bpp >= 2) g = dst[1];\
165 if(bpp >= 3) b = dst[2];\
166 if(bpp >= 4) a = dst[3];\
167 for(; i < size; i+=bpp) {\
168 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
169 if(bpp == 1) continue;\
170 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
171 if(bpp == 2) continue;\
172 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
173 if(bpp == 3) continue;\
174 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
178 #define UNROLL_FILTER(op)\
179 if(bpp == 1) UNROLL1(1, op)\
180 else if(bpp == 2) UNROLL1(2, op)\
181 else if(bpp == 3) UNROLL1(3, op)\
182 else if(bpp == 4) UNROLL1(4, op)\
184 /* NOTE: 'dst' can be equal to 'last' */
185 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
186 uint8_t *src, uint8_t *last, int size, int bpp)
188 int i, p, r, g, b, a;
190 switch(filter_type) {
191 case PNG_FILTER_VALUE_NONE:
192 memcpy(dst, src, size);
194 case PNG_FILTER_VALUE_SUB:
195 for(i = 0; i < bpp; i++) {
200 for(; i < size; i+=bpp) {
201 int s = *(int*)(src+i);
202 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
206 #define OP_SUB(x,s,l) x+s
207 UNROLL_FILTER(OP_SUB);
210 case PNG_FILTER_VALUE_UP:
211 dsp->add_bytes_l2(dst, src, last, size);
213 case PNG_FILTER_VALUE_AVG:
214 for(i = 0; i < bpp; i++) {
218 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
219 UNROLL_FILTER(OP_AVG);
221 case PNG_FILTER_VALUE_PAETH:
222 for(i = 0; i < bpp; i++) {
226 if(bpp > 1 && size > 4) {
227 // would write off the end of the array if we let it process the last pixel with bpp=3
228 int w = bpp==4 ? size : size-3;
229 dsp->add_png_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
232 ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
237 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width)
240 unsigned int r, g, b, a;
242 for(j = 0;j < width; j++) {
247 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
253 /* process exactly one decompressed row */
254 static void png_handle_row(PNGDecContext *s)
256 uint8_t *ptr, *last_row;
259 if (!s->interlace_type) {
260 ptr = s->image_buf + s->image_linesize * s->y;
261 /* need to swap bytes correctly for RGB_ALPHA */
262 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
263 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
264 s->last_row, s->row_size, s->bpp);
265 memcpy(s->last_row, s->tmp_row, s->row_size);
266 convert_to_rgb32(ptr, s->tmp_row, s->width);
268 /* in normal case, we avoid one copy */
270 last_row = s->last_row;
272 last_row = ptr - s->image_linesize;
274 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
275 last_row, s->row_size, s->bpp);
278 if (s->y == s->height) {
279 s->state |= PNG_ALLIMAGE;
284 ptr = s->image_buf + s->image_linesize * s->y;
285 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
286 /* if we already read one row, it is time to stop to
287 wait for the next one */
290 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
291 s->last_row, s->pass_row_size, s->bpp);
292 memcpy(s->last_row, s->tmp_row, s->pass_row_size);
295 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
296 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
297 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
298 s->color_type, s->last_row);
301 if (s->y == s->height) {
303 if (s->pass == NB_PASSES - 1) {
304 s->state |= PNG_ALLIMAGE;
309 s->pass_row_size = ff_png_pass_row_size(s->pass,
312 s->crow_size = s->pass_row_size + 1;
313 if (s->pass_row_size != 0)
315 /* skip pass if empty row */
324 static int png_decode_idat(PNGDecContext *s, int length)
327 s->zstream.avail_in = length;
328 s->zstream.next_in = s->bytestream;
329 s->bytestream += length;
331 if(s->bytestream > s->bytestream_end)
334 /* decode one line if possible */
335 while (s->zstream.avail_in > 0) {
336 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
337 if (ret != Z_OK && ret != Z_STREAM_END) {
340 if (s->zstream.avail_out == 0) {
341 if (!(s->state & PNG_ALLIMAGE)) {
344 s->zstream.avail_out = s->crow_size;
345 s->zstream.next_out = s->crow_buf;
351 static int decode_frame(AVCodecContext *avctx,
352 void *data, int *data_size,
353 const uint8_t *buf, int buf_size)
355 PNGDecContext * const s = avctx->priv_data;
356 AVFrame *picture = data;
357 AVFrame * const p= (AVFrame*)&s->picture;
358 uint32_t tag, length;
363 s->bytestream_end= buf + buf_size;
365 /* check signature */
366 if (memcmp(s->bytestream, ff_pngsig, 8) != 0)
371 // memset(s, 0, sizeof(PNGDecContext));
373 s->zstream.zalloc = ff_png_zalloc;
374 s->zstream.zfree = ff_png_zfree;
375 s->zstream.opaque = NULL;
376 ret = inflateInit(&s->zstream);
381 if (s->bytestream >= s->bytestream_end)
383 length = bytestream_get_be32(&s->bytestream);
384 if (length > 0x7fffffff)
386 tag32 = bytestream_get_be32(&s->bytestream);
387 tag = bswap_32(tag32);
389 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
392 ((tag >> 16) & 0xff),
393 ((tag >> 24) & 0xff), length);
396 case MKTAG('I', 'H', 'D', 'R'):
399 s->width = bytestream_get_be32(&s->bytestream);
400 s->height = bytestream_get_be32(&s->bytestream);
401 if(avcodec_check_dimensions(avctx, s->width, s->height)){
402 s->width= s->height= 0;
405 s->bit_depth = *s->bytestream++;
406 s->color_type = *s->bytestream++;
407 s->compression_type = *s->bytestream++;
408 s->filter_type = *s->bytestream++;
409 s->interlace_type = *s->bytestream++;
410 crc = bytestream_get_be32(&s->bytestream);
411 s->state |= PNG_IHDR;
413 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",
414 s->width, s->height, s->bit_depth, s->color_type,
415 s->compression_type, s->filter_type, s->interlace_type);
418 case MKTAG('I', 'D', 'A', 'T'):
419 if (!(s->state & PNG_IHDR))
421 if (!(s->state & PNG_IDAT)) {
422 /* init image info */
423 avctx->width = s->width;
424 avctx->height = s->height;
426 s->channels = ff_png_get_nb_channels(s->color_type);
427 s->bits_per_pixel = s->bit_depth * s->channels;
428 s->bpp = (s->bits_per_pixel + 7) >> 3;
429 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
431 if (s->bit_depth == 8 &&
432 s->color_type == PNG_COLOR_TYPE_RGB) {
433 avctx->pix_fmt = PIX_FMT_RGB24;
434 } else if (s->bit_depth == 8 &&
435 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
436 avctx->pix_fmt = PIX_FMT_RGB32;
437 } else if (s->bit_depth == 8 &&
438 s->color_type == PNG_COLOR_TYPE_GRAY) {
439 avctx->pix_fmt = PIX_FMT_GRAY8;
440 } else if (s->bit_depth == 16 &&
441 s->color_type == PNG_COLOR_TYPE_GRAY) {
442 avctx->pix_fmt = PIX_FMT_GRAY16BE;
443 } else if (s->bit_depth == 1 &&
444 s->color_type == PNG_COLOR_TYPE_GRAY) {
445 avctx->pix_fmt = PIX_FMT_MONOBLACK;
446 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
447 avctx->pix_fmt = PIX_FMT_PAL8;
452 avctx->release_buffer(avctx, p);
455 if(avctx->get_buffer(avctx, p) < 0){
456 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
459 p->pict_type= FF_I_TYPE;
461 p->interlaced_frame = !!s->interlace_type;
463 /* compute the compressed row size */
464 if (!s->interlace_type) {
465 s->crow_size = s->row_size + 1;
468 s->pass_row_size = ff_png_pass_row_size(s->pass,
471 s->crow_size = s->pass_row_size + 1;
474 av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
475 s->row_size, s->crow_size);
477 s->image_buf = p->data[0];
478 s->image_linesize = p->linesize[0];
479 /* copy the palette if needed */
480 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
481 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
482 /* empty row is used if differencing to the first row */
483 s->last_row = av_mallocz(s->row_size);
486 if (s->interlace_type ||
487 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
488 s->tmp_row = av_malloc(s->row_size);
493 s->crow_buf = av_malloc(s->row_size + 1);
496 s->zstream.avail_out = s->crow_size;
497 s->zstream.next_out = s->crow_buf;
499 s->state |= PNG_IDAT;
500 if (png_decode_idat(s, length) < 0)
503 crc = bytestream_get_be32(&s->bytestream);
505 case MKTAG('P', 'L', 'T', 'E'):
509 if ((length % 3) != 0 || length > 256 * 3)
511 /* read the palette */
514 r = *s->bytestream++;
515 g = *s->bytestream++;
516 b = *s->bytestream++;
517 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
520 s->palette[i] = (0xff << 24);
522 s->state |= PNG_PLTE;
523 crc = bytestream_get_be32(&s->bytestream);
526 case MKTAG('t', 'R', 'N', 'S'):
530 /* read the transparency. XXX: Only palette mode supported */
531 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
533 !(s->state & PNG_PLTE))
535 for(i=0;i<length;i++) {
536 v = *s->bytestream++;
537 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
539 crc = bytestream_get_be32(&s->bytestream);
542 case MKTAG('I', 'E', 'N', 'D'):
543 if (!(s->state & PNG_ALLIMAGE))
545 crc = bytestream_get_be32(&s->bytestream);
550 s->bytestream += length + 4;
555 *picture= *(AVFrame*)&s->picture;
556 *data_size = sizeof(AVPicture);
558 ret = s->bytestream - s->bytestream_start;
560 inflateEnd(&s->zstream);
561 av_freep(&s->crow_buf);
562 av_freep(&s->last_row);
563 av_freep(&s->tmp_row);
570 static int png_dec_init(AVCodecContext *avctx){
571 PNGDecContext *s = avctx->priv_data;
573 avcodec_get_frame_defaults((AVFrame*)&s->picture);
574 avctx->coded_frame= (AVFrame*)&s->picture;
575 dsputil_init(&s->dsp, avctx);
580 AVCodec png_decoder = {
584 sizeof(PNGDecContext),
589 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,