png: K&R formatting cosmetics

pull/293/head
Vittorio Giovara 11 years ago
parent f7518f1a0e
commit c598b569fb
  1. 63
      libavcodec/pngdec.c
  2. 3
      libavcodec/pngdsp.c
  3. 44
      libavcodec/pngenc.c

@ -73,8 +73,8 @@ static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
}; };
/* NOTE: we try to construct a good looking image at each pass. width /* NOTE: we try to construct a good looking image at each pass. width
is the original image width. We also do pixel format conversion at * is the original image width. We also do pixel format conversion at
this stage */ * this stage */
static void png_put_interlaced_row(uint8_t *dst, int width, static void png_put_interlaced_row(uint8_t *dst, int width,
int bits_per_pixel, int pass, int bits_per_pixel, int pass,
int color_type, const uint8_t *src) int color_type, const uint8_t *src)
@ -131,7 +131,8 @@ static void png_put_interlaced_row(uint8_t *dst, int width,
} }
} }
void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp) void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
int w, int bpp)
{ {
int i; int i;
for (i = 0; i < w; i++) { for (i = 0; i < w; i++) {
@ -160,30 +161,41 @@ void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w
#define UNROLL1(bpp, op) { \ #define UNROLL1(bpp, op) { \
r = dst[0]; \ r = dst[0]; \
if(bpp >= 2) g = dst[1];\ if (bpp >= 2) \
if(bpp >= 3) b = dst[2];\ g = dst[1]; \
if(bpp >= 4) a = dst[3];\ if (bpp >= 3) \
b = dst[2]; \
if (bpp >= 4) \
a = dst[3]; \
for (; i < size; i += bpp) { \ for (; i < size; i += bpp) { \
dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \ dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
if(bpp == 1) continue;\ if (bpp == 1) \
continue; \
dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \ dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
if(bpp == 2) continue;\ if (bpp == 2) \
continue; \
dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \ dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
if(bpp == 3) continue;\ if (bpp == 3) \
continue; \
dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \ dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
} \ } \
} }
#define UNROLL_FILTER(op) \ #define UNROLL_FILTER(op) \
if(bpp == 1) UNROLL1(1, op)\ if (bpp == 1) \
else if(bpp == 2) UNROLL1(2, op)\ UNROLL1(1, op) \
else if(bpp == 3) UNROLL1(3, op)\ else if (bpp == 2) \
else if(bpp == 4) UNROLL1(4, op)\ UNROLL1(2, op) \
else if (bpp == 3) \
UNROLL1(3, op) \
else if (bpp == 4) \
UNROLL1(4, op) \
else { \ else { \
for (; i < size; i += bpp) { \ for (; i < size; i += bpp) { \
int j; \ int j; \
for (j = 0; j < bpp; j++) \ for (j = 0; j < bpp; j++) \
dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\ dst[i + j] = op(dst[i + j - bpp], \
src[i + j], last[i + j]); \
} \ } \
} }
@ -198,9 +210,8 @@ static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
memcpy(dst, src, size); memcpy(dst, src, size);
break; break;
case PNG_FILTER_VALUE_SUB: case PNG_FILTER_VALUE_SUB:
for (i = 0; i < bpp; i++) { for (i = 0; i < bpp; i++)
dst[i] = src[i]; dst[i] = src[i];
}
if (bpp == 4) { if (bpp == 4) {
p = *(int *)dst; p = *(int *)dst;
for (; i < size; i += bpp) { for (; i < size; i += bpp) {
@ -230,7 +241,8 @@ static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
dst[i] = p + src[i]; dst[i] = p + src[i];
} }
if (bpp > 1 && size > 4) { if (bpp > 1 && size > 4) {
// would write off the end of the array if we let it process the last pixel with bpp=3 /* would write off the end of the array if we let it process
* the last pixel with bpp=3 */
int w = bpp == 4 ? size : size - 3; int w = bpp == 4 ? size : size - 3;
dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp); dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
i = w; i = w;
@ -262,7 +274,8 @@ static av_always_inline void convert_to_rgb32_loco(uint8_t *dst,
} }
} }
static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco) static void convert_to_rgb32(uint8_t *dst, const uint8_t *src,
int width, int loco)
{ {
if (loco) if (loco)
convert_to_rgb32_loco(dst, src, width, 1); convert_to_rgb32_loco(dst, src, width, 1);
@ -292,7 +305,8 @@ static void png_handle_row(PNGDecContext *s)
if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) { if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1, png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
s->last_row, s->row_size, s->bpp); s->last_row, s->row_size, s->bpp);
convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO); convert_to_rgb32(ptr, s->tmp_row, s->width,
s->filter_type == PNG_FILTER_TYPE_LOCO);
FFSWAP(uint8_t *, s->last_row, s->tmp_row); FFSWAP(uint8_t *, s->last_row, s->tmp_row);
} else { } else {
/* in normal case, we avoid one copy */ /* in normal case, we avoid one copy */
@ -321,7 +335,7 @@ static void png_handle_row(PNGDecContext *s)
ptr = s->image_buf + s->image_linesize * s->y; ptr = s->image_buf + s->image_linesize * s->y;
if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) { if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
/* if we already read one row, it is time to stop to /* if we already read one row, it is time to stop to
wait for the next one */ * wait for the next one */
if (got_line) if (got_line)
break; break;
png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1, png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
@ -379,7 +393,8 @@ static int png_decode_idat(PNGDecContext *s, int length)
s->zstream.next_out = s->crow_buf; s->zstream.next_out = s->crow_buf;
} }
if (ret == Z_STREAM_END && s->zstream.avail_in > 0) { if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
av_log(NULL, AV_LOG_WARNING, "%d undecompressed bytes left in buffer\n", s->zstream.avail_in); av_log(NULL, AV_LOG_WARNING,
"%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
return 0; return 0;
} }
} }
@ -553,9 +568,8 @@ static int decode_frame(AVCodecContext *avctx,
b = bytestream2_get_byte(&s->gb); b = bytestream2_get_byte(&s->gb);
s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b; s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
} }
for (; i < 256; i++) { for (; i < 256; i++)
s->palette[i] = (0xff << 24); s->palette[i] = (0xff << 24);
}
s->state |= PNG_PLTE; s->state |= PNG_PLTE;
bytestream2_skip(&s->gb, 4); /* crc */ bytestream2_skip(&s->gb, 4); /* crc */
} }
@ -597,9 +611,8 @@ static int decode_frame(AVCodecContext *avctx,
uint8_t *pd_last = s->prev->data[0]; uint8_t *pd_last = s->prev->data[0];
for (j = 0; j < s->height; j++) { for (j = 0; j < s->height; j++) {
for (i = 0; i < s->width * s->bpp; i++) { for (i = 0; i < s->width * s->bpp; i++)
pd[i] += pd_last[i]; pd[i] += pd_last[i];
}
pd += s->image_linesize; pd += s->image_linesize;
pd_last += s->image_linesize; pd_last += s->image_linesize;
} }

@ -45,5 +45,6 @@ av_cold void ff_pngdsp_init(PNGDSPContext *dsp)
dsp->add_bytes_l2 = add_bytes_l2_c; dsp->add_bytes_l2 = add_bytes_l2_c;
dsp->add_paeth_prediction = ff_add_png_paeth_prediction; dsp->add_paeth_prediction = ff_add_png_paeth_prediction;
if (ARCH_X86) ff_pngdsp_init_x86(dsp); if (ARCH_X86)
ff_pngdsp_init_x86(dsp);
} }

