~ubuntu-branches/ubuntu/oneiric/libav/oneiric-security

« back to all changes in this revision

Viewing changes to .pc/post-0.7.1/0022-Fix-incorrect-max_lowres-values.patch/libavcodec/pngdec.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2011-09-28 09:18:34 UTC
  • mfrom: (1.3.7 sid)
  • Revision ID: package-import@ubuntu.com-20110928091834-w415mnuh06h4zpvc
Tags: 4:0.7.1-7ubuntu2
Revert "Convert package to include multiarch support."

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * PNG image format
 
3
 * Copyright (c) 2003 Fabrice Bellard
 
4
 *
 
5
 * This file is part of Libav.
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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
 
20
 */
 
21
#include "libavutil/imgutils.h"
 
22
#include "avcodec.h"
 
23
#include "bytestream.h"
 
24
#include "png.h"
 
25
#include "dsputil.h"
 
26
 
 
27
/* TODO:
 
28
 * - add 2, 4 and 16 bit depth support
 
29
 */
 
30
 
 
31
#include <zlib.h>
 
32
 
 
33
//#define DEBUG
 
34
 
 
35
typedef struct PNGDecContext {
 
36
    DSPContext dsp;
 
37
 
 
38
    const uint8_t *bytestream;
 
39
    const uint8_t *bytestream_start;
 
40
    const uint8_t *bytestream_end;
 
41
    AVFrame picture1, picture2;
 
42
    AVFrame *current_picture, *last_picture;
 
43
 
 
44
    int state;
 
45
    int width, height;
 
46
    int bit_depth;
 
47
    int color_type;
 
48
    int compression_type;
 
49
    int interlace_type;
 
50
    int filter_type;
 
51
    int channels;
 
52
    int bits_per_pixel;
 
53
    int bpp;
 
54
 
 
55
    uint8_t *image_buf;
 
56
    int image_linesize;
 
57
    uint32_t palette[256];
 
58
    uint8_t *crow_buf;
 
59
    uint8_t *last_row;
 
60
    uint8_t *tmp_row;
 
61
    int pass;
 
62
    int crow_size; /* compressed row size (include filter type) */
 
63
    int row_size; /* decompressed row size */
 
64
    int pass_row_size; /* decompress row size of the current pass */
 
65
    int y;
 
66
    z_stream zstream;
 
67
} PNGDecContext;
 
68
 
 
69
/* Mask to determine which y pixels can be written in a pass */
 
70
static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
 
71
    0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
 
72
};
 
73
 
 
74
/* Mask to determine which pixels to overwrite while displaying */
 
75
static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
 
76
    0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
 
77
};
 
78
 
 
79
/* NOTE: we try to construct a good looking image at each pass. width
 
80
   is the original image width. We also do pixel format conversion at
 
81
   this stage */
 
82
static void png_put_interlaced_row(uint8_t *dst, int width,
 
83
                                   int bits_per_pixel, int pass,
 
84
                                   int color_type, const uint8_t *src)
 
85
{
 
86
    int x, mask, dsp_mask, j, src_x, b, bpp;
 
87
    uint8_t *d;
 
88
    const uint8_t *s;
 
89
 
 
90
    mask = ff_png_pass_mask[pass];
 
91
    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);
 
103
            }
 
104
            if ((mask << j) & 0x80)
 
105
                src_x++;
 
106
        }
 
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];
 
117
                }
 
118
                d += bpp;
 
119
                if ((mask << j) & 0x80)
 
120
                    s += bpp;
 
121
            }
 
122
        } else {
 
123
            for(x = 0; x < width; x++) {
 
124
                j = x & 7;
 
125
                if ((dsp_mask << j) & 0x80) {
 
126
                    memcpy(d, s, bpp);
 
127
                }
 
128
                d += bpp;
 
129
                if ((mask << j) & 0x80)
 
130
                    s += bpp;
 
131
            }
 
132
        }
 
133
        break;
 
134
    }
 
135
}
 
136
 
 
137
void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
 
138
{
 
139
    int i;
 
140
    for(i = 0; i < w; i++) {
 
141
        int a, b, c, p, pa, pb, pc;
 
142
 
 
143
        a = dst[i - bpp];
 
144
        b = top[i];
 
145
        c = top[i - bpp];
 
146
 
 
147
        p = b - c;
 
148
        pc = a - c;
 
149
 
 
150
        pa = abs(p);
 
151
        pb = abs(pc);
 
152
        pc = abs(p + pc);
 
153
 
 
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];
 
161
    }
 
162
}
 
163
 
 
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]);\
 
177
    }\
 
