int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)

{

    const AVCodec *orig_codec = dest->codec;

    uint8_t *orig_priv_data = dest->priv_data;



    if (avcodec_is_open(dest)) { // check that the dest context is uninitialized

        av_log(dest, AV_LOG_ERROR,

               "Tried to copy AVCodecContext %p into already-initialized %p\n",

               src, dest);

        return AVERROR(EINVAL);

    }



    av_opt_free(dest);

    av_free(dest->priv_data);



    memcpy(dest, src, sizeof(*dest));



    dest->priv_data       = orig_priv_data;

    dest->codec           = orig_codec;



    /* set values specific to opened codecs back to their default state */

    dest->slice_offset    = NULL;

    dest->hwaccel         = NULL;

    dest->internal        = NULL;



    /* reallocate values that should be allocated separately */

    dest->rc_eq           = NULL;

    dest->extradata       = NULL;

    dest->intra_matrix    = NULL;

    dest->inter_matrix    = NULL;

    dest->rc_override     = NULL;

    dest->subtitle_header = NULL;

    if (src->rc_eq) {

        dest->rc_eq = av_strdup(src->rc_eq);

        if (!dest->rc_eq)

            return AVERROR(ENOMEM);

    }



#define alloc_and_copy_or_fail(obj, size, pad) \

    if (src->obj && size > 0) { \

        dest->obj = av_malloc(size + pad); \

        if (!dest->obj) \

            goto fail; \

        memcpy(dest->obj, src->obj, size); \

        if (pad) \

            memset(((uint8_t *) dest->obj) + size, 0, pad); \

    }

    alloc_and_copy_or_fail(extradata,    src->extradata_size,

                           FF_INPUT_BUFFER_PADDING_SIZE);

    alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);

    alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);

    alloc_and_copy_or_fail(rc_override,  src->rc_override_count * sizeof(*src->rc_override), 0);

    alloc_and_copy_or_fail(subtitle_header, src->subtitle_header_size, 1);

    dest->subtitle_header_size = src->subtitle_header_size;

#undef alloc_and_copy_or_fail



    return 0;



fail:

    av_freep(&dest->rc_override);

    av_freep(&dest->intra_matrix);

    av_freep(&dest->inter_matrix);

    av_freep(&dest->extradata);

    av_freep(&dest->rc_eq);

    return AVERROR(ENOMEM);

}

int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)

{

    const AVCodec *orig_codec = dest->codec;

    uint8_t *orig_priv_data = dest->priv_data;



    if (avcodec_is_open(dest)) { 

        av_log(dest, AV_LOG_ERROR,

               "Tried to copy AVCodecContext %p into already-initialized %p\n",

               src, dest);

        return AVERROR(EINVAL);

    }



    av_opt_free(dest);

    av_free(dest->priv_data);



    memcpy(dest, src, sizeof(*dest));



    dest->priv_data       = orig_priv_data;

    dest->codec           = orig_codec;



    

    dest->slice_offset    = NULL;

    dest->hwaccel         = NULL;

    dest->internal        = NULL;



    

    dest->rc_eq           = NULL;

    dest->extradata       = NULL;

    dest->intra_matrix    = NULL;

    dest->inter_matrix    = NULL;

    dest->rc_override     = NULL;

    dest->subtitle_header = NULL;

    if (src->rc_eq) {

        dest->rc_eq = av_strdup(src->rc_eq);

        if (!dest->rc_eq)

            return AVERROR(ENOMEM);

    }



#define alloc_and_copy_or_fail(obj, size, pad) \

    if (src->obj && size > 0) { \

        dest->obj = av_malloc(size + pad); \

        if (!dest->obj) \

            goto fail; \

        memcpy(dest->obj, src->obj, size); \

        if (pad) \

            memset(((uint8_t *) dest->obj) + size, 0, pad); \

    }

    alloc_and_copy_or_fail(extradata,    src->extradata_size,

                           FF_INPUT_BUFFER_PADDING_SIZE);

    alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);

    alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);

    alloc_and_copy_or_fail(rc_override,  src->rc_override_count * sizeof(*src->rc_override), 0);

    alloc_and_copy_or_fail(subtitle_header, src->subtitle_header_size, 1);

    dest->subtitle_header_size = src->subtitle_header_size;

#undef alloc_and_copy_or_fail



    return 0;



fail:

    av_freep(&dest->rc_override);

    av_freep(&dest->intra_matrix);

    av_freep(&dest->inter_matrix);

    av_freep(&dest->extradata);

    av_freep(&dest->rc_eq);

    return AVERROR(ENOMEM);

}

int FUNC_0(AVCodecContext *VAR_0, const AVCodecContext *VAR_1)

