• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/pngdec.c

Go to the documentation of this file.
00001 /*
00002  * PNG image format
00003  * Copyright (c) 2003 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 #include "avcodec.h"
00022 #include "bytestream.h"
00023 #include "png.h"
00024 #include "dsputil.h"
00025 
00026 /* TODO:
00027  * - add 2, 4 and 16 bit depth support
00028  */
00029 
00030 #include <zlib.h>
00031 
00032 //#define DEBUG
00033 
00034 typedef struct PNGDecContext {
00035     DSPContext dsp;
00036 
00037     const uint8_t *bytestream;
00038     const uint8_t *bytestream_start;
00039     const uint8_t *bytestream_end;
00040     AVFrame picture1, picture2;
00041     AVFrame *current_picture, *last_picture;
00042 
00043     int state;
00044     int width, height;
00045     int bit_depth;
00046     int color_type;
00047     int compression_type;
00048     int interlace_type;
00049     int filter_type;
00050     int channels;
00051     int bits_per_pixel;
00052     int bpp;
00053 
00054     uint8_t *image_buf;
00055     int image_linesize;
00056     uint32_t palette[256];
00057     uint8_t *crow_buf;
00058     uint8_t *last_row;
00059     uint8_t *tmp_row;
00060     int pass;
00061     int crow_size; /* compressed row size (include filter type) */
00062     int row_size; /* decompressed row size */
00063     int pass_row_size; /* decompress row size of the current pass */
00064     int y;
00065     z_stream zstream;
00066 } PNGDecContext;
00067 
00068 /* Mask to determine which y pixels can be written in a pass */
00069 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
00070     0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
00071 };
00072 
00073 /* Mask to determine which pixels to overwrite while displaying */
00074 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
00075     0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
00076 };
00077 
00078 /* NOTE: we try to construct a good looking image at each pass. width
00079    is the original image width. We also do pixel format conversion at
00080    this stage */
00081 static void png_put_interlaced_row(uint8_t *dst, int width,
00082                                    int bits_per_pixel, int pass,
00083                                    int color_type, const uint8_t *src)
00084 {
00085     int x, mask, dsp_mask, j, src_x, b, bpp;
00086     uint8_t *d;
00087     const uint8_t *s;
00088 
00089     mask = ff_png_pass_mask[pass];
00090     dsp_mask = png_pass_dsp_mask[pass];
00091     switch(bits_per_pixel) {
00092     case 1:
00093         /* we must initialize the line to zero before writing to it */
00094         if (pass == 0)
00095             memset(dst, 0, (width + 7) >> 3);
00096         src_x = 0;
00097         for(x = 0; x < width; x++) {
00098             j = (x & 7);
00099             if ((dsp_mask << j) & 0x80) {
00100                 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
00101                 dst[x >> 3] |= b << (7 - j);
00102             }
00103             if ((mask << j) & 0x80)
00104                 src_x++;
00105         }
00106         break;
00107     default:
00108         bpp = bits_per_pixel >> 3;
00109         d = dst;
00110         s = src;
00111         if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00112             for(x = 0; x < width; x++) {
00113                 j = x & 7;
00114                 if ((dsp_mask << j) & 0x80) {
00115                     *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
00116                 }
00117                 d += bpp;
00118                 if ((mask << j) & 0x80)
00119                     s += bpp;
00120             }
00121         } else {
00122             for(x = 0; x < width; x++) {
00123                 j = x & 7;
00124                 if ((dsp_mask << j) & 0x80) {
00125                     memcpy(d, s, bpp);
00126                 }
00127                 d += bpp;
00128                 if ((mask << j) & 0x80)
00129                     s += bpp;
00130             }
00131         }
00132         break;
00133     }
00134 }
00135 
00136 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
00137 {
00138     int i;
00139     for(i = 0; i < w; i++) {
00140         int a, b, c, p, pa, pb, pc;
00141 
00142         a = dst[i - bpp];
00143         b = top[i];
00144         c = top[i - bpp];
00145 
00146         p = b - c;
00147         pc = a - c;
00148 
00149         pa = abs(p);
00150         pb = abs(pc);
00151         pc = abs(p + pc);
00152 
00153         if (pa <= pb && pa <= pc)
00154             p = a;
00155         else if (pb <= pc)
00156             p = b;
00157         else
00158             p = c;
00159         dst[i] = p + src[i];
00160     }
00161 }
00162 
00163 #define UNROLL1(bpp, op) {\
00164                  r = dst[0];\
00165     if(bpp >= 2) g = dst[1];\
00166     if(bpp >= 3) b = dst[2];\
00167     if(bpp >= 4) a = dst[3];\
00168     for(; i < size; i+=bpp) {\
00169         dst[i+0] = r = op(r, src[i+0], last[i+0]);\
00170         if(bpp == 1) continue;\
00171         dst[i+1] = g = op(g, src[i+1], last[i+1]);\
00172         if(bpp == 2) continue;\
00173         dst[i+2] = b = op(b, src[i+2], last[i+2]);\
00174         if(bpp == 3) continue;\
00175         dst[i+3] = a = op(a, src[i+3], last[i+3]);\
00176     }\
00177 }
00178 
00179 #define UNROLL_FILTER(op)\
00180          if(bpp == 1) UNROLL1(1, op)\
00181     else if(bpp == 2) UNROLL1(2, op)\
00182     else if(bpp == 3) UNROLL1(3, op)\
00183     else if(bpp == 4) UNROLL1(4, op)\
00184     else {\
00185         for (; i < size; i += bpp) {\
00186             int j;\
00187             for (j = 0; j < bpp; j++)\
00188                 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
00189         }\
00190     }
00191 
00192 /* NOTE: 'dst' can be equal to 'last' */
00193 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
00194                            uint8_t *src, uint8_t *last, int size, int bpp)
00195 {
00196     int i, p, r, g, b, a;
00197 
00198     switch(filter_type) {
00199     case PNG_FILTER_VALUE_NONE:
00200         memcpy(dst, src, size);
00201         break;
00202     case PNG_FILTER_VALUE_SUB:
00203         for(i = 0; i < bpp; i++) {
00204             dst[i] = src[i];
00205         }
00206         if(bpp == 4) {
00207             p = *(int*)dst;
00208             for(; i < size; i+=bpp) {
00209                 int s = *(int*)(src+i);
00210                 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
00211                 *(int*)(dst+i) = p;
00212             }
00213         } else {
00214 #define OP_SUB(x,s,l) x+s
00215             UNROLL_FILTER(OP_SUB);
00216         }
00217         break;
00218     case PNG_FILTER_VALUE_UP:
00219         dsp->add_bytes_l2(dst, src, last, size);
00220         break;
00221     case PNG_FILTER_VALUE_AVG:
00222         for(i = 0; i < bpp; i++) {
00223             p = (last[i] >> 1);
00224             dst[i] = p + src[i];
00225         }
00226 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
00227         UNROLL_FILTER(OP_AVG);
00228         break;
00229     case PNG_FILTER_VALUE_PAETH:
00230         for(i = 0; i < bpp; i++) {
00231             p = last[i];
00232             dst[i] = p + src[i];
00233         }
00234         if(bpp > 1 && size > 4) {
00235             // would write off the end of the array if we let it process the last pixel with bpp=3
00236             int w = bpp==4 ? size : size-3;
00237             dsp->add_png_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
00238             i = w;
00239         }
00240         ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
00241         break;
00242     }
00243 }
00244 
00245 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
00246 {
00247     int j;
00248     unsigned int r, g, b, a;
00249 
00250     for(j = 0;j < width; j++) {
00251         r = src[0];
00252         g = src[1];
00253         b = src[2];
00254         a = src[3];
00255         if(loco) {
00256             r = (r+g)&0xff;
00257             b = (b+g)&0xff;
00258         }
00259         *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
00260         dst += 4;
00261         src += 4;
00262     }
00263 }
00264 
00265 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
00266 {
00267     if(loco)
00268         convert_to_rgb32_loco(dst, src, width, 1);
00269     else
00270         convert_to_rgb32_loco(dst, src, width, 0);
00271 }
00272 
00273 static void deloco_rgb24(uint8_t *dst, int size)
00274 {
00275     int i;
00276     for(i=0; i<size; i+=3) {
00277         int g = dst[i+1];
00278         dst[i+0] += g;
00279         dst[i+2] += g;
00280     }
00281 }
00282 
00283 /* process exactly one decompressed row */
00284 static void png_handle_row(PNGDecContext *s)
00285 {
00286     uint8_t *ptr, *last_row;
00287     int got_line;
00288 
00289     if (!s->interlace_type) {
00290         ptr = s->image_buf + s->image_linesize * s->y;
00291         /* need to swap bytes correctly for RGB_ALPHA */
00292         if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00293             png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
00294                            s->last_row, s->row_size, s->bpp);
00295             convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
00296             FFSWAP(uint8_t*, s->last_row, s->tmp_row);
00297         } else {
00298             /* in normal case, we avoid one copy */
00299             if (s->y == 0)
00300                 last_row = s->last_row;
00301             else
00302                 last_row = ptr - s->image_linesize;
00303 
00304             png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
00305                            last_row, s->row_size, s->bpp);
00306         }
00307         /* loco lags by 1 row so that it doesn't interfere with top prediction */
00308         if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
00309             s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
00310             deloco_rgb24(ptr - s->image_linesize, s->row_size);
00311         s->y++;
00312         if (s->y == s->height) {
00313             s->state |= PNG_ALLIMAGE;
00314             if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
00315                 s->color_type == PNG_COLOR_TYPE_RGB)
00316                 deloco_rgb24(ptr, s->row_size);
00317         }
00318     } else {
00319         got_line = 0;
00320         for(;;) {
00321             ptr = s->image_buf + s->image_linesize * s->y;
00322             if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
00323                 /* if we already read one row, it is time to stop to
00324                    wait for the next one */
00325                 if (got_line)
00326                     break;
00327                 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
00328                                s->last_row, s->pass_row_size, s->bpp);
00329                 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
00330                 got_line = 1;
00331             }
00332             if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
00333                 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
00334                 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
00335                                        s->color_type, s->last_row);
00336             }
00337             s->y++;
00338             if (s->y == s->height) {
00339                 for(;;) {
00340                     if (s->pass == NB_PASSES - 1) {
00341                         s->state |= PNG_ALLIMAGE;
00342                         goto the_end;
00343                     } else {
00344                         s->pass++;
00345                         s->y = 0;
00346                         s->pass_row_size = ff_png_pass_row_size(s->pass,
00347                                                              s->bits_per_pixel,
00348                                                              s->width);
00349                         s->crow_size = s->pass_row_size + 1;
00350                         if (s->pass_row_size != 0)
00351                             break;
00352                         /* skip pass if empty row */
00353                     }
00354                 }
00355             }
00356         }
00357     the_end: ;
00358     }
00359 }
00360 
00361 static int png_decode_idat(PNGDecContext *s, int length)
00362 {
00363     int ret;
00364     s->zstream.avail_in = length;
00365     s->zstream.next_in = s->bytestream;
00366     s->bytestream += length;
00367 
00368     if(s->bytestream > s->bytestream_end)
00369         return -1;
00370 
00371     /* decode one line if possible */
00372     while (s->zstream.avail_in > 0) {
00373         ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
00374         if (ret != Z_OK && ret != Z_STREAM_END) {
00375             return -1;
00376         }
00377         if (s->zstream.avail_out == 0) {
00378             if (!(s->state & PNG_ALLIMAGE)) {
00379                 png_handle_row(s);
00380             }
00381             s->zstream.avail_out = s->crow_size;
00382             s->zstream.next_out = s->crow_buf;
00383         }
00384     }
00385     return 0;
00386 }
00387 
00388 static int decode_frame(AVCodecContext *avctx,
00389                         void *data, int *data_size,
00390                         AVPacket *avpkt)
00391 {
00392     const uint8_t *buf = avpkt->data;
00393     int buf_size = avpkt->size;
00394     PNGDecContext * const s = avctx->priv_data;
00395     AVFrame *picture = data;
00396     AVFrame *p;
00397     uint8_t *crow_buf_base = NULL;
00398     uint32_t tag, length;
00399     int ret, crc;
00400 
00401     FFSWAP(AVFrame *, s->current_picture, s->last_picture);
00402     avctx->coded_frame= s->current_picture;
00403     p = s->current_picture;
00404 
00405     s->bytestream_start=
00406     s->bytestream= buf;
00407     s->bytestream_end= buf + buf_size;
00408 
00409     /* check signature */
00410     if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
00411         memcmp(s->bytestream, ff_mngsig, 8) != 0)
00412         return -1;
00413     s->bytestream+= 8;
00414     s->y=
00415     s->state=0;
00416 //    memset(s, 0, sizeof(PNGDecContext));
00417     /* init the zlib */
00418     s->zstream.zalloc = ff_png_zalloc;
00419     s->zstream.zfree = ff_png_zfree;
00420     s->zstream.opaque = NULL;
00421     ret = inflateInit(&s->zstream);
00422     if (ret != Z_OK)
00423         return -1;
00424     for(;;) {
00425         int tag32;
00426         if (s->bytestream >= s->bytestream_end)
00427             goto fail;
00428         length = bytestream_get_be32(&s->bytestream);
00429         if (length > 0x7fffffff)
00430             goto fail;
00431         tag32 = bytestream_get_be32(&s->bytestream);
00432         tag = bswap_32(tag32);
00433         dprintf(avctx, "png: tag=%c%c%c%c length=%u\n",
00434                 (tag & 0xff),
00435                 ((tag >> 8) & 0xff),
00436                 ((tag >> 16) & 0xff),
00437                 ((tag >> 24) & 0xff), length);
00438         switch(tag) {
00439         case MKTAG('I', 'H', 'D', 'R'):
00440             if (length != 13)
00441                 goto fail;
00442             s->width = bytestream_get_be32(&s->bytestream);
00443             s->height = bytestream_get_be32(&s->bytestream);
00444             if(avcodec_check_dimensions(avctx, s->width, s->height)){
00445                 s->width= s->height= 0;
00446                 goto fail;
00447             }
00448             s->bit_depth = *s->bytestream++;
00449             s->color_type = *s->bytestream++;
00450             s->compression_type = *s->bytestream++;
00451             s->filter_type = *s->bytestream++;
00452             s->interlace_type = *s->bytestream++;
00453             crc = bytestream_get_be32(&s->bytestream);
00454             s->state |= PNG_IHDR;
00455             dprintf(avctx, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
00456                     s->width, s->height, s->bit_depth, s->color_type,
00457                     s->compression_type, s->filter_type, s->interlace_type);
00458             break;
00459         case MKTAG('I', 'D', 'A', 'T'):
00460             if (!(s->state & PNG_IHDR))
00461                 goto fail;
00462             if (!(s->state & PNG_IDAT)) {
00463                 /* init image info */
00464                 avctx->width = s->width;
00465                 avctx->height = s->height;
00466 
00467                 s->channels = ff_png_get_nb_channels(s->color_type);
00468                 s->bits_per_pixel = s->bit_depth * s->channels;
00469                 s->bpp = (s->bits_per_pixel + 7) >> 3;
00470                 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
00471 
00472                 if (s->bit_depth == 8 &&
00473                     s->color_type == PNG_COLOR_TYPE_RGB) {
00474                     avctx->pix_fmt = PIX_FMT_RGB24;
00475                 } else if (s->bit_depth == 8 &&
00476                            s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00477                     avctx->pix_fmt = PIX_FMT_RGB32;
00478                 } else if (s->bit_depth == 8 &&
00479                            s->color_type == PNG_COLOR_TYPE_GRAY) {
00480                     avctx->pix_fmt = PIX_FMT_GRAY8;
00481                 } else if (s->bit_depth == 16 &&
00482                            s->color_type == PNG_COLOR_TYPE_GRAY) {
00483                     avctx->pix_fmt = PIX_FMT_GRAY16BE;
00484                 } else if (s->bit_depth == 16 &&
00485                            s->color_type == PNG_COLOR_TYPE_RGB) {
00486                     avctx->pix_fmt = PIX_FMT_RGB48BE;
00487                 } else if (s->bit_depth == 1 &&
00488                            s->color_type == PNG_COLOR_TYPE_GRAY) {
00489                     avctx->pix_fmt = PIX_FMT_MONOBLACK;
00490                 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
00491                     avctx->pix_fmt = PIX_FMT_PAL8;
00492                 } else if (s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
00493                     avctx->pix_fmt = PIX_FMT_Y400A;
00494                 } else {
00495                     goto fail;
00496                 }
00497                 if(p->data[0])
00498                     avctx->release_buffer(avctx, p);
00499 
00500                 p->reference= 0;
00501                 if(avctx->get_buffer(avctx, p) < 0){
00502                     av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00503                     goto fail;
00504                 }
00505                 p->pict_type= FF_I_TYPE;
00506                 p->key_frame= 1;
00507                 p->interlaced_frame = !!s->interlace_type;
00508 
00509                 /* compute the compressed row size */
00510                 if (!s->interlace_type) {
00511                     s->crow_size = s->row_size + 1;
00512                 } else {
00513                     s->pass = 0;
00514                     s->pass_row_size = ff_png_pass_row_size(s->pass,
00515                                                          s->bits_per_pixel,
00516                                                          s->width);
00517                     s->crow_size = s->pass_row_size + 1;
00518                 }
00519                 dprintf(avctx, "row_size=%d crow_size =%d\n",
00520                         s->row_size, s->crow_size);
00521                 s->image_buf = p->data[0];
00522                 s->image_linesize = p->linesize[0];
00523                 /* copy the palette if needed */
00524                 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
00525                     memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
00526                 /* empty row is used if differencing to the first row */
00527                 s->last_row = av_mallocz(s->row_size);
00528                 if (!s->last_row)
00529                     goto fail;
00530                 if (s->interlace_type ||
00531                     s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00532                     s->tmp_row = av_malloc(s->row_size);
00533                     if (!s->tmp_row)
00534                         goto fail;
00535                 }
00536                 /* compressed row */
00537                 crow_buf_base = av_malloc(s->row_size + 16);
00538                 if (!crow_buf_base)
00539                     goto fail;
00540 
00541                 /* we want crow_buf+1 to be 16-byte aligned */
00542                 s->crow_buf = crow_buf_base + 15;
00543                 s->zstream.avail_out = s->crow_size;
00544                 s->zstream.next_out = s->crow_buf;
00545             }
00546             s->state |= PNG_IDAT;
00547             if (png_decode_idat(s, length) < 0)
00548                 goto fail;
00549             /* skip crc */
00550             crc = bytestream_get_be32(&s->bytestream);
00551             break;
00552         case MKTAG('P', 'L', 'T', 'E'):
00553             {
00554                 int n, i, r, g, b;
00555 
00556                 if ((length % 3) != 0 || length > 256 * 3)
00557                     goto skip_tag;
00558                 /* read the palette */
00559                 n = length / 3;
00560                 for(i=0;i<n;i++) {
00561                     r = *s->bytestream++;
00562                     g = *s->bytestream++;
00563                     b = *s->bytestream++;
00564                     s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
00565                 }
00566                 for(;i<256;i++) {
00567                     s->palette[i] = (0xff << 24);
00568                 }
00569                 s->state |= PNG_PLTE;
00570                 crc = bytestream_get_be32(&s->bytestream);
00571             }
00572             break;
00573         case MKTAG('t', 'R', 'N', 'S'):
00574             {
00575                 int v, i;
00576 
00577                 /* read the transparency. XXX: Only palette mode supported */
00578                 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
00579                     length > 256 ||
00580                     !(s->state & PNG_PLTE))
00581                     goto skip_tag;
00582                 for(i=0;i<length;i++) {
00583                     v = *s->bytestream++;
00584                     s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
00585                 }
00586                 crc = bytestream_get_be32(&s->bytestream);
00587             }
00588             break;
00589         case MKTAG('I', 'E', 'N', 'D'):
00590             if (!(s->state & PNG_ALLIMAGE))
00591                 goto fail;
00592             crc = bytestream_get_be32(&s->bytestream);
00593             goto exit_loop;
00594         default:
00595             /* skip tag */
00596         skip_tag:
00597             s->bytestream += length + 4;
00598             break;
00599         }
00600     }
00601  exit_loop:
00602      /* handle p-frames only if a predecessor frame is available */
00603      if(s->last_picture->data[0] != NULL) {
00604          if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
00605             int i, j;
00606             uint8_t *pd = s->current_picture->data[0];
00607             uint8_t *pd_last = s->last_picture->data[0];
00608 
00609             for(j=0; j < s->height; j++) {
00610                 for(i=0; i < s->width * s->bpp; i++) {
00611                     pd[i] += pd_last[i];
00612                 }
00613                 pd += s->image_linesize;
00614                 pd_last += s->image_linesize;
00615             }
00616         }
00617     }
00618 
00619     *picture= *s->current_picture;
00620     *data_size = sizeof(AVFrame);
00621 
00622     ret = s->bytestream - s->bytestream_start;
00623  the_end:
00624     inflateEnd(&s->zstream);
00625     av_free(crow_buf_base);
00626     s->crow_buf = NULL;
00627     av_freep(&s->last_row);
00628     av_freep(&s->tmp_row);
00629     return ret;
00630  fail:
00631     ret = -1;
00632     goto the_end;
00633 }
00634 
00635 static av_cold int png_dec_init(AVCodecContext *avctx){
00636     PNGDecContext *s = avctx->priv_data;
00637 
00638     s->current_picture = &s->picture1;
00639     s->last_picture = &s->picture2;
00640     avcodec_get_frame_defaults(&s->picture1);
00641     avcodec_get_frame_defaults(&s->picture2);
00642     dsputil_init(&s->dsp, avctx);
00643 
00644     return 0;
00645 }
00646 
00647 static av_cold int png_dec_end(AVCodecContext *avctx)
00648 {
00649     PNGDecContext *s = avctx->priv_data;
00650 
00651     if (s->picture1.data[0])
00652         avctx->release_buffer(avctx, &s->picture1);
00653     if (s->picture2.data[0])
00654         avctx->release_buffer(avctx, &s->picture2);
00655 
00656     return 0;
00657 }
00658 
00659 AVCodec png_decoder = {
00660     "png",
00661     AVMEDIA_TYPE_VIDEO,
00662     CODEC_ID_PNG,
00663     sizeof(PNGDecContext),
00664     png_dec_init,
00665     NULL,
00666     png_dec_end,
00667     decode_frame,
00668     CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
00669     NULL,
00670     .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
00671 };

Generated on Fri Sep 16 2011 17:17:41 for FFmpeg by  doxygen 1.7.1