~ubuntu-branches/debian/experimental/libav/experimental

« back to all changes in this revision

Viewing changes to libavcodec/pngenc.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2014-08-10 09:45:02 UTC
  • mfrom: (1.3.14)
  • mto: (1.3.16)
  • mto: This revision was merged to the branch mainline in revision 41.
  • Revision ID: package-import@ubuntu.com-20140810094502-mmdupdml8tixclg2
Tags: upstream-11~alpha1
ImportĀ upstreamĀ versionĀ 11~alpha1

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * License along with Libav; if not, write to the Free Software
19
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
20
 */
 
21
 
21
22
#include "avcodec.h"
22
23
#include "bytestream.h"
23
 
#include "dsputil.h"
 
24
#include "huffyuvencdsp.h"
24
25
#include "png.h"
25
26
 
26
27
/* TODO:
32
33
#define IOBUF_SIZE 4096
33
34
 
34
35
typedef struct PNGEncContext {
35
 
    DSPContext dsp;
 
36
    HuffYUVEncDSPContext hdsp;
36
37
 
37
38
    uint8_t *bytestream;
38
39
    uint8_t *bytestream_start;
53
54
    const uint8_t *s;
54
55
 
55
56
    mask = ff_png_pass_mask[pass];
56
 
    switch(bits_per_pixel) {
 
57
    switch (bits_per_pixel) {
57
58
    case 1:
58
59
        memset(dst, 0, row_size);
59
60
        dst_x = 0;
60
 
        for(x = 0; x < width; x++) {
 
61
        for (x = 0; x < width; x++) {
61
62
            j = (x & 7);
62
63
            if ((mask << j) & 0x80) {
63
64
                b = (src[x >> 3] >> (7 - j)) & 1;
70
71
        bpp = bits_per_pixel >> 3;
71
72
        d = dst;
72
73
        s = src;
73
 
        for(x = 0; x < width; x++) {
 
74
        for (x = 0; x < width; x++) {
74
75
            j = x & 7;
75
76
            if ((mask << j) & 0x80) {
76
77
                memcpy(d, s, bpp);
82
83
    }
83
84
}
84
85
 
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,
 
87
                                     int w, int bpp)
86
88
{
87
89
    int i;
88
 
    for(i = 0; i < w; i++) {
 
90
    for (i = 0; i < w; i++) {
89
91
        int a, b, c, p, pa, pb, pc;
90
92
 
91
93
        a = src[i - bpp];
92
94
        b = top[i];
93
95
        c = top[i - bpp];
94
96
 
95
 
        p = b - c;
 
97
        p  = b - c;
96
98
        pc = a - c;
97
99
 
98
100
        pa = abs(p);
109
111
    }
110
112
}
111
113
 
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)
114
116
{
115
117
    int i;
116
118
 
117
 
    switch(filter_type) {
 
119
    switch (filter_type) {
118
120
    case PNG_FILTER_VALUE_NONE:
119
121
        memcpy(dst, src, size);
120
122
        break;
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);
124
126
        break;
125
127
    case PNG_FILTER_VALUE_UP:
126
 
        dsp->diff_bytes(dst, src, top, size);
 
128
        c->hdsp.diff_bytes(dst, src, top, size);
127
129
        break;
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);
131
 
        for(; i < size; i++)
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);
133
135
        break;
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);
138
140
        break;
139
141
    }
140
142
}
144
146
{
145
147
    int pred = s->filter_type;
146
148
    assert(bpp || !pred);
147
 
    if(!top && pred)
 
149
    if (!top && pred)
148
150
        pred = PNG_FILTER_VALUE_SUB;
149
 
    if(pred == PNG_FILTER_VALUE_MIXED) {
 
151
    if (pred == PNG_FILTER_VALUE_MIXED) {
150
152
        int i;
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);
155
157
            buf1[0] = pred;
156
158
            cost = 0;
157
 
            for(i=0; i<=size; i++)
158
 
                cost += abs((int8_t)buf1[i]);
159
 
            if(cost < bcost) {
 
159
            for (i = 0; i <= size; i++)
 
160
                cost += abs((int8_t) buf1[i]);
 
161
            if (cost < bcost) {
160
162
                bcost = cost;
161
 
                FFSWAP(uint8_t*, buf1, buf2);
 
163
                FFSWAP(uint8_t *, buf1, buf2);
162
164
            }
163
165
        }
164
166
        return buf2;
165
167
    } else {
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);
167
169
        dst[0] = pred;
168
170
        return dst;
169
171
    }
176
178
    unsigned int v;
177
179
 
178
180
    d = dst;
179
 
    for(j = 0; j < width; j++) {
180
 
        v = ((const uint32_t *)src)[j];
 
181
    for (j = 0; j < width; j++) {
 
182
        v    = ((const uint32_t *) src)[j];
181
183
        d[0] = v >> 16;
182
184
        d[1] = v >> 8;
183
185
        d[2] = v;
184
186
        d[3] = v >> 24;
185
 
        d += 4;
 
187
        d   += 4;
186
188
    }
187
189
}
188
190
 
211
213
    int ret;
212
214
 
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);
217
219
        if (ret != Z_OK)
218
220
            return -1;
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;
224
227
        }
225
228
    }
226
229
    return 0;
229
232
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
230
233
                        const AVFrame *pict, int *got_packet)
231
234
{
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;
237
 
    uint8_t *ptr, *top;
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;
242
245
 
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:
 
249
        bit_depth = 16;
 
250
        color_type = PNG_COLOR_TYPE_RGB_ALPHA;
 
251
        break;
 
252
    case AV_PIX_FMT_RGB48BE:
 
253
        bit_depth = 16;
 
254
        color_type = PNG_COLOR_TYPE_RGB;
 
255
        break;
245
256
    case AV_PIX_FMT_RGB32:
246
 
        bit_depth = 8;
 
257
        bit_depth  = 8;
247
258
        color_type = PNG_COLOR_TYPE_RGB_ALPHA;
248
259
        break;
249
260
    case AV_PIX_FMT_RGB24:
250
 
        bit_depth = 8;
 
261
        bit_depth  = 8;
251
262
        color_type = PNG_COLOR_TYPE_RGB;
252
263
        break;
253
264
    case AV_PIX_FMT_GRAY16BE:
254
 
        bit_depth = 16;
 
265
        bit_depth  = 16;
255
266
        color_type = PNG_COLOR_TYPE_GRAY;
256
267
        break;
257
268
    case AV_PIX_FMT_GRAY8:
258
 
        bit_depth = 8;
 
269
        bit_depth  = 8;
259
270
        color_type = PNG_COLOR_TYPE_GRAY;
260
271
        break;
261
272
    case AV_PIX_FMT_MONOBLACK:
262
 
        bit_depth = 1;
 
273
        bit_depth  = 1;
263
274
        color_type = PNG_COLOR_TYPE_GRAY;
264
275
        break;
265
276
    case AV_PIX_FMT_PAL8:
266
 
        bit_depth = 8;
 
277
        bit_depth  = 8;
267
278
        color_type = PNG_COLOR_TYPE_PALETTE;
268
279
        break;
269
280
    default:
270
281
        return -1;
271
282
    }
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;
274
285
 
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);
283
294
    if (ret != Z_OK)
301
312
    crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
302
313
    if (!crow_base)
303
314
        goto fail;
304
 
    crow_buf = crow_base + 15; // pixel data should be aligned, but there's a control byte before it
 
315
    // pixel data should be aligned, but there's a control byte before it
 
316
    crow_buf = crow_base + 15;
305
317
    if (is_progressive) {
306
318
        progressive_buf = av_malloc(row_size + 1);
307
319
        if (!progressive_buf)
324
336
 
325
337
    AV_WB32(s->buf, avctx->width);
326
338
    AV_WB32(s->buf + 4, avctx->height);
327
 
    s->buf[8] = bit_depth;
328
 
    s->buf[9] = color_type;
 
339
    s->buf[8]  = bit_depth;
 
340
    s->buf[9]  = color_type;
329
341
    s->buf[10] = 0; /* compression type */
