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

Generated on Wed Apr 11 2012 07:31:34 for FFmpeg by  doxygen 1.7.1