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 = (int[]){0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}[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 png_write_chunk(uint8_t **f, uint32_t tag,
00176 const uint8_t *buf, int length)
00177 {
00178 uint32_t crc;
00179 uint8_t tagbuf[4];
00180
00181 bytestream_put_be32(f, length);
00182 crc = crc32(0, Z_NULL, 0);
00183 AV_WL32(tagbuf, tag);
00184 crc = crc32(crc, tagbuf, 4);
00185 bytestream_put_be32(f, av_bswap32(tag));
00186 if (length > 0) {
00187 crc = crc32(crc, buf, length);
00188 memcpy(*f, buf, length);
00189 *f += length;
00190 }
00191 bytestream_put_be32(f, crc);
00192 }
00193
00194
00195 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
00196 {
00197 int ret;
00198
00199 s->zstream.avail_in = size;
00200 s->zstream.next_in = (uint8_t *)data;
00201 while (s->zstream.avail_in > 0) {
00202 ret = deflate(&s->zstream, Z_NO_FLUSH);
00203 if (ret != Z_OK)
00204 return -1;
00205 if (s->zstream.avail_out == 0) {
00206 if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
00207 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
00208 s->zstream.avail_out = IOBUF_SIZE;
00209 s->zstream.next_out = s->buf;
00210 }
00211 }
00212 return 0;
00213 }
00214
00215 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00216 PNGEncContext *s = avctx->priv_data;
00217 AVFrame *pict = data;
00218 AVFrame * const p= &s->picture;
00219 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
00220 int bits_per_pixel, pass_row_size;
00221 int compression_level;
00222 uint8_t *ptr, *top;
00223 uint8_t *crow_base = NULL, *crow_buf, *crow;
00224 uint8_t *progressive_buf = NULL;
00225 uint8_t *top_buf = NULL;
00226
00227 *p = *pict;
00228 p->pict_type= AV_PICTURE_TYPE_I;
00229 p->key_frame= 1;
00230
00231 s->bytestream_start=
00232 s->bytestream= buf;
00233 s->bytestream_end= buf+buf_size;
00234
00235 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
00236 switch(avctx->pix_fmt) {
00237 case PIX_FMT_RGBA64BE:
00238 bit_depth = 16;
00239 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
00240 break;
00241 case PIX_FMT_RGB48BE:
00242 bit_depth = 16;
00243 color_type = PNG_COLOR_TYPE_RGB;
00244 break;
00245 case PIX_FMT_RGBA:
00246 bit_depth = 8;
00247 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
00248 break;
00249 case PIX_FMT_RGB24:
00250 bit_depth = 8;
00251 color_type = PNG_COLOR_TYPE_RGB;
00252 break;
00253 case PIX_FMT_GRAY16BE:
00254 bit_depth = 16;
00255 color_type = PNG_COLOR_TYPE_GRAY;
00256 break;
00257 case PIX_FMT_GRAY8:
00258 bit_depth = 8;
00259 color_type = PNG_COLOR_TYPE_GRAY;
00260 break;
00261 case PIX_FMT_GRAY8A:
00262 bit_depth = 8;
00263 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
00264 break;
00265 case PIX_FMT_MONOBLACK:
00266 bit_depth = 1;
00267 color_type = PNG_COLOR_TYPE_GRAY;
00268 break;
00269 case PIX_FMT_PAL8:
00270 bit_depth = 8;
00271 color_type = PNG_COLOR_TYPE_PALETTE;
00272 break;
00273 default:
00274 return -1;
00275 }
00276 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
00277 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
00278
00279 s->zstream.zalloc = ff_png_zalloc;
00280 s->zstream.zfree = ff_png_zfree;
00281 s->zstream.opaque = NULL;
00282 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
00283 Z_DEFAULT_COMPRESSION :
00284 av_clip(avctx->compression_level, 0, 9);
00285 ret = deflateInit2(&s->zstream, compression_level,
00286 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
00287 if (ret != Z_OK)
00288 return -1;
00289 crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
00290 if (!crow_base)
00291 goto fail;
00292 crow_buf = crow_base + 15;
00293 if (is_progressive) {
00294 progressive_buf = av_malloc(row_size + 1);
00295 if (!progressive_buf)
00296 goto fail;
00297 }
00298 if (is_progressive) {
00299 top_buf = av_malloc(row_size + 1);
00300 if (!top_buf)
00301 goto fail;
00302 }
00303
00304
00305 memcpy(s->bytestream, ff_pngsig, 8);
00306 s->bytestream += 8;
00307
00308 AV_WB32(s->buf, avctx->width);
00309 AV_WB32(s->buf + 4, avctx->height);
00310 s->buf[8] = bit_depth;
00311 s->buf[9] = color_type;
00312 s->buf[10] = 0;
00313 s->buf[11] = 0;
00314 s->buf[12] = is_progressive;
00315
00316 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
00317
00318
00319 if (color_type == PNG_COLOR_TYPE_PALETTE) {
00320 int has_alpha, alpha, i;
00321 unsigned int v;
00322 uint32_t *palette;
00323 uint8_t *alpha_ptr;
00324
00325 palette = (uint32_t *)p->data[1];
00326 ptr = s->buf;
00327 alpha_ptr = s->buf + 256 * 3;
00328 has_alpha = 0;
00329 for(i = 0; i < 256; i++) {
00330 v = palette[i];
00331 alpha = v >> 24;
00332 if (alpha != 0xff)
00333 has_alpha = 1;
00334 *alpha_ptr++ = alpha;
00335 bytestream_put_be24(&ptr, v);
00336 }
00337 png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
00338 if (has_alpha) {
00339 png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
00340 }
00341 }
00342
00343
00344 s->zstream.avail_out = IOBUF_SIZE;
00345 s->zstream.next_out = s->buf;
00346 if (is_progressive) {
00347 int pass;
00348
00349 for(pass = 0; pass < NB_PASSES; pass++) {
00350
00351
00352 pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
00353 if (pass_row_size > 0) {
00354 top = NULL;
00355 for(y = 0; y < avctx->height; y++) {
00356 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
00357 ptr = p->data[0] + y * p->linesize[0];
00358 FFSWAP(uint8_t*, progressive_buf, top_buf);
00359 png_get_interlaced_row(progressive_buf, pass_row_size,
00360 bits_per_pixel, pass,
00361 ptr, avctx->width);
00362 crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
00363 png_write_row(s, crow, pass_row_size + 1);
00364 top = progressive_buf;
00365 }
00366 }
00367 }
00368 }
00369 } else {
00370 top = NULL;
00371 for(y = 0; y < avctx->height; y++) {
00372 ptr = p->data[0] + y * p->linesize[0];
00373 crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
00374 png_write_row(s, crow, row_size + 1);
00375 top = ptr;
00376 }
00377 }
00378
00379 for(;;) {
00380 ret = deflate(&s->zstream, Z_FINISH);
00381 if (ret == Z_OK || ret == Z_STREAM_END) {
00382 len = IOBUF_SIZE - s->zstream.avail_out;
00383 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
00384 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
00385 }
00386 s->zstream.avail_out = IOBUF_SIZE;
00387 s->zstream.next_out = s->buf;
00388 if (ret == Z_STREAM_END)
00389 break;
00390 } else {
00391 goto fail;
00392 }
00393 }
00394 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
00395
00396 ret = s->bytestream - s->bytestream_start;
00397 the_end:
00398 av_free(crow_base);
00399 av_free(progressive_buf);
00400 av_free(top_buf);
00401 deflateEnd(&s->zstream);
00402 return ret;
00403 fail:
00404 ret = -1;
00405 goto the_end;
00406 }
00407
00408 static av_cold int png_enc_init(AVCodecContext *avctx){
00409 PNGEncContext *s = avctx->priv_data;
00410
00411 avcodec_get_frame_defaults(&s->picture);
00412 avctx->coded_frame= &s->picture;
00413 dsputil_init(&s->dsp, avctx);
00414
00415 s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
00416 if(avctx->pix_fmt == PIX_FMT_MONOBLACK)
00417 s->filter_type = PNG_FILTER_VALUE_NONE;
00418
00419 return 0;
00420 }
00421
00422 AVCodec ff_png_encoder = {
00423 .name = "png",
00424 .type = AVMEDIA_TYPE_VIDEO,
00425 .id = CODEC_ID_PNG,
00426 .priv_data_size = sizeof(PNGEncContext),
00427 .init = png_enc_init,
00428 .encode = encode_frame,
00429 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA,
00430 PIX_FMT_RGB48BE, PIX_FMT_RGBA64BE,
00431 PIX_FMT_PAL8,
00432 PIX_FMT_GRAY8, PIX_FMT_GRAY8A,
00433 PIX_FMT_GRAY16BE,
00434 PIX_FMT_MONOBLACK, PIX_FMT_NONE},
00435 .long_name= NULL_IF_CONFIG_SMALL("PNG image"),
00436 };