avcodec/gifdec: rewrite functions that fill block/frame

Also fix AVFrame's negative linesize decoding.
This commit is contained in:
Paul B Mahol 2023-09-22 19:31:47 +02:00
parent 1242d172e2
commit 24f63cb2b7

View File

@ -59,7 +59,7 @@ typedef struct GifState {
/* depending on disposal method we store either part of the image /* depending on disposal method we store either part of the image
* drawn on the canvas or background color that * drawn on the canvas or background color that
* should be used upon disposal */ * should be used upon disposal */
uint32_t * stored_img; uint8_t *stored_img;
int stored_img_size; int stored_img_size;
int stored_bg_color; int stored_bg_color;
@ -86,52 +86,55 @@ static void gif_read_palette(GifState *s, uint32_t *pal, int nb)
static void gif_fill(AVFrame *picture, uint32_t color) static void gif_fill(AVFrame *picture, uint32_t color)
{ {
uint32_t *p = (uint32_t *)picture->data[0]; const ptrdiff_t linesize = picture->linesize[0];
uint32_t *p_end = p + (picture->linesize[0] / sizeof(uint32_t)) * picture->height; uint8_t *py = picture->data[0];
const int w = picture->width;
const int h = picture->height;
for (; p < p_end; p++) for (int y = 0; y < h; y++) {
*p = color; uint32_t *px = (uint32_t *)py;
for (int x = 0; x < w; x++)
px[x] = color;
py += linesize;
}
} }
static void gif_fill_rect(AVFrame *picture, uint32_t color, int l, int t, int w, int h) static void gif_fill_rect(AVFrame *picture, uint32_t color, int l, int t, int w, int h)
{ {
const int linesize = picture->linesize[0] / sizeof(uint32_t); const ptrdiff_t linesize = picture->linesize[0];
const uint32_t *py = (uint32_t *)picture->data[0] + t * linesize; uint8_t *py = picture->data[0] + t * linesize;
const uint32_t *pr, *pb = py + h * linesize;
uint32_t *px;
for (; py < pb; py += linesize) { for (int y = 0; y < h; y++) {
px = (uint32_t *)py + l; uint32_t *px = ((uint32_t *)py) + l;
pr = px + w; for (int x = 0; x < w; x++)
px[x] = color;
for (; px < pr; px++) py += linesize;
*px = color;
} }
} }
static void gif_copy_img_rect(const uint32_t *src, uint32_t *dst, static void gif_copy_img_rect(const uint8_t *src, uint8_t *dst,
int linesize, int l, int t, int w, int h) ptrdiff_t src_linesize,
ptrdiff_t dst_linesize,
int l, int t, int w, int h)
{ {
const int y_start = t * linesize; const uint8_t *src_py = src;
const uint32_t *src_px, uint8_t *dst_py = dst;
*src_py = src + y_start,
*dst_py = dst + y_start;
const uint32_t *src_pb = src_py + h * linesize;
uint32_t *dst_px;
for (; src_py < src_pb; src_py += linesize, dst_py += linesize) { src_py += t * src_linesize;
src_px = src_py + l; dst_py += t * dst_linesize;
dst_px = (uint32_t *)dst_py + l; for (int y = 0; y < h; y++) {
memcpy(dst_py + l * 4, src_py + l * 4, w * 4);
memcpy(dst_px, src_px, w * sizeof(uint32_t)); src_py += src_linesize;
dst_py += dst_linesize;
} }
} }
static int gif_read_image(GifState *s, AVFrame *frame) static int gif_read_image(GifState *s, AVFrame *frame)
{ {
int left, top, width, height, bits_per_pixel, code_size, flags, pw; int left, top, width, height, bits_per_pixel, code_size, flags, pw;
int is_interleaved, has_local_palette, y, pass, y1, linesize, pal_size, lzwed_len; int is_interleaved, has_local_palette, y, pass, y1, pal_size, lzwed_len;
uint32_t *ptr, *pal, *px, *pr, *ptr1; uint32_t *ptr, *pal, *px, *pr, *ptr1;
ptrdiff_t linesize;
int ret; int ret;
uint8_t *idx; uint8_t *idx;
@ -214,8 +217,8 @@ static int gif_read_image(GifState *s, AVFrame *frame)
if (s->gce_prev_disposal == GCE_DISPOSAL_BACKGROUND) { if (s->gce_prev_disposal == GCE_DISPOSAL_BACKGROUND) {
gif_fill_rect(frame, s->stored_bg_color, s->gce_l, s->gce_t, s->gce_w, s->gce_h); gif_fill_rect(frame, s->stored_bg_color, s->gce_l, s->gce_t, s->gce_w, s->gce_h);
} else if (s->gce_prev_disposal == GCE_DISPOSAL_RESTORE) { } else if (s->gce_prev_disposal == GCE_DISPOSAL_RESTORE) {
gif_copy_img_rect(s->stored_img, (uint32_t *)frame->data[0], gif_copy_img_rect(s->stored_img, frame->data[0],
frame->linesize[0] / sizeof(uint32_t), s->gce_l, s->gce_t, s->gce_w, s->gce_h); FFABS(frame->linesize[0]), frame->linesize[0], s->gce_l, s->gce_t, s->gce_w, s->gce_h);
} }
s->gce_prev_disposal = s->gce_disposal; s->gce_prev_disposal = s->gce_disposal;
@ -230,12 +233,12 @@ static int gif_read_image(GifState *s, AVFrame *frame)
else else
s->stored_bg_color = s->bg_color; s->stored_bg_color = s->bg_color;
} else if (s->gce_disposal == GCE_DISPOSAL_RESTORE) { } else if (s->gce_disposal == GCE_DISPOSAL_RESTORE) {
av_fast_malloc(&s->stored_img, &s->stored_img_size, frame->linesize[0] * frame->height); av_fast_malloc(&s->stored_img, &s->stored_img_size, FFABS(frame->linesize[0]) * frame->height);
if (!s->stored_img) if (!s->stored_img)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
gif_copy_img_rect((uint32_t *)frame->data[0], s->stored_img, gif_copy_img_rect(frame->data[0], s->stored_img,
frame->linesize[0] / sizeof(uint32_t), left, top, pw, height); frame->linesize[0], FFABS(frame->linesize[0]), left, top, pw, height);
} }
} }
@ -252,8 +255,8 @@ static int gif_read_image(GifState *s, AVFrame *frame)
} }
/* read all the image */ /* read all the image */
linesize = frame->linesize[0] / sizeof(uint32_t); linesize = frame->linesize[0];
ptr1 = (uint32_t *)frame->data[0] + top * linesize + left; ptr1 = (uint32_t *)(frame->data[0] + top * linesize) + left;
ptr = ptr1; ptr = ptr1;
pass = 0; pass = 0;
y1 = 0; y1 = 0;
@ -278,24 +281,24 @@ static int gif_read_image(GifState *s, AVFrame *frame)
case 0: case 0:
case 1: case 1:
y1 += 8; y1 += 8;
ptr += linesize * 8; ptr += linesize * 2;
break; break;
case 2: case 2:
y1 += 4; y1 += 4;
ptr += linesize * 4; ptr += linesize * 1;
break; break;
case 3: case 3:
y1 += 2; y1 += 2;
ptr += linesize * 2; ptr += linesize / 2;
break; break;
} }
while (y1 >= height) { while (y1 >= height) {
y1 = 4 >> pass; y1 = 4 >> pass;
ptr = ptr1 + linesize * y1; ptr = ptr1 + linesize / 4 * y1;
pass++; pass++;
} }
} else { } else {
ptr += linesize; ptr += linesize / 4;
} }
} }