int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
    int i;
    int w= s->width;
    int h= s->height;
    InternalBuffer *buf;
    int *picture_number;
    if(pic->data[0]!=NULL) {
        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
        return -1;
    }
    if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
        av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
        return -1;
    }
    if(avcodec_check_dimensions(s,w,h))
        return -1;
    if(s->internal_buffer==NULL){
        s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
    }
#if 0
    s->internal_buffer= av_fast_realloc(
        s->internal_buffer,
        &s->internal_buffer_size,
        sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
        );
#endif
    buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
    picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
    (*picture_number)++;
    if(buf->base[0]){
        pic->age= *picture_number - buf->last_pic_num;
        buf->last_pic_num= *picture_number;
    }else{
        int h_chroma_shift, v_chroma_shift;
        int pixel_size, size[3];
        AVPicture picture;
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
        if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
            w+= EDGE_WIDTH*2;
            h+= EDGE_WIDTH*2;
        }
        avpicture_fill(&picture, NULL, s->pix_fmt, w, h);
        pixel_size= picture.linesize[0]*8 / w;
//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d\n", (int)picture.data[1], w, h, s->pix_fmt);
        assert(pixel_size>=1);
            //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
        if(pixel_size == 3*8)
            w= ALIGN(w, STRIDE_ALIGN<<h_chroma_shift);
        else
            w= ALIGN(pixel_size*w, STRIDE_ALIGN<<(h_chroma_shift+3)) / pixel_size;
        size[1] = avpicture_fill(&picture, NULL, s->pix_fmt, w, h);
        size[0] = picture.linesize[0] * h;
        size[1] -= size[0];
        if(picture.data[2])
            size[1]= size[2]= size[1]/2;
        else
            size[2]= 0;
        buf->last_pic_num= -256*256*256*64;
        memset(buf->base, 0, sizeof(buf->base));
        memset(buf->data, 0, sizeof(buf->data));
        for(i=0; i<3 && size[i]; i++){
            const int h_shift= i==0 ? 0 : h_chroma_shift;
            const int v_shift= i==0 ? 0 : v_chroma_shift;
            buf->linesize[i]= picture.linesize[i];
            buf->base[i]= av_malloc(size[i]+16); //FIXME 16
            if(buf->base[i]==NULL) return -1;
            memset(buf->base[i], 128, size[i]);
            // no edge if EDEG EMU or not planar YUV, we check for PAL8 redundantly to protect against a exploitable bug regression ...
            if((s->flags&CODEC_FLAG_EMU_EDGE) || (s->pix_fmt == PIX_FMT_PAL8) || !size[2])
                buf->data[i] = buf->base[i];
            else
                buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), STRIDE_ALIGN);
        }
        pic->age= 256*256*256*64;
    }
    pic->type= FF_BUFFER_TYPE_INTERNAL;
    for(i=0; i<4; i++){
        pic->base[i]= buf->base[i];
        pic->data[i]= buf->data[i];
        pic->linesize[i]= buf->linesize[i];
    }
    s->internal_buffer_count++;
    return 0;
}
int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
    int i;
    int w= s->width;
    int h= s->height;
    InternalBuffer *buf;
    int *picture_number;
    if(pic->data[0]!=NULL) {
        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
        return -1;
    }
    if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
        av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
        return -1;
    }
    if(avcodec_check_dimensions(s,w,h))
        return -1;
    if(s->internal_buffer==NULL){
        s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
    }
#if 0
    s->internal_buffer= av_fast_realloc(
        s->internal_buffer,
        &s->internal_buffer_size,
        sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)
        );
