static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,

                               AVFrame *p)

{

    size_t x, y;

    uint8_t *buffer = av_malloc(s->image_linesize * s->height);



    if (!buffer)

        return AVERROR(ENOMEM);



    if (s->blend_op == APNG_BLEND_OP_OVER &&

        avctx->pix_fmt != AV_PIX_FMT_RGBA &&

        avctx->pix_fmt != AV_PIX_FMT_GRAY8A &&

        avctx->pix_fmt != AV_PIX_FMT_PAL8) {

        avpriv_request_sample(avctx, "Blending with pixel format %s",

                              av_get_pix_fmt_name(avctx->pix_fmt));

        return AVERROR_PATCHWELCOME;

    }



    // Do the disposal operation specified by the last frame on the frame

    if (s->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) {

        ff_thread_await_progress(&s->last_picture, INT_MAX, 0);

        memcpy(buffer, s->last_picture.f->data[0], s->image_linesize * s->height);



        if (s->last_dispose_op == APNG_DISPOSE_OP_BACKGROUND)

            for (y = s->last_y_offset; y < s->last_y_offset + s->last_h; ++y)

                memset(buffer + s->image_linesize * y + s->bpp * s->last_x_offset, 0, s->bpp * s->last_w);



        memcpy(s->previous_picture.f->data[0], buffer, s->image_linesize * s->height);

        ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);

    } else {

        ff_thread_await_progress(&s->previous_picture, INT_MAX, 0);

        memcpy(buffer, s->previous_picture.f->data[0], s->image_linesize * s->height);

    }



    // Perform blending

    if (s->blend_op == APNG_BLEND_OP_SOURCE) {

        for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {

            size_t row_start = s->image_linesize * y + s->bpp * s->x_offset;

            memcpy(buffer + row_start, p->data[0] + row_start, s->bpp * s->cur_w);

        }

    } else { // APNG_BLEND_OP_OVER

        for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {

            uint8_t *foreground = p->data[0] + s->image_linesize * y + s->bpp * s->x_offset;

            uint8_t *background = buffer + s->image_linesize * y + s->bpp * s->x_offset;

            for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += s->bpp, background += s->bpp) {

                size_t b;

                uint8_t foreground_alpha, background_alpha, output_alpha;

                uint8_t output[4];



                // Since we might be blending alpha onto alpha, we use the following equations:

                // output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha

                // output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha



                switch (avctx->pix_fmt) {

                case AV_PIX_FMT_RGBA:

                    foreground_alpha = foreground[3];

                    background_alpha = background[3];

                    break;



                case AV_PIX_FMT_GRAY8A:

                    foreground_alpha = foreground[1];

                    background_alpha = background[1];

                    break;



                case AV_PIX_FMT_PAL8:

                    foreground_alpha = s->palette[foreground[0]] >> 24;

                    background_alpha = s->palette[background[0]] >> 24;

                    break;

                }



                if (foreground_alpha == 0)

                    continue;



                if (foreground_alpha == 255) {

                    memcpy(background, foreground, s->bpp);

                    continue;

                }



                if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {

                    // TODO: Alpha blending with PAL8 will likely need the entire image converted over to RGBA first

                    avpriv_request_sample(avctx, "Alpha blending palette samples");

                    background[0] = foreground[0];

                    continue;

                }



                output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);



                for (b = 0; b < s->bpp - 1; ++b) {

                    if (output_alpha == 0) {

                        output[b] = 0;

                    } else if (background_alpha == 255) {

                        output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);

                    } else {

                        output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);

                    }

                }

                output[b] = output_alpha;

                memcpy(background, output, s->bpp);

            }

        }

    }



    // Copy blended buffer into the frame and free

    memcpy(p->data[0], buffer, s->image_linesize * s->height);

    av_free(buffer);



    return 0;

}

static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,

                               AVFrame *p)

