static int decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src){

    H264Context *h= dst->priv_data, *h1= src->priv_data;

    MpegEncContext * const s = &h->s, * const s1 = &h1->s;

    int inited = s->context_initialized, err;

    int i;



    if(dst == src || !s1->context_initialized) return 0;



    err = ff_mpeg_update_thread_context(dst, src);

    if(err) return err;



    //FIXME handle width/height changing

    if(!inited){

        for(i = 0; i < MAX_SPS_COUNT; i++)

            av_freep(h->sps_buffers + i);



        for(i = 0; i < MAX_PPS_COUNT; i++)

            av_freep(h->pps_buffers + i);



        memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); //copy all fields after MpegEnc

        memset(h->sps_buffers, 0, sizeof(h->sps_buffers));

        memset(h->pps_buffers, 0, sizeof(h->pps_buffers));

        ff_h264_alloc_tables(h);

        context_init(h);



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

            h->rbsp_buffer[i] = NULL;

            h->rbsp_buffer_size[i] = 0;

        }



        h->thread_context[0] = h;



        // frame_start may not be called for the next thread (if it's decoding a bottom field)

        // so this has to be allocated here

        h->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);



        s->dsp.clear_blocks(h->mb);

    }



    //extradata/NAL handling

    h->is_avc          = h1->is_avc;



    //SPS/PPS

    copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS));

    h->sps             = h1->sps;

    copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS));

    h->pps             = h1->pps;



    //Dequantization matrices

    //FIXME these are big - can they be only copied when PPS changes?

    copy_fields(h, h1, dequant4_buffer, dequant4_coeff);



    for(i=0; i<6; i++)

        h->dequant4_coeff[i] = h->dequant4_buffer[0] + (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);



    for(i=0; i<2; i++)

        h->dequant8_coeff[i] = h->dequant8_buffer[0] + (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);



    h->dequant_coeff_pps = h1->dequant_coeff_pps;



    //POC timing

    copy_fields(h, h1, poc_lsb, redundant_pic_count);



    //reference lists

    copy_fields(h, h1, ref_count, intra_gb);

    copy_fields(h, h1, short_ref, cabac_init_idc);



    copy_picture_range(h->short_ref,   h1->short_ref,   32, s, s1);

    copy_picture_range(h->long_ref,    h1->long_ref,    32,  s, s1);

    copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1);



    h->last_slice_type = h1->last_slice_type;



    if(!s->current_picture_ptr) return 0;



    if(!s->dropable) {

        ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);

        h->prev_poc_msb     = h->poc_msb;

        h->prev_poc_lsb     = h->poc_lsb;

    }

    h->prev_frame_num_offset= h->frame_num_offset;

    h->prev_frame_num       = h->frame_num;

    h->outputed_poc         = h->next_outputed_poc;



    return 0;

}

static int decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src){

    H264Context *h= dst->priv_data, *h1= src->priv_data;

    MpegEncContext * const s = &h->s, * const s1 = &h1->s;

    int inited = s->context_initialized, err;

    int i;



    if(dst == src || !s1->context_initialized) return 0;



    err = ff_mpeg_update_thread_context(dst, src);

    if(err) return err;



    

    if(!inited){

        for(i = 0; i < MAX_SPS_COUNT; i++)

            av_freep(h->sps_buffers + i);



        for(i = 0; i < MAX_PPS_COUNT; i++)

            av_freep(h->pps_buffers + i);



        memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); 

        memset(h->sps_buffers, 0, sizeof(h->sps_buffers));

        memset(h->pps_buffers, 0, sizeof(h->pps_buffers));

        ff_h264_alloc_tables(h);

        context_init(h);



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

            h->rbsp_buffer[i] = NULL;

            h->rbsp_buffer_size[i] = 0;

        }



        h->thread_context[0] = h;



        

        

        h->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);



        s->dsp.clear_blocks(h->mb);

    }



    

    h->is_avc          = h1->is_avc;



    

    copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS));

    h->sps             = h1->sps;

    copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS));

    h->pps             = h1->pps;



    

    

    copy_fields(h, h1, dequant4_buffer, dequant4_coeff);



    for(i=0; i<6; i++)

        h->dequant4_coeff[i] = h->dequant4_buffer[0] + (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);



    for(i=0; i<2; i++)

        h->dequant8_coeff[i] = h->dequant8_buffer[0] + (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);



    h->dequant_coeff_pps = h1->dequant_coeff_pps;



    

    copy_fields(h, h1, poc_lsb, redundant_pic_count);



    

    copy_fields(h, h1, ref_count, intra_gb);

    copy_fields(h, h1, short_ref, cabac_init_idc);



    copy_picture_range(h->short_ref,   h1->short_ref,   32, s, s1);

    copy_picture_range(h->long_ref,    h1->long_ref,    32,  s, s1);

    copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1);



    h->last_slice_type = h1->last_slice_type;



    if(!s->current_picture_ptr) return 0;



    if(!s->dropable) {

        ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);

        h->prev_poc_msb     = h->poc_msb;

        h->prev_poc_lsb     = h->poc_lsb;

    }

    h->prev_frame_num_offset= h->frame_num_offset;

    h->prev_frame_num       = h->frame_num;

    h->outputed_poc         = h->next_outputed_poc;



    return 0;

}

    copy_picture_range(h->long_ref,    h1->long_ref,    32,  s, s1);
