asfdec: also read Metadata Library Object
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / pngdec.c
blobb4e1faeae898ea02bbce136dd0bf54c32bdcb4b1
1 /*
2 * PNG image format
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"
22 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "internal.h"
25 #include "png.h"
26 #include "pngdsp.h"
28 /* TODO:
29 * - add 2, 4 and 16 bit depth support
32 #include <zlib.h>
34 //#define DEBUG
36 typedef struct PNGDecContext {
37 PNGDSPContext dsp;
39 GetByteContext gb;
40 AVFrame picture1, picture2;
41 AVFrame *current_picture, *last_picture;
43 int state;
44 int width, height;
45 int bit_depth;
46 int color_type;
47 int compression_type;
48 int interlace_type;
49 int filter_type;
50 int channels;
51 int bits_per_pixel;
52 int bpp;
54 uint8_t *image_buf;
55 int image_linesize;
56 uint32_t palette[256];
57 uint8_t *crow_buf;
58 uint8_t *last_row;
59 uint8_t *tmp_row;
60 int pass;
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 */
64 int y;
65 z_stream zstream;
66 } PNGDecContext;
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
80 this stage */
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;
86 uint8_t *d;
87 const uint8_t *s;
89 mask = ff_png_pass_mask[pass];
90 dsp_mask = png_pass_dsp_mask[pass];
92 switch (bits_per_pixel) {
93 case 1:
94 /* we must initialize the line to zero before writing to it */
95 if (pass == 0)
96 memset(dst, 0, (width + 7) >> 3);
97 src_x = 0;
98 for (x = 0; x < width; x++) {
99 j = (x & 7);
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)
105 src_x++;
107 break;
108 default:
109 bpp = bits_per_pixel >> 3;
110 d = dst;
111 s = src;
112 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
113 for (x = 0; x < width; x++) {
114 j = x & 7;
115 if ((dsp_mask << j) & 0x80) {
116 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
118 d += bpp;
119 if ((mask << j) & 0x80)
120 s += bpp;
122 } else {
123 for(x = 0; x < width; x++) {
124 j = x & 7;
125 if ((dsp_mask << j) & 0x80) {
126 memcpy(d, s, bpp);
128 d += bpp;
129 if ((mask << j) & 0x80)
130 s += bpp;
133 break;
137 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
139 int i;
140 for (i = 0; i < w; i++) {
141 int a, b, c, p, pa, pb, pc;
143 a = dst[i - bpp];
144 b = top[i];
145 c = top[i - bpp];
147 p = b - c;
148 pc = a - c;
150 pa = abs(p);
151 pb = abs(pc);
152 pc = abs(p + pc);
154 if (pa <= pb && pa <= pc)
155 p = a;
156 else if (pb <= pc)
157 p = b;
158 else
159 p = c;
160 dst[i] = p + src[i];
164 #define UNROLL1(bpp, op) {\
165 r = dst[0];\
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)\
185 else {\
186 for (; i < size; i += bpp) {\
187 int j;\
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);
202 break;
203 case PNG_FILTER_VALUE_SUB:
204 for (i = 0; i < bpp; i++) {
205 dst[i] = src[i];
207 if (bpp == 4) {
208 p = *(int*)dst;
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;
214 } else {
215 #define OP_SUB(x,s,l) x+s
216 UNROLL_FILTER(OP_SUB);
218 break;
219 case PNG_FILTER_VALUE_UP:
220 dsp->add_bytes_l2(dst, src, last, size);
221 break;
222 case PNG_FILTER_VALUE_AVG:
223 for (i = 0; i < bpp; i++) {
224 p = (last[i] >> 1);
225 dst[i] = p + src[i];
227 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
228 UNROLL_FILTER(OP_AVG);
229 break;
230 case PNG_FILTER_VALUE_PAETH:
231 for (i = 0; i < bpp; i++) {
232 p = last[i];
233 dst[i] = p + src[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);
239 i = w;
241 ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
242 break;
246 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst,
247 const uint8_t *src,
248 int width, int loco)
250 int j;
251 unsigned int r, g, b, a;
253 for (j = 0; j < width; j++) {
254 r = src[0];
255 g = src[1];
256 b = src[2];
257 a = src[3];
258 if (loco) {
259 r = (r + g) & 0xff;
260 b = (b + g) & 0xff;
262 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
263 dst += 4;
264 src += 4;
268 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
270 if (loco)
271 convert_to_rgb32_loco(dst, src, width, 1);
272 else
273 convert_to_rgb32_loco(dst, src, width, 0);
276 static void deloco_rgb24(uint8_t *dst, int size)
278 int i;
279 for (i = 0; i < size; i += 3) {
280 int g = dst[i + 1];
281 dst[i + 0] += g;
282 dst[i + 2] += g;
286 /* process exactly one decompressed row */
287 static void png_handle_row(PNGDecContext *s)
289 uint8_t *ptr, *last_row;
290 int got_line;
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);
300 } else {
301 /* in normal case, we avoid one copy */
302 if (s->y == 0)
303 last_row = s->last_row;
304 else
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);
314 s->y++;
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);
321 } else {
322 got_line = 0;
323 for (;;) {
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 */
328 if (got_line)
329 break;
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);
333 got_line = 1;
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);
340 s->y++;
341 if (s->y == s->height) {
342 for (;;) {
343 if (s->pass == NB_PASSES - 1) {
344 s->state |= PNG_ALLIMAGE;
345 goto the_end;
346 } else {
347 s->pass++;
348 s->y = 0;
349 s->pass_row_size = ff_png_pass_row_size(s->pass,
350 s->bits_per_pixel,
351 s->width);
352 s->crow_size = s->pass_row_size + 1;
353 if (s->pass_row_size != 0)
354 break;
355 /* skip pass if empty row */
360 the_end: ;
364 static int png_decode_idat(PNGDecContext *s, int length)
366 int ret;
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) {
375 return -1;
377 if (s->zstream.avail_out == 0) {
378 if (!(s->state & PNG_ALLIMAGE)) {
379 png_handle_row(s);
381 s->zstream.avail_out = s->crow_size;
382 s->zstream.next_out = s->crow_buf;
385 return 0;
388 static int decode_frame(AVCodecContext *avctx,
389 void *data, int *got_frame,
390 AVPacket *avpkt)
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;
397 AVFrame *p;
398 uint32_t tag, length;
399 int ret;
401 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
402 avctx->coded_frame = s->current_picture;
403 p = s->current_picture;
405 /* check signature */
406 if (buf_size < 8 ||
407 memcmp(buf, ff_pngsig, 8) != 0 &&
408 memcmp(buf, ff_mngsig, 8) != 0)
409 return -1;
411 bytestream2_init(&s->gb, buf + 8, buf_size - 8);
412 s->y = s->state = 0;
414 /* init the zlib */
415 s->zstream.zalloc = ff_png_zalloc;
416 s->zstream.zfree = ff_png_zfree;
417 s->zstream.opaque = NULL;
418 ret = inflateInit(&s->zstream);
419 if (ret != Z_OK)
420 return -1;
421 for (;;) {
422 if (bytestream2_get_bytes_left(&s->gb) <= 0)
423 goto fail;
424 length = bytestream2_get_be32(&s->gb);
425 if (length > 0x7fffffff)
426 goto fail;
427 tag = bytestream2_get_le32(&s->gb);
428 av_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
429 (tag & 0xff),
430 ((tag >> 8) & 0xff),
431 ((tag >> 16) & 0xff),
432 ((tag >> 24) & 0xff), length);
433 switch (tag) {
434 case MKTAG('I', 'H', 'D', 'R'):
435 if (length != 13)
436 goto fail;
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;
441 goto fail;
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);
454 break;
455 case MKTAG('I', 'D', 'A', 'T'):
456 if (!(s->state & PNG_IHDR))
457 goto fail;
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;
492 } else {
493 goto fail;
495 if (p->data[0])
496 avctx->release_buffer(avctx, p);
498 p->reference = 0;
499 if (ff_get_buffer(avctx, p) < 0) {
500 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
501 goto fail;
503 p->pict_type = AV_PICTURE_TYPE_I;
504 p->key_frame = 1;
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;
510 } else {
511 s->pass = 0;
512 s->pass_row_size = ff_png_pass_row_size(s->pass,
513 s->bits_per_pixel,
514 s->width);
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);
526 if (!s->last_row)
527 goto fail;
528 if (s->interlace_type ||
529 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
530 s->tmp_row = av_malloc(s->row_size);
531 if (!s->tmp_row)
532 goto fail;
534 /* compressed row */
535 crow_buf_base = av_malloc(s->row_size + 16);
536 if (!crow_buf_base)
537 goto fail;
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)
546 goto fail;
547 bytestream2_skip(&s->gb, 4); /* crc */
548 break;
549 case MKTAG('P', 'L', 'T', 'E'):
551 int n, i, r, g, b;
553 if ((length % 3) != 0 || length > 256 * 3)
554 goto skip_tag;
555 /* read the palette */
556 n = length / 3;
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 */
569 break;
570 case MKTAG('t', 'R', 'N', 'S'):
572 int v, i;
574 /* read the transparency. XXX: Only palette mode supported */
575 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
576 length > 256 ||
577 !(s->state & PNG_PLTE))
578 goto skip_tag;
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 */
585 break;
586 case MKTAG('I', 'E', 'N', 'D'):
587 if (!(s->state & PNG_ALLIMAGE))
588 goto fail;
589 bytestream2_skip(&s->gb, 4); /* crc */
590 goto exit_loop;
591 default:
592 /* skip tag */
593 skip_tag:
594 bytestream2_skip(&s->gb, length + 4);
595 break;
598 exit_loop:
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)) {
602 int i, j;
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++) {
608 pd[i] += pd_last[i];
610 pd += s->image_linesize;
611 pd_last += s->image_linesize;
616 *picture = *s->current_picture;
617 *got_frame = 1;
619 ret = bytestream2_tell(&s->gb);
620 the_end:
621 inflateEnd(&s->zstream);
622 av_free(crow_buf_base);
623 s->crow_buf = NULL;
624 av_freep(&s->last_row);
625 av_freep(&s->tmp_row);
626 return ret;
627 fail:
628 ret = -1;
629 goto the_end;
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);
642 return 0;
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);
654 return 0;
657 AVCodec ff_png_decoder = {
658 .name = "png",
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"),