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 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, 0xff, 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         src_x = 0;
00061         for(x = 0; x < width; x++) {
00062             j = (x & 7);
00063             if ((dsp_mask << j) & 0x80) {
00064                 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
00065                 dst[x >> 3] &= 0xFF7F>>j;
00066                 dst[x >> 3] |= b << (7 - j);
00067             }
00068             if ((mask << j) & 0x80)
00069                 src_x++;
00070         }
00071         break;
00072     case 2:
00073         src_x = 0;
00074         for(x = 0; x < width; x++) {
00075             int j2 = 2*(x&3);
00076             j = (x & 7);
00077             if ((dsp_mask << j) & 0x80) {
00078                 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
00079                 dst[x >> 2] &= 0xFF3F>>j2;
00080                 dst[x >> 2] |= b << (6 - j2);
00081             }
00082             if ((mask << j) & 0x80)
00083                 src_x++;
00084         }
00085         break;
00086     case 4:
00087         src_x = 0;
00088         for(x = 0; x < width; x++) {
00089             int j2 = 4*(x&1);
00090             j = (x & 7);
00091             if ((dsp_mask << j) & 0x80) {
00092                 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
00093                 dst[x >> 1] &= 0xFF0F>>j2;
00094                 dst[x >> 1] |= b << (4 - j2);
00095             }
00096             if ((mask << j) & 0x80)
00097                 src_x++;
00098         }
00099         break;
00100     default:
00101         bpp = bits_per_pixel >> 3;
00102         d = dst;
00103         s = src;
00104             for(x = 0; x < width; x++) {
00105                 j = x & 7;
00106                 if ((dsp_mask << j) & 0x80) {
00107                     memcpy(d, s, bpp);
00108                 }
00109                 d += bpp;
00110                 if ((mask << j) & 0x80)
00111                     s += bpp;
00112             }
00113         break;
00114     }
00115 }
00116 
00117 // 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size
00118 #define pb_7f (~0UL/255 * 0x7f)
00119 #define pb_80 (~0UL/255 * 0x80)
00120 
00121 static void add_bytes_l2_c(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
00122 {
00123     long i;
00124     for(i=0; i<=w-sizeof(long); i+=sizeof(long)){
00125         long a = *(long*)(src1+i);
00126         long b = *(long*)(src2+i);
00127         *(long*)(dst+i) = ((a&pb_7f) + (b&pb_7f)) ^ ((a^b)&pb_80);
00128     }
00129     for(; i<w; i++)
00130         dst[i] = src1[i]+src2[i];
00131 }
00132 
00133 static void add_paeth_prediction_c(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
00134 {
00135     int i;
00136     for(i = 0; i < w; i++) {
00137         int a, b, c, p, pa, pb, pc;
00138 
00139         a = dst[i - bpp];
00140         b = top[i];
00141         c = top[i - bpp];
00142 
00143         p = b - c;
00144         pc = a - c;
00145 
00146         pa = abs(p);
00147         pb = abs(pc);
00148         pc = abs(p + pc);
00149 
00150         if (pa <= pb && pa <= pc)
00151             p = a;
00152         else if (pb <= pc)
00153             p = b;
00154         else
00155             p = c;
00156         dst[i] = p + src[i];
00157     }
00158 }
00159 
00160 #define UNROLL1(bpp, op) {\
00161                  r = dst[0];\
00162     if(bpp >= 2) g = dst[1];\
00163     if(bpp >= 3) b = dst[2];\
00164     if(bpp >= 4) a = dst[3];\
00165     for(; i < size; i+=bpp) {\
00166         dst[i+0] = r = op(r, src[i+0], last[i+0]);\
00167         if(bpp == 1) continue;\
00168         dst[i+1] = g = op(g, src[i+1], last[i+1]);\
00169         if(bpp == 2) continue;\
00170         dst[i+2] = b = op(b, src[i+2], last[i+2]);\
00171         if(bpp == 3) continue;\
00172         dst[i+3] = a = op(a, src[i+3], last[i+3]);\
00173     }\
00174 }
00175 
00176 #define UNROLL_FILTER(op)\
00177          if(bpp == 1) UNROLL1(1, op)\
00178     else if(bpp == 2) UNROLL1(2, op)\
00179     else if(bpp == 3) UNROLL1(3, op)\
00180     else if(bpp == 4) UNROLL1(4, op)\
00181     else {\
00182         for (; i < size; i += bpp) {\
00183             int j;\
00184             for (j = 0; j < bpp; j++)\
00185                 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
00186         }\
00187     }
00188 
00189 /* NOTE: 'dst' can be equal to 'last' */
00190 static void png_filter_row(PNGDecContext *s, uint8_t *dst, int filter_type,
00191                            uint8_t *src, uint8_t *last, int size, int bpp)
00192 {
00193     int i, p, r, g, b, a;
00194 
00195     switch(filter_type) {
00196     case PNG_FILTER_VALUE_NONE:
00197         memcpy(dst, src, size);
00198         break;
00199     case PNG_FILTER_VALUE_SUB:
00200         for(i = 0; i < bpp; i++) {
00201             dst[i] = src[i];
00202         }
00203         if(bpp == 4) {
00204             p = *(int*)dst;
00205             for(; i < size; i+=bpp) {
00206                 int s = *(int*)(src+i);
00207                 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
00208                 *(int*)(dst+i) = p;
00209             }
00210         } else {
00211 #define OP_SUB(x,s,l) x+s
00212             UNROLL_FILTER(OP_SUB);
00213         }
00214         break;
00215     case PNG_FILTER_VALUE_UP:
00216         s->add_bytes_l2(dst, src, last, size);
00217         break;
00218     case PNG_FILTER_VALUE_AVG:
00219         for(i = 0; i < bpp; i++) {
00220             p = (last[i] >> 1);
00221             dst[i] = p + src[i];
00222         }
00223 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
00224         UNROLL_FILTER(OP_AVG);
00225         break;
00226     case PNG_FILTER_VALUE_PAETH:
00227         for(i = 0; i < bpp; i++) {
00228             p = last[i];
00229             dst[i] = p + src[i];
00230         }
00231         if(bpp > 2 && size > 4) {
00232             // would write off the end of the array if we let it process the last pixel with bpp=3
00233             int w = bpp==4 ? size : size-3;
00234             s->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
00235             i = w;
00236         }
00237         add_paeth_prediction_c(dst+i, src+i, last+i, size-i, bpp);
00238         break;
00239     }
00240 }
00241 
00242 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
00243 {
00244     int j;
00245     unsigned int r, g, b, a;
00246 
00247     for(j = 0;j < width; j++) {
00248         r = src[0];
00249         g = src[1];
00250         b = src[2];
00251         a = src[3];
00252         if(loco) {
00253             r = (r+g)&0xff;
00254             b = (b+g)&0xff;
00255         }
00256         dst[0] = r;
00257         dst[1] = g;
00258         dst[2] = b;
00259         dst[3] = a;
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         memcpy(dst, src, width * 4);
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, 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, 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, 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                 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
00334                                        s->color_type, s->last_row);
00335             }
00336             s->y++;
00337             if (s->y == s->height) {
00338                 memset(s->last_row, 0, s->row_size);
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 = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
00365     s->zstream.next_in = s->gb.buffer;
00366     bytestream2_skip(&s->gb, length);
00367 
00368     /* decode one line if possible */
00369     while (s->zstream.avail_in > 0) {
00370         ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
00371         if (ret != Z_OK && ret != Z_STREAM_END) {
00372             return -1;
00373         }
00374         if (s->zstream.avail_out == 0) {
00375             if (!(s->state & PNG_ALLIMAGE)) {
00376                 png_handle_row(s);
00377             }
00378             s->zstream.avail_out = s->crow_size;
00379             s->zstream.next_out = s->crow_buf;
00380         }
00381     }
00382     return 0;
00383 }
00384 
00385 static int decode_frame(AVCodecContext *avctx,
00386                         void *data, int *data_size,
00387                         AVPacket *avpkt)
00388 {
00389     const uint8_t *buf = avpkt->data;
00390     int buf_size = avpkt->size;
00391     PNGDecContext * const s = avctx->priv_data;
00392     AVFrame *picture = data;
00393     AVFrame *p;
00394     uint8_t *crow_buf_base = NULL;
00395     uint32_t tag, length;
00396     int ret;
00397 
00398     FFSWAP(AVFrame *, s->current_picture, s->last_picture);
00399     avctx->coded_frame= s->current_picture;
00400     p = s->current_picture;
00401 
00402     /* check signature */
00403     if (buf_size < 8 ||
00404         memcmp(buf, ff_pngsig, 8) != 0 &&
00405         memcmp(buf, ff_mngsig, 8) != 0)
00406         return -1;
00407 
00408     bytestream2_init(&s->gb, buf + 8, buf_size - 8);
00409     s->y=
00410     s->state=0;
00411 //    memset(s, 0, sizeof(PNGDecContext));
00412     /* init the zlib */
00413     s->zstream.zalloc = ff_png_zalloc;
00414     s->zstream.zfree = ff_png_zfree;
00415     s->zstream.opaque = NULL;
00416     ret = inflateInit(&s->zstream);
00417     if (ret != Z_OK)
00418         return -1;
00419     for(;;) {
00420         if (bytestream2_get_bytes_left(&s->gb) <= 0)
00421             goto fail;
00422         length = bytestream2_get_be32(&s->gb);
00423         if (length > 0x7fffffff)
00424             goto fail;
00425         tag = bytestream2_get_le32(&s->gb);
00426         if (avctx->debug & FF_DEBUG_STARTCODE)
00427             av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
00428                 (tag & 0xff),
00429                 ((tag >> 8) & 0xff),
00430                 ((tag >> 16) & 0xff),
00431                 ((tag >> 24) & 0xff), length);
00432         switch(tag) {
00433         case MKTAG('I', 'H', 'D', 'R'):
00434             if (length != 13)
00435                 goto fail;
00436             s->width  = bytestream2_get_be32(&s->gb);
00437             s->height = bytestream2_get_be32(&s->gb);
00438             if(av_image_check_size(s->width, s->height, 0, avctx)){
00439                 s->width= s->height= 0;
00440                 goto fail;
00441             }
00442             s->bit_depth        = bytestream2_get_byte(&s->gb);
00443             s->color_type       = bytestream2_get_byte(&s->gb);
00444             s->compression_type = bytestream2_get_byte(&s->gb);
00445             s->filter_type      = bytestream2_get_byte(&s->gb);
00446             s->interlace_type   = bytestream2_get_byte(&s->gb);
00447             bytestream2_skip(&s->gb, 4); /* crc */
00448             s->state |= PNG_IHDR;
00449             if (avctx->debug & FF_DEBUG_PICT_INFO)
00450                 av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
00451                     s->width, s->height, s->bit_depth, s->color_type,
00452                     s->compression_type, s->filter_type, s->interlace_type);
00453             break;
00454         case MKTAG('I', 'D', 'A', 'T'):
00455             if (!(s->state & PNG_IHDR))
00456                 goto fail;
00457             if (!(s->state & PNG_IDAT)) {
00458                 /* init image info */
00459                 avctx->width = s->width;
00460                 avctx->height = s->height;
00461 
00462                 s->channels = ff_png_get_nb_channels(s->color_type);
00463                 s->bits_per_pixel = s->bit_depth * s->channels;
00464                 s->bpp = (s->bits_per_pixel + 7) >> 3;
00465                 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
00466 
00467                 if (s->bit_depth == 8 &&
00468                     s->color_type == PNG_COLOR_TYPE_RGB) {
00469                     avctx->pix_fmt = PIX_FMT_RGB24;
00470                 } else if (s->bit_depth == 8 &&
00471                            s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00472                     avctx->pix_fmt = PIX_FMT_RGBA;
00473                 } else if (s->bit_depth == 8 &&
00474                            s->color_type == PNG_COLOR_TYPE_GRAY) {
00475                     avctx->pix_fmt = PIX_FMT_GRAY8;
00476                 } else if (s->bit_depth == 16 &&
00477                            s->color_type == PNG_COLOR_TYPE_GRAY) {
00478                     avctx->pix_fmt = PIX_FMT_GRAY16BE;
00479                 } else if (s->bit_depth == 16 &&
00480                            s->color_type == PNG_COLOR_TYPE_RGB) {
00481                     avctx->pix_fmt = PIX_FMT_RGB48BE;
00482                 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
00483                     avctx->pix_fmt = PIX_FMT_PAL8;
00484                 } else if (s->bit_depth == 1) {
00485                     avctx->pix_fmt = PIX_FMT_MONOBLACK;
00486                 } else if (s->bit_depth == 8 &&
00487                            s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
00488                     avctx->pix_fmt = PIX_FMT_GRAY8A;
00489                 } else {
00490                     av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
00491                                                 "and color type %d\n",
00492                                                  s->bit_depth, s->color_type);
00493                     goto fail;
00494                 }
00495                 if(p->data[0])
00496                     avctx->release_buffer(avctx, p);
00497 
00498                 p->reference= 3;
00499                 if(avctx->get_buffer(avctx, p) < 0){
00500                     av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00501                     goto fail;
00502                 }
00503                 p->pict_type= AV_PICTURE_TYPE_I;
00504                 p->key_frame= 1;
00505                 p->interlaced_frame = !!s->interlace_type;
00506 
00507                 /* compute the compressed row size */
00508                 if (!s->interlace_type) {
00509                     s->crow_size = s->row_size + 1;
00510                 } else {
00511                     s->pass = 0;
00512                     s->pass_row_size = ff_png_pass_row_size(s->pass,
00513                                                          s->bits_per_pixel,
00514                                                          s->width);
00515                     s->crow_size = s->pass_row_size + 1;
00516                 }
00517                 av_dlog(avctx, "row_size=%d crow_size =%d\n",
00518                         s->row_size, s->crow_size);
00519                 s->image_buf = p->data[0];
00520                 s->image_linesize = p->linesize[0];
00521                 /* copy the palette if needed */
00522                 if (avctx->pix_fmt == PIX_FMT_PAL8)
00523                     memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
00524                 /* empty row is used if differencing to the first row */
00525                 s->last_row = av_mallocz(s->row_size);
00526                 if (!s->last_row)
00527                     goto fail;
00528                 if (s->interlace_type ||
00529                     s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00530                     s->tmp_row = av_malloc(s->row_size);
00531                     if (!s->tmp_row)
00532                         goto fail;
00533                 }
00534                 /* compressed row */
00535                 crow_buf_base = av_malloc(s->row_size + 16);
00536                 if (!crow_buf_base)
00537                     goto fail;
00538 
00539                 /* we want crow_buf+1 to be 16-byte aligned */
00540                 s->crow_buf = crow_buf_base + 15;
00541                 s->zstream.avail_out = s->crow_size;
00542                 s->zstream.next_out = s->crow_buf;
00543             }
00544             s->state |= PNG_IDAT;
00545             if (png_decode_idat(s, length) < 0)
00546                 goto fail;
00547             bytestream2_skip(&s->gb, 4); /* crc */
00548             break;
00549         case MKTAG('P', 'L', 'T', 'E'):
00550             {
00551                 int n, i, r, g, b;
00552 
00553                 if ((length % 3) != 0 || length > 256 * 3)
00554                     goto skip_tag;
00555                 /* read the palette */
00556                 n = length / 3;
00557                 for(i=0;i<n;i++) {
00558                     r = bytestream2_get_byte(&s->gb);
00559                     g = bytestream2_get_byte(&s->gb);
00560                     b = bytestream2_get_byte(&s->gb);
00561                     s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
00562                 }
00563                 for(;i<256;i++) {
00564                     s->palette[i] = (0xff << 24);
00565                 }
00566                 s->state |= PNG_PLTE;
00567                 bytestream2_skip(&s->gb, 4); /* crc */
00568             }
00569             break;
00570         case MKTAG('t', 'R', 'N', 'S'):
00571             {
00572                 int v, i;
00573 
00574                 /* read the transparency. XXX: Only palette mode supported */
00575                 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
00576                     length > 256 ||
00577                     !(s->state & PNG_PLTE))
00578                     goto skip_tag;
00579                 for(i=0;i<length;i++) {
00580                     v = bytestream2_get_byte(&s->gb);
00581                     s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
00582                 }
00583                 bytestream2_skip(&s->gb, 4); /* crc */
00584             }
00585             break;
00586         case MKTAG('I', 'E', 'N', 'D'):
00587             if (!(s->state & PNG_ALLIMAGE))
00588                 goto fail;
00589             bytestream2_skip(&s->gb, 4); /* crc */
00590             goto exit_loop;
00591         default:
00592             /* skip tag */
00593         skip_tag:
00594             bytestream2_skip(&s->gb, length + 4);
00595             break;
00596         }
00597     }
00598  exit_loop:
00599 
00600     if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
00601         int i, j;
00602         uint8_t *pd = s->current_picture->data[0];
00603         for(j=0; j < s->height; j++) {
00604             for(i=s->width/8-1; i>=0; i--) {
00605                 pd[8*i+7]=  pd[i]    &1;
00606                 pd[8*i+6]= (pd[i]>>1)&1;
00607                 pd[8*i+5]= (pd[i]>>2)&1;
00608                 pd[8*i+4]= (pd[i]>>3)&1;
00609                 pd[8*i+3]= (pd[i]>>4)&1;
00610                 pd[8*i+2]= (pd[i]>>5)&1;
00611                 pd[8*i+1]= (pd[i]>>6)&1;
00612                 pd[8*i+0]=  pd[i]>>7;
00613             }
00614             pd += s->image_linesize;
00615         }
00616     }
00617     if(s->bits_per_pixel == 2){
00618         int i, j;
00619         uint8_t *pd = s->current_picture->data[0];
00620         for(j=0; j < s->height; j++) {
00621             for(i=s->width/4-1; i>=0; i--) {
00622                 pd[4*i+3]=  pd[i]    &3;
00623                 pd[4*i+2]= (pd[i]>>2)&3;
00624                 pd[4*i+1]= (pd[i]>>4)&3;
00625                 pd[4*i+0]=  pd[i]>>6;
00626             }
00627             pd += s->image_linesize;
00628         }
00629     }
00630     if(s->bits_per_pixel == 4){
00631         int i, j;
00632         uint8_t *pd = s->current_picture->data[0];
00633         for(j=0; j < s->height; j++) {
00634             for(i=s->width/2-1; i>=0; i--) {
00635                 pd[2*i+1]= pd[i]&15;
00636                 pd[2*i+0]= pd[i]>>4;
00637             }
00638             pd += s->image_linesize;
00639         }
00640     }
00641 
00642      /* handle p-frames only if a predecessor frame is available */
00643      if(s->last_picture->data[0] != NULL) {
00644          if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
00645             int i, j;
00646             uint8_t *pd = s->current_picture->data[0];
00647             uint8_t *pd_last = s->last_picture->data[0];
00648 
00649             for(j=0; j < s->height; j++) {
00650                 for(i=0; i < s->width * s->bpp; i++) {
00651                     pd[i] += pd_last[i];
00652                 }
00653                 pd += s->image_linesize;
00654                 pd_last += s->image_linesize;
00655             }
00656         }
00657     }
00658 
00659     *picture= *s->current_picture;
00660     *data_size = sizeof(AVFrame);
00661 
00662     ret = bytestream2_tell(&s->gb);
00663  the_end:
00664     inflateEnd(&s->zstream);
00665     av_free(crow_buf_base);
00666     s->crow_buf = NULL;
00667     av_freep(&s->last_row);
00668     av_freep(&s->tmp_row);
00669     return ret;
00670  fail:
00671     ret = -1;
00672     goto the_end;
00673 }
00674 
00675 static av_cold int png_dec_init(AVCodecContext *avctx)
00676 {
00677     PNGDecContext *s = avctx->priv_data;
00678 
00679     s->current_picture = &s->picture1;
00680     s->last_picture = &s->picture2;
00681     avcodec_get_frame_defaults(&s->picture1);
00682     avcodec_get_frame_defaults(&s->picture2);
00683 
00684 #if HAVE_MMX
00685     ff_png_init_mmx(s);
00686 #endif
00687 
00688     if (!s->add_paeth_prediction)
00689         s->add_paeth_prediction = add_paeth_prediction_c;
00690     if (!s->add_bytes_l2)
00691         s->add_bytes_l2 = add_bytes_l2_c;
00692 
00693     return 0;
00694 }
00695 
00696 static av_cold int png_dec_end(AVCodecContext *avctx)
00697 {
00698     PNGDecContext *s = avctx->priv_data;
00699 
00700     if (s->picture1.data[0])
00701         avctx->release_buffer(avctx, &s->picture1);
00702     if (s->picture2.data[0])
00703         avctx->release_buffer(avctx, &s->picture2);
00704 
00705     return 0;
00706 }
00707 
00708 AVCodec ff_png_decoder = {
00709     .name           = "png",
00710     .type           = AVMEDIA_TYPE_VIDEO,
00711     .id             = CODEC_ID_PNG,
00712     .priv_data_size = sizeof(PNGDecContext),
00713     .init           = png_dec_init,
00714     .close          = png_dec_end,
00715     .decode         = decode_frame,
00716     .capabilities   = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
00717     .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
00718 };