178
}
 
179
 
 
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]);\
 
190
        }\
 
191
    }
 
192
 
 
193
/* NOTE: 'dst' can be equal to 'last' */
 
194
static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
 
195
                           uint8_t *src, uint8_t *last, int size, int bpp)
 
196
{
 
197
    int i, p, r, g, b, a;
 
198
 
 
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];
 
206
        }
 
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;
 
213
            }
 
214
        } else {
 
215
#define OP_SUB(x,s,l) x+s
 
216
            UNROLL_FILTER(OP_SUB);
 
217
        }
 
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];
 
226
        }
 
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];
 
234
        }
 
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_png_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
 
239
            i = w;
 
240
        }
 
241
        ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
 
242
        break;
 
243
    }
 
244
}
 
245
 
 
246
static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
 
247
{
 
248
    int j;
 
249
    unsigned int r, g, b, a;
 
250
 
 
251
    for(j = 0;j < width; j++) {
 
252
        r = src[0];
 
253
        g = src[1];
 
254
        b = src[2];
 
255
        a = src[3];
 
256
        if(loco) {
 
257
            r = (r+g)&0xff;
 
258
            b = (b+g)&0xff;
 
259
        }
 
260
        *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
 
261
        dst += 4;
 
262
        src += 4;
 
263
    }
 
264
}
 
265
 
 
266
static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
 
267
{
 
268
    if(loco)
 
269
        convert_to_rgb32_loco(dst, src, width, 1);
 
270
    else
 
271
        convert_to_rgb32_loco(dst, src, width, 0);
 
272
}
 
273
 
 
274
static void deloco_rgb24(uint8_t *dst, int size)
 
275
{
 
276
    int i;
 
277
    for(i=0; i<size; i+=3) {
 
278
        int g = dst[i+1];
 
279
        dst[i+0] += g;
 
280
        dst[i+2] += g;
 
281
    }
 
282
}
 
283
 
 
284
/* process exactly one decompressed row */
 
285
static void png_handle_row(PNGDecContext *s)
 
286
{
 
287
    uint8_t *ptr, *last_row;
 
288
    int got_line;
 
289
 
 
290
    if (!s->interlace_type) {
 
291
        ptr = s->image_buf + s->image_linesize * s->y;
 
292
        /* need to swap bytes correctly for RGB_ALPHA */
 
293
        if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
 
294
            png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
 
295
                           s->last_row, s->row_size, s->bpp);
 
296
            convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
 
297
            FFSWAP(uint8_t*, s->last_row, s->tmp_row);
 
298
        } else {
 
299
            /* in normal case, we avoid one copy */
 
300
            if (s->y == 0)
 
301
                last_row = s->last_row;
 
302
            else
 
303
                last_row = ptr - s->image_linesize;
 
304
 
 
305
            png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
 
306
                           last_row, s->row_size, s->bpp);
 
307
        }
 
308
        /* loco lags by 1 row so that it doesn't interfere with top prediction */
 
309
        if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
 
310
            s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
 
311
            deloco_rgb24(ptr - s->image_linesize, s->row_size);
 
312
        s->y++;
 
313
        if (s->y == s->height) {
 
314
            s->state |= PNG_ALLIMAGE;
 
315
            if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
 
316
                s->color_type == PNG_COLOR_TYPE_RGB)
 
317
                deloco_rgb24(ptr, s->row_size);
 
318
        }
 
319
    } else {
 
320
        got_line = 0;
 
321
        for(;;) {
 
322
            ptr = s->image_buf + s->image_linesize * s->y;
 
323
            if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
 
324
                /* if we already read one row, it is time to stop to
 
325
                   wait for the next one */
 
326
                if (got_line)
 
327
                    break;
 
328
                png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
 
329
                               s->last_row, s->pass_row_size, s->bpp);
 
330
                FFSWAP(uint8_t*, s->last_row, s->tmp_row);
 
331
                got_line = 1;
 
332
            }
 
333
            if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
 
334
                /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
 
335
                png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
 
336
                                       s->color_type, s->last_row);
 
337
            }
 
338
            s->y++;
 
339
            if (s->y == s->height) {
 
340
                for(;;) {
 
341
                    if (s->pass == NB_PASSES - 1) {
 
342
                        s->state |= PNG_ALLIMAGE;
 
343
                        goto the_end;
 
344
                    } else {
 
345
                        s->pass++;
 
346
                        s->y = 0;
 
347
                        s->pass_row_size = ff_png_pass_row_size(s->pass,
 
348
                                                             s->bits_per_pixel,
 
349
                                                             s->width);
 
350
                        s->crow_size = s->pass_row_size + 1;
 
351
                        if (s->pass_row_size != 0)
 
352
                            break;
 
353
                        /* skip pass if empty row */
 
354
                    }
 
355
                }
 
356
            }
 