{

    size_t x, y;

    uint8_t *buffer = av_malloc(s->image_linesize * s->height);



    if (!buffer)

        return AVERROR(ENOMEM);



    if (s->blend_op == APNG_BLEND_OP_OVER &&

        avctx->pix_fmt != AV_PIX_FMT_RGBA &&

        avctx->pix_fmt != AV_PIX_FMT_GRAY8A &&

        avctx->pix_fmt != AV_PIX_FMT_PAL8) {

        avpriv_request_sample(avctx, "Blending with pixel format %s",

                              av_get_pix_fmt_name(avctx->pix_fmt));

        return AVERROR_PATCHWELCOME;

    }



    

    if (s->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) {

        ff_thread_await_progress(&s->last_picture, INT_MAX, 0);

        memcpy(buffer, s->last_picture.f->data[0], s->image_linesize * s->height);



        if (s->last_dispose_op == APNG_DISPOSE_OP_BACKGROUND)

            for (y = s->last_y_offset; y < s->last_y_offset + s->last_h; ++y)

                memset(buffer + s->image_linesize * y + s->bpp * s->last_x_offset, 0, s->bpp * s->last_w);



        memcpy(s->previous_picture.f->data[0], buffer, s->image_linesize * s->height);

        ff_thread_report_progress(&s->previous_picture, INT_MAX, 0);

    } else {

        ff_thread_await_progress(&s->previous_picture, INT_MAX, 0);

        memcpy(buffer, s->previous_picture.f->data[0], s->image_linesize * s->height);

    }



    

    if (s->blend_op == APNG_BLEND_OP_SOURCE) {

        for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {

            size_t row_start = s->image_linesize * y + s->bpp * s->x_offset;

            memcpy(buffer + row_start, p->data[0] + row_start, s->bpp * s->cur_w);

        }

    } else { 

        for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {

            uint8_t *foreground = p->data[0] + s->image_linesize * y + s->bpp * s->x_offset;

            uint8_t *background = buffer + s->image_linesize * y + s->bpp * s->x_offset;

            for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += s->bpp, background += s->bpp) {

                size_t b;

                uint8_t foreground_alpha, background_alpha, output_alpha;

                uint8_t output[4];



                

                

                



                switch (avctx->pix_fmt) {

                case AV_PIX_FMT_RGBA:

                    foreground_alpha = foreground[3];

                    background_alpha = background[3];

                    break;



                case AV_PIX_FMT_GRAY8A:

                    foreground_alpha = foreground[1];

                    background_alpha = background[1];

                    break;



                case AV_PIX_FMT_PAL8:

                    foreground_alpha = s->palette[foreground[0]] >> 24;

                    background_alpha = s->palette[background[0]] >> 24;

                    break;

                }



                if (foreground_alpha == 0)

                    continue;



                if (foreground_alpha == 255) {

                    memcpy(background, foreground, s->bpp);

                    continue;

                }



                if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {

                    

                    avpriv_request_sample(avctx, "Alpha blending palette samples");

                    background[0] = foreground[0];

                    continue;

                }



                output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);



                for (b = 0; b < s->bpp - 1; ++b) {

                    if (output_alpha == 0) {

                        output[b] = 0;

                    } else if (background_alpha == 255) {

                        output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);

                    } else {

                        output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);

                    }

                }

                output[b] = output_alpha;

                memcpy(background, output, s->bpp);

            }

        }

    }



    

    memcpy(p->data[0], buffer, s->image_linesize * s->height);

    av_free(buffer);



    return 0;

}

    uint8_t *buffer = av_malloc(s->image_linesize * s->height);     if (!buffer)         return AVERROR(ENOMEM);
static int FUNC_0(AVCodecContext *VAR_0, PNGDecContext *VAR_1,

                               AVFrame *VAR_2)

