• 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 
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->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
00483                            s->color_type == PNG_COLOR_TYPE_PALETTE) {
00484                     avctx->pix_fmt = PIX_FMT_PAL8;
00485                 } else if (s->bit_depth == 1) {
00486                     avctx->pix_fmt = PIX_FMT_MONOBLACK;
00487                 } else if (s->bit_depth == 8 &&
00488                            s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
00489                     avctx->pix_fmt = PIX_FMT_Y400A;
00490                 } else {
00491                     av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
00492                                                 "and color type %d\n",
00493                                                  s->bit_depth, s->color_type);
00494                     goto fail;
00495                 }
00496                 if(p->data[0])
00497                     avctx->release_buffer(avctx, p);
00498 
00499                 p->reference= 3;
00500                 if(avctx->get_buffer(avctx, p) < 0){
00501                     av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00502                     goto fail;
00503                 }
00504                 p->pict_type= AV_PICTURE_TYPE_I;
00505                 p->key_frame= 1;
00506                 p->interlaced_frame = !!s->interlace_type;
00507 
00508                 /* compute the compressed row size */
00509                 if (!s->interlace_type) {
00510                     s->crow_size = s->row_size + 1;
00511                 } else {
00512                     s->pass = 0;
00513                     s->pass_row_size = ff_png_pass_row_size(s->pass,
00514                                                          s->bits_per_pixel,
00515                                                          s->width);
00516                     s->crow_size = s->pass_row_size + 1;
00517                 }
00518                 av_dlog(avctx, "row_size=%d crow_size =%d\n",
00519                         s->row_size, s->crow_size);
00520                 s->image_buf = p->data[0];
00521                 s->image_linesize = p->linesize[0];
00522                 /* copy the palette if needed */
00523                 if (avctx->pix_fmt == PIX_FMT_PAL8)
00524                     memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
00525                 /* empty row is used if differencing to the first row */
00526                 s->last_row = av_mallocz(s->row_size);
00527                 if (!s->last_row)
00528                     goto fail;
00529                 if (s->interlace_type ||
00530                     s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00531                     s->tmp_row = av_malloc(s->row_size);
00532                     if (!s->tmp_row)
00533                         goto fail;
00534                 }
00535                 /* compressed row */
00536                 crow_buf_base = av_malloc(s->row_size + 16);
00537                 if (!crow_buf_base)
00538                     goto fail;
00539 
00540                 /* we want crow_buf+1 to be 16-byte aligned */
00541                 s->crow_buf = crow_buf_base + 15;
00542                 s->zstream.avail_out = s->crow_size;
00543                 s->zstream.next_out = s->crow_buf;
00544             }
00545             s->state |= PNG_IDAT;
00546             if (png_decode_idat(s, length) < 0)
00547                 goto fail;
00548             bytestream2_skip(&s->gb, 4); /* crc */
00549             break;
00550         case MKTAG('P', 'L', 'T', 'E'):
00551             {
00552                 int n, i, r, g, b;
00553 
00554                 if ((length % 3) != 0 || length > 256 * 3)
00555                     goto skip_tag;
00556                 /* read the palette */
00557                 n = length / 3;
00558                 for(i=0;i<n;i++) {
00559                     r = bytestream2_get_byte(&s->gb);
00560                     g = bytestream2_get_byte(&s->gb);
00561                     b = bytestream2_get_byte(&s->gb);
00562                     s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
00563                 }
00564                 for(;i<256;i++) {
00565                     s->palette[i] = (0xff << 24);
00566                 }
00567                 s->state |= PNG_PLTE;
00568                 bytestream2_skip(&s->gb, 4); /* crc */
00569             }
00570             break;
00571         case MKTAG('t', 'R', 'N', 'S'):
00572             {
00573                 int v, i;
00574 
00575                 /* read the transparency. XXX: Only palette mode supported */
00576                 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
00577                     length > 256 ||
00578                     !(s->state & PNG_PLTE))
00579                     goto skip_tag;
00580                 for(i=0;i<length;i++) {
00581                     v = bytestream2_get_byte(&s->gb);
00582                     s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
00583                 }
00584                 bytestream2_skip(&s->gb, 4); /* crc */
00585             }
00586             break;
00587         case MKTAG('I', 'E', 'N', 'D'):
00588             if (!(s->state & PNG_ALLIMAGE))
00589                 goto fail;
00590             bytestream2_skip(&s->gb, 4); /* crc */
00591             goto exit_loop;
00592         default:
00593             /* skip tag */
00594         skip_tag:
00595             bytestream2_skip(&s->gb, length + 4);
00596             break;
00597         }
00598     }
00599  exit_loop:
00600 
00601     if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
00602         int i, j;
00603         uint8_t *pd = s->current_picture->data[0];
00604         for(j=0; j < s->height; j++) {
00605             for(i=s->width/8-1; i>=0; i--) {
00606                 pd[8*i+7]=  pd[i]    &1;
00607                 pd[8*i+6]= (pd[i]>>1)&1;
00608                 pd[8*i+5]= (pd[i]>>2)&1;
00609                 pd[8*i+4]= (pd[i]>>3)&1;
00610                 pd[8*i+3]= (pd[i]>>4)&1;
00611                 pd[8*i+2]= (pd[i]>>5)&1;
00612                 pd[8*i+1]= (pd[i]>>6)&1;
00613                 pd[8*i+0]=  pd[i]>>7;
00614             }
00615             pd += s->image_linesize;
00616         }
00617     }
00618     if(s->bits_per_pixel == 2){
00619         int i, j;
00620         uint8_t *pd = s->current_picture->data[0];
00621         for(j=0; j < s->height; j++) {
00622             for(i=s->width/4-1; i>=0; i--) {
00623                 pd[4*i+3]=  pd[i]    &3;
00624                 pd[4*i+2]= (pd[i]>>2)&3;
00625                 pd[4*i+1]= (pd[i]>>4)&3;
00626                 pd[4*i+0]=  pd[i]>>6;
00627             }
00628             pd += s->image_linesize;
00629         }
00630     }
00631     if(s->bits_per_pixel == 4){
00632         int i, j;
00633         uint8_t *pd = s->current_picture->data[0];
00634         for(j=0; j < s->height; j++) {
00635             for(i=s->width/2-1; i>=0; i--) {
00636                 pd[2*i+1]= pd[i]&15;
00637                 pd[2*i+0]= pd[i]>>4;
00638             }
00639             pd += s->image_linesize;
00640         }
00641     }
00642 
00643      /* handle p-frames only if a predecessor frame is available */
00644      if(s->last_picture->data[0] != NULL) {
00645          if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
00646             int i, j;
00647             uint8_t *pd = s->current_picture->data[0];
00648             uint8_t *pd_last = s->last_picture->data[0];
00649 
00650             for(j=0; j < s->height; j++) {
00651                 for(i=0; i < s->width * s->bpp; i++) {
00652                     pd[i] += pd_last[i];
00653                 }
00654                 pd += s->image_linesize;
00655                 pd_last += s->image_linesize;
00656             }
00657         }
00658     }
00659 
00660     *picture= *s->current_picture;
00661     *data_size = sizeof(AVFrame);
00662 
00663     ret = bytestream2_tell(&s->gb);
00664  the_end:
00665     inflateEnd(&s->zstream);
00666     av_free(crow_buf_base);
00667     s->crow_buf = NULL;
00668     av_freep(&s->last_row);
00669     av_freep(&s->tmp_row);
00670     return ret;
00671  fail:
00672     ret = -1;
00673     goto the_end;
00674 }
00675 
00676 static av_cold int png_dec_init(AVCodecContext *avctx)
00677 {
00678     PNGDecContext *s = avctx->priv_data;
00679 
00680     s->current_picture = &s->picture1;
00681     s->last_picture = &s->picture2;
00682     avcodec_get_frame_defaults(&s->picture1);
00683     avcodec_get_frame_defaults(&s->picture2);
00684 
00685 #if HAVE_MMX
00686     ff_png_init_mmx(s);
00687 #endif
00688 
00689     if (!s->add_paeth_prediction)
00690         s->add_paeth_prediction = add_paeth_prediction_c;
00691     if (!s->add_bytes_l2)
00692         s->add_bytes_l2 = add_bytes_l2_c;
00693 
00694     return 0;
00695 }
00696 
00697 static av_cold int png_dec_end(AVCodecContext *avctx)
00698 {
00699     PNGDecContext *s = avctx->priv_data;
00700 
00701     if (s->picture1.data[0])
00702         avctx->release_buffer(avctx, &s->picture1);
00703     if (s->picture2.data[0])
00704         avctx->release_buffer(avctx, &s->picture2);
00705 
00706     return 0;
00707 }
00708 
00709 AVCodec ff_png_decoder = {
00710     .name           = "png",
00711     .type           = AVMEDIA_TYPE_VIDEO,
00712     .id             = CODEC_ID_PNG,
00713     .priv_data_size = sizeof(PNGDecContext),
00714     .init           = png_dec_init,
00715     .close          = png_dec_end,
00716     .decode         = decode_frame,
00717     .capabilities   = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
00718     .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
00719 };
Generated on Fri Feb 1 2013 14:34:41 for FFmpeg by doxygen 1.7.1