@ -18,6 +18,7 @@
* License along with Libav; if not, write to the Free Software * License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "avcodec.h" #include "avcodec.h"
#include "bytestream.h" #include "bytestream.h"
#include "dsputil.h" #include "dsputil.h"
@ -82,7 +83,8 @@ static void png_get_interlaced_row(uint8_t *dst, int row_size,
} }
} }
static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp) static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
int w, int bpp)
{ {
int i; int i;
for (i = 0; i < w; i++) { for (i = 0; i < w; i++) {
@ -218,7 +220,8 @@ static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
return -1; return -1;
if (s->zstream.avail_out == 0) { if (s->zstream.avail_out == 0) {
if (s->bytestream_end - s->bytestream > IOBUF_SIZE + 100) if (s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE); png_write_chunk(&s->bytestream,
MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
s->zstream.avail_out = IOBUF_SIZE; s->zstream.avail_out = IOBUF_SIZE;
s->zstream.next_out = s->buf; s->zstream.next_out = s->buf;
} }
@ -234,8 +237,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
int bit_depth, color_type, y, len, row_size, ret, is_progressive; int bit_depth, color_type, y, len, row_size, ret, is_progressive;
int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size; int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
int compression_level; int compression_level;
uint8_t *ptr, *top; uint8_t *ptr, *top, *crow_buf, *crow;
uint8_t *crow_base = NULL, *crow_buf, *crow; uint8_t *crow_base = NULL;
uint8_t *progressive_buf = NULL; uint8_t *progressive_buf = NULL;
uint8_t *rgba_buf = NULL; uint8_t *rgba_buf = NULL;
uint8_t *top_buf = NULL; uint8_t *top_buf = NULL;
@ -275,9 +278,9 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
s->zstream.zalloc = ff_png_zalloc; s->zstream.zalloc = ff_png_zalloc;
s->zstream.zfree = ff_png_zfree; s->zstream.zfree = ff_png_zfree;
s->zstream.opaque = NULL; s->zstream.opaque = NULL;
compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ? compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
Z_DEFAULT_COMPRESSION : ? Z_DEFAULT_COMPRESSION
av_clip(avctx->compression_level, 0, 9); : av_clip(avctx->compression_level, 0, 9);
ret = deflateInit2(&s->zstream, compression_level, ret = deflateInit2(&s->zstream, compression_level,
Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY); Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
if (ret != Z_OK) if (ret != Z_OK)
@ -301,7 +304,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED)); crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
if (!crow_base) if (!crow_base)
goto fail; goto fail;
crow_buf = crow_base + 15; // pixel data should be aligned, but there's a control byte before it // pixel data should be aligned, but there's a control byte before it
crow_buf = crow_base + 15;
if (is_progressive) { if (is_progressive) {
progressive_buf = av_malloc(row_size + 1); progressive_buf = av_malloc(row_size + 1);
if (!progressive_buf) if (!progressive_buf)
@ -351,9 +355,11 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
*alpha_ptr++ = alpha; *alpha_ptr++ = alpha;
bytestream_put_be24(&ptr, v); bytestream_put_be24(&ptr, v);
} }
png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3); png_write_chunk(&s->bytestream,
MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
if (has_alpha) { if (has_alpha) {
png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256); png_write_chunk(&s->bytestream,
MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
} }
} }
@ -365,11 +371,11 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
for (pass = 0; pass < NB_PASSES; pass++) { for (pass = 0; pass < NB_PASSES; pass++) {
/* NOTE: a pass is completely omitted if no pixels would be /* NOTE: a pass is completely omitted if no pixels would be
output */ * output */
pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width); pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
if (pass_row_size > 0) { if (pass_row_size > 0) {
top = NULL; top = NULL;
for(y = 0; y < avctx->height; y++) { for (y = 0; y < avctx->height; y++)
if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) { if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
ptr = p->data[0] + y * p->linesize[0]; ptr = p->data[0] + y * p->linesize[0];
FFSWAP(uint8_t *, progressive_buf, top_buf); FFSWAP(uint8_t *, progressive_buf, top_buf);
@ -380,13 +386,13 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
png_get_interlaced_row(progressive_buf, pass_row_size, png_get_interlaced_row(progressive_buf, pass_row_size,
bits_per_pixel, pass, bits_per_pixel, pass,
ptr, avctx->width); ptr, avctx->width);
crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3); crow = png_choose_filter(s, crow_buf, progressive_buf,
top, pass_row_size, bits_per_pixel >> 3);
png_write_row(s, crow, pass_row_size + 1); png_write_row(s, crow, pass_row_size + 1);
top = progressive_buf; top = progressive_buf;
} }
} }
} }
}
} else { } else {
top = NULL; top = NULL;
for (y = 0; y < avctx->height; y++) { for (y = 0; y < avctx->height; y++) {
@ -396,7 +402,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
convert_from_rgb32(rgba_buf, ptr, avctx->width); convert_from_rgb32(rgba_buf, ptr, avctx->width);
ptr = rgba_buf; ptr = rgba_buf;
} }
crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3); crow = png_choose_filter(s, crow_buf, ptr, top,
row_size, bits_per_pixel >> 3);
png_write_row(s, crow, row_size + 1); png_write_row(s, crow, row_size + 1);
top = ptr; top = ptr;
} }
@ -436,7 +443,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
goto the_end; goto the_end;
} }
static av_cold int png_enc_init(AVCodecContext *avctx){ static av_cold int png_enc_init(AVCodecContext *avctx)
{
PNGEncContext *s = avctx->priv_data; PNGEncContext *s = avctx->priv_data;
avctx->coded_frame = av_frame_alloc(); avctx->coded_frame = av_frame_alloc();
@ -448,7 +456,9 @@ static av_cold int png_enc_init(AVCodecContext *avctx){
ff_dsputil_init(&s->dsp, avctx); ff_dsputil_init(&s->dsp, avctx);
s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED); s->filter_type = av_clip(avctx->prediction_method,
PNG_FILTER_VALUE_NONE,
PNG_FILTER_VALUE_MIXED);
if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK) if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
s->filter_type = PNG_FILTER_VALUE_NONE; s->filter_type = PNG_FILTER_VALUE_NONE;

Loading…
Cancel
Save