357
        }
 
358
    the_end: ;
 
359
    }
 
360
}
 
361
 
 
362
static int png_decode_idat(PNGDecContext *s, int length)
 
363
{
 
364
    int ret;
 
365
    s->zstream.avail_in = length;
 
366
    s->zstream.next_in = s->bytestream;
 
367
    s->bytestream += length;
 
368
 
 
369
    if(s->bytestream > s->bytestream_end)
 
370
        return -1;
 
371
 
 
372
    /* decode one line if possible */
 
373
    while (s->zstream.avail_in > 0) {
 
374
        ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
 
375
        if (ret != Z_OK && ret != Z_STREAM_END) {
 
376
            return -1;
 
377
        }
 
378
        if (s->zstream.avail_out == 0) {
 
379
            if (!(s->state & PNG_ALLIMAGE)) {
 
380
                png_handle_row(s);
 
381
            }
 
382
            s->zstream.avail_out = s->crow_size;
 
383
            s->zstream.next_out = s->crow_buf;
 
384
        }
 
385
    }
 
386
    return 0;
 
387
}
 
388
 
 
389
static int decode_frame(AVCodecContext *avctx,
 
390
                        void *data, int *data_size,
 
391
                        AVPacket *avpkt)
 
392
{
 
393
    const uint8_t *buf = avpkt->data;
 
394
    int buf_size = avpkt->size;
 
395
    PNGDecContext * const s = avctx->priv_data;
 
396
    AVFrame *picture = data;
 
397
    AVFrame *p;
 
398
    uint8_t *crow_buf_base = NULL;
 
399
    uint32_t tag, length;
 
400
    int ret;
 
401
 
 
402
    FFSWAP(AVFrame *, s->current_picture, s->last_picture);
 
403
    avctx->coded_frame= s->current_picture;
 
404
    p = s->current_picture;
 
405
 
 
406
    s->bytestream_start=
 
407
    s->bytestream= buf;
 
408
    s->bytestream_end= buf + buf_size;
 
409
 
 
410
    /* check signature */
 
411
    if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
 
412
        memcmp(s->bytestream, ff_mngsig, 8) != 0)
 
413
        return -1;
 
414
    s->bytestream+= 8;
 
415
    s->y=
 
416
    s->state=0;
 
417
//    memset(s, 0, sizeof(PNGDecContext));
 
418
    /* init the zlib */
 
419
    s->zstream.zalloc = ff_png_zalloc;
 
420
    s->zstream.zfree = ff_png_zfree;
 
421
    s->zstream.opaque = NULL;
 
422
    ret = inflateInit(&s->zstream);
 
423
    if (ret != Z_OK)
 
424
        return -1;
 
425
    for(;;) {
 
426
        int tag32;
 
427
        if (s->bytestream >= s->bytestream_end)
 
428
            goto fail;
 
429
        length = bytestream_get_be32(&s->bytestream);
 
430
        if (length > 0x7fffffff)
 
431
            goto fail;
 
432
        tag32 = bytestream_get_be32(&s->bytestream);
 
433
        tag = av_bswap32(tag32);
 
434
        av_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
 
435
                (tag & 0xff),
 
436
                ((tag >> 8) & 0xff),
 
437
                ((tag >> 16) & 0xff),
 
438
                ((tag >> 24) & 0xff), length);
 
