Revision 984b8e4d libavcodec/mpegvideo.c

View differences:

libavcodec/mpegvideo.c
2141 2141
    return 0;
2142 2142
}
2143 2143

  
2144
static int estimate_best_b_count(MpegEncContext *s){
2145
    AVCodec *codec= avcodec_find_encoder(s->avctx->codec_id);
2146
    AVCodecContext *c= avcodec_alloc_context();
2147
    AVFrame input[FF_MAX_B_FRAMES+2];
2148
    const int scale= 0;
2149
    int i, j, out_size;
2150
    int outbuf_size= (s->width * s->height) >> (2*scale); //FIXME
2151
    uint8_t *outbuf= av_malloc(outbuf_size);
2152
    ImgReSampleContext *resample;
2153
    int64_t best_rd= INT64_MAX;
2154
    int best_b_count= -1;
2155
    const int lambda2= s->lambda2;
2156

  
2157
    c->width = s->width >> scale;
2158
    c->height= s->height>> scale;
2159
    c->flags= CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/;
2160
    c->flags|= s->avctx->flags & CODEC_FLAG_QPEL;
2161
    c->mb_decision= s->avctx->mb_decision;
2162
    c->me_cmp= s->avctx->me_cmp;
2163
    c->mb_cmp= s->avctx->mb_cmp;
2164
    c->me_sub_cmp= s->avctx->me_sub_cmp;
2165
    c->pix_fmt = PIX_FMT_YUV420P;
2166
    c->time_base= s->avctx->time_base;
2167
    c->max_b_frames= s->max_b_frames;
2168

  
2169
    if (avcodec_open(c, codec) < 0)
2170
        return -1;
2171

  
2172
    resample= img_resample_init(c->width, c->height, s->width, s->height); //FIXME use sws
2173

  
2174
    for(i=0; i<s->max_b_frames+2; i++){
2175
        int ysize= c->width*c->height;
2176
        int csize= (c->width/2)*(c->height/2);
2177

  
2178
        avcodec_get_frame_defaults(&input[i]);
2179
        input[i].data[0]= av_malloc(ysize + 2*csize);
2180
        input[i].data[1]= input[i].data[0] + ysize;
2181
        input[i].data[2]= input[i].data[1] + csize;
2182
        input[i].linesize[0]= c->width;
2183
        input[i].linesize[1]=
2184
        input[i].linesize[2]= c->width/2;
2185

  
2186
        if(!i || s->input_picture[i-1])
2187
            img_resample(resample, &input[i], i ? s->input_picture[i-1] : s->next_picture_ptr);
2188
    }
2189

  
2190
    for(j=0; j<s->max_b_frames+1; j++){
2191
        int64_t rd=0;
2192

  
2193
        if(!s->input_picture[j])
2194
            break;
2195

  
2196
        c->error[0]= c->error[1]= c->error[2]= 0;
2197

  
2198
        input[0].pict_type= I_TYPE;
2199
        input[0].quality= 2 * FF_QP2LAMBDA;
2200
        out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]);
2201

  
2202
        for(i=0; i<s->max_b_frames+1; i++){
2203
            int is_p= i % (j+1) == j || i==s->max_b_frames;
2204

  
2205
            input[i+1].pict_type= is_p ? P_TYPE : B_TYPE;
2206
            input[i+1].quality= s->rc_context.last_qscale_for[input[i+1].pict_type];
2207
            out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i+1]);
2208
            rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
2209
        }
2210

  
2211
        /* get the delayed frames */
2212
        while(out_size){
2213
            out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
2214
            rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
2215
        }
2216

  
2217
        rd += c->error[0] + c->error[1] + c->error[2];
2218

  
2219
        if(rd < best_rd){
2220
            best_rd= rd;
2221
            best_b_count= j;
2222
        }
2223
    }
2224

  
2225
    av_freep(&outbuf);
2226
    avcodec_close(c);
2227
    av_freep(&c);
2228
    img_resample_close(resample);
2229

  
2230
    for(i=0; i<s->max_b_frames+2; i++){
2231
        av_freep(&input[i].data[0]);
2232
    }
2233

  
2234
    return best_b_count;
2235
}
2236

  
2144 2237
static void select_input_picture(MpegEncContext *s){
2145 2238
    int i;
2146 2239

  
......
2217 2310
                for(i=0; i<b_frames+1; i++){
2218 2311
                    s->input_picture[i]->b_frame_score=0;
2219 2312
                }
2313
            }else if(s->avctx->b_frame_strategy==2){
2314
                b_frames= estimate_best_b_count(s);
2220 2315
            }else{
2221 2316
                av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
2222 2317
                b_frames=0;

Also available in: Unified diff