Statistics
| Branch: | Revision:

ffmpeg / libavcodec / pngenc.c @ b958a7f1

History | View | Annotate | Download (9.16 KB)

1
/*
2
 * PNG image format
3
 * Copyright (c) 2003 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avcodec.h"
22
#include "bytestream.h"
23
#include "png.h"
24

    
25
/* TODO:
26
 * - add 2, 4 and 16 bit depth support
27
 * - use filters when generating a png (better compression)
28
 */
29

    
30
#include <zlib.h>
31

    
32
//#define DEBUG
33

    
34
static void png_get_interlaced_row(uint8_t *dst, int row_size,
35
                                   int bits_per_pixel, int pass,
36
                                   const uint8_t *src, int width)
37
{
38
    int x, mask, dst_x, j, b, bpp;
39
    uint8_t *d;
40
    const uint8_t *s;
41

    
42
    mask = ff_png_pass_mask[pass];
43
    switch(bits_per_pixel) {
44
    case 1:
45
        memset(dst, 0, row_size);
46
        dst_x = 0;
47
        for(x = 0; x < width; x++) {
48
            j = (x & 7);
49
            if ((mask << j) & 0x80) {
50
                b = (src[x >> 3] >> (7 - j)) & 1;
51
                dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
52
                dst_x++;
53
            }
54
        }
55
        break;
56
    default:
57
        bpp = bits_per_pixel >> 3;
58
        d = dst;
59
        s = src;
60
        for(x = 0; x < width; x++) {
61
            j = x & 7;
62
            if ((mask << j) & 0x80) {
63
                memcpy(d, s, bpp);
64
                d += bpp;
65
            }
66
            s += bpp;
67
        }
68
        break;
69
    }
70
}
71

    
72
static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
73
{
74
    uint8_t *d;
75
    int j;
76
    unsigned int v;
77

    
78
    d = dst;
79
    for(j = 0; j < width; j++) {
80
        v = ((const uint32_t *)src)[j];
81
        d[0] = v >> 16;
82
        d[1] = v >> 8;
83
        d[2] = v;
84
        d[3] = v >> 24;
85
        d += 4;
86
    }
87
}
88

    
89
static void png_write_chunk(uint8_t **f, uint32_t tag,
90
                            const uint8_t *buf, int length)
91
{
92
    uint32_t crc;
93
    uint8_t tagbuf[4];
94

    
95
    bytestream_put_be32(f, length);
96
    crc = crc32(0, Z_NULL, 0);
97
    AV_WL32(tagbuf, tag);
98
    crc = crc32(crc, tagbuf, 4);
99
    bytestream_put_be32(f, bswap_32(tag));
100
    if (length > 0) {
101
        crc = crc32(crc, buf, length);
102
        memcpy(*f, buf, length);
103
        *f += length;
104
    }
105
    bytestream_put_be32(f, crc);
106
}
107

    
108
/* XXX: do filtering */
109
static int png_write_row(PNGContext *s, const uint8_t *data, int size)
110
{
111
    int ret;
112

    
113
    s->zstream.avail_in = size;
114
    s->zstream.next_in = (uint8_t *)data;
115
    while (s->zstream.avail_in > 0) {
116
        ret = deflate(&s->zstream, Z_NO_FLUSH);
117
        if (ret != Z_OK)
118
            return -1;
119
        if (s->zstream.avail_out == 0) {
120
            if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
121
                png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
122
            s->zstream.avail_out = IOBUF_SIZE;
123
            s->zstream.next_out = s->buf;
124
        }
125
    }
126
    return 0;
127
}
128

    
129
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
130
    PNGContext *s = avctx->priv_data;
131
    AVFrame *pict = data;
132
    AVFrame * const p= (AVFrame*)&s->picture;
133
    int bit_depth, color_type, y, len, row_size, ret, is_progressive;
134
    int bits_per_pixel, pass_row_size;
135
    uint8_t *ptr;
136
    uint8_t *crow_buf = NULL;
137
    uint8_t *tmp_buf = NULL;
138

    
139
    *p = *pict;
140
    p->pict_type= FF_I_TYPE;
141
    p->key_frame= 1;
142

    
143
    s->bytestream_start=
144
    s->bytestream= buf;
145
    s->bytestream_end= buf+buf_size;
146

    
147
    is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
148
    switch(avctx->pix_fmt) {
149
    case PIX_FMT_RGB32:
150
        bit_depth = 8;
151
        color_type = PNG_COLOR_TYPE_RGB_ALPHA;
152
        break;
153
    case PIX_FMT_RGB24:
154
        bit_depth = 8;
155
        color_type = PNG_COLOR_TYPE_RGB;
156
        break;
157
    case PIX_FMT_GRAY8:
158
        bit_depth = 8;
159
        color_type = PNG_COLOR_TYPE_GRAY;
160
        break;
161
    case PIX_FMT_MONOBLACK:
162
        bit_depth = 1;
163
        color_type = PNG_COLOR_TYPE_GRAY;
164
        break;
165
    case PIX_FMT_PAL8:
166
        bit_depth = 8;
167
        color_type = PNG_COLOR_TYPE_PALETTE;
168
        break;
169
    default:
170
        return -1;
171
    }