330
342
    s->buf[11] = 0; /* filter type */
331
343
    s->buf[12] = is_progressive; /* interlace type */
339
351
        uint32_t *palette;
340
352
        uint8_t *alpha_ptr;
341
353
 
342
 
        palette = (uint32_t *)p->data[1];
343
 
        ptr = s->buf;
 
354
        palette   = (uint32_t *)p->data[1];
 
355
        ptr       = s->buf;
344
356
        alpha_ptr = s->buf + 256 * 3;
345
357
        has_alpha = 0;
346
 
        for(i = 0; i < 256; i++) {
347
 
            v = palette[i];
 
358
        for (i = 0; i < 256; i++) {
 
359
            v     = palette[i];
348
360
            alpha = v >> 24;
349
361
            if (alpha && alpha != 0xff)
350
362
                has_alpha = 1;
351
363
            *alpha_ptr++ = alpha;
352
364
            bytestream_put_be24(&ptr, v);
353
365
        }
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);
355
368
        if (has_alpha) {
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);
357
371
        }
358
372
    }
359
373
 
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) {
364
378
        int pass;
365
379
 
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
368
 
               output */
 
382
             * output */
369
383
            pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
370
384
            if (pass_row_size > 0) {
371
385
                top = NULL;
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);
378
392
                            ptr = rgba_buf;
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;
386
401
                    }
387
 
                }
388
402
            }
389
403
        }
390
404
    } else {
391
405
        top = NULL;
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);
397
411
                ptr = rgba_buf;
398
412
            }
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);
401
416
            top = ptr;
402
417
        }
403
418
    }
404
419
    /* compress last bytes */
405
 
    for(;;) {
 
420
    for (;;) {
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;
410
425
                png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
411
426
            }
412
427
            s->zstream.avail_out = IOBUF_SIZE;
413
 
            s->zstream.next_out = s->buf;
 
428
            s->zstream.next_out  = s->buf;
414
429
            if (ret == Z_STREAM_END)
415
430
                break;
416
431
        } else {
424
439
    *got_packet = 1;
425
440
    ret         = 0;
426
441
 
427
 
 the_end:
 
442
the_end:
428
443
    av_free(crow_base);
429
444
    av_free(progressive_buf);
430
445
    av_free(rgba_buf);
431
446
    av_free(top_buf);
432
447
    deflateEnd(&s->zstream);
433
448
    return ret;
434
 
 fail:
 
449
fail:
435
450
    ret = -1;
436
451
    goto the_end;
437
452
}
438
453
 
439
 
static av_cold int png_enc_init(AVCodecContext *avctx){
 
454
static av_cold int png_enc_init(AVCodecContext *avctx)
 
455
{
440
456
    PNGEncContext *s = avctx->priv_data;
441
457
 
442
458
    avctx->coded_frame = av_frame_alloc();
446
462
    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
447
463
    avctx->coded_frame->key_frame = 1;
448
464
 
449
 
    ff_dsputil_init(&s->dsp, avctx);
 
465
    ff_huffyuvencdsp_init(&s->hdsp);
450
466
 
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;
454
472
 
455
473
    return 0;
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,
475
 
        AV_PIX_FMT_GRAY16BE,
 
493
        AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_GRAY16BE,
476
494
        AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE
477
495
    },
478
496
};