439
        switch(tag) {
 
440
        case MKTAG('I', 'H', 'D', 'R'):
 
441
            if (length != 13)
 
442
                goto fail;
 
443
            s->width = bytestream_get_be32(&s->bytestream);
 
444
            s->height = bytestream_get_be32(&s->bytestream);
 
445
            if(av_image_check_size(s->width, s->height, 0, avctx)){
 
446
                s->width= s->height= 0;
 
447
                goto fail;
 
448
            }
 
449
            s->bit_depth = *s->bytestream++;
 
450
            s->color_type = *s->bytestream++;
 
451
            s->compression_type = *s->bytestream++;
 
452
            s->filter_type = *s->bytestream++;
 
453
            s->interlace_type = *s->bytestream++;
 
454
            s->bytestream += 4; /* crc */
 
455
            s->state |= PNG_IHDR;
 
456
            av_dlog(avctx, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
 
457
                    s->width, s->height, s->bit_depth, s->color_type,
 
458
                    s->compression_type, s->filter_type, s->interlace_type);
 
459
            break;
 
460
        case MKTAG('I', 'D', 'A', 'T'):
 
461
            if (!(s->state & PNG_IHDR))
 
462
                goto fail;
 
463
            if (!(s->state & PNG_IDAT)) {
 
464
                /* init image info */
 
465
                avctx->width = s->width;
 
466
                avctx->height = s->height;
 
467
 
 
468
                s->channels = ff_png_get_nb_channels(s->color_type);
 
469
                s->bits_per_pixel = s->bit_depth * s->channels;
 
470
                s->bpp = (s->bits_per_pixel + 7) >> 3;
 
471
                s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
 
472
 
 
473
                if (s->bit_depth == 8 &&
 
474
                    s->color_type == PNG_COLOR_TYPE_RGB) {
 
475
                    avctx->pix_fmt = PIX_FMT_RGB24;
 
476
                } else if (s->bit_depth == 8 &&
 
477
                           s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
 
478
                    avctx->pix_fmt = PIX_FMT_RGB32;
 
479
                } else if (s->bit_depth == 8 &&
 
480
                           s->color_type == PNG_COLOR_TYPE_GRAY) {
 
481
                    avctx->pix_fmt = PIX_FMT_GRAY8;
 
482
                } else if (s->bit_depth == 16 &&
 
483
                           s->color_type == PNG_COLOR_TYPE_GRAY) {
 
484
                    avctx->pix_fmt = PIX_FMT_GRAY16BE;
 
485
                } else if (s->bit_depth == 16 &&
 
486
                           s->color_type == PNG_COLOR_TYPE_RGB) {
 
487
                    avctx->pix_fmt = PIX_FMT_RGB48BE;
 
488
                } else if (s->bit_depth == 1 &&
 
489
                           s->color_type == PNG_COLOR_TYPE_GRAY) {
 
490
                    avctx->pix_fmt = PIX_FMT_MONOBLACK;
 
491
                } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
 
492
                    avctx->pix_fmt = PIX_FMT_PAL8;
 
493
                } else if (s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
 
494
                    avctx->pix_fmt = PIX_FMT_Y400A;
 
495
                } else {
 
496
                    goto fail;
 
497
                }
 
498
                if(p->data[0])
 
499
                    avctx->release_buffer(avctx, p);
 
500
 
 
501
                p->reference= 0;
 
502
                if(avctx->get_buffer(avctx, p) < 0){
 
503
                    av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
 
504
                    goto fail;
 
505
                }
 
506
                p->pict_type= AV_PICTURE_TYPE_I;
 
507
                p->key_frame= 1;
 
508
                p->interlaced_frame = !!s->interlace_type;
 
509
 
 
510
                /* compute the compressed row size */
 
511
                if (!s->interlace_type) {
 
512
                    s->crow_size = s->row_size + 1;
 
513
                } else {
 
514
                    s->pass = 0;
 
515
                    s->pass_row_size = ff_png_pass_row_size(s->pass,
 
516
                                                         s->bits_per_pixel,
 
517
                                                         s->width);
 
518
                    s->crow_size = s->pass_row_size + 1;
 
519
                }
 
520
                av_dlog(avctx, "row_size=%d crow_size =%d\n",
 
521
                        s->row_size, s->crow_size);
 
522
                s->image_buf = p->data[0];
 
523
                s->image_linesize = p->linesize[0];
 
524
                /* copy the palette if needed */
 
525
                if (s->color_type == PNG_COLOR_TYPE_PALETTE)
 
526
                    memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
 
527
                /* empty row is used if differencing to the first row */
 
528
                s->last_row = av_mallocz(s->row_size);
 
529
                if (!s->last_row)
 
530
                    goto fail;
 
531
                if (s->interlace_type ||
 
532
                    s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
 
533
                    s->tmp_row = av_malloc(s->row_size);
 
534
                    if (!s->tmp_row)
 
535
                        goto fail;
 
536
                }
 
537
                /* compressed row */
 
538
                crow_buf_base = av_malloc(s->row_size + 16);
 
539
                if (!crow_buf_base)
 
540
                    goto fail;
 
541
 
 
542
                /* we want crow_buf+1 to be 16-byte aligned */
 
543
                s->crow_buf = crow_buf_base + 15;
 
544
                s->zstream.avail_out = s->crow_size;
 
545
                s->zstream.next_out = s->crow_buf;
 
546
            }
 
547
            s->state |= PNG_IDAT;
 
548
            if (png_decode_idat(s, length) < 0)
 
549
                goto fail;
 
550
            s->bytestream += 4; /* crc */
 