172
    bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
173
    row_size = (avctx->width * bits_per_pixel + 7) >> 3;
174

    
175
    s->zstream.zalloc = ff_png_zalloc;
176
    s->zstream.zfree = ff_png_zfree;
177
    s->zstream.opaque = NULL;
178
    ret = deflateInit2(&s->zstream, Z_DEFAULT_COMPRESSION,
179
                       Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
180
    if (ret != Z_OK)
181
        return -1;
182
    crow_buf = av_malloc(row_size + 1);
183
    if (!crow_buf)
184
        goto fail;
185
    if (is_progressive) {
186
        tmp_buf = av_malloc(row_size + 1);
187
        if (!tmp_buf)
188
            goto fail;
189
    }
190

    
191
    /* write png header */
192
    memcpy(s->bytestream, ff_pngsig, 8);
193
    s->bytestream += 8;
194

    
195
    AV_WB32(s->buf, avctx->width);
196
    AV_WB32(s->buf + 4, avctx->height);
197
    s->buf[8] = bit_depth;
198
    s->buf[9] = color_type;
199
    s->buf[10] = 0; /* compression type */
200
    s->buf[11] = 0; /* filter type */
201
    s->buf[12] = is_progressive; /* interlace type */
202

    
203
    png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
204

    
205
    /* put the palette if needed */
206
    if (color_type == PNG_COLOR_TYPE_PALETTE) {
207
        int has_alpha, alpha, i;
208
        unsigned int v;
209
        uint32_t *palette;
210
        uint8_t *alpha_ptr;
211

    
212
        palette = (uint32_t *)p->data[1];
213
        ptr = s->buf;
214
        alpha_ptr = s->buf + 256 * 3;
215
        has_alpha = 0;
216
        for(i = 0; i < 256; i++) {
217
            v = palette[i];
218
            alpha = v >> 24;
219
            if (alpha && alpha != 0xff)
220
                has_alpha = 1;
221
            *alpha_ptr++ = alpha;
222
            bytestream_put_be24(&ptr, v);
223
        }
224
        png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
225
        if (has_alpha) {
226
            png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
227
        }
228
    }
229

    
230
    /* now put each row */
231
    s->zstream.avail_out = IOBUF_SIZE;
232
    s->zstream.next_out = s->buf;
233
    if (is_progressive) {
234
        uint8_t *ptr1;
235
        int pass;
236

    
237
        for(pass = 0; pass < NB_PASSES; pass++) {
238
            /* NOTE: a pass is completely omited if no pixels would be
239
               output */
240
            pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
241
            if (pass_row_size > 0) {
242
                for(y = 0; y < avctx->height; y++) {
243
                    if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
244
                        ptr = p->data[0] + y * p->linesize[0];
245
                        if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
246
                            convert_from_rgb32(tmp_buf, ptr, avctx->width);
247
                            ptr1 = tmp_buf;
248
                        } else {
249
                            ptr1 = ptr;
250
                        }
251
                        png_get_interlaced_row(crow_buf + 1, pass_row_size,
252
                                               bits_per_pixel, pass,
253
                                               ptr1, avctx->width);
254
                        crow_buf[0] = PNG_FILTER_VALUE_NONE;
255
                        png_write_row(s, crow_buf, pass_row_size + 1);
256
                    }
257
                }
258
            }
259
        }
260
    } else {
261
        for(y = 0; y < avctx->height; y++) {
262
            ptr = p->data[0] + y * p->linesize[0];
263
            if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
264
                convert_from_rgb32(crow_buf + 1, ptr, avctx->width);
265
            else
266
                memcpy(crow_buf + 1, ptr, row_size);
267
            crow_buf[0] = PNG_FILTER_VALUE_NONE;
268
            png_write_row(s, crow_buf, row_size + 1);
269
        }
270
    }
271
    /* compress last bytes */
272
    for(;;) {
273
        ret = deflate(&s->zstream, Z_FINISH);
274
        if (ret == Z_OK || ret == Z_STREAM_END) {
275
            len = IOBUF_SIZE - s->zstream.avail_out;
276
            if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
277
                png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
278
            }
279
            s->zstream.avail_out = IOBUF_SIZE;
280
            s->zstream.next_out = s->buf;
281
            if (ret == Z_STREAM_END)
282
                break;
283
        } else {
284
            goto fail;
285
        }
286
    }
287
    png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
288

    
289
    ret = s->bytestream - s->bytestream_start;
290
 the_end:
291
    av_free(crow_buf);
292
    av_free(tmp_buf);
293
    deflateEnd(&s->zstream);
294
    return ret;
295
 fail:
296
    ret = -1;
297
    goto the_end;
298
}
299

    
300
AVCodec png_encoder = {
301
    "png",
302
    CODEC_TYPE_VIDEO,
303
    CODEC_ID_PNG,
304
    sizeof(PNGContext),
305
    ff_png_common_init,
306
    encode_frame,
307
    NULL, //encode_end,
308
    .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},
309
};