static int libkvazaar_encode(AVCodecContext *avctx,

                             AVPacket *avpkt,

                             const AVFrame *frame,

                             int *got_packet_ptr)

{

    int retval = 0;

    kvz_picture *img_in = NULL;



    kvz_data_chunk *data_out = NULL;

    uint32_t len_out = 0;

    kvz_frame_info frame_info;



    LibkvazaarContext *ctx = avctx->priv_data;



    *got_packet_ptr = 0;



    if (frame) {

        int i = 0;



        av_assert0(frame->width == ctx->config->width);

        av_assert0(frame->height == ctx->config->height);

        av_assert0(frame->format == avctx->pix_fmt);



        // Allocate input picture for kvazaar.

        img_in = ctx->api->picture_alloc(frame->width, frame->height);

        if (!img_in) {

            av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");

            retval = AVERROR(ENOMEM);

            goto done;

        }



        // Copy pixels from frame to img_in.

        for (i = 0; i < 3; ++i) {

            uint8_t *dst = img_in->data[i];

            uint8_t *src = frame->data[i];

            int width = (i == 0) ? frame->width : (frame->width / 2);

            int height = (i == 0) ? frame->height : (frame->height / 2);

            int y = 0;

            for (y = 0; y < height; ++y) {

                memcpy(dst, src, width);

                src += frame->linesize[i];

                dst += width;

            }

        }

    }



    if (!ctx->api->encoder_encode(ctx->encoder, img_in,

                                  &data_out, &len_out,

                                  NULL, NULL,

                                  &frame_info)) {

        av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");

        retval = AVERROR_EXTERNAL;

        goto done;

    }



    if (data_out) {

        kvz_data_chunk *chunk = NULL;

        uint64_t written = 0;



        retval = ff_alloc_packet(avpkt, len_out);

        if (retval < 0) {

            av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");

            goto done;

        }



        for (chunk = data_out; chunk != NULL; chunk = chunk->next) {

            av_assert0(written + chunk->len <= len_out);

            memcpy(avpkt->data + written, chunk->data, chunk->len);

            written += chunk->len;

        }

        *got_packet_ptr = 1;



        ctx->api->chunk_free(data_out);

        data_out = NULL;



        avpkt->flags = 0;

        // IRAP VCL NAL unit types span the range

        // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].

        if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&

                frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {

            avpkt->flags |= AV_PKT_FLAG_KEY;

        }

    }



done:

    ctx->api->picture_free(img_in);

    ctx->api->chunk_free(data_out);

    return retval;

}

static int libkvazaar_encode(AVCodecContext *avctx,

                             AVPacket *avpkt,

                             const AVFrame *frame,

                             int *got_packet_ptr)

{

    int retval = 0;

    kvz_picture *img_in = NULL;



    kvz_data_chunk *data_out = NULL;

    uint32_t len_out = 0;

    kvz_frame_info frame_info;



    LibkvazaarContext *ctx = avctx->priv_data;



    *got_packet_ptr = 0;



    if (frame) {

        int i = 0;



        av_assert0(frame->width == ctx->config->width);

        av_assert0(frame->height == ctx->config->height);

        av_assert0(frame->format == avctx->pix_fmt);



        

        img_in = ctx->api->picture_alloc(frame->width, frame->height);

        if (!img_in) {

            av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");

            retval = AVERROR(ENOMEM);

            goto done;

        }



        

        for (i = 0; i < 3; ++i) {

            uint8_t *dst = img_in->data[i];

            uint8_t *src = frame->data[i];

            int width = (i == 0) ? frame->width : (frame->width / 2);

            int height = (i == 0) ? frame->height : (frame->height / 2);

            int y = 0;

            for (y = 0; y < height; ++y) {

                memcpy(dst, src, width);

                src += frame->linesize[i];

                dst += width;

            }

        }

    }



    if (!ctx->api->encoder_encode(ctx->encoder, img_in,

                                  &data_out, &len_out,

                                  NULL, NULL,

                                  &frame_info)) {

        av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");

        retval = AVERROR_EXTERNAL;

        goto done;

    }



    if (data_out) {

        kvz_data_chunk *chunk = NULL;

        uint64_t written = 0;



        retval = ff_alloc_packet(avpkt, len_out);

        if (retval < 0) {

            av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");

            goto done;

        }



        for (chunk = data_out; chunk != NULL; chunk = chunk->next) {

            av_assert0(written + chunk->len <= len_out);

            memcpy(avpkt->data + written, chunk->data, chunk->len);

            written += chunk->len;

        }

        *got_packet_ptr = 1;



        ctx->api->chunk_free(data_out);

        data_out = NULL;



        avpkt->flags = 0;

        

        

        if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&

                frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {

            avpkt->flags |= AV_PKT_FLAG_KEY;

        }

    }



done:

    ctx->api->picture_free(img_in);

    ctx->api->chunk_free(data_out);

    return retval;

}

        av_assert0(frame->width == ctx->config->width);         av_assert0(frame->height == ctx->config->height);         av_assert0(frame->format == avctx->pix_fmt);
static int FUNC_0(AVCodecContext *VAR_0,

                             AVPacket *VAR_1,

                             const AVFrame *VAR_2,

                             int *VAR_3)

{

    int VAR_4 = 0;

    kvz_picture *img_in = NULL;



    kvz_data_chunk *data_out = NULL;

    uint32_t len_out = 0;

    kvz_frame_info frame_info;



    LibkvazaarContext *ctx = VAR_0->priv_data;



    *VAR_3 = 0;



    if (VAR_2) {

        int VAR_5 = 0;



        av_assert0(VAR_2->VAR_6 == ctx->config->VAR_6);

        av_assert0(VAR_2->VAR_7 == ctx->config->VAR_7);

        av_assert0(VAR_2->format == VAR_0->pix_fmt);



        

        img_in = ctx->api->picture_alloc(VAR_2->VAR_6, VAR_2->VAR_7);

        if (!img_in) {

            av_log(VAR_0, AV_LOG_ERROR, "Failed to allocate picture.\n");

            VAR_4 = AVERROR(ENOMEM);

            goto done;

        }



        

        for (VAR_5 = 0; VAR_5 < 3; ++VAR_5) {

            uint8_t *dst = img_in->data[VAR_5];

            uint8_t *src = VAR_2->data[VAR_5];

            int VAR_6 = (VAR_5 == 0) ? VAR_2->VAR_6 : (VAR_2->VAR_6 / 2);

            int VAR_7 = (VAR_5 == 0) ? VAR_2->VAR_7 : (VAR_2->VAR_7 / 2);

            int VAR_8 = 0;

            for (VAR_8 = 0; VAR_8 < VAR_7; ++VAR_8) {

                memcpy(dst, src, VAR_6);

                src += VAR_2->linesize[VAR_5];

                dst += VAR_6;

            }

        }

    }



    if (!ctx->api->encoder_encode(ctx->encoder, img_in,

                                  &data_out, &len_out,

                                  NULL, NULL,

                                  &frame_info)) {

        av_log(VAR_0, AV_LOG_ERROR, "Failed to encode VAR_2.\n");

        VAR_4 = AVERROR_EXTERNAL;

        goto done;

    }



    if (data_out) {

        kvz_data_chunk *chunk = NULL;

        uint64_t written = 0;



        VAR_4 = ff_alloc_packet(VAR_1, len_out);

        if (VAR_4 < 0) {

            av_log(VAR_0, AV_LOG_ERROR, "Failed to allocate output packet.\n");

            goto done;

        }



        for (chunk = data_out; chunk != NULL; chunk = chunk->next) {

            av_assert0(written + chunk->len <= len_out);

            memcpy(VAR_1->data + written, chunk->data, chunk->len);

            written += chunk->len;

        }

        *VAR_3 = 1;



        ctx->api->chunk_free(data_out);

        data_out = NULL;



        VAR_1->flags = 0;

        

        

        if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&

                frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {

            VAR_1->flags |= AV_PKT_FLAG_KEY;

        }

    }



done:

    ctx->api->picture_free(img_in);

    ctx->api->chunk_free(data_out);

    return VAR_4;

}

static int FUNC_0(AVCodecContext *VAR_0,
AVPacket *VAR_1,
const AVFrame *VAR_2,
int *VAR_3)
{ int VAR_4 = 0; kvz_picture *img_in = NULL; kvz_data_chunk *data_out = NULL; uint32_t len_out = 0; kvz_frame_info frame_info; LibkvazaarContext *ctx = VAR_0->priv_data; *VAR_3 = 0; if (VAR_2) { int VAR_5 = 0; av_assert0(VAR_2->VAR_6 == ctx->config->VAR_6); av_assert0(VAR_2->VAR_7 == ctx->config->VAR_7); av_assert0(VAR_2->format == VAR_0->pix_fmt); img_in = ctx->api->picture_alloc(VAR_2->VAR_6, VAR_2->VAR_7); if (!img_in) { av_log(VAR_0, AV_LOG_ERROR, "Failed to allocate picture.\n"); VAR_4 = AVERROR(ENOMEM); goto done; } for (VAR_5 = 0; VAR_5 < 3; ++VAR_5) { uint8_t *dst = img_in->data[VAR_5]; uint8_t *src = VAR_2->data[VAR_5]; int VAR_6 = (VAR_5 == 0) ? VAR_2->VAR_6 : (VAR_2->VAR_6 / 2); int VAR_7 = (VAR_5 == 0) ? VAR_2->VAR_7 : (VAR_2->VAR_7 / 2); int VAR_8 = 0; for (VAR_8 = 0; VAR_8 < VAR_7; ++VAR_8) { memcpy(dst, src, VAR_6); src += VAR_2->linesize[VAR_5]; dst += VAR_6; } } } if (!ctx->api->encoder_encode(ctx->encoder, img_in,
&data_out, &len_out,
NULL, NULL,
&frame_info)) { av_log(VAR_0, AV_LOG_ERROR, "Failed to encode VAR_2.\n"); VAR_4 = AVERROR_EXTERNAL; goto done; } if (data_out) { kvz_data_chunk *chunk = NULL; uint64_t written = 0; VAR_4 = ff_alloc_packet(VAR_1, len_out); if (VAR_4 < 0) { av_log(VAR_0, AV_LOG_ERROR, "Failed to allocate output packet.\n"); goto done; } for (chunk = data_out; chunk != NULL; chunk = chunk->next) { av_assert0(written + chunk->len <= len_out); memcpy(VAR_1->data + written, chunk->data, chunk->len); written += chunk->len; } *VAR_3 = 1; ctx->api->chunk_free(data_out); data_out = NULL; VAR_1->flags = 0; if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) { VAR_1->flags |= AV_PKT_FLAG_KEY; } } done:
ctx->api->picture_free(img_in); ctx->api->chunk_free(data_out); return VAR_4; }