png: K&R formatting cosmetics

This commit is contained in:
Vittorio Giovara 2014-03-14 18:32:00 +01:00
parent f7518f1a0e
commit c598b569fb
4 changed files with 207 additions and 183 deletions

View File

@ -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
is the original image width. We also do pixel format conversion at
this stage */
* is the original image width. We also do pixel format conversion at
* this stage */
static void png_put_interlaced_row(uint8_t *dst, int width,
int bits_per_pixel, int pass,
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;
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) { \
r = dst[0]; \
if(bpp >= 2) g = dst[1];\
if(bpp >= 3) b = dst[2];\
if(bpp >= 4) a = dst[3];\
if (bpp >= 2) \
g = dst[1]; \
if (bpp >= 3) \
b = dst[2]; \
if (bpp >= 4) \
a = dst[3]; \
for (; i < size; i += bpp) { \
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]); \
if(bpp == 2) continue;\
if (bpp == 2) \
continue; \
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]); \
} \
}
#define UNROLL_FILTER(op) \
if(bpp == 1) UNROLL1(1, op)\
else if(bpp == 2) UNROLL1(2, op)\
else if(bpp == 3) UNROLL1(3, op)\
else if(bpp == 4) UNROLL1(4, op)\
if (bpp == 1) \
UNROLL1(1, op) \
else if (bpp == 2) \
UNROLL1(2, op) \
else if (bpp == 3) \
UNROLL1(3, op) \
else if (bpp == 4) \
UNROLL1(4, op) \
else { \
for (; i < size; i += bpp) { \
int 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);
break;
case PNG_FILTER_VALUE_SUB:
for (i = 0; i < bpp; i++) {
for (i = 0; i < bpp; i++)
dst[i] = src[i];
}
if (bpp == 4) {
p = *(int *)dst;
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];
}
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;
dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
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)
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) {
png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
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);
} else {
/* 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;
if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
/* 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)
break;
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;
}
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;
}
}
@ -553,9 +568,8 @@ static int decode_frame(AVCodecContext *avctx,
b = bytestream2_get_byte(&s->gb);
s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
}
for (; i < 256; i++) {
for (; i < 256; i++)
s->palette[i] = (0xff << 24);
}
s->state |= PNG_PLTE;
bytestream2_skip(&s->gb, 4); /* crc */
}
@ -597,9 +611,8 @@ static int decode_frame(AVCodecContext *avctx,
uint8_t *pd_last = s->prev->data[0];
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 += s->image_linesize;
pd_last += s->image_linesize;
}

View File

@ -45,5 +45,6 @@ av_cold void ff_pngdsp_init(PNGDSPContext *dsp)
dsp->add_bytes_l2 = add_bytes_l2_c;
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);
}

View File

@ -18,6 +18,7 @@
* License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "bytestream.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;
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;
if (s->zstream.avail_out == 0) {
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.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 bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
int compression_level;
uint8_t *ptr, *top;
uint8_t *crow_base = NULL, *crow_buf, *crow;
uint8_t *ptr, *top, *crow_buf, *crow;
uint8_t *crow_base = NULL;
uint8_t *progressive_buf = NULL;
uint8_t *rgba_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.zfree = ff_png_zfree;
s->zstream.opaque = NULL;
compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
Z_DEFAULT_COMPRESSION :
av_clip(avctx->compression_level, 0, 9);
compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
? Z_DEFAULT_COMPRESSION
: av_clip(avctx->compression_level, 0, 9);
ret = deflateInit2(&s->zstream, compression_level,
Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
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));
if (!crow_base)
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) {
progressive_buf = av_malloc(row_size + 1);
if (!progressive_buf)
@ -351,9 +355,11 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
*alpha_ptr++ = alpha;
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) {
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++) {
/* 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);
if (pass_row_size > 0) {
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) {
ptr = p->data[0] + y * p->linesize[0];
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,
bits_per_pixel, pass,
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);
top = progressive_buf;
}
}
}
}
} else {
top = NULL;
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);
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);
top = ptr;
}
@ -436,7 +443,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
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;
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);
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)
s->filter_type = PNG_FILTER_VALUE_NONE;