#endif
    buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
    picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; 
    (*picture_number)++;
    if(buf->base[0]){
        pic->age= *picture_number - buf->last_pic_num;
        buf->last_pic_num= *picture_number;
    }else{
        int h_chroma_shift, v_chroma_shift;
        int pixel_size, size[3];
        AVPicture picture;
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
        if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
            w+= EDGE_WIDTH*2;
            h+= EDGE_WIDTH*2;
        }
        avpicture_fill(&picture, NULL, s->pix_fmt, w, h);
        pixel_size= picture.linesize[0]*8 / w;

        assert(pixel_size>=1);
            
        if(pixel_size == 3*8)
            w= ALIGN(w, STRIDE_ALIGN<<h_chroma_shift);
        else
            w= ALIGN(pixel_size*w, STRIDE_ALIGN<<(h_chroma_shift+3)) / pixel_size;
        size[1] = avpicture_fill(&picture, NULL, s->pix_fmt, w, h);
        size[0] = picture.linesize[0] * h;
        size[1] -= size[0];
        if(picture.data[2])
            size[1]= size[2]= size[1]/2;
        else
            size[2]= 0;
        buf->last_pic_num= -256*256*256*64;
        memset(buf->base, 0, sizeof(buf->base));
        memset(buf->data, 0, sizeof(buf->data));
        for(i=0; i<3 && size[i]; i++){
            const int h_shift= i==0 ? 0 : h_chroma_shift;
            const int v_shift= i==0 ? 0 : v_chroma_shift;
            buf->linesize[i]= picture.linesize[i];
            buf->base[i]= av_malloc(size[i]+16); 
            if(buf->base[i]==NULL) return -1;
            memset(buf->base[i], 128, size[i]);
            
            if((s->flags&CODEC_FLAG_EMU_EDGE) || (s->pix_fmt == PIX_FMT_PAL8) || !size[2])
                buf->data[i] = buf->base[i];
            else
                buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), STRIDE_ALIGN);
        }
        pic->age= 256*256*256*64;
    }
    pic->type= FF_BUFFER_TYPE_INTERNAL;
    for(i=0; i<4; i++){
        pic->base[i]= buf->base[i];
        pic->data[i]= buf->data[i];
        pic->linesize[i]= buf->linesize[i];
    }
    s->internal_buffer_count++;
    return 0;
}
int FUNC_0(AVCodecContext *VAR_0, AVFrame *VAR_1){
    int VAR_2;
    int VAR_3= VAR_0->width;
    int VAR_4= VAR_0->height;
    InternalBuffer *buf;
    int *VAR_5;
    if(VAR_1->data[0]!=NULL) {
        av_log(VAR_0, AV_LOG_ERROR, "VAR_1->data[0]!=NULL in FUNC_0\n");
        return -1;
    }
    if(VAR_0->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
        av_log(VAR_0, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
        return -1;
    }
    if(avcodec_check_dimensions(VAR_0,VAR_3,VAR_4))
        return -1;
    if(VAR_0->internal_buffer==NULL){
        VAR_0->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
    }
#if 0
    VAR_0->internal_buffer= av_fast_realloc(
        VAR_0->internal_buffer,
        &VAR_0->internal_buffer_size,
        sizeof(InternalBuffer)*FFMAX(99,  VAR_0->internal_buffer_count+1)
        );
#endif
    buf= &((InternalBuffer*)VAR_0->internal_buffer)[VAR_0->internal_buffer_count];
    VAR_5= &(((InternalBuffer*)VAR_0->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; 
    (*VAR_5)++;
    if(buf->base[0]){
        VAR_1->age= *VAR_5 - buf->last_pic_num;
        buf->last_pic_num= *VAR_5;
    }else{
        int VAR_6, VAR_7;
        int VAR_8, VAR_9[3];
        AVPicture picture;
        avcodec_get_chroma_sub_sample(VAR_0->pix_fmt, &VAR_6, &VAR_7);
        if(!(VAR_0->flags&CODEC_FLAG_EMU_EDGE)){
            VAR_3+= EDGE_WIDTH*2;
            VAR_4+= EDGE_WIDTH*2;
        }
        avpicture_fill(&picture, NULL, VAR_0->pix_fmt, VAR_3, VAR_4);
        VAR_8= picture.linesize[0]*8 / VAR_3;

        assert(VAR_8>=1);
            
        if(VAR_8 == 3*8)
            VAR_3= ALIGN(VAR_3, STRIDE_ALIGN<<VAR_6);
        else
            VAR_3= ALIGN(VAR_8*VAR_3, STRIDE_ALIGN<<(VAR_6+3)) / VAR_8;
        VAR_9[1] = avpicture_fill(&picture, NULL, VAR_0->pix_fmt, VAR_3, VAR_4);
        VAR_9[0] = picture.linesize[0] * VAR_4;
        VAR_9[1] -= VAR_9[0];
        if(picture.data[2])
            VAR_9[1]= VAR_9[2]= VAR_9[1]/2;
        else
            VAR_9[2]= 0;
        buf->last_pic_num= -256*256*256*64;
        memset(buf->base, 0, sizeof(buf->base));
        memset(buf->data, 0, sizeof(buf->data));
        for(VAR_2=0; VAR_2<3 && VAR_9[VAR_2]; VAR_2++){
            const int VAR_10= VAR_2==0 ? 0 : VAR_6;
            const int VAR_11= VAR_2==0 ? 0 : VAR_7;
            buf->linesize[VAR_2]= picture.linesize[VAR_2];
            buf->base[VAR_2]= av_malloc(VAR_9[VAR_2]+16); 
            if(buf->base[VAR_2]==NULL) return -1;
            memset(buf->base[VAR_2], 128, VAR_9[VAR_2]);
            
            if((VAR_0->flags&CODEC_FLAG_EMU_EDGE) || (VAR_0->pix_fmt == PIX_FMT_PAL8) || !VAR_9[2])
                buf->data[VAR_2] = buf->base[VAR_2];
            else
                buf->data[VAR_2] = buf->base[VAR_2] + ALIGN((buf->linesize[VAR_2]*EDGE_WIDTH>>VAR_11) + (EDGE_WIDTH>>VAR_10), STRIDE_ALIGN);
        }
        VAR_1->age= 256*256*256*64;
    }
    VAR_1->type= FF_BUFFER_TYPE_INTERNAL;
    for(VAR_2=0; VAR_2<4; VAR_2++){
        VAR_1->base[VAR_2]= buf->base[VAR_2];
        VAR_1->data[VAR_2]= buf->data[VAR_2];
        VAR_1->linesize[VAR_2]= buf->linesize[VAR_2];
    }
    VAR_0->internal_buffer_count++;
    return 0;
}
int FUNC_0(AVCodecContext *VAR_0, AVFrame *VAR_1){ int VAR_2; int VAR_3= VAR_0->width; int VAR_4= VAR_0->height; InternalBuffer *buf; int *VAR_5; if(VAR_1->data[0]!=NULL) { av_log(VAR_0, AV_LOG_ERROR, "VAR_1->data[0]!=NULL in FUNC_0\n"); return -1; } if(VAR_0->internal_buffer_count >= INTERNAL_BUFFER_SIZE) { av_log(VAR_0, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n"); return -1; } if(avcodec_check_dimensions(VAR_0,VAR_3,VAR_4))
return -1; if(VAR_0->internal_buffer==NULL){ VAR_0->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer)); } #if 0
VAR_0->internal_buffer= av_fast_realloc(
VAR_0->internal_buffer,
&VAR_0->internal_buffer_size,
sizeof(InternalBuffer)*FFMAX(99,  VAR_0->internal_buffer_count+1)
); #endif
buf= &((InternalBuffer*)VAR_0->internal_buffer)[VAR_0->internal_buffer_count]; VAR_5= &(((InternalBuffer*)VAR_0->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; (*VAR_5)++; if(buf->base[0]){ VAR_1->age= *VAR_5 - buf->last_pic_num; buf->last_pic_num= *VAR_5; }else{ int VAR_6, VAR_7; int VAR_8, VAR_9[3]; AVPicture picture; avcodec_get_chroma_sub_sample(VAR_0->pix_fmt, &VAR_6, &VAR_7); if(!(VAR_0->flags&CODEC_FLAG_EMU_EDGE)){ VAR_3+= EDGE_WIDTH*2; VAR_4+= EDGE_WIDTH*2; } avpicture_fill(&picture, NULL, VAR_0->pix_fmt, VAR_3, VAR_4); VAR_8= picture.linesize[0]*8 / VAR_3; assert(VAR_8>=1); if(VAR_8 == 3*8)
VAR_3= ALIGN(VAR_3, STRIDE_ALIGN<<VAR_6); else
VAR_3= ALIGN(VAR_8*VAR_3, STRIDE_ALIGN<<(VAR_6+3)) / VAR_8; VAR_9[1] = avpicture_fill(&picture, NULL, VAR_0->pix_fmt, VAR_3, VAR_4); VAR_9[0] = picture.linesize[0] * VAR_4; VAR_9[1] -= VAR_9[0]; if(picture.data[2])
VAR_9[1]= VAR_9[2]= VAR_9[1]/2; else
VAR_9[2]= 0; buf->last_pic_num= -256*256*256*64; memset(buf->base, 0, sizeof(buf->base)); memset(buf->data, 0, sizeof(buf->data)); for(VAR_2=0; VAR_2<3 && VAR_9[VAR_2]; VAR_2++){ const int VAR_10= VAR_2==0 ? 0 : VAR_6; const int VAR_11= VAR_2==0 ? 0 : VAR_7; buf->linesize[VAR_2]= picture.linesize[VAR_2]; buf->base[VAR_2]= av_malloc(VAR_9[VAR_2]+16); if(buf->base[VAR_2]==NULL) return -1; memset(buf->base[VAR_2], 128, VAR_9[VAR_2]); if((VAR_0->flags&CODEC_FLAG_EMU_EDGE) || (VAR_0->pix_fmt == PIX_FMT_PAL8) || !VAR_9[2])
buf->data[VAR_2] = buf->base[VAR_2]; else
buf->data[VAR_2] = buf->base[VAR_2] + ALIGN((buf->linesize[VAR_2]*EDGE_WIDTH>>VAR_11) + (EDGE_WIDTH>>VAR_10), STRIDE_ALIGN); } VAR_1->age= 256*256*256*64; } VAR_1->type= FF_BUFFER_TYPE_INTERNAL; for(VAR_2=0; VAR_2<4; VAR_2++){ VAR_1->base[VAR_2]= buf->base[VAR_2]; VAR_1->data[VAR_2]= buf->data[VAR_2]; VAR_1->linesize[VAR_2]= buf->linesize[VAR_2]; } VAR_0->internal_buffer_count++; return 0; }