comparison mpegvideo.c @ 3068:9aa8d8587bea libavcodec

fixing bframe strategy 2 bits vs. bytes factor of 8 error 16 byte offset error some other minor things
author michael
date Fri, 27 Jan 2006 13:19:13 +0000
parents 0b546eab515d
children 1363655b443c
comparison
equal deleted inserted replaced
3067:d084a83f2117 3068:9aa8d8587bea
2056 pic->data[i]= pic_arg->data[i]; 2056 pic->data[i]= pic_arg->data[i];
2057 pic->linesize[i]= pic_arg->linesize[i]; 2057 pic->linesize[i]= pic_arg->linesize[i];
2058 } 2058 }
2059 alloc_picture(s, (Picture*)pic, 1); 2059 alloc_picture(s, (Picture*)pic, 1);
2060 }else{ 2060 }else{
2061 int offset= 16;
2062 i= ff_find_unused_picture(s, 0); 2061 i= ff_find_unused_picture(s, 0);
2063 2062
2064 pic= (AVFrame*)&s->picture[i]; 2063 pic= (AVFrame*)&s->picture[i];
2065 pic->reference= 3; 2064 pic->reference= 3;
2066 2065
2067 alloc_picture(s, (Picture*)pic, 0); 2066 alloc_picture(s, (Picture*)pic, 0);
2068 2067
2069 if( pic->data[0] + offset == pic_arg->data[0] 2068 if( pic->data[0] + INPLACE_OFFSET == pic_arg->data[0]
2070 && pic->data[1] + offset == pic_arg->data[1] 2069 && pic->data[1] + INPLACE_OFFSET == pic_arg->data[1]
2071 && pic->data[2] + offset == pic_arg->data[2]){ 2070 && pic->data[2] + INPLACE_OFFSET == pic_arg->data[2]){
2072 // empty 2071 // empty
2073 }else{ 2072 }else{
2074 int h_chroma_shift, v_chroma_shift; 2073 int h_chroma_shift, v_chroma_shift;
2075 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift); 2074 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
2076 2075
2080 int h_shift= i ? h_chroma_shift : 0; 2079 int h_shift= i ? h_chroma_shift : 0;
2081 int v_shift= i ? v_chroma_shift : 0; 2080 int v_shift= i ? v_chroma_shift : 0;
2082 int w= s->width >>h_shift; 2081 int w= s->width >>h_shift;
2083 int h= s->height>>v_shift; 2082 int h= s->height>>v_shift;
2084 uint8_t *src= pic_arg->data[i]; 2083 uint8_t *src= pic_arg->data[i];
2085 uint8_t *dst= pic->data[i] + offset; 2084 uint8_t *dst= pic->data[i] + INPLACE_OFFSET;
2086 2085
2087 if(src_stride==dst_stride) 2086 if(src_stride==dst_stride)
2088 memcpy(dst, src, src_stride*h); 2087 memcpy(dst, src, src_stride*h);
2089 else{ 2088 else{
2090 while(h--){ 2089 while(h--){
2145 static int estimate_best_b_count(MpegEncContext *s){ 2144 static int estimate_best_b_count(MpegEncContext *s){
2146 AVCodec *codec= avcodec_find_encoder(s->avctx->codec_id); 2145 AVCodec *codec= avcodec_find_encoder(s->avctx->codec_id);
2147 AVCodecContext *c= avcodec_alloc_context(); 2146 AVCodecContext *c= avcodec_alloc_context();
2148 AVFrame input[FF_MAX_B_FRAMES+2]; 2147 AVFrame input[FF_MAX_B_FRAMES+2];
2149 const int scale= s->avctx->brd_scale; 2148 const int scale= s->avctx->brd_scale;
2150 int i, j, out_size; 2149 int i, j, out_size, p_lambda, b_lambda, lambda2;
2151 int outbuf_size= s->width * s->height; //FIXME 2150 int outbuf_size= s->width * s->height; //FIXME
2152 uint8_t *outbuf= av_malloc(outbuf_size); 2151 uint8_t *outbuf= av_malloc(outbuf_size);
2153 ImgReSampleContext *resample; 2152 ImgReSampleContext *resample;
2154 int64_t best_rd= INT64_MAX; 2153 int64_t best_rd= INT64_MAX;
2155 int best_b_count= -1; 2154 int best_b_count= -1;
2156 const int lambda2= s->lambda2; 2155
2156 // emms_c();
2157 p_lambda= s->last_lambda_for[P_TYPE]; //s->next_picture_ptr->quality;
2158 b_lambda= s->last_lambda_for[B_TYPE]; //p_lambda *ABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
2159 if(!b_lambda) b_lambda= p_lambda; //FIXME we should do this somewhere else
2160 lambda2= (b_lambda*b_lambda + (1<<FF_LAMBDA_SHIFT)/2 ) >> FF_LAMBDA_SHIFT;
2157 2161
2158 c->width = s->width >> scale; 2162 c->width = s->width >> scale;
2159 c->height= s->height>> scale; 2163 c->height= s->height>> scale;
2160 c->flags= CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/; 2164 c->flags= CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/;
2161 c->flags|= s->avctx->flags & CODEC_FLAG_QPEL; 2165 c->flags|= s->avctx->flags & CODEC_FLAG_QPEL;
2173 resample= img_resample_init(c->width, c->height, s->width, s->height); //FIXME use sws 2177 resample= img_resample_init(c->width, c->height, s->width, s->height); //FIXME use sws
2174 2178
2175 for(i=0; i<s->max_b_frames+2; i++){ 2179 for(i=0; i<s->max_b_frames+2; i++){
2176 int ysize= c->width*c->height; 2180 int ysize= c->width*c->height;
2177 int csize= (c->width/2)*(c->height/2); 2181 int csize= (c->width/2)*(c->height/2);
2182 Picture pre_input, *pre_input_ptr= i ? s->input_picture[i-1] : s->next_picture_ptr;
2183
2184 if(pre_input_ptr)
2185 pre_input= *pre_input_ptr;
2186
2187 if(pre_input.type != FF_BUFFER_TYPE_SHARED && i){
2188 pre_input.data[0]+=INPLACE_OFFSET;
2189 pre_input.data[1]+=INPLACE_OFFSET;
2190 pre_input.data[2]+=INPLACE_OFFSET;
2191 }
2178 2192
2179 avcodec_get_frame_defaults(&input[i]); 2193 avcodec_get_frame_defaults(&input[i]);
2180 input[i].data[0]= av_malloc(ysize + 2*csize); 2194 input[i].data[0]= av_malloc(ysize + 2*csize);
2181 input[i].data[1]= input[i].data[0] + ysize; 2195 input[i].data[1]= input[i].data[0] + ysize;
2182 input[i].data[2]= input[i].data[1] + csize; 2196 input[i].data[2]= input[i].data[1] + csize;
2183 input[i].linesize[0]= c->width; 2197 input[i].linesize[0]= c->width;
2184 input[i].linesize[1]= 2198 input[i].linesize[1]=
2185 input[i].linesize[2]= c->width/2; 2199 input[i].linesize[2]= c->width/2;
2186 2200
2187 if(!i || s->input_picture[i-1]) 2201 if(!i || s->input_picture[i-1])
2188 img_resample(resample, &input[i], i ? s->input_picture[i-1] : s->next_picture_ptr); 2202 img_resample(resample, &input[i], &pre_input);
2189 } 2203 }
2190 2204
2191 for(j=0; j<s->max_b_frames+1; j++){ 2205 for(j=0; j<s->max_b_frames+1; j++){
2192 int64_t rd=0; 2206 int64_t rd=0;
2193 2207
2195 break; 2209 break;
2196 2210
2197 c->error[0]= c->error[1]= c->error[2]= 0; 2211 c->error[0]= c->error[1]= c->error[2]= 0;
2198 2212
2199 input[0].pict_type= I_TYPE; 2213 input[0].pict_type= I_TYPE;
2200 input[0].quality= 2 * FF_QP2LAMBDA; 2214 input[0].quality= 1 * FF_QP2LAMBDA;
2201 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]); 2215 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]);
2202 rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT; 2216 // rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
2203 2217
2204 for(i=0; i<s->max_b_frames+1; i++){ 2218 for(i=0; i<s->max_b_frames+1; i++){
2205 int is_p= i % (j+1) == j || i==s->max_b_frames; 2219 int is_p= i % (j+1) == j || i==s->max_b_frames;
2206 2220
2207 input[i+1].pict_type= is_p ? P_TYPE : B_TYPE; 2221 input[i+1].pict_type= is_p ? P_TYPE : B_TYPE;
2208 input[i+1].quality= s->last_lambda_for[input[i+1].pict_type]; 2222 input[i+1].quality= is_p ? p_lambda : b_lambda;
2209 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i+1]); 2223 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i+1]);
2210 rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT; 2224 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
2211 } 2225 }
2212 2226
2213 /* get the delayed frames */ 2227 /* get the delayed frames */
2214 while(out_size){ 2228 while(out_size){
2215 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL); 2229 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
2216 rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT; 2230 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
2217 } 2231 }
2218 2232
2219 rd += c->error[0] + c->error[1] + c->error[2]; 2233 rd += c->error[0] + c->error[1] + c->error[2];
2220 2234
2221 if(rd < best_rd){ 2235 if(rd < best_rd){
2389 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER 2403 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
2390 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL); 2404 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
2391 2405
2392 s->current_picture_ptr= s->reordered_input_picture[0]; 2406 s->current_picture_ptr= s->reordered_input_picture[0];
2393 for(i=0; i<4; i++){ 2407 for(i=0; i<4; i++){
2394 s->new_picture.data[i]+=16; 2408 s->new_picture.data[i]+= INPLACE_OFFSET;
2395 } 2409 }
2396 } 2410 }
2397 copy_picture(&s->current_picture, s->current_picture_ptr); 2411 copy_picture(&s->current_picture, s->current_picture_ptr);
2398 2412
2399 s->picture_number= s->new_picture.display_picture_number; 2413 s->picture_number= s->new_picture.display_picture_number;