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