if(s->reordered_input_picture[0]->pict_type==B_TYPE){ s->reordered_input_picture[0]->reference=0;
s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE;  if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){ int i= find_unused_picture(s, 0); Picture *pic= &s->picture[i];  s->new_picture= *s->reordered_input_picture[0];   for(i=0; i<4; i++) s->reordered_input_picture[0]->data[i]= NULL; s->reordered_input_picture[0]->type= 0;  pic->pict_type = s->reordered_input_picture[0]->pict_type; pic->quality   = s->reordered_input_picture[0]->quality; pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number; pic->reference = s->reordered_input_picture[0]->reference;  alloc_picture(s, pic, 0);  s->current_picture= *pic; }else{ assert(   s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);  s->new_picture= *s->reordered_input_picture[0];  for(i=0; i<4; i++){ s->reordered_input_picture[0]->data[i]-=16; } s->current_picture= *s->reordered_input_picture[0];
static void select_input_picture(MpegEncContext *s){    int i;    const int encoding_delay= s->max_b_frames;    int coded_pic_num=0;        if(s->reordered_input_picture[0])        coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;//printf("cpn:%d\n", coded_pic_num);    for(i=1; i<MAX_PICTURE_COUNT; i++)        s->reordered_input_picture[i-1]= s->reordered_input_picture[i];    s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;    /* set next picture types & ordering */    if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){        if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture.data[0]==NULL || s->intra_only){            s->reordered_input_picture[0]= s->input_picture[0];            s->reordered_input_picture[0]->pict_type= I_TYPE;            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;        }else{            int b_frames;                        if(s->flags&CODEC_FLAG_PASS2){                for(i=0; i<s->max_b_frames+1; i++){                    int pict_num= s->input_picture[0]->display_picture_number + i;                    int pict_type= s->rc_context.entry[pict_num].new_pict_type;                    s->input_picture[i]->pict_type= pict_type;                                        if(i + 1 >= s->rc_context.num_entries) break;                }            }                    if(s->input_picture[0]->pict_type){                /* user selected pict_type */                for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){                    if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;                }                            if(b_frames > s->max_b_frames){                    fprintf(stderr, "warning, too many bframes in a row\n");                    b_frames = s->max_b_frames;                }            }else if(s->b_frame_strategy==0){                b_frames= s->max_b_frames;            }else if(s->b_frame_strategy==1){                for(i=1; i<s->max_b_frames+1; i++){                    if(s->input_picture[i]->b_frame_score==0){                        s->input_picture[i]->b_frame_score=                             get_intra_count(s, s->input_picture[i  ]->data[0] + 16,                                                s->input_picture[i-1]->data[0] + 16, s->linesize) + 1;                    }                }                for(i=0; i<s->max_b_frames; i++){                    if(s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;                }                                                b_frames= FFMAX(0, i-1);                                /* reset scores */                for(i=0; i<b_frames+1; i++){                    s->input_picture[i]->b_frame_score=0;                }            }else{                fprintf(stderr, "illegal b frame strategy\n");                b_frames=0;            }            emms_c();//static int b_count=0;//b_count+= b_frames;//printf("b_frames: %d\n", b_count);                                    s->reordered_input_picture[0]= s->input_picture[b_frames];            if(   s->picture_in_gop_number + b_frames >= s->gop_size                || s->reordered_input_picture[0]->pict_type== I_TYPE)                s->reordered_input_picture[0]->pict_type= I_TYPE;            else                s->reordered_input_picture[0]->pict_type= P_TYPE;            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;            for(i=0; i<b_frames; i++){                coded_pic_num++;                s->reordered_input_picture[i+1]= s->input_picture[i];                s->reordered_input_picture[i+1]->pict_type= B_TYPE;                s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;            }        }    }        if(s->reordered_input_picture[0]){        if(s->reordered_input_picture[0]->pict_type==B_TYPE){            s->reordered_input_picture[0]->reference=0;        }        s->current_picture= *s->reordered_input_picture[0];        s->new_picture= s->current_picture;        s->new_picture.data[0]+=16;        s->new_picture.data[1]+=16;        s->new_picture.data[2]+=16;            s->picture_number= s->new_picture.display_picture_number;//printf("dpn:%d\n", s->picture_number);    }else{       memset(&s->new_picture, 0, sizeof(Picture));    }}