551
            break;
 
552
        case MKTAG('P', 'L', 'T', 'E'):
 
553
            {
 
554
                int n, i, r, g, b;
 
555
 
 
556
                if ((length % 3) != 0 || length > 256 * 3)
 
557
                    goto skip_tag;
 
558
                /* read the palette */
 
559
                n = length / 3;
 
560
                for(i=0;i<n;i++) {
 
561
                    r = *s->bytestream++;
 
562
                    g = *s->bytestream++;
 
563
                    b = *s->bytestream++;
 
564
                    s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
 
565
                }
 
566
                for(;i<256;i++) {
 
567
                    s->palette[i] = (0xff << 24);
 
568
                }
 
569
                s->state |= PNG_PLTE;
 
570
                s->bytestream += 4; /* crc */
 
571
            }
 
572
            break;
 
573
        case MKTAG('t', 'R', 'N', 'S'):
 
574
            {
 
575
                int v, i;
 
576
 
 
577
                /* read the transparency. XXX: Only palette mode supported */
 
578
                if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
 
579
                    length > 256 ||
 
580
                    !(s->state & PNG_PLTE))
 
581
                    goto skip_tag;
 
582
                for(i=0;i<length;i++) {
 
583
                    v = *s->bytestream++;
 
584
                    s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
 
585
                }
 
586
                s->bytestream += 4; /* crc */
 
587
            }
 
588
            break;
 
589
        case MKTAG('I', 'E', 'N', 'D'):
 
590
            if (!(s->state & PNG_ALLIMAGE))
 
591
                goto fail;
 
592
            s->bytestream += 4; /* crc */
 
593
            goto exit_loop;
 
594
        default:
 
595
            /* skip tag */
 
596
        skip_tag:
 
597
            s->bytestream += length + 4;
 
598
            break;
 
599
        }
 
600
    }
 
601
 exit_loop:
 
602
     /* handle p-frames only if a predecessor frame is available */
 
603
     if(s->last_picture->data[0] != NULL) {
 
604
         if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
 
605
            int i, j;
 
606
            uint8_t *pd = s->current_picture->data[0];
 
607
            uint8_t *pd_last = s->last_picture->data[0];
 
608
 
 
609
            for(j=0; j < s->height; j++) {
 
610
                for(i=0; i < s->width * s->bpp; i++) {
 
611
                    pd[i] += pd_last[i];
 
612
                }
 
613
                pd += s->image_linesize;
 
614
                pd_last += s->image_linesize;
 
615
            }
 
616
        }
 
617
    }
 
618
 
 
619
    *picture= *s->current_picture;
 
620
    *data_size = sizeof(AVFrame);
 
621
 
 
622
    ret = s->bytestream - s->bytestream_start;
 
623
 the_end:
 
624
    inflateEnd(&s->zstream);
 
625
    av_free(crow_buf_base);
 
626
    s->crow_buf = NULL;
 
627
    av_freep(&s->last_row);
 
628
    av_freep(&s->tmp_row);
 
629
    return ret;
 
630
 fail:
 
631
    ret = -1;
 
632
    goto the_end;
 
633
}
 
634
 
 
635
static av_cold int png_dec_init(AVCodecContext *avctx){
 
636
    PNGDecContext *s = avctx->priv_data;
 
637
 
 
638
    s->current_picture = &s->picture1;
 
639
    s->last_picture = &s->picture2;
 
640
    avcodec_get_frame_defaults(&s->picture1);
 
641
    avcodec_get_frame_defaults(&s->picture2);
 
642
    dsputil_init(&s->dsp, avctx);
 
643
 
 
644
    return 0;
 
645
}
 
646
 
 
647
static av_cold int png_dec_end(AVCodecContext *avctx)
 
648
{
 
649
    PNGDecContext *s = avctx->priv_data;
 
650
 
 
651
    if (s->picture1.data[0])
 
652
        avctx->release_buffer(avctx, &s->picture1);
 
653
    if (s->picture2.data[0])
 
654
        avctx->release_buffer(avctx, &s->picture2);
 
655
 
 
656
    return 0;
 
657
}
 
658
 
 
659
AVCodec ff_png_decoder = {
 
660
    "png",
 
661
    AVMEDIA_TYPE_VIDEO,
 
662
    CODEC_ID_PNG,
 
663
    sizeof(PNGDecContext),
 
664
    png_dec_init,
 
665
    NULL,
 
666
    png_dec_end,
 
667
    decode_frame,
 
668
    CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
 
669
    NULL,
 
670
    .max_lowres = 5,
 
671
    .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
 
672
};