]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/png.c
10l: Rename missed occurrences of CONFIG_EBX_AVAILABLE to HAVE_EBX_AVAILABLE.
[ffmpeg] / libavcodec / png.c
index 2e45787551e639434a29537a9654c47a2be06133..05b4512b7c576f94827b4269f5e0b88c874a19e8 100644 (file)
@@ -2,19 +2,21 @@
  * PNG image format
  * Copyright (c) 2003 Fabrice Bellard.
  *
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avcodec.h"
 
@@ -23,7 +25,6 @@
  * - use filters when generating a png (better compression)
  */
 
-#ifdef CONFIG_ZLIB
 #include <zlib.h>
 
 //#define DEBUG
@@ -90,12 +91,14 @@ static unsigned int get32(uint8_t **b){
     return ((*b)[-4]<<24) + ((*b)[-3]<<16) + ((*b)[-2]<<8) + (*b)[-1];
 }
 
+#ifdef CONFIG_ENCODERS
 static void put32(uint8_t **b, unsigned int v){
     *(*b)++= v>>24;
     *(*b)++= v>>16;
     *(*b)++= v>>8;
     *(*b)++= v;
 }
+#endif
 
 static const uint8_t pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
 
@@ -233,6 +236,7 @@ static void png_put_interlaced_row(uint8_t *dst, int width,
     }
 }
 
+#ifdef CONFIG_ENCODERS
 static void png_get_interlaced_row(uint8_t *dst, int row_size,
                                    int bits_per_pixel, int pass,
                                    const uint8_t *src, int width)
@@ -270,6 +274,7 @@ static void png_get_interlaced_row(uint8_t *dst, int row_size,
         break;
     }
 }
+#endif
 
 /* XXX: optimize */
 /* NOTE: 'dst' can be equal to 'last' */
@@ -338,7 +343,8 @@ static void png_filter_row(uint8_t *dst, int filter_type,
     }
 }
 
-static void convert_from_rgba32(uint8_t *dst, const uint8_t *src, int width)
+#ifdef CONFIG_ENCODERS
+static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
 {
     uint8_t *d;
     int j;
@@ -346,7 +352,7 @@ static void convert_from_rgba32(uint8_t *dst, const uint8_t *src, int width)
 
     d = dst;
     for(j = 0; j < width; j++) {
-        v = ((uint32_t *)src)[j];
+        v = ((const uint32_t *)src)[j];
         d[0] = v >> 16;
         d[1] = v >> 8;
         d[2] = v;
@@ -354,8 +360,10 @@ static void convert_from_rgba32(uint8_t *dst, const uint8_t *src, int width)
         d += 4;
     }
 }
+#endif
 
-static void convert_to_rgba32(uint8_t *dst, const uint8_t *src, int width)
+#ifdef CONFIG_DECODERS
+static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width)
 {
     int j;
     unsigned int r, g, b, a;
@@ -384,7 +392,7 @@ static void png_handle_row(PNGContext *s)
             png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
                            s->last_row, s->row_size, s->bpp);
             memcpy(s->last_row, s->tmp_row, s->row_size);
-            convert_to_rgba32(ptr, s->tmp_row, s->width);
+            convert_to_rgb32(ptr, s->tmp_row, s->width);
         } else {
             /* in normal case, we avoid one copy */
             if (s->y == 0)
@@ -414,7 +422,7 @@ static void png_handle_row(PNGContext *s)
                 got_line = 1;
             }
             if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
-                /* NOTE: rgba32 is handled directly in png_put_interlaced_row */
+                /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
                 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
                                        s->color_type, s->last_row);
             }
