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