static int FUNC_0(AVCodecContext *VAR_0, const AVCodecContext *VAR_1){

    H264Context *h= VAR_0->priv_data, *h1= VAR_1->priv_data;

    MpegEncContext * const s = &h->s, * const s1 = &h1->s;

    int VAR_2 = s->context_initialized, VAR_3;

    int VAR_4;



    if(VAR_0 == VAR_1 || !s1->context_initialized) return 0;



    VAR_3 = ff_mpeg_update_thread_context(VAR_0, VAR_1);

    if(VAR_3) return VAR_3;



    

    if(!VAR_2){

        for(VAR_4 = 0; VAR_4 < MAX_SPS_COUNT; VAR_4++)

            av_freep(h->sps_buffers + VAR_4);



        for(VAR_4 = 0; VAR_4 < MAX_PPS_COUNT; VAR_4++)

            av_freep(h->pps_buffers + VAR_4);



        memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); 

        memset(h->sps_buffers, 0, sizeof(h->sps_buffers));

        memset(h->pps_buffers, 0, sizeof(h->pps_buffers));

        ff_h264_alloc_tables(h);

        context_init(h);



        for(VAR_4=0; VAR_4<2; VAR_4++){

            h->rbsp_buffer[VAR_4] = NULL;

            h->rbsp_buffer_size[VAR_4] = 0;

        }



        h->thread_context[0] = h;



        

        

        h->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);



        s->dsp.clear_blocks(h->mb);

    }



    

    h->is_avc          = h1->is_avc;



    

    copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS));

    h->sps             = h1->sps;

    copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS));

    h->pps             = h1->pps;



    

    

    copy_fields(h, h1, dequant4_buffer, dequant4_coeff);



    for(VAR_4=0; VAR_4<6; VAR_4++)

        h->dequant4_coeff[VAR_4] = h->dequant4_buffer[0] + (h1->dequant4_coeff[VAR_4] - h1->dequant4_buffer[0]);



    for(VAR_4=0; VAR_4<2; VAR_4++)

        h->dequant8_coeff[VAR_4] = h->dequant8_buffer[0] + (h1->dequant8_coeff[VAR_4] - h1->dequant8_buffer[0]);



    h->dequant_coeff_pps = h1->dequant_coeff_pps;



    

    copy_fields(h, h1, poc_lsb, redundant_pic_count);



    

    copy_fields(h, h1, ref_count, intra_gb);

    copy_fields(h, h1, short_ref, cabac_init_idc);



    copy_picture_range(h->short_ref,   h1->short_ref,   32, s, s1);

    copy_picture_range(h->long_ref,    h1->long_ref,    32,  s, s1);

    copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1);



    h->last_slice_type = h1->last_slice_type;



    if(!s->current_picture_ptr) return 0;



    if(!s->dropable) {

        ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);

        h->prev_poc_msb     = h->poc_msb;

        h->prev_poc_lsb     = h->poc_lsb;

    }

    h->prev_frame_num_offset= h->frame_num_offset;

    h->prev_frame_num       = h->frame_num;

    h->outputed_poc         = h->next_outputed_poc;



    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0, const AVCodecContext *VAR_1){ H264Context *h= VAR_0->priv_data, *h1= VAR_1->priv_data; MpegEncContext * const s = &h->s, * const s1 = &h1->s; int VAR_2 = s->context_initialized, VAR_3; int VAR_4; if(VAR_0 == VAR_1 || !s1->context_initialized) return 0; VAR_3 = ff_mpeg_update_thread_context(VAR_0, VAR_1); if(VAR_3) return VAR_3; if(!VAR_2){ for(VAR_4 = 0; VAR_4 < MAX_SPS_COUNT; VAR_4++) av_freep(h->sps_buffers + VAR_4); for(VAR_4 = 0; VAR_4 < MAX_PPS_COUNT; VAR_4++) av_freep(h->pps_buffers + VAR_4); memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); memset(h->sps_buffers, 0, sizeof(h->sps_buffers)); memset(h->pps_buffers, 0, sizeof(h->pps_buffers)); ff_h264_alloc_tables(h); context_init(h); for(VAR_4=0; VAR_4<2; VAR_4++){ h->rbsp_buffer[VAR_4] = NULL; h->rbsp_buffer_size[VAR_4] = 0; } h->thread_context[0] = h; h->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize); s->dsp.clear_blocks(h->mb); } h->is_avc          = h1->is_avc; copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS)); h->sps             = h1->sps; copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS)); h->pps             = h1->pps; copy_fields(h, h1, dequant4_buffer, dequant4_coeff); for(VAR_4=0; VAR_4<6; VAR_4++) h->dequant4_coeff[VAR_4] = h->dequant4_buffer[0] + (h1->dequant4_coeff[VAR_4] - h1->dequant4_buffer[0]); for(VAR_4=0; VAR_4<2; VAR_4++) h->dequant8_coeff[VAR_4] = h->dequant8_buffer[0] + (h1->dequant8_coeff[VAR_4] - h1->dequant8_buffer[0]); h->dequant_coeff_pps = h1->dequant_coeff_pps; copy_fields(h, h1, poc_lsb, redundant_pic_count); copy_fields(h, h1, ref_count, intra_gb); copy_fields(h, h1, short_ref, cabac_init_idc); copy_picture_range(h->short_ref,   h1->short_ref,   32, s, s1); copy_picture_range(h->long_ref,    h1->long_ref,    32,  s, s1); copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1); h->last_slice_type = h1->last_slice_type; if(!s->current_picture_ptr) return 0; if(!s->dropable) { ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index); h->prev_poc_msb     = h->poc_msb; h->prev_poc_lsb     = h->poc_lsb; } h->prev_frame_num_offset= h->frame_num_offset; h->prev_frame_num       = h->frame_num; h->outputed_poc         = h->next_outputed_poc; return 0; }