00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022 #include "bytestream.h"
00023 #include "dsputil.h"
00024 #include "png.h"
00025
00026
00027
00028
00029
00030 #include <zlib.h>
00031
00032
00033
00034 #define IOBUF_SIZE 4096
00035
00036 typedef struct PNGEncContext {
00037 DSPContext dsp;
00038
00039 uint8_t *bytestream;
00040 uint8_t *bytestream_start;
00041 uint8_t *bytestream_end;
00042 AVFrame picture;
00043
00044 int filter_type;
00045
00046 z_stream zstream;
00047 uint8_t buf[IOBUF_SIZE];
00048 } PNGEncContext;
00049
00050 static void png_get_interlaced_row(uint8_t *dst, int row_size,
00051 int bits_per_pixel, int pass,
00052 const uint8_t *src, int width)
00053 {
00054 int x, mask, dst_x, j, b, bpp;
00055 uint8_t *d;
00056 const uint8_t *s;
00057
00058 mask = ff_png_pass_mask[pass];
00059 switch(bits_per_pixel) {
00060 case 1:
00061 memset(dst, 0, row_size);
00062 dst_x = 0;
00063 for(x = 0; x < width; x++) {
00064 j = (x & 7);
00065 if ((mask << j) & 0x80) {
00066 b = (src[x >> 3] >> (7 - j)) & 1;
00067 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
00068 dst_x++;
00069 }
00070 }
00071 break;
00072 default:
00073 bpp = bits_per_pixel >> 3;
00074 d = dst;
00075 s = src;
00076 for(x = 0; x < width; x++) {
00077 j = x & 7;
00078 if ((mask << j) & 0x80) {
00079 memcpy(d, s, bpp);
00080 d += bpp;
00081 }
00082 s += bpp;
00083 }
00084 break;
00085 }
00086 }
00087
00088 static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
00089 {
00090 int i;
00091 for(i = 0; i < w; i++) {
00092 int a, b, c, p, pa, pb, pc;
00093
00094 a = src[i - bpp];
00095 b = top[i];
00096 c = top[i - bpp];
00097
00098 p = b - c;
00099 pc = a - c;
00100
00101 pa = abs(p);
00102 pb = abs(pc);
00103 pc = abs(p + pc);
00104
00105 if (pa <= pb && pa <= pc)
00106 p = a;
00107 else if (pb <= pc)
00108 p = b;
00109 else
00110 p = c;
00111 dst[i] = src[i] - p;
00112 }
00113 }
00114
00115 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
00116 uint8_t *src, uint8_t *top, int size, int bpp)
00117 {
00118 int i;
00119
00120 switch(filter_type) {
00121 case PNG_FILTER_VALUE_NONE:
00122 memcpy(dst, src, size);
00123 break;
00124 case PNG_FILTER_VALUE_SUB:
00125 dsp->diff_bytes(dst, src, src-bpp, size);
00126 memcpy(dst, src, bpp);
00127 break;
00128 case PNG_FILTER_VALUE_UP:
00129 dsp->diff_bytes(dst, src, top, size);
00130 break;
00131 case PNG_FILTER_VALUE_AVG:
00132 for(i = 0; i < bpp; i++)
00133 dst[i] = src[i] - (top[i] >> 1);
00134 for(; i < size; i++)
00135 dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
00136 break;
00137 case PNG_FILTER_VALUE_PAETH:
00138 for(i = 0; i < bpp; i++)
00139 dst[i] = src[i] - top[i];
00140 sub_png_paeth_prediction(dst+i, src+i, top+i, size-i, bpp);
00141 break;
00142 }
00143 }
00144
00145 static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
00146 uint8_t *src, uint8_t *top, int size, int bpp)
00147 {
00148 int pred = s->filter_type;
00149 assert(bpp || !pred);
00150 if(!top && pred)
00151 pred = PNG_FILTER_VALUE_SUB;
00152 if(pred == PNG_FILTER_VALUE_MIXED) {
00153 int i;
00154 int cost, bcost = INT_MAX;
00155 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
00156 for(pred=0; pred<5; pred++) {
00157 png_filter_row(&s->dsp, buf1+1, pred, src, top, size, bpp);
00158 buf1[0] = pred;
00159 cost = 0;
00160 for(i=0; i<=size; i++)
00161 cost += abs((int8_t)buf1[i]);
00162 if(cost < bcost) {
00163 bcost = cost;
00164 FFSWAP(uint8_t*, buf1, buf2);
00165 }
00166 }
00167 return buf2;
00168 } else {
00169 png_filter_row(&s->dsp, dst+1, pred, src, top, size, bpp);
00170 dst[0] = pred;
00171 return dst;
00172 }
00173 }
00174
00175 static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
00176 {
00177 uint8_t *d;
00178 int j;
00179 unsigned int v;
00180
00181 d = dst;
00182 for(j = 0; j < width; j++) {
00183 v = ((const uint32_t *)src)[j];
00184 d[0] = v >> 16;
00185 d[1] = v >> 8;
00186 d[2] = v;
00187 d[3] = v >> 24;
00188 d += 4;
00189 }
00190 }
00191
00192 static void png_write_chunk(uint8_t **f, uint32_t tag,
00193 const uint8_t *buf, int length)
00194 {
00195 uint32_t crc;
00196 uint8_t tagbuf[4];
00197
00198 bytestream_put_be32(f, length);
00199 crc = crc32(0, Z_NULL, 0);
00200 AV_WL32(tagbuf, tag);
00201 crc = crc32(crc, tagbuf, 4);
00202 bytestream_put_be32(f, av_bswap32(tag));
00203 if (length > 0) {
00204 crc = crc32(crc, buf, length);
00205 memcpy(*f, buf, length);
00206 *f += length;
00207 }
00208 bytestream_put_be32(f, crc);
00209 }
00210
00211
00212 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
00213 {
00214 int ret;
00215
00216 s->zstream.avail_in = size;
00217 s->zstream.next_in = (uint8_t *)data;
00218 while (s->zstream.avail_in > 0) {
00219 ret = deflate(&s->zstream, Z_NO_FLUSH);
00220 if (ret != Z_OK)
00221 return -1;
00222 if (s->zstream.avail_out == 0) {
00223 if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
00224 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
00225 s->zstream.avail_out = IOBUF_SIZE;
00226 s->zstream.next_out = s->buf;
00227 }
00228 }
00229 return 0;
00230 }
00231
00232 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00233 PNGEncContext *s = avctx->priv_data;
00234 AVFrame *pict = data;
00235 AVFrame * const p= &s->picture;
00236 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
00237 int bits_per_pixel, pass_row_size;
00238 int compression_level;
00239 uint8_t *ptr, *top;
00240 uint8_t *crow_base = NULL, *crow_buf, *crow;
00241 uint8_t *progressive_buf = NULL;
00242 uint8_t *rgba_buf = NULL;
00243 uint8_t *top_buf = NULL;
00244
00245 *p = *pict;
00246 p->pict_type= AV_PICTURE_TYPE_I;
00247 p->key_frame= 1;
00248
00249 s->bytestream_start=
00250 s->bytestream= buf;
00251 s->bytestream_end= buf+buf_size;
00252
00253 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
00254 switch(avctx->pix_fmt) {
00255 case PIX_FMT_RGB32:
00256 bit_depth = 8;
00257 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
00258 break;
00259 case PIX_FMT_RGB24:
00260 bit_depth = 8;
00261 color_type = PNG_COLOR_TYPE_RGB;
00262 break;
00263 case PIX_FMT_GRAY8:
00264 bit_depth = 8;
00265 color_type = PNG_COLOR_TYPE_GRAY;
00266 break;
00267 case PIX_FMT_MONOBLACK:
00268 bit_depth = 1;
00269 color_type = PNG_COLOR_TYPE_GRAY;
00270 break;
00271 case PIX_FMT_PAL8:
00272 bit_depth = 8;
00273 color_type = PNG_COLOR_TYPE_PALETTE;
00274 break;
00275 default:
00276 return -1;
00277 }
00278 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
00279 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
00280
00281 s->zstream.zalloc = ff_png_zalloc;
00282 s->zstream.zfree = ff_png_zfree;
00283 s->zstream.opaque = NULL;
00284 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
00285 Z_DEFAULT_COMPRESSION :
00286 av_clip(avctx->compression_level, 0, 9);
00287 ret = deflateInit2(&s->zstream, compression_level,
00288 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
00289 if (ret != Z_OK)
00290 return -1;
00291 crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
00292 if (!crow_base)
00293 goto fail;
00294 crow_buf = crow_base + 15;
00295 if (is_progressive) {
00296 progressive_buf = av_malloc(row_size + 1);
00297 if (!progressive_buf)
00298 goto fail;
00299 }
00300 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00301 rgba_buf = av_malloc(row_size + 1);
00302 if (!rgba_buf)
00303 goto fail;
00304 }
00305 if (is_progressive || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00306 top_buf = av_malloc(row_size + 1);
00307 if (!top_buf)
00308 goto fail;
00309 }
00310
00311
00312 memcpy(s->bytestream, ff_pngsig, 8);
00313 s->bytestream += 8;
00314
00315 AV_WB32(s->buf, avctx->width);
00316 AV_WB32(s->buf + 4, avctx->height);
00317 s->buf[8] = bit_depth;
00318 s->buf[9] = color_type;
00319 s->buf[10] = 0;
00320 s->buf[11] = 0;
00321 s->buf[12] = is_progressive;
00322
00323 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
00324
00325
00326 if (color_type == PNG_COLOR_TYPE_PALETTE) {
00327 int has_alpha, alpha, i;
00328 unsigned int v;
00329 uint32_t *palette;
00330 uint8_t *alpha_ptr;
00331
00332 palette = (uint32_t *)p->data[1];
00333 ptr = s->buf;
00334 alpha_ptr = s->buf + 256 * 3;
00335 has_alpha = 0;
00336 for(i = 0; i < 256; i++) {
00337 v = palette[i];
00338 alpha = v >> 24;
00339 if (alpha != 0xff)
00340 has_alpha = 1;
00341 *alpha_ptr++ = alpha;
00342 bytestream_put_be24(&ptr, v);
00343 }
00344 png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
00345 if (has_alpha) {
00346 png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
00347 }
00348 }
00349
00350
00351 s->zstream.avail_out = IOBUF_SIZE;
00352 s->zstream.next_out = s->buf;
00353 if (is_progressive) {
00354 int pass;
00355
00356 for(pass = 0; pass < NB_PASSES; pass++) {
00357
00358
00359 pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
00360 if (pass_row_size > 0) {
00361 top = NULL;
00362 for(y = 0; y < avctx->height; y++) {
00363 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
00364 ptr = p->data[0] + y * p->linesize[0];
00365 FFSWAP(uint8_t*, progressive_buf, top_buf);
00366 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00367 convert_from_rgb32(rgba_buf, ptr, avctx->width);
00368 ptr = rgba_buf;
00369 }
00370 png_get_interlaced_row(progressive_buf, pass_row_size,
00371 bits_per_pixel, pass,
00372 ptr, avctx->width);
00373 crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
00374 png_write_row(s, crow, pass_row_size + 1);
00375 top = progressive_buf;
00376 }
00377 }
00378 }
00379 }
00380 } else {
00381 top = NULL;
00382 for(y = 0; y < avctx->height; y++) {
00383 ptr = p->data[0] + y * p->linesize[0];
00384 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00385 FFSWAP(uint8_t*, rgba_buf, top_buf);
00386 convert_from_rgb32(rgba_buf, ptr, avctx->width);
00387 ptr = rgba_buf;
00388 }
00389 crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
00390 png_write_row(s, crow, row_size + 1);
00391 top = ptr;
00392 }
00393 }
00394
00395 for(;;) {
00396 ret = deflate(&s->zstream, Z_FINISH);
00397 if (ret == Z_OK || ret == Z_STREAM_END) {
00398 len = IOBUF_SIZE - s->zstream.avail_out;
00399 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
00400 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
00401 }
00402 s->zstream.avail_out = IOBUF_SIZE;
00403 s->zstream.next_out = s->buf;
00404 if (ret == Z_STREAM_END)
00405 break;
00406 } else {
00407 goto fail;
00408 }
00409 }
00410 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
00411
00412 ret = s->bytestream - s->bytestream_start;
00413 the_end:
00414 av_free(crow_base);
00415 av_free(progressive_buf);
00416 av_free(rgba_buf);
00417 av_free(top_buf);
00418 deflateEnd(&s->zstream);
00419 return ret;
00420 fail:
00421 ret = -1;
00422 goto the_end;
00423 }
00424
00425 static av_cold int png_enc_init(AVCodecContext *avctx){
00426 PNGEncContext *s = avctx->priv_data;
00427
00428 avcodec_get_frame_defaults(&s->picture);
00429 avctx->coded_frame= &s->picture;
00430 dsputil_init(&s->dsp, avctx);
00431
00432 s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
00433 if(avctx->pix_fmt == PIX_FMT_MONOBLACK)
00434 s->filter_type = PNG_FILTER_VALUE_NONE;
00435
00436 return 0;
00437 }
00438
00439 AVCodec ff_png_encoder = {
00440 .name = "png",
00441 .type = AVMEDIA_TYPE_VIDEO,
00442 .id = CODEC_ID_PNG,
00443 .priv_data_size = sizeof(PNGEncContext),
00444 .init = png_enc_init,
00445 .encode = encode_frame,
00446 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, PIX_FMT_NONE},
00447 .long_name= NULL_IF_CONFIG_SMALL("PNG image"),
00448 };