{

    size_t x, y;

    uint8_t *buffer = av_malloc(VAR_1->image_linesize * VAR_1->height);



    if (!buffer)

        return AVERROR(ENOMEM);



    if (VAR_1->blend_op == APNG_BLEND_OP_OVER &&

        VAR_0->pix_fmt != AV_PIX_FMT_RGBA &&

        VAR_0->pix_fmt != AV_PIX_FMT_GRAY8A &&

        VAR_0->pix_fmt != AV_PIX_FMT_PAL8) {

        avpriv_request_sample(VAR_0, "Blending with pixel format %VAR_1",

                              av_get_pix_fmt_name(VAR_0->pix_fmt));

        return AVERROR_PATCHWELCOME;

    }



    

    if (VAR_1->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) {

        ff_thread_await_progress(&VAR_1->last_picture, INT_MAX, 0);

        memcpy(buffer, VAR_1->last_picture.f->data[0], VAR_1->image_linesize * VAR_1->height);



        if (VAR_1->last_dispose_op == APNG_DISPOSE_OP_BACKGROUND)

            for (y = VAR_1->last_y_offset; y < VAR_1->last_y_offset + VAR_1->last_h; ++y)

                memset(buffer + VAR_1->image_linesize * y + VAR_1->bpp * VAR_1->last_x_offset, 0, VAR_1->bpp * VAR_1->last_w);



        memcpy(VAR_1->previous_picture.f->data[0], buffer, VAR_1->image_linesize * VAR_1->height);

        ff_thread_report_progress(&VAR_1->previous_picture, INT_MAX, 0);

    } else {

        ff_thread_await_progress(&VAR_1->previous_picture, INT_MAX, 0);

        memcpy(buffer, VAR_1->previous_picture.f->data[0], VAR_1->image_linesize * VAR_1->height);

    }



    

    if (VAR_1->blend_op == APNG_BLEND_OP_SOURCE) {

        for (y = VAR_1->y_offset; y < VAR_1->y_offset + VAR_1->cur_h; ++y) {

            size_t row_start = VAR_1->image_linesize * y + VAR_1->bpp * VAR_1->x_offset;

            memcpy(buffer + row_start, VAR_2->data[0] + row_start, VAR_1->bpp * VAR_1->cur_w);

        }

    } else { 

        for (y = VAR_1->y_offset; y < VAR_1->y_offset + VAR_1->cur_h; ++y) {

            uint8_t *foreground = VAR_2->data[0] + VAR_1->image_linesize * y + VAR_1->bpp * VAR_1->x_offset;

            uint8_t *background = buffer + VAR_1->image_linesize * y + VAR_1->bpp * VAR_1->x_offset;

            for (x = VAR_1->x_offset; x < VAR_1->x_offset + VAR_1->cur_w; ++x, foreground += VAR_1->bpp, background += VAR_1->bpp) {

                size_t b;

                uint8_t foreground_alpha, background_alpha, output_alpha;

                uint8_t output[4];



                

                

                



                switch (VAR_0->pix_fmt) {

                case AV_PIX_FMT_RGBA:

                    foreground_alpha = foreground[3];

                    background_alpha = background[3];

                    break;



                case AV_PIX_FMT_GRAY8A:

                    foreground_alpha = foreground[1];

                    background_alpha = background[1];

                    break;



                case AV_PIX_FMT_PAL8:

                    foreground_alpha = VAR_1->palette[foreground[0]] >> 24;

                    background_alpha = VAR_1->palette[background[0]] >> 24;

                    break;

                }



                if (foreground_alpha == 0)

                    continue;



                if (foreground_alpha == 255) {

                    memcpy(background, foreground, VAR_1->bpp);

                    continue;

                }



                if (VAR_0->pix_fmt == AV_PIX_FMT_PAL8) {

                    

                    avpriv_request_sample(VAR_0, "Alpha blending palette samples");

                    background[0] = foreground[0];

                    continue;

                }



                output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);



                for (b = 0; b < VAR_1->bpp - 1; ++b) {

                    if (output_alpha == 0) {

                        output[b] = 0;

                    } else if (background_alpha == 255) {

                        output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);

                    } else {

                        output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);

                    }

                }

                output[b] = output_alpha;

                memcpy(background, output, VAR_1->bpp);

            }

        }

    }



    

    memcpy(VAR_2->data[0], buffer, VAR_1->image_linesize * VAR_1->height);

    av_free(buffer);



    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0, PNGDecContext *VAR_1,
