}else if(s->max_b_frames==0){ s->input_pict_type = P_TYPE; }else{ if(s->b_frames_since_non_b < s->max_b_frames) s->input_pict_type = B_TYPE; else s->input_pict_type = P_TYPE;
int MPV_encode_picture(AVCodecContext *avctx,                       unsigned char *buf, int buf_size, void *data){    MpegEncContext *s = avctx->priv_data;    AVPicture *pict = data;    s->input_qscale = avctx->quality;    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);    s->force_input_type= (avctx->flags&CODEC_FLAG_TYPE) ?	(avctx->key_frame ? I_TYPE : P_TYPE) : 0;    if (!s->intra_only) {        /* first picture of GOP is intra */        if (s->input_picture_in_gop_number % s->gop_size==0 || s->force_input_type==I_TYPE){            s->input_picture_in_gop_number=0;            s->input_pict_type = I_TYPE;        }else if(s->max_b_frames==0){            s->input_pict_type = P_TYPE;        }else{            if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ                s->input_pict_type = B_TYPE;            else                s->input_pict_type = P_TYPE;        }    } else {        s->input_pict_type = I_TYPE;    }    reorder_input(s, pict);        /* output? */    if(s->coded_order[0].picture[0]){        s->pict_type= s->coded_order[0].pict_type;        if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */            s->qscale= s->coded_order[0].qscale;        s->force_type= s->coded_order[0].force_type;        s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;        s->picture_number= s->coded_order[0].picture_number;        MPV_frame_start(s);        encode_picture(s, s->picture_number);        avctx->key_frame = (s->pict_type == I_TYPE);        avctx->header_bits = s->header_bits;        avctx->mv_bits     = s->mv_bits;        avctx->misc_bits   = s->misc_bits;        avctx->i_tex_bits  = s->i_tex_bits;        avctx->p_tex_bits  = s->p_tex_bits;        avctx->i_count     = s->i_count;        avctx->p_count     = s->p_count;        avctx->skip_count  = s->skip_count;        MPV_frame_end(s);        if (s->out_format == FMT_MJPEG)            mjpeg_picture_trailer(s);        avctx->quality = s->qscale;    }    s->input_picture_number++;    s->input_picture_in_gop_number++;    flush_put_bits(&s->pb);    s->last_frame_bits= s->frame_bits;    s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;    s->total_bits += s->frame_bits;    avctx->frame_bits  = s->frame_bits;//printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits);    if (avctx->get_psnr) {        /* At this point pict->data should have the original frame   */        /* an s->current_picture should have the coded/decoded frame */        get_psnr(pict->data, s->current_picture,                 pict->linesize, s->linesize, avctx);//        printf("%f\n", avctx->psnr_y);    }    return pbBufPtr(&s->pb) - s->pb.buf;}