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"
26 * - add 2, 4 and 16 bit depth support
33 typedef struct PNGDecContext {
35 uint8_t *bytestream_start;
36 uint8_t *bytestream_end;
52 uint32_t palette[256];
57 int crow_size; /* compressed row size (include filter type) */
58 int row_size; /* decompressed row size */
59 int pass_row_size; /* decompress row size of the current pass */
64 /* Mask to determine which y pixels can be written in a pass */
65 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
66 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
69 /* Mask to determine which pixels to overwrite while displaying */
70 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
71 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
74 /* NOTE: we try to construct a good looking image at each pass. width
75 is the original image width. We also do pixel format conversion at
77 static void png_put_interlaced_row(uint8_t *dst, int width,
78 int bits_per_pixel, int pass,
79 int color_type, const uint8_t *src)
81 int x, mask, dsp_mask, j, src_x, b, bpp;
85 mask = ff_png_pass_mask[pass];
86 dsp_mask = png_pass_dsp_mask[pass];
87 switch(bits_per_pixel) {
89 /* we must initialize the line to zero before writing to it */
91 memset(dst, 0, (width + 7) >> 3);
93 for(x = 0; x < width; x++) {
95 if ((dsp_mask << j) & 0x80) {
96 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
97 dst[x >> 3] |= b << (7 - j);
99 if ((mask << j) & 0x80)
104 bpp = bits_per_pixel >> 3;
107 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
108 for(x = 0; x < width; x++) {
110 if ((dsp_mask << j) & 0x80) {
111 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
114 if ((mask << j) & 0x80)
118 for(x = 0; x < width; x++) {
120 if ((dsp_mask << j) & 0x80) {
124 if ((mask << j) & 0x80)
133 /* NOTE: 'dst' can be equal to 'last' */
134 static void png_filter_row(uint8_t *dst, int filter_type,
135 uint8_t *src, uint8_t *last, int size, int bpp)
139 switch(filter_type) {
140 case PNG_FILTER_VALUE_NONE:
141 memcpy(dst, src, size);
143 case PNG_FILTER_VALUE_SUB:
144 for(i = 0; i < bpp; i++) {
147 for(i = bpp; i < size; i++) {
152 case PNG_FILTER_VALUE_UP:
153 for(i = 0; i < size; i++) {
158 case PNG_FILTER_VALUE_AVG:
159 for(i = 0; i < bpp; i++) {
163 for(i = bpp; i < size; i++) {
164 p = ((dst[i - bpp] + last[i]) >> 1);
168 case PNG_FILTER_VALUE_PAETH:
169 for(i = 0; i < bpp; i++) {
173 for(i = bpp; i < size; i++) {
174 int a, b, c, pa, pb, pc;
187 if (pa <= pb && pa <= pc)
199 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width)
202 unsigned int r, g, b, a;
204 for(j = 0;j < width; j++) {
209 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
215 /* process exactly one decompressed row */
216 static void png_handle_row(PNGDecContext *s)
218 uint8_t *ptr, *last_row;
221 if (!s->interlace_type) {
222 ptr = s->image_buf + s->image_linesize * s->y;
223 /* need to swap bytes correctly for RGB_ALPHA */
224 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
225 png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
226 s->last_row, s->row_size, s->bpp);
227 memcpy(s->last_row, s->tmp_row, s->row_size);
228 convert_to_rgb32(ptr, s->tmp_row, s->width);
230 /* in normal case, we avoid one copy */
232 last_row = s->last_row;
234 last_row = ptr - s->image_linesize;
236 png_filter_row(ptr, s->crow_buf[0], s->crow_buf + 1,
237 last_row, s->row_size, s->bpp);
240 if (s->y == s->height) {
241 s->state |= PNG_ALLIMAGE;
246 ptr = s->image_buf + s->image_linesize * s->y;
247 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
248 /* if we already read one row, it is time to stop to
249 wait for the next one */
252 png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
253 s->last_row, s->pass_row_size, s->bpp);
254 memcpy(s->last_row, s->tmp_row, s->pass_row_size);
257 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
258 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
259 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
260 s->color_type, s->last_row);
263 if (s->y == s->height) {
265 if (s->pass == NB_PASSES - 1) {
266 s->state |= PNG_ALLIMAGE;
271 s->pass_row_size = ff_png_pass_row_size(s->pass,
274 s->crow_size = s->pass_row_size + 1;
275 if (s->pass_row_size != 0)
277 /* skip pass if empty row */
286 static int png_decode_idat(PNGDecContext *s, int length)
289 s->zstream.avail_in = length;
290 s->zstream.next_in = s->bytestream;
291 s->bytestream += length;
293 if(s->bytestream > s->bytestream_end)
296 /* decode one line if possible */
297 while (s->zstream.avail_in > 0) {
298 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
299 if (ret != Z_OK && ret != Z_STREAM_END) {
302 if (s->zstream.avail_out == 0) {
303 if (!(s->state & PNG_ALLIMAGE)) {
306 s->zstream.avail_out = s->crow_size;
307 s->zstream.next_out = s->crow_buf;
313 static int decode_frame(AVCodecContext *avctx,
314 void *data, int *data_size,
315 uint8_t *buf, int buf_size)
317 PNGDecContext * const s = avctx->priv_data;
318 AVFrame *picture = data;
319 AVFrame * const p= (AVFrame*)&s->picture;
320 uint32_t tag, length;
325 s->bytestream_end= buf + buf_size;
327 /* check signature */
328 if (memcmp(s->bytestream, ff_pngsig, 8) != 0)
333 // memset(s, 0, sizeof(PNGDecContext));
335 s->zstream.zalloc = ff_png_zalloc;
336 s->zstream.zfree = ff_png_zfree;
337 s->zstream.opaque = NULL;
338 ret = inflateInit(&s->zstream);
343 if (s->bytestream >= s->bytestream_end)
345 length = bytestream_get_be32(&s->bytestream);
346 if (length > 0x7fffffff)
348 tag32 = bytestream_get_be32(&s->bytestream);
349 tag = bswap_32(tag32);
351 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
354 ((tag >> 16) & 0xff),
355 ((tag >> 24) & 0xff), length);
358 case MKTAG('I', 'H', 'D', 'R'):
361 s->width = bytestream_get_be32(&s->bytestream);
362 s->height = bytestream_get_be32(&s->bytestream);
363 if(avcodec_check_dimensions(avctx, s->width, s->height)){
364 s->width= s->height= 0;
367 s->bit_depth = *s->bytestream++;
368 s->color_type = *s->bytestream++;
369 s->compression_type = *s->bytestream++;
370 s->filter_type = *s->bytestream++;
371 s->interlace_type = *s->bytestream++;
372 crc = bytestream_get_be32(&s->bytestream);
373 s->state |= PNG_IHDR;
375 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",
376 s->width, s->height, s->bit_depth, s->color_type,
377 s->compression_type, s->filter_type, s->interlace_type);
380 case MKTAG('I', 'D', 'A', 'T'):
381 if (!(s->state & PNG_IHDR))
383 if (!(s->state & PNG_IDAT)) {
384 /* init image info */
385 avctx->width = s->width;
386 avctx->height = s->height;
388 s->channels = ff_png_get_nb_channels(s->color_type);
389 s->bits_per_pixel = s->bit_depth * s->channels;
390 s->bpp = (s->bits_per_pixel + 7) >> 3;
391 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
393 if (s->bit_depth == 8 &&
394 s->color_type == PNG_COLOR_TYPE_RGB) {
395 avctx->pix_fmt = PIX_FMT_RGB24;
396 } else if (s->bit_depth == 8 &&
397 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
398 avctx->pix_fmt = PIX_FMT_RGB32;
399 } else if (s->bit_depth == 8 &&
400 s->color_type == PNG_COLOR_TYPE_GRAY) {
401 avctx->pix_fmt = PIX_FMT_GRAY8;
402 } else if (s->bit_depth == 16 &&
403 s->color_type == PNG_COLOR_TYPE_GRAY) {
404 avctx->pix_fmt = PIX_FMT_GRAY16BE;
405 } else if (s->bit_depth == 1 &&
406 s->color_type == PNG_COLOR_TYPE_GRAY) {
407 avctx->pix_fmt = PIX_FMT_MONOBLACK;
408 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
409 avctx->pix_fmt = PIX_FMT_PAL8;
414 avctx->release_buffer(avctx, p);
417 if(avctx->get_buffer(avctx, p) < 0){
418 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
421 p->pict_type= FF_I_TYPE;
423 p->interlaced_frame = !!s->interlace_type;
425 /* compute the compressed row size */
426 if (!s->interlace_type) {
427 s->crow_size = s->row_size + 1;
430 s->pass_row_size = ff_png_pass_row_size(s->pass,
433 s->crow_size = s->pass_row_size + 1;
436 av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
437 s->row_size, s->crow_size);
439 s->image_buf = p->data[0];
440 s->image_linesize = p->linesize[0];
441 /* copy the palette if needed */
442 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
443 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
444 /* empty row is used if differencing to the first row */
445 s->last_row = av_mallocz(s->row_size);
448 if (s->interlace_type ||
449 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
450 s->tmp_row = av_malloc(s->row_size);
455 s->crow_buf = av_malloc(s->row_size + 1);
458 s->zstream.avail_out = s->crow_size;
459 s->zstream.next_out = s->crow_buf;
461 s->state |= PNG_IDAT;
462 if (png_decode_idat(s, length) < 0)
465 crc = bytestream_get_be32(&s->bytestream);
467 case MKTAG('P', 'L', 'T', 'E'):
471 if ((length % 3) != 0 || length > 256 * 3)
473 /* read the palette */
476 r = *s->bytestream++;
477 g = *s->bytestream++;
478 b = *s->bytestream++;
479 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
482 s->palette[i] = (0xff << 24);
484 s->state |= PNG_PLTE;
485 crc = bytestream_get_be32(&s->bytestream);
488 case MKTAG('t', 'R', 'N', 'S'):
492 /* read the transparency. XXX: Only palette mode supported */
493 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
495 !(s->state & PNG_PLTE))
497 for(i=0;i<length;i++) {
498 v = *s->bytestream++;
499 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
501 crc = bytestream_get_be32(&s->bytestream);
504 case MKTAG('I', 'E', 'N', 'D'):
505 if (!(s->state & PNG_ALLIMAGE))
507 crc = bytestream_get_be32(&s->bytestream);
512 s->bytestream += length + 4;
517 *picture= *(AVFrame*)&s->picture;
518 *data_size = sizeof(AVPicture);
520 ret = s->bytestream - s->bytestream_start;
522 inflateEnd(&s->zstream);
523 av_freep(&s->crow_buf);
524 av_freep(&s->last_row);
525 av_freep(&s->tmp_row);
532 static int png_dec_init(AVCodecContext *avctx){
533 PNGDecContext *s = avctx->priv_data;
535 avcodec_get_frame_defaults((AVFrame*)&s->picture);
536 avctx->coded_frame= (AVFrame*)&s->picture;
541 AVCodec png_decoder = {
545 sizeof(PNGDecContext),
550 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,