Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
[ffmpeg-lucabe.git] / libavcodec / pngdec.c
blobfadbcd07c945ec0acd92dae487e544abc6afa6a6
1 /*
2 * PNG image format
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
21 #include "avcodec.h"
22 #include "bytestream.h"
23 #include "png.h"
24 #include "dsputil.h"
26 /* TODO:
27 * - add 2, 4 and 16 bit depth support
30 #include <zlib.h>
32 //#define DEBUG
34 typedef struct PNGDecContext {
35 DSPContext dsp;
37 const uint8_t *bytestream;
38 const uint8_t *bytestream_start;
39 const uint8_t *bytestream_end;
40 AVFrame picture;
42 int state;
43 int width, height;
44 int bit_depth;
45 int color_type;
46 int compression_type;
47 int interlace_type;
48 int filter_type;
49 int channels;
50 int bits_per_pixel;
51 int bpp;
53 uint8_t *image_buf;
54 int image_linesize;
55 uint32_t palette[256];
56 uint8_t *crow_buf;
57 uint8_t *last_row;
58 uint8_t *tmp_row;
59 int pass;
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 */
63 int y;
64 z_stream zstream;
65 } PNGDecContext;
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
79 this stage */
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;
85 uint8_t *d;
86 const uint8_t *s;
88 mask = ff_png_pass_mask[pass];
89 dsp_mask = png_pass_dsp_mask[pass];
90 switch(bits_per_pixel) {
91 case 1:
92 /* we must initialize the line to zero before writing to it */
93 if (pass == 0)
94 memset(dst, 0, (width + 7) >> 3);
95 src_x = 0;
96 for(x = 0; x < width; x++) {
97 j = (x & 7);
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)
103 src_x++;
105 break;
106 default:
107 bpp = bits_per_pixel >> 3;
108 d = dst;
109 s = src;
110 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
111 for(x = 0; x < width; x++) {
112 j = x & 7;
113 if ((dsp_mask << j) & 0x80) {
114 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
116 d += bpp;
117 if ((mask << j) & 0x80)
118 s += bpp;
120 } else {
121 for(x = 0; x < width; x++) {
122 j = x & 7;
123 if ((dsp_mask << j) & 0x80) {
124 memcpy(d, s, bpp);
126 d += bpp;
127 if ((mask << j) & 0x80)
128 s += bpp;
131 break;
135 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
137 int i;
138 for(i = 0; i < w; i++) {
139 int a, b, c, p, pa, pb, pc;
141 a = dst[i - bpp];
142 b = top[i];
143 c = top[i - bpp];
145 p = b - c;
146 pc = a - c;
148 pa = abs(p);
149 pb = abs(pc);
150 pc = abs(p + pc);
152 if (pa <= pb && pa <= pc)
153 p = a;
154 else if (pb <= pc)
155 p = b;
156 else
157 p = c;
158 dst[i] = p + src[i];
162 #define UNROLL1(bpp, op) {\
163 r = dst[0];\
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);
193 break;
194 case PNG_FILTER_VALUE_SUB:
195 for(i = 0; i < bpp; i++) {
196 dst[i] = src[i];
198 if(bpp == 4) {
199 p = *(int*)dst;
200 for(; i < size; i+=bpp) {
201 int s = *(int*)(src+i);
202 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
203 *(int*)(dst+i) = p;
205 } else {
206 #define OP_SUB(x,s,l) x+s
207 UNROLL_FILTER(OP_SUB);
209 break;
210 case PNG_FILTER_VALUE_UP:
211 dsp->add_bytes_l2(dst, src, last, size);
212 break;
213 case PNG_FILTER_VALUE_AVG:
214 for(i = 0; i < bpp; i++) {
215 p = (last[i] >> 1);
216 dst[i] = p + src[i];
218 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
219 UNROLL_FILTER(OP_AVG);
220 break;
221 case PNG_FILTER_VALUE_PAETH:
222 for(i = 0; i < bpp; i++) {
223 p = last[i];
224 dst[i] = p + src[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);
230 i = w;
232 ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
233 break;
237 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
239 int j;
240 unsigned int r, g, b, a;
242 for(j = 0;j < width; j++) {
243 r = src[0];
244 g = src[1];
245 b = src[2];
246 a = src[3];
247 if(loco) {
248 r = (r+g)&0xff;
249 b = (b+g)&0xff;
251 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
252 dst += 4;
253 src += 4;
257 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
259 if(loco)
260 convert_to_rgb32_loco(dst, src, width, 1);
261 else
262 convert_to_rgb32_loco(dst, src, width, 0);
265 static void deloco_rgb24(uint8_t *dst, int size)
267 int i;
268 for(i=0; i<size; i+=3) {
269 int g = dst[i+1];
270 dst[i+0] += g;
271 dst[i+2] += g;
275 /* process exactly one decompressed row */
276 static void png_handle_row(PNGDecContext *s)
278 uint8_t *ptr, *last_row;
279 int got_line;
281 if (!s->interlace_type) {
282 ptr = s->image_buf + s->image_linesize * s->y;
283 /* need to swap bytes correctly for RGB_ALPHA */
284 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
285 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
286 s->last_row, s->row_size, s->bpp);
287 convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
288 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
289 } else {
290 /* in normal case, we avoid one copy */
291 if (s->y == 0)
292 last_row = s->last_row;
293 else
294 last_row = ptr - s->image_linesize;
296 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
297 last_row, s->row_size, s->bpp);
299 /* loco lags by 1 row so that it doesn't interfere with top prediction */
300 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
301 s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
302 deloco_rgb24(ptr - s->image_linesize, s->row_size);
303 s->y++;
304 if (s->y == s->height) {
305 s->state |= PNG_ALLIMAGE;
306 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
307 s->color_type == PNG_COLOR_TYPE_RGB)
308 deloco_rgb24(ptr, s->row_size);
310 } else {
311 got_line = 0;
312 for(;;) {
313 ptr = s->image_buf + s->image_linesize * s->y;
314 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
315 /* if we already read one row, it is time to stop to
316 wait for the next one */
317 if (got_line)
318 break;
319 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
320 s->last_row, s->pass_row_size, s->bpp);
321 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
322 got_line = 1;
324 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
325 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
326 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
327 s->color_type, s->last_row);
329 s->y++;
330 if (s->y == s->height) {
331 for(;;) {
332 if (s->pass == NB_PASSES - 1) {
333 s->state |= PNG_ALLIMAGE;
334 goto the_end;
335 } else {
336 s->pass++;
337 s->y = 0;
338 s->pass_row_size = ff_png_pass_row_size(s->pass,
339 s->bits_per_pixel,
340 s->width);
341 s->crow_size = s->pass_row_size + 1;
342 if (s->pass_row_size != 0)
343 break;
344 /* skip pass if empty row */
349 the_end: ;
353 static int png_decode_idat(PNGDecContext *s, int length)
355 int ret;
356 s->zstream.avail_in = length;
357 s->zstream.next_in = s->bytestream;
358 s->bytestream += length;
360 if(s->bytestream > s->bytestream_end)
361 return -1;
363 /* decode one line if possible */
364 while (s->zstream.avail_in > 0) {
365 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
366 if (ret != Z_OK && ret != Z_STREAM_END) {
367 return -1;
369 if (s->zstream.avail_out == 0) {
370 if (!(s->state & PNG_ALLIMAGE)) {
371 png_handle_row(s);
373 s->zstream.avail_out = s->crow_size;
374 s->zstream.next_out = s->crow_buf;
377 return 0;
380 static int decode_frame(AVCodecContext *avctx,
381 void *data, int *data_size,
382 AVPacket *avpkt)
384 const uint8_t *buf = avpkt->data;
385 int buf_size = avpkt->size;
386 PNGDecContext * const s = avctx->priv_data;
387 AVFrame *picture = data;
388 AVFrame * const p= &s->picture;
389 uint32_t tag, length;
390 int ret, crc;
392 s->bytestream_start=
393 s->bytestream= buf;
394 s->bytestream_end= buf + buf_size;
396 /* check signature */
397 if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
398 memcmp(s->bytestream, ff_mngsig, 8) != 0)
399 return -1;
400 s->bytestream+= 8;
401 s->y=
402 s->state=0;
403 // memset(s, 0, sizeof(PNGDecContext));
404 /* init the zlib */
405 s->zstream.zalloc = ff_png_zalloc;
406 s->zstream.zfree = ff_png_zfree;
407 s->zstream.opaque = NULL;
408 ret = inflateInit(&s->zstream);
409 if (ret != Z_OK)
410 return -1;
411 for(;;) {
412 int tag32;
413 if (s->bytestream >= s->bytestream_end)
414 goto fail;
415 length = bytestream_get_be32(&s->bytestream);
416 if (length > 0x7fffffff)
417 goto fail;
418 tag32 = bytestream_get_be32(&s->bytestream);
419 tag = bswap_32(tag32);
420 #ifdef DEBUG
421 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
422 (tag & 0xff),
423 ((tag >> 8) & 0xff),
424 ((tag >> 16) & 0xff),
425 ((tag >> 24) & 0xff), length);
426 #endif
427 switch(tag) {
428 case MKTAG('I', 'H', 'D', 'R'):
429 if (length != 13)
430 goto fail;
431 s->width = bytestream_get_be32(&s->bytestream);
432 s->height = bytestream_get_be32(&s->bytestream);
433 if(avcodec_check_dimensions(avctx, s->width, s->height)){
434 s->width= s->height= 0;
435 goto fail;
437 s->bit_depth = *s->bytestream++;
438 s->color_type = *s->bytestream++;
439 s->compression_type = *s->bytestream++;
440 s->filter_type = *s->bytestream++;
441 s->interlace_type = *s->bytestream++;
442 crc = bytestream_get_be32(&s->bytestream);
443 s->state |= PNG_IHDR;
444 #ifdef DEBUG
445 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",
446 s->width, s->height, s->bit_depth, s->color_type,
447 s->compression_type, s->filter_type, s->interlace_type);
448 #endif
449 break;
450 case MKTAG('I', 'D', 'A', 'T'):
451 if (!(s->state & PNG_IHDR))
452 goto fail;
453 if (!(s->state & PNG_IDAT)) {
454 /* init image info */
455 avctx->width = s->width;
456 avctx->height = s->height;
458 s->channels = ff_png_get_nb_channels(s->color_type);
459 s->bits_per_pixel = s->bit_depth * s->channels;
460 s->bpp = (s->bits_per_pixel + 7) >> 3;
461 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
463 if (s->bit_depth == 8 &&
464 s->color_type == PNG_COLOR_TYPE_RGB) {
465 avctx->pix_fmt = PIX_FMT_RGB24;
466 } else if (s->bit_depth == 8 &&
467 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
468 avctx->pix_fmt = PIX_FMT_RGB32;
469 } else if (s->bit_depth == 8 &&
470 s->color_type == PNG_COLOR_TYPE_GRAY) {
471 avctx->pix_fmt = PIX_FMT_GRAY8;
472 } else if (s->bit_depth == 16 &&
473 s->color_type == PNG_COLOR_TYPE_GRAY) {
474 avctx->pix_fmt = PIX_FMT_GRAY16BE;
475 } else if (s->bit_depth == 1 &&
476 s->color_type == PNG_COLOR_TYPE_GRAY) {
477 avctx->pix_fmt = PIX_FMT_MONOBLACK;
478 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
479 avctx->pix_fmt = PIX_FMT_PAL8;
480 } else {
481 goto fail;
483 if(p->data[0])
484 avctx->release_buffer(avctx, p);
486 p->reference= 0;
487 if(avctx->get_buffer(avctx, p) < 0){
488 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
489 goto fail;
491 p->pict_type= FF_I_TYPE;
492 p->key_frame= 1;
493 p->interlaced_frame = !!s->interlace_type;
495 /* compute the compressed row size */
496 if (!s->interlace_type) {
497 s->crow_size = s->row_size + 1;
498 } else {
499 s->pass = 0;
500 s->pass_row_size = ff_png_pass_row_size(s->pass,
501 s->bits_per_pixel,
502 s->width);
503 s->crow_size = s->pass_row_size + 1;
505 #ifdef DEBUG
506 av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
507 s->row_size, s->crow_size);
508 #endif
509 s->image_buf = p->data[0];
510 s->image_linesize = p->linesize[0];
511 /* copy the palette if needed */
512 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
513 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
514 /* empty row is used if differencing to the first row */
515 s->last_row = av_mallocz(s->row_size);
516 if (!s->last_row)
517 goto fail;
518 if (s->interlace_type ||
519 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
520 s->tmp_row = av_malloc(s->row_size);
521 if (!s->tmp_row)
522 goto fail;
524 /* compressed row */
525 s->crow_buf = av_malloc(s->row_size + 1);
526 if (!s->crow_buf)
527 goto fail;
528 s->zstream.avail_out = s->crow_size;
529 s->zstream.next_out = s->crow_buf;
531 s->state |= PNG_IDAT;
532 if (png_decode_idat(s, length) < 0)
533 goto fail;
534 /* skip crc */
535 crc = bytestream_get_be32(&s->bytestream);
536 break;
537 case MKTAG('P', 'L', 'T', 'E'):
539 int n, i, r, g, b;
541 if ((length % 3) != 0 || length > 256 * 3)
542 goto skip_tag;
543 /* read the palette */
544 n = length / 3;
545 for(i=0;i<n;i++) {
546 r = *s->bytestream++;
547 g = *s->bytestream++;
548 b = *s->bytestream++;
549 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
551 for(;i<256;i++) {
552 s->palette[i] = (0xff << 24);
554 s->state |= PNG_PLTE;
555 crc = bytestream_get_be32(&s->bytestream);
557 break;
558 case MKTAG('t', 'R', 'N', 'S'):
560 int v, i;
562 /* read the transparency. XXX: Only palette mode supported */
563 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
564 length > 256 ||
565 !(s->state & PNG_PLTE))
566 goto skip_tag;
567 for(i=0;i<length;i++) {
568 v = *s->bytestream++;
569 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
571 crc = bytestream_get_be32(&s->bytestream);
573 break;
574 case MKTAG('I', 'E', 'N', 'D'):
575 if (!(s->state & PNG_ALLIMAGE))
576 goto fail;
577 crc = bytestream_get_be32(&s->bytestream);
578 goto exit_loop;
579 default:
580 /* skip tag */
581 skip_tag:
582 s->bytestream += length + 4;
583 break;
586 exit_loop:
587 *picture= s->picture;
588 *data_size = sizeof(AVFrame);
590 ret = s->bytestream - s->bytestream_start;
591 the_end:
592 inflateEnd(&s->zstream);
593 av_freep(&s->crow_buf);
594 av_freep(&s->last_row);
595 av_freep(&s->tmp_row);
596 return ret;
597 fail:
598 ret = -1;
599 goto the_end;
602 static av_cold int png_dec_init(AVCodecContext *avctx){
603 PNGDecContext *s = avctx->priv_data;
605 avcodec_get_frame_defaults(&s->picture);
606 avctx->coded_frame= &s->picture;
607 dsputil_init(&s->dsp, avctx);
609 return 0;
612 AVCodec png_decoder = {
613 "png",
614 CODEC_TYPE_VIDEO,
615 CODEC_ID_PNG,
616 sizeof(PNGDecContext),
617 png_dec_init,
618 NULL,
619 NULL, //decode_end,
620 decode_frame,
621 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
622 NULL,
623 .long_name = NULL_IF_CONFIG_SMALL("PNG image"),