AVFrame *VAR_2)
{ size_t x, y; uint8_t *buffer = av_malloc(VAR_1->image_linesize * VAR_1->height); if (!buffer)
return AVERROR(ENOMEM); if (VAR_1->blend_op == APNG_BLEND_OP_OVER &&
VAR_0->pix_fmt != AV_PIX_FMT_RGBA &&
VAR_0->pix_fmt != AV_PIX_FMT_GRAY8A &&
VAR_0->pix_fmt != AV_PIX_FMT_PAL8) { avpriv_request_sample(VAR_0, "Blending with pixel format %VAR_1",
av_get_pix_fmt_name(VAR_0->pix_fmt)); return AVERROR_PATCHWELCOME; } if (VAR_1->last_dispose_op != APNG_DISPOSE_OP_PREVIOUS) { ff_thread_await_progress(&VAR_1->last_picture, INT_MAX, 0); memcpy(buffer, VAR_1->last_picture.f->data[0], VAR_1->image_linesize * VAR_1->height); if (VAR_1->last_dispose_op == APNG_DISPOSE_OP_BACKGROUND)
for (y = VAR_1->last_y_offset; y < VAR_1->last_y_offset + VAR_1->last_h; ++y) memset(buffer + VAR_1->image_linesize * y + VAR_1->bpp * VAR_1->last_x_offset, 0, VAR_1->bpp * VAR_1->last_w); memcpy(VAR_1->previous_picture.f->data[0], buffer, VAR_1->image_linesize * VAR_1->height); ff_thread_report_progress(&VAR_1->previous_picture, INT_MAX, 0); } else { ff_thread_await_progress(&VAR_1->previous_picture, INT_MAX, 0); memcpy(buffer, VAR_1->previous_picture.f->data[0], VAR_1->image_linesize * VAR_1->height); } if (VAR_1->blend_op == APNG_BLEND_OP_SOURCE) { for (y = VAR_1->y_offset; y < VAR_1->y_offset + VAR_1->cur_h; ++y) { size_t row_start = VAR_1->image_linesize * y + VAR_1->bpp * VAR_1->x_offset; memcpy(buffer + row_start, VAR_2->data[0] + row_start, VAR_1->bpp * VAR_1->cur_w); } } else { for (y = VAR_1->y_offset; y < VAR_1->y_offset + VAR_1->cur_h; ++y) { uint8_t *foreground = VAR_2->data[0] + VAR_1->image_linesize * y + VAR_1->bpp * VAR_1->x_offset; uint8_t *background = buffer + VAR_1->image_linesize * y + VAR_1->bpp * VAR_1->x_offset; for (x = VAR_1->x_offset; x < VAR_1->x_offset + VAR_1->cur_w; ++x, foreground += VAR_1->bpp, background += VAR_1->bpp) { size_t b; uint8_t foreground_alpha, background_alpha, output_alpha; uint8_t output[4]; switch (VAR_0->pix_fmt) { case AV_PIX_FMT_RGBA:
foreground_alpha = foreground[3]; background_alpha = background[3]; break; case AV_PIX_FMT_GRAY8A:
foreground_alpha = foreground[1]; background_alpha = background[1]; break; case AV_PIX_FMT_PAL8:
foreground_alpha = VAR_1->palette[foreground[0]] >> 24; background_alpha = VAR_1->palette[background[0]] >> 24; break; } if (foreground_alpha == 0)
continue; if (foreground_alpha == 255) { memcpy(background, foreground, VAR_1->bpp); continue; } if (VAR_0->pix_fmt == AV_PIX_FMT_PAL8) { avpriv_request_sample(VAR_0, "Alpha blending palette samples"); background[0] = foreground[0]; continue; } output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha); for (b = 0; b < VAR_1->bpp - 1; ++b) { if (output_alpha == 0) { output[b] = 0; } else if (background_alpha == 255) { output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]); } else { output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha); } } output[b] = output_alpha; memcpy(background, output, VAR_1->bpp); } } } memcpy(VAR_2->data[0], buffer, VAR_1->image_linesize * VAR_1->height); av_free(buffer); return 0; }