@@ -507,7 +515,7 @@ static int decode_frame(AVCodecContext *avctx,
         tag32 = get32(&s->bytestream);
         tag = bswap_32(tag32);
 #ifdef DEBUG
-        printf("png: tag=%c%c%c%c length=%u\n",
+        av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
                (tag & 0xff),
                ((tag >> 8) & 0xff),
                ((tag >> 16) & 0xff),
@@ -531,7 +539,7 @@ static int decode_frame(AVCodecContext *avctx,
             crc = get32(&s->bytestream);
             s->state |= PNG_IHDR;
 #ifdef DEBUG
-            printf("width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
+            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",
                    s->width, s->height, s->bit_depth, s->color_type,
                    s->compression_type, s->filter_type, s->interlace_type);
 #endif
@@ -554,10 +562,13 @@ static int decode_frame(AVCodecContext *avctx,
                     avctx->pix_fmt = PIX_FMT_RGB24;
                 } else if (s->bit_depth == 8 &&
                            s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
-                    avctx->pix_fmt = PIX_FMT_RGBA32;
+                    avctx->pix_fmt = PIX_FMT_RGB32;
                 } else if (s->bit_depth == 8 &&
                            s->color_type == PNG_COLOR_TYPE_GRAY) {
                     avctx->pix_fmt = PIX_FMT_GRAY8;
+                } else if (s->bit_depth == 16 &&
+                           s->color_type == PNG_COLOR_TYPE_GRAY) {
+                    avctx->pix_fmt = PIX_FMT_GRAY16BE;
                 } else if (s->bit_depth == 1 &&
                            s->color_type == PNG_COLOR_TYPE_GRAY) {
                     avctx->pix_fmt = PIX_FMT_MONOBLACK;
@@ -589,7 +600,7 @@ static int decode_frame(AVCodecContext *avctx,
                     s->crow_size = s->pass_row_size + 1;
                 }
 #ifdef DEBUG
-                printf("row_size=%d crow_size =%d\n",
+                av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
                        s->row_size, s->crow_size);
 #endif
                 s->image_buf = p->data[0];
@@ -684,7 +695,9 @@ static int decode_frame(AVCodecContext *avctx,
     ret = -1;
     goto the_end;
 }
+#endif
 
+#ifdef CONFIG_ENCODERS
 static void png_write_chunk(uint8_t **f, uint32_t tag,
                             const uint8_t *buf, int length)
 {
@@ -736,6 +749,7 @@ static int png_write_row(PNGContext *s, const uint8_t *data, int size)
     }
     return 0;
 }
+#endif /* CONFIG_ENCODERS */
 
 static int common_init(AVCodecContext *avctx){
     PNGContext *s = avctx->priv_data;
@@ -747,6 +761,7 @@ static int common_init(AVCodecContext *avctx){
     return 0;
 }
 
+#ifdef CONFIG_ENCODERS
 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
     PNGContext *s = avctx->priv_data;
     AVFrame *pict = data;
@@ -767,7 +782,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
 
     is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
     switch(avctx->pix_fmt) {
-    case PIX_FMT_RGBA32:
+    case PIX_FMT_RGB32:
         bit_depth = 8;
         color_type = PNG_COLOR_TYPE_RGB_ALPHA;
         break;
@@ -837,7 +852,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
         for(i = 0; i < 256; i++) {
             v = palette[i];
             alpha = v >> 24;
-            if (alpha != 0xff)
+            if (alpha && alpha != 0xff)
                 has_alpha = 1;
             *alpha_ptr++ = alpha;
             ptr[0] = v >> 16;
@@ -867,7 +882,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
                     if ((png_pass_ymask[pass] << (y & 7)) & 0x80) {
                         ptr = p->data[0] + y * p->linesize[0];
                         if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
-                            convert_from_rgba32(tmp_buf, ptr, avctx->width);
+                            convert_from_rgb32(tmp_buf, ptr, avctx->width);
                             ptr1 = tmp_buf;
                         } else {
                             ptr1 = ptr;
@@ -885,7 +900,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
         for(y = 0; y < avctx->height; y++) {
             ptr = p->data[0] + y * p->linesize[0];
             if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-                convert_from_rgba32(crow_buf + 1, ptr, avctx->width);
+                convert_from_rgb32(crow_buf + 1, ptr, avctx->width);
             else
                 memcpy(crow_buf + 1, ptr, row_size);
             crow_buf[0] = PNG_FILTER_VALUE_NONE;
@@ -920,7 +935,9 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
     ret = -1;
     goto the_end;
 }
+#endif
 
+#ifdef CONFIG_PNG_DECODER
 AVCodec png_decoder = {
     "png",
     CODEC_TYPE_VIDEO,
@@ -933,6 +950,7 @@ AVCodec png_decoder = {
     0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
     NULL
 };
+#endif
 
 #ifdef CONFIG_PNG_ENCODER
 AVCodec png_encoder = {
@@ -943,7 +961,6 @@ AVCodec png_encoder = {
     common_init,
     encode_frame,
     NULL, //encode_end,
-    .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},
+    .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},
 };
 #endif // CONFIG_PNG_ENCODER
-#endif