{

    const AVCodec *VAR_2 = VAR_0->codec;

    uint8_t *orig_priv_data = VAR_0->priv_data;



    if (avcodec_is_open(VAR_0)) { 

        av_log(VAR_0, AV_LOG_ERROR,

               "Tried to copy AVCodecContext %p into already-initialized %p\n",

               VAR_1, VAR_0);

        return AVERROR(EINVAL);

    }



    av_opt_free(VAR_0);

    av_free(VAR_0->priv_data);



    memcpy(VAR_0, VAR_1, sizeof(*VAR_0));



    VAR_0->priv_data       = orig_priv_data;

    VAR_0->codec           = VAR_2;



    

    VAR_0->slice_offset    = NULL;

    VAR_0->hwaccel         = NULL;

    VAR_0->internal        = NULL;



    

    VAR_0->rc_eq           = NULL;

    VAR_0->extradata       = NULL;

    VAR_0->intra_matrix    = NULL;

    VAR_0->inter_matrix    = NULL;

    VAR_0->rc_override     = NULL;

    VAR_0->subtitle_header = NULL;

    if (VAR_1->rc_eq) {

        VAR_0->rc_eq = av_strdup(VAR_1->rc_eq);

        if (!VAR_0->rc_eq)

            return AVERROR(ENOMEM);

    }



#define alloc_and_copy_or_fail(obj, size, pad) \

    if (VAR_1->obj && size > 0) { \

        VAR_0->obj = av_malloc(size + pad); \

        if (!VAR_0->obj) \

            goto fail; \

        memcpy(VAR_0->obj, VAR_1->obj, size); \

        if (pad) \

            memset(((uint8_t *) VAR_0->obj) + size, 0, pad); \

    }

    alloc_and_copy_or_fail(extradata,    VAR_1->extradata_size,

                           FF_INPUT_BUFFER_PADDING_SIZE);

    alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);

    alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);

    alloc_and_copy_or_fail(rc_override,  VAR_1->rc_override_count * sizeof(*VAR_1->rc_override), 0);

    alloc_and_copy_or_fail(subtitle_header, VAR_1->subtitle_header_size, 1);

    VAR_0->subtitle_header_size = VAR_1->subtitle_header_size;

#undef alloc_and_copy_or_fail



    return 0;



fail:

    av_freep(&VAR_0->rc_override);

    av_freep(&VAR_0->intra_matrix);

    av_freep(&VAR_0->inter_matrix);

    av_freep(&VAR_0->extradata);

    av_freep(&VAR_0->rc_eq);

    return AVERROR(ENOMEM);

}

int FUNC_0(AVCodecContext *VAR_0, const AVCodecContext *VAR_1)
{ const AVCodec *VAR_2 = VAR_0->codec; uint8_t *orig_priv_data = VAR_0->priv_data; if (avcodec_is_open(VAR_0)) { av_log(VAR_0, AV_LOG_ERROR,
"Tried to copy AVCodecContext %p into already-initialized %p\n",
VAR_1, VAR_0); return AVERROR(EINVAL); } av_opt_free(VAR_0); av_free(VAR_0->priv_data); memcpy(VAR_0, VAR_1, sizeof(*VAR_0)); VAR_0->priv_data       = orig_priv_data; VAR_0->codec           = VAR_2; VAR_0->slice_offset    = NULL; VAR_0->hwaccel         = NULL; VAR_0->internal        = NULL; VAR_0->rc_eq           = NULL; VAR_0->extradata       = NULL; VAR_0->intra_matrix    = NULL; VAR_0->inter_matrix    = NULL; VAR_0->rc_override     = NULL; VAR_0->subtitle_header = NULL; if (VAR_1->rc_eq) { VAR_0->rc_eq = av_strdup(VAR_1->rc_eq); if (!VAR_0->rc_eq)
return AVERROR(ENOMEM); } #define alloc_and_copy_or_fail(obj, size, pad) \
if (VAR_1->obj && size > 0) { \ VAR_0->obj = av_malloc(size + pad); \ if (!VAR_0->obj) \
goto fail; \ memcpy(VAR_0->obj, VAR_1->obj, size); \ if (pad) \
memset(((uint8_t *) VAR_0->obj) + size, 0, pad); \ } alloc_and_copy_or_fail(extradata,    VAR_1->extradata_size,
FF_INPUT_BUFFER_PADDING_SIZE); alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0); alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0); alloc_and_copy_or_fail(rc_override,  VAR_1->rc_override_count * sizeof(*VAR_1->rc_override), 0); alloc_and_copy_or_fail(subtitle_header, VAR_1->subtitle_header_size, 1); VAR_0->subtitle_header_size = VAR_1->subtitle_header_size; #undef alloc_and_copy_or_fail
return 0; fail:
av_freep(&VAR_0->rc_override); av_freep(&VAR_0->intra_matrix); av_freep(&VAR_0->inter_matrix); av_freep(&VAR_0->extradata); av_freep(&VAR_0->rc_eq); return AVERROR(ENOMEM); }