• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 #include "libavutil/imgutils.h"
00022 #include "avcodec.h"
00023 #include "bytestream.h"
00024 #include "png.h"
00025 #include "dsputil.h"
00026 
00027 /* TODO:
00028  * - add 2, 4 and 16 bit depth support
00029  */
00030 
00031 #include <zlib.h>
00032 
00033 //#define DEBUG
00034 
00035 typedef struct PNGDecContext {
00036     DSPContext dsp;
00037 
00038     const uint8_t *bytestream;
00039     const uint8_t *bytestream_start;
00040     const uint8_t *bytestream_end;
00041     AVFrame picture1, picture2;
00042     AVFrame *current_picture, *last_picture;
00043 
00044     int state;
00045     int width, height;
00046     int bit_depth;
00047     int color_type;
00048     int compression_type;
00049     int interlace_type;
00050     int filter_type;
00051     int channels;
00052     int bits_per_pixel;
00053     int bpp;
00054 
00055     uint8_t *image_buf;
00056     int image_linesize;
00057     uint32_t palette[256];
00058     uint8_t *crow_buf;
00059     uint8_t *last_row;
00060     uint8_t *tmp_row;
00061     int pass;
00062     int crow_size; /* compressed row size (include filter type) */
00063     int row_size; /* decompressed row size */
00064     int pass_row_size; /* decompress row size of the current pass */
00065     int y;
00066     z_stream zstream;
00067 } PNGDecContext;
00068 
00069 /* Mask to determine which y pixels can be written in a pass */
00070 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
00071     0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
00072 };
00073 
00074 /* Mask to determine which pixels to overwrite while displaying */
00075 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
00076     0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
00077 };
00078 
00079 /* NOTE: we try to construct a good looking image at each pass. width
00080    is the original image width. We also do pixel format conversion at
00081    this stage */
00082 static void png_put_interlaced_row(uint8_t *dst, int width,
00083                                    int bits_per_pixel, int pass,
00084                                    int color_type, const uint8_t *src)
00085 {
00086     int x, mask, dsp_mask, j, src_x, b, bpp;
00087     uint8_t *d;
00088     const uint8_t *s;
00089 
00090     mask = ff_png_pass_mask[pass];
00091     dsp_mask = png_pass_dsp_mask[pass];
00092     switch(bits_per_pixel) {
00093     case 1:
00094         /* we must initialize the line to zero before writing to it */
00095         if (pass == 0)
00096             memset(dst, 0, (width + 7) >> 3);
00097         src_x = 0;
00098         for(x = 0; x < width; x++) {
00099             j = (x & 7);
00100             if ((dsp_mask << j) & 0x80) {
00101                 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
00102                 dst[x >> 3] |= b << (7 - j);
00103             }
00104             if ((mask << j) & 0x80)
00105                 src_x++;
00106         }
00107         break;
00108     default:
00109         bpp = bits_per_pixel >> 3;
00110         d = dst;
00111         s = src;
00112         if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00113             for(x = 0; x < width; x++) {
00114                 j = x & 7;
00115                 if ((dsp_mask << j) & 0x80) {
00116                     *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
00117                 }
00118                 d += bpp;
00119                 if ((mask << j) & 0x80)
00120                     s += bpp;
00121             }
00122         } else {
00123             for(x = 0; x < width; x++) {
00124                 j = x & 7;
00125                 if ((dsp_mask << j) & 0x80) {
00126                     memcpy(d, s, bpp);
00127                 }
00128                 d += bpp;
00129                 if ((mask << j) & 0x80)
00130                     s += bpp;
00131             }
00132         }
00133         break;
00134     }
00135 }
00136 
00137 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
00138 {
00139     int i;
00140     for(i = 0; i < w; i++) {
00141         int a, b, c, p, pa, pb, pc;
00142 
00143         a = dst[i - bpp];
00144         b = top[i];
00145         c = top[i - bpp];
00146 
00147         p = b - c;
00148         pc = a - c;
00149 
00150         pa = abs(p);
00151         pb = abs(pc);
00152         pc = abs(p + pc);
00153 
00154         if (pa <= pb && pa <= pc)
00155             p = a;
00156         else if (pb <= pc)
00157             p = b;
00158         else
00159             p = c;
00160         dst[i] = p + src[i];
00161     }
00162 }
00163 
00164 #define UNROLL1(bpp, op) {\
00165                  r = dst[0];\
00166     if(bpp >= 2) g = dst[1];\
00167     if(bpp >= 3) b = dst[2];\
00168     if(bpp >= 4) a = dst[3];\
00169     for(; i < size; i+=bpp) {\
00170         dst[i+0] = r = op(r, src[i+0], last[i+0]);\
00171         if(bpp == 1) continue;\
00172         dst[i+1] = g = op(g, src[i+1], last[i+1]);\
00173         if(bpp == 2) continue;\
00174         dst[i+2] = b = op(b, src[i+2], last[i+2]);\
00175         if(bpp == 3) continue;\
00176         dst[i+3] = a = op(a, src[i+3], last[i+3]);\
00177     }\
00178 }
00179 
00180 #define UNROLL_FILTER(op)\
00181          if(bpp == 1) UNROLL1(1, op)\
00182     else if(bpp == 2) UNROLL1(2, op)\
00183     else if(bpp == 3) UNROLL1(3, op)\
00184     else if(bpp == 4) UNROLL1(4, op)\
00185     else {\
00186         for (; i < size; i += bpp) {\
00187             int j;\
00188             for (j = 0; j < bpp; j++)\
00189                 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
00190         }\
00191     }
00192 
00193 /* NOTE: 'dst' can be equal to 'last' */
00194 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
00195                            uint8_t *src, uint8_t *last, int size, int bpp)
00196 {
00197     int i, p, r, g, b, a;
00198 
00199     switch(filter_type) {
00200     case PNG_FILTER_VALUE_NONE:
00201         memcpy(dst, src, size);
00202         break;
00203     case PNG_FILTER_VALUE_SUB:
00204         for(i = 0; i < bpp; i++) {
00205             dst[i] = src[i];
00206         }
00207         if(bpp == 4) {
00208             p = *(int*)dst;
00209             for(; i < size; i+=bpp) {
00210                 int s = *(int*)(src+i);
00211                 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
00212                 *(int*)(dst+i) = p;
00213             }
00214         } else {
00215 #define OP_SUB(x,s,l) x+s
00216             UNROLL_FILTER(OP_SUB);
00217         }
00218         break;
00219     case PNG_FILTER_VALUE_UP:
00220         dsp->add_bytes_l2(dst, src, last, size);
00221         break;
00222     case PNG_FILTER_VALUE_AVG:
00223         for(i = 0; i < bpp; i++) {
00224             p = (last[i] >> 1);
00225             dst[i] = p + src[i];
00226         }
00227 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
00228         UNROLL_FILTER(OP_AVG);
00229         break;
00230     case PNG_FILTER_VALUE_PAETH:
00231         for(i = 0; i < bpp; i++) {
00232             p = last[i];
00233             dst[i] = p + src[i];
00234         }
00235         if(bpp > 1 && size > 4) {
00236             // would write off the end of the array if we let it process the last pixel with bpp=3
00237             int w = bpp==4 ? size : size-3;
00238             dsp->add_png_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
00239             i = w;
00240         }
00241         ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
00242         break;
00243     }
00244 }
00245 
00246 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
00247 {
00248     int j;
00249     unsigned int r, g, b, a;
00250 
00251     for(j = 0;j < width; j++) {
00252         r = src[0];
00253         g = src[1];
00254         b = src[2];
00255         a = src[3];
00256         if(loco) {
00257             r = (r+g)&0xff;
00258             b = (b+g)&0xff;
00259         }
00260         *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
00261         dst += 4;
00262         src += 4;
00263     }
00264 }
00265 
00266 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
00267 {
00268     if(loco)
00269         convert_to_rgb32_loco(dst, src, width, 1);
00270     else
00271         convert_to_rgb32_loco(dst, src, width, 0);
00272 }
00273 
00274 static void deloco_rgb24(uint8_t *dst, int size)
00275 {
00276     int i;
00277     for(i=0; i<size; i+=3) {
00278         int g = dst[i+1];
00279         dst[i+0] += g;
00280         dst[i+2] += g;
00281     }
00282 }
00283 
00284 /* process exactly one decompressed row */
00285 static void png_handle_row(PNGDecContext *s)
00286 {
00287     uint8_t *ptr, *last_row;
00288     int got_line;
00289 
00290     if (!s->interlace_type) {
00291         ptr = s->image_buf + s->image_linesize * s->y;
00292         /* need to swap bytes correctly for RGB_ALPHA */
00293         if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00294             png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
00295                            s->last_row, s->row_size, s->bpp);
00296             convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
00297             FFSWAP(uint8_t*, s->last_row, s->tmp_row);
00298         } else {
00299             /* in normal case, we avoid one copy */
00300             if (s->y == 0)
00301                 last_row = s->last_row;
00302             else
00303                 last_row = ptr - s->image_linesize;
00304 
00305             png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
00306                            last_row, s->row_size, s->bpp);
00307         }
00308         /* loco lags by 1 row so that it doesn't interfere with top prediction */
00309         if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
00310             s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
00311             deloco_rgb24(ptr - s->image_linesize, s->row_size);
00312         s->y++;
00313         if (s->y == s->height) {
00314             s->state |= PNG_ALLIMAGE;
00315             if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
00316                 s->color_type == PNG_COLOR_TYPE_RGB)
00317                 deloco_rgb24(ptr, s->row_size);
00318         }
00319     } else {
00320         got_line = 0;
00321         for(;;) {
00322             ptr = s->image_buf + s->image_linesize * s->y;
00323             if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
00324                 /* if we already read one row, it is time to stop to
00325                    wait for the next one */
00326                 if (got_line)
00327                     break;
00328                 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
00329                                s->last_row, s->pass_row_size, s->bpp);
00330                 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
00331                 got_line = 1;
00332             }
00333             if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
00334                 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
00335                 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
00336                                        s->color_type, s->last_row);
00337             }
00338             s->y++;
00339             if (s->y == s->height) {
00340                 for(;;) {
00341                     if (s->pass == NB_PASSES - 1) {
00342                         s->state |= PNG_ALLIMAGE;
00343                         goto the_end;
00344                     } else {
00345                         s->pass++;
00346                         s->y = 0;
00347                         s->pass_row_size = ff_png_pass_row_size(s->pass,
00348                                                              s->bits_per_pixel,
00349                                                              s->width);
00350                         s->crow_size = s->pass_row_size + 1;
00351                         if (s->pass_row_size != 0)
00352                             break;
00353                         /* skip pass if empty row */
00354                     }
00355                 }
00356             }
00357         }
00358     the_end: ;
00359     }
00360 }
00361 
00362 static int png_decode_idat(PNGDecContext *s, int length)
00363 {
00364     int ret;
00365     s->zstream.avail_in = length;
00366     s->zstream.next_in = s->bytestream;
00367     s->bytestream += length;
00368 
00369     if(s->bytestream > s->bytestream_end)
00370         return -1;
00371 
00372     /* decode one line if possible */
00373     while (s->zstream.avail_in > 0) {
00374         ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
00375         if (ret != Z_OK && ret != Z_STREAM_END) {
00376             return -1;
00377         }
00378         if (s->zstream.avail_out == 0) {
00379             if (!(s->state & PNG_ALLIMAGE)) {
00380                 png_handle_row(s);
00381             }
00382             s->zstream.avail_out = s->crow_size;
00383             s->zstream.next_out = s->crow_buf;
00384         }
00385     }
00386     return 0;
00387 }
00388 
00389 static int decode_frame(AVCodecContext *avctx,
00390                         void *data, int *data_size,
00391                         AVPacket *avpkt)
00392 {
00393     const uint8_t *buf = avpkt->data;
00394     int buf_size = avpkt->size;
00395     PNGDecContext * const s = avctx->priv_data;
00396     AVFrame *picture = data;
00397     AVFrame *p;
00398     uint8_t *crow_buf_base = NULL;
00399     uint32_t tag, length;
00400     int ret;
00401 
00402     FFSWAP(AVFrame *, s->current_picture, s->last_picture);
00403     avctx->coded_frame= s->current_picture;
00404     p = s->current_picture;
00405 
00406     s->bytestream_start=
00407     s->bytestream= buf;
00408     s->bytestream_end= buf + buf_size;
00409 
00410     /* check signature */
00411     if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
00412         memcmp(s->bytestream, ff_mngsig, 8) != 0)
00413         return -1;
00414     s->bytestream+= 8;
00415     s->y=
00416     s->state=0;
00417 //    memset(s, 0, sizeof(PNGDecContext));
00418     /* init the zlib */
00419     s->zstream.zalloc = ff_png_zalloc;
00420     s->zstream.zfree = ff_png_zfree;
00421     s->zstream.opaque = NULL;
00422     ret = inflateInit(&s->zstream);
00423     if (ret != Z_OK)
00424         return -1;
00425     for(;;) {
00426         int tag32;
00427         if (s->bytestream >= s->bytestream_end)
00428             goto fail;
00429         length = bytestream_get_be32(&s->bytestream);
00430         if (length > 0x7fffffff)
00431             goto fail;
00432         tag32 = bytestream_get_be32(&s->bytestream);
00433         tag = av_bswap32(tag32);
00434         av_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
00435                 (tag & 0xff),
00436                 ((tag >> 8) & 0xff),
00437                 ((tag >> 16) & 0xff),
00438                 ((tag >> 24) & 0xff), length);
00439         switch(tag) {
00440         case MKTAG('I', 'H', 'D', 'R'):
00441             if (length != 13)
00442                 goto fail;
00443             s->width = bytestream_get_be32(&s->bytestream);
00444             s->height = bytestream_get_be32(&s->bytestream);
00445             if(av_image_check_size(s->width, s->height, 0, avctx)){
00446                 s->width= s->height= 0;
00447                 goto fail;
00448             }
00449             s->bit_depth = *s->bytestream++;
00450             s->color_type = *s->bytestream++;
00451             s->compression_type = *s->bytestream++;
00452             s->filter_type = *s->bytestream++;
00453             s->interlace_type = *s->bytestream++;
00454             s->bytestream += 4; /* crc */
00455             s->state |= PNG_IHDR;
00456             av_dlog(avctx, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
00457                     s->width, s->height, s->bit_depth, s->color_type,
00458                     s->compression_type, s->filter_type, s->interlace_type);
00459             break;
00460         case MKTAG('I', 'D', 'A', 'T'):
00461             if (!(s->state & PNG_IHDR))
00462                 goto fail;
00463             if (!(s->state & PNG_IDAT)) {
00464                 /* init image info */
00465                 avctx->width = s->width;
00466                 avctx->height = s->height;
00467 
00468                 s->channels = ff_png_get_nb_channels(s->color_type);
00469                 s->bits_per_pixel = s->bit_depth * s->channels;
00470                 s->bpp = (s->bits_per_pixel + 7) >> 3;
00471                 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
00472 
00473                 if (s->bit_depth == 8 &&
00474                     s->color_type == PNG_COLOR_TYPE_RGB) {
00475                     avctx->pix_fmt = PIX_FMT_RGB24;
00476                 } else if (s->bit_depth == 8 &&
00477                            s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00478                     avctx->pix_fmt = PIX_FMT_RGB32;
00479                 } else if (s->bit_depth == 8 &&
00480                            s->color_type == PNG_COLOR_TYPE_GRAY) {
00481                     avctx->pix_fmt = PIX_FMT_GRAY8;
00482                 } else if (s->bit_depth == 16 &&
00483                            s->color_type == PNG_COLOR_TYPE_GRAY) {
00484                     avctx->pix_fmt = PIX_FMT_GRAY16BE;
00485                 } else if (s->bit_depth == 16 &&
00486                            s->color_type == PNG_COLOR_TYPE_RGB) {
00487                     avctx->pix_fmt = PIX_FMT_RGB48BE;
00488                 } else if (s->bit_depth == 1 &&
00489                            s->color_type == PNG_COLOR_TYPE_GRAY) {
00490                     avctx->pix_fmt = PIX_FMT_MONOBLACK;
00491                 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
00492                     avctx->pix_fmt = PIX_FMT_PAL8;
00493                 } else if (s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
00494                     avctx->pix_fmt = PIX_FMT_Y400A;
00495                 } else {
00496                     goto fail;
00497                 }
00498                 if(p->data[0])
00499                     avctx->release_buffer(avctx, p);
00500 
00501                 p->reference= 0;
00502                 if(avctx->get_buffer(avctx, p) < 0){
00503                     av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00504                     goto fail;
00505                 }
00506                 p->pict_type= AV_PICTURE_TYPE_I;
00507                 p->key_frame= 1;
00508                 p->interlaced_frame = !!s->interlace_type;
00509 
00510                 /* compute the compressed row size */
00511                 if (!s->interlace_type) {
00512                     s->crow_size = s->row_size + 1;
00513                 } else {
00514                     s->pass = 0;
00515                     s->pass_row_size = ff_png_pass_row_size(s->pass,
00516                                                          s->bits_per_pixel,
00517                                                          s->width);
00518                     s->crow_size = s->pass_row_size + 1;
00519                 }
00520                 av_dlog(avctx, "row_size=%d crow_size =%d\n",
00521                         s->row_size, s->crow_size);
00522                 s->image_buf = p->data[0];
00523                 s->image_linesize = p->linesize[0];
00524                 /* copy the palette if needed */
00525                 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
00526                     memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
00527                 /* empty row is used if differencing to the first row */
00528                 s->last_row = av_mallocz(s->row_size);
00529                 if (!s->last_row)
00530                     goto fail;
00531                 if (s->interlace_type ||
00532                     s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00533                     s->tmp_row = av_malloc(s->row_size);
00534                     if (!s->tmp_row)
00535                         goto fail;
00536                 }
00537                 /* compressed row */
00538                 crow_buf_base = av_malloc(s->row_size + 16);
00539                 if (!crow_buf_base)
00540                     goto fail;
00541 
00542                 /* we want crow_buf+1 to be 16-byte aligned */
00543                 s->crow_buf = crow_buf_base + 15;
00544                 s->zstream.avail_out = s->crow_size;
00545                 s->zstream.next_out = s->crow_buf;
00546             }
00547             s->state |= PNG_IDAT;
00548             if (png_decode_idat(s, length) < 0)
00549                 goto fail;
00550             s->bytestream += 4; /* crc */
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                 s->bytestream += 4; /* crc */
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                 s->bytestream += 4; /* crc */
00587             }
00588             break;
00589         case MKTAG('I', 'E', 'N', 'D'):
00590             if (!(s->state & PNG_ALLIMAGE))
00591                 goto fail;
00592             s->bytestream += 4; /* crc */
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 ff_png_decoder = {
00660     .name           = "png",
00661     .type           = AVMEDIA_TYPE_VIDEO,
00662     .id             = CODEC_ID_PNG,
00663     .priv_data_size = sizeof(PNGDecContext),
00664     .init           = png_dec_init,
00665     .close          = png_dec_end,
00666     .decode         = decode_frame,
00667     .capabilities   = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
00668     .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
00669 };
Generated on Sat Mar 17 2012 12:57:48 for Libav by doxygen 1.7.1