55
56
mask = ff_png_pass_mask[pass];
56
switch(bits_per_pixel) {
57
switch (bits_per_pixel) {
58
59
memset(dst, 0, row_size);
60
for(x = 0; x < width; x++) {
61
for (x = 0; x < width; x++) {
62
63
if ((mask << j) & 0x80) {
63
64
b = (src[x >> 3] >> (7 - j)) & 1;
85
static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
86
static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
88
for(i = 0; i < w; i++) {
90
for (i = 0; i < w; i++) {
89
91
int a, b, c, p, pa, pb, pc;
112
static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
114
static void png_filter_row(PNGEncContext *c, uint8_t *dst, int filter_type,
113
115
uint8_t *src, uint8_t *top, int size, int bpp)
117
switch(filter_type) {
119
switch (filter_type) {
118
120
case PNG_FILTER_VALUE_NONE:
119
121
memcpy(dst, src, size);
121
123
case PNG_FILTER_VALUE_SUB:
122
dsp->diff_bytes(dst, src, src-bpp, size);
124
c->hdsp.diff_bytes(dst, src, src - bpp, size);
123
125
memcpy(dst, src, bpp);
125
127
case PNG_FILTER_VALUE_UP:
126
dsp->diff_bytes(dst, src, top, size);
128
c->hdsp.diff_bytes(dst, src, top, size);
128
130
case PNG_FILTER_VALUE_AVG:
129
for(i = 0; i < bpp; i++)
131
for (i = 0; i < bpp; i++)
130
132
dst[i] = src[i] - (top[i] >> 1);
132
dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
133
for (; i < size; i++)
134
dst[i] = src[i] - ((src[i - bpp] + top[i]) >> 1);
134
136
case PNG_FILTER_VALUE_PAETH:
135
for(i = 0; i < bpp; i++)
137
for (i = 0; i < bpp; i++)
136
138
dst[i] = src[i] - top[i];
137
sub_png_paeth_prediction(dst+i, src+i, top+i, size-i, bpp);
139
sub_png_paeth_prediction(dst + i, src + i, top + i, size - i, bpp);
145
147
int pred = s->filter_type;
146
148
assert(bpp || !pred);
148
150
pred = PNG_FILTER_VALUE_SUB;
149
if(pred == PNG_FILTER_VALUE_MIXED) {
151
if (pred == PNG_FILTER_VALUE_MIXED) {
151
153
int cost, bcost = INT_MAX;
152
154
uint8_t *buf1 = dst, *buf2 = dst + size + 16;
153
for(pred=0; pred<5; pred++) {
154
png_filter_row(&s->dsp, buf1+1, pred, src, top, size, bpp);
155
for (pred = 0; pred < 5; pred++) {
156
png_filter_row(s, buf1 + 1, pred, src, top, size, bpp);
157
for(i=0; i<=size; i++)
158
cost += abs((int8_t)buf1[i]);
159
for (i = 0; i <= size; i++)
160
cost += abs((int8_t) buf1[i]);
161
FFSWAP(uint8_t*, buf1, buf2);
163
FFSWAP(uint8_t *, buf1, buf2);
166
png_filter_row(&s->dsp, dst+1, pred, src, top, size, bpp);
168
png_filter_row(s, dst + 1, pred, src, top, size, bpp);
213
215
s->zstream.avail_in = size;
214
s->zstream.next_in = (uint8_t *)data;
216
s->zstream.next_in = data;
215
217
while (s->zstream.avail_in > 0) {
216
218
ret = deflate(&s->zstream, Z_NO_FLUSH);
219
221
if (s->zstream.avail_out == 0) {
220
if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
221
png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
222
if (s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
223
png_write_chunk(&s->bytestream,
224
MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
222
225
s->zstream.avail_out = IOBUF_SIZE;
223
s->zstream.next_out = s->buf;
226
s->zstream.next_out = s->buf;
229
232
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
230
233
const AVFrame *pict, int *got_packet)
232
PNGEncContext *s = avctx->priv_data;
233
const AVFrame * const p = pict;
235
PNGEncContext *s = avctx->priv_data;
236
const AVFrame *const p = pict;
234
237
int bit_depth, color_type, y, len, row_size, ret, is_progressive;
235
238
int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
236
239
int compression_level;
238
uint8_t *crow_base = NULL, *crow_buf, *crow;
240
uint8_t *ptr, *top, *crow_buf, *crow;
241
uint8_t *crow_base = NULL;
239
242
uint8_t *progressive_buf = NULL;
240
uint8_t *rgba_buf = NULL;
241
uint8_t *top_buf = NULL;
243
uint8_t *rgba_buf = NULL;
244
uint8_t *top_buf = NULL;
243
246
is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
244
switch(avctx->pix_fmt) {
247
switch (avctx->pix_fmt) {
248
case AV_PIX_FMT_RGBA64BE:
250
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
252
case AV_PIX_FMT_RGB48BE:
254
color_type = PNG_COLOR_TYPE_RGB;
245
256
case AV_PIX_FMT_RGB32:
247
258
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
249
260
case AV_PIX_FMT_RGB24:
251
262
color_type = PNG_COLOR_TYPE_RGB;
253
264
case AV_PIX_FMT_GRAY16BE:
255
266
color_type = PNG_COLOR_TYPE_GRAY;
257
268
case AV_PIX_FMT_GRAY8:
259
270
color_type = PNG_COLOR_TYPE_GRAY;
261
272
case AV_PIX_FMT_MONOBLACK:
263
274
color_type = PNG_COLOR_TYPE_GRAY;
265
276
case AV_PIX_FMT_PAL8:
267
278
color_type = PNG_COLOR_TYPE_PALETTE;
272
283
bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
273
row_size = (avctx->width * bits_per_pixel + 7) >> 3;
284
row_size = (avctx->width * bits_per_pixel + 7) >> 3;
275
286
s->zstream.zalloc = ff_png_zalloc;
276
s->zstream.zfree = ff_png_zfree;
287
s->zstream.zfree = ff_png_zfree;
277
288
s->zstream.opaque = NULL;
278
compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
279
Z_DEFAULT_COMPRESSION :
280
av_clip(avctx->compression_level, 0, 9);
289
compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
290
? Z_DEFAULT_COMPRESSION
291
: av_clip(avctx->compression_level, 0, 9);
281
292
ret = deflateInit2(&s->zstream, compression_level,
282
293
Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
339
351
uint32_t *palette;
340
352
uint8_t *alpha_ptr;
342
palette = (uint32_t *)p->data[1];
354
palette = (uint32_t *)p->data[1];
344
356
alpha_ptr = s->buf + 256 * 3;
346
for(i = 0; i < 256; i++) {
358
for (i = 0; i < 256; i++) {
349
361
if (alpha && alpha != 0xff)
351
363
*alpha_ptr++ = alpha;
352
364
bytestream_put_be24(&ptr, v);
354
png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
366
png_write_chunk(&s->bytestream,
367
MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
356
png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
369
png_write_chunk(&s->bytestream,
370
MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
360
374
/* now put each row */
361
375
s->zstream.avail_out = IOBUF_SIZE;
362
s->zstream.next_out = s->buf;
376
s->zstream.next_out = s->buf;
363
377
if (is_progressive) {
366
for(pass = 0; pass < NB_PASSES; pass++) {
380
for (pass = 0; pass < NB_PASSES; pass++) {
367
381
/* NOTE: a pass is completely omitted if no pixels would be
369
383
pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
370
384
if (pass_row_size > 0) {
372
for(y = 0; y < avctx->height; y++) {
386
for (y = 0; y < avctx->height; y++)
373
387
if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
374
388
ptr = p->data[0] + y * p->linesize[0];
375
FFSWAP(uint8_t*, progressive_buf, top_buf);
389
FFSWAP(uint8_t *, progressive_buf, top_buf);
376
390
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
377
391
convert_from_rgb32(rgba_buf, ptr, avctx->width);
380
394
png_get_interlaced_row(progressive_buf, pass_row_size,
381
395
bits_per_pixel, pass,
382
396
ptr, avctx->width);
383
crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
397
crow = png_choose_filter(s, crow_buf, progressive_buf,
398
top, pass_row_size, bits_per_pixel >> 3);
384
399
png_write_row(s, crow, pass_row_size + 1);
385
400
top = progressive_buf;
392
for(y = 0; y < avctx->height; y++) {
406
for (y = 0; y < avctx->height; y++) {
393
407
ptr = p->data[0] + y * p->linesize[0];
394
408
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
395
FFSWAP(uint8_t*, rgba_buf, top_buf);
409
FFSWAP(uint8_t *, rgba_buf, top_buf);
396
410
convert_from_rgb32(rgba_buf, ptr, avctx->width);
399
crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
413
crow = png_choose_filter(s, crow_buf, ptr, top,
414
row_size, bits_per_pixel >> 3);
400
415
png_write_row(s, crow, row_size + 1);
404
419
/* compress last bytes */
406
421
ret = deflate(&s->zstream, Z_FINISH);
407
422
if (ret == Z_OK || ret == Z_STREAM_END) {
408
423
len = IOBUF_SIZE - s->zstream.avail_out;
446
462
avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
447
463
avctx->coded_frame->key_frame = 1;
449
ff_dsputil_init(&s->dsp, avctx);
465
ff_huffyuvencdsp_init(&s->hdsp);
451
s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
452
if(avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
467
s->filter_type = av_clip(avctx->prediction_method,
468
PNG_FILTER_VALUE_NONE,
469
PNG_FILTER_VALUE_MIXED);
470
if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
453
471
s->filter_type = PNG_FILTER_VALUE_NONE;
470
488
.init = png_enc_init,
471
489
.close = png_enc_close,
472
490
.encode2 = encode_frame,
473
.pix_fmts = (const enum AVPixelFormat[]){
491
.pix_fmts = (const enum AVPixelFormat[]) {
474
492
AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
493
AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_GRAY16BE,
476
494
AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE