CVS: ffmpeg/libavcodec mpegvideo.c,1.498,1.499
Update of /cvsroot/ffmpeg/ffmpeg/libavcodec In directory mail:/var2/tmp/cvs-serv28677 Modified Files: mpegvideo.c Log Message: b frame strategy 2 Index: mpegvideo.c =================================================================== RCS file: /cvsroot/ffmpeg/ffmpeg/libavcodec/mpegvideo.c,v retrieving revision 1.498 retrieving revision 1.499 diff -u -d -r1.498 -r1.499 --- mpegvideo.c 22 Dec 2005 22:48:09 -0000 1.498 +++ mpegvideo.c 25 Dec 2005 17:30:52 -0000 1.499 @@ -2141,6 +2141,99 @@ return 0; } +static int estimate_best_b_count(MpegEncContext *s){ + AVCodec *codec= avcodec_find_encoder(s->avctx->codec_id); + AVCodecContext *c= avcodec_alloc_context(); + AVFrame input[FF_MAX_B_FRAMES+2]; + const int scale= 0; + int i, j, out_size; + int outbuf_size= (s->width * s->height) >> (2*scale); //FIXME + uint8_t *outbuf= av_malloc(outbuf_size); + ImgReSampleContext *resample; + int64_t best_rd= INT64_MAX; + int best_b_count= -1; + const int lambda2= s->lambda2; + + c->width = s->width >> scale; + c->height= s->height>> scale; + c->flags= CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/; + c->flags|= s->avctx->flags & CODEC_FLAG_QPEL; + c->mb_decision= s->avctx->mb_decision; + c->me_cmp= s->avctx->me_cmp; + c->mb_cmp= s->avctx->mb_cmp; + c->me_sub_cmp= s->avctx->me_sub_cmp; + c->pix_fmt = PIX_FMT_YUV420P; + c->time_base= s->avctx->time_base; + c->max_b_frames= s->max_b_frames; + + if (avcodec_open(c, codec) < 0) + return -1; + + resample= img_resample_init(c->width, c->height, s->width, s->height); //FIXME use sws + + for(i=0; i<s->max_b_frames+2; i++){ + int ysize= c->width*c->height; + int csize= (c->width/2)*(c->height/2); + + avcodec_get_frame_defaults(&input[i]); + input[i].data[0]= av_malloc(ysize + 2*csize); + input[i].data[1]= input[i].data[0] + ysize; + input[i].data[2]= input[i].data[1] + csize; + input[i].linesize[0]= c->width; + input[i].linesize[1]= + input[i].linesize[2]= c->width/2; + + if(!i || s->input_picture[i-1]) + img_resample(resample, &input[i], i ? s->input_picture[i-1] : s->next_picture_ptr); + } + + for(j=0; j<s->max_b_frames+1; j++){ + int64_t rd=0; + + if(!s->input_picture[j]) + break; + + c->error[0]= c->error[1]= c->error[2]= 0; + + input[0].pict_type= I_TYPE; + input[0].quality= 2 * FF_QP2LAMBDA; + out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]); + + for(i=0; i<s->max_b_frames+1; i++){ + int is_p= i % (j+1) == j || i==s->max_b_frames; + + input[i+1].pict_type= is_p ? P_TYPE : B_TYPE; + input[i+1].quality= s->rc_context.last_qscale_for[input[i+1].pict_type]; + out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i+1]); + rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT; + } + + /* get the delayed frames */ + while(out_size){ + out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL); + rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT; + } + + rd += c->error[0] + c->error[1] + c->error[2]; + + if(rd < best_rd){ + best_rd= rd; + best_b_count= j; + } + } + + av_freep(&outbuf); + avcodec_close(c); + av_freep(&c); + img_resample_close(resample); + + for(i=0; i<s->max_b_frames+2; i++){ + av_freep(&input[i].data[0]); + } + + return best_b_count; +} + static void select_input_picture(MpegEncContext *s){ int i; @@ -2217,6 +2310,8 @@ for(i=0; i<b_frames+1; i++){ s->input_picture[i]->b_frame_score=0; } + }else if(s->avctx->b_frame_strategy==2){ + b_frames= estimate_best_b_count(s); }else{ av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n"); b_frames=0;
On Sun, Dec 25, 2005 at 06:30:55PM +0100, Michael Niedermayer CVS wrote:
Update of /cvsroot/ffmpeg/ffmpeg/libavcodec In directory mail:/var2/tmp/cvs-serv28677
Modified Files: mpegvideo.c Log Message: b frame strategy 2
LOL, nothing like brute force... :) I assume this gives results but is insanely slow, right? :) Rich
Hi On Sun, Dec 25, 2005 at 02:50:37PM -0500, Rich Felker wrote:
On Sun, Dec 25, 2005 at 06:30:55PM +0100, Michael Niedermayer CVS wrote:
Update of /cvsroot/ffmpeg/ffmpeg/libavcodec In directory mail:/var2/tmp/cvs-serv28677
Modified Files: mpegvideo.c Log Message: b frame strategy 2
LOL, nothing like brute force... :) I assume this gives results but is insanely slow, right? :)
yes, though you can do the b frame decission on downscaled video with -brd_scale ... which isnt that slow at all, and most important the brute force code gives as a target to aim for when trying to implement a faster b frame decission ... [...] -- Michael
Hi, On 12/25/05, Michael Niedermayer CVS <michael at mplayerhq.hu> wrote:
Update of /cvsroot/ffmpeg/ffmpeg/libavcodec In directory mail:/var2/tmp/cvs-serv28677
Modified Files: mpegvideo.c Log Message: b frame strategy 2
How should I update the man page with? vb_strategy=2: uses an adaptive number of B-frames Smth like that? Is that option supposed to have an impact on encoding speed (compared to 0 or 1)? Guillaume -- Life is pleasant. Death is peaceful. It's the transition that's troublesome. Isaac Asimov (1920 - 1992) MPlayer's doc is offline. Find some fresh one here: http://tuxrip.free.fr//MPlayer-DOCS-HTML/en/ http://tuxrip.free.fr//MPlayer-DOCS-HTML/fr/
Hi On Sun, Dec 25, 2005 at 08:53:00PM +0100, Guillaume POIRIER wrote:
Hi,
On 12/25/05, Michael Niedermayer CVS <michael at mplayerhq.hu> wrote:
Update of /cvsroot/ffmpeg/ffmpeg/libavcodec In directory mail:/var2/tmp/cvs-serv28677
Modified Files: mpegvideo.c Log Message: b frame strategy 2
How should I update the man page with?
vb_strategy=2: uses an adaptive number of B-frames
Smth like that? Is that option supposed to have an impact on encoding speed (compared to 0 or 1)?
2 is slow, and more or less optimal, speed and quality can be tuned with -brd_scale and -vmax_b_frames speed should increase by a factor of 4 every time brd_scale is increased by 1 [...] -- Michael
Hi, On 12/25/05, Michael Niedermayer <michaelni at gmx.at> wrote:
Hi
On Sun, Dec 25, 2005 at 08:53:00PM +0100, Guillaume POIRIER wrote:
Hi,
On 12/25/05, Michael Niedermayer CVS <michael at mplayerhq.hu> wrote:
Update of /cvsroot/ffmpeg/ffmpeg/libavcodec In directory mail:/var2/tmp/cvs-serv28677
Modified Files: mpegvideo.c Log Message: b frame strategy 2
How should I update the man page with?
vb_strategy=2: uses an adaptive number of B-frames
Smth like that? Is that option supposed to have an impact on encoding speed (compared to 0 or 1)?
2 is slow, and more or less optimal, speed and quality can be tuned with -brd_scale and -vmax_b_frames speed should increase by a factor of 4 every time brd_scale is increased by 1
Ok thanks, I'm gonna commit a manpage update shortly with your tips. However, I have one more question: does vb_strategy still only works during the first pass? Guillaume -- Life is pleasant. Death is peaceful. It's the transition that's troublesome. Isaac Asimov (1920 - 1992) MPlayer's doc is offline. Find some fresh one here: http://tuxrip.free.fr//MPlayer-DOCS-HTML/en/ http://tuxrip.free.fr//MPlayer-DOCS-HTML/fr/
Hi On Sun, Dec 25, 2005 at 10:28:23PM +0100, Guillaume POIRIER wrote:
Hi,
On 12/25/05, Michael Niedermayer <michaelni at gmx.at> wrote:
Hi
On Sun, Dec 25, 2005 at 08:53:00PM +0100, Guillaume POIRIER wrote:
Hi,
On 12/25/05, Michael Niedermayer CVS <michael at mplayerhq.hu> wrote:
Update of /cvsroot/ffmpeg/ffmpeg/libavcodec In directory mail:/var2/tmp/cvs-serv28677
Modified Files: mpegvideo.c Log Message: b frame strategy 2
How should I update the man page with?
vb_strategy=2: uses an adaptive number of B-frames
Smth like that? Is that option supposed to have an impact on encoding speed (compared to 0 or 1)?
2 is slow, and more or less optimal, speed and quality can be tuned with -brd_scale and -vmax_b_frames speed should increase by a factor of 4 every time brd_scale is increased by 1
Ok thanks, I'm gonna commit a manpage update shortly with your tips. However, I have one more question: does vb_strategy still only works during the first pass?
yes, the 2nd pass always uses the decissions from the first [...] -- Michael
Michael Niedermayer CVS wrote:
Update of /cvsroot/ffmpeg/ffmpeg/libavcodec In directory mail:/var2/tmp/cvs-serv28677
Modified Files: mpegvideo.c Log Message: b frame strategy 2
I finally got around to testing this, with the usual source: The Matrix. I used mencoder CVS from 2006-01-19. My general encoding command is: --- for i in 1:turbo 2 ; do mencoder ~/dumpstream/matrix.vob -aid 128 -oac copy -vf \ crop=718:356:0:60,scale=640:272 -sws 9 -ovc lavc -lavcopts \ vcodec=mpeg4:vbitrate=581:psnr:vpass=$i:mbd=2:mv0:trell:cbp:precmp=2:\ cmp=2:subcmp=2:predia=2:dia=2:preme=2:vme=5:v4mv:last_pred=2:vqcomp=0.6:\ vmax_b_frames=2:qpel:sc_factor=6 -ofps 24000/1001 -o test.avi done --- Here are some various numeric results. --- vb_strategy=0 pass 1: PSNR: Y:40.09, Cb:44.35, Cr:44.66, All:41.13 user 44m32.577s pass 2: PSNR: Y:42.10, Cb:45.17, Cr:45.87, All:42.97 user 253m48.068s frame counts: I: 3418 1% P: 61938 31% B: 130710 66% average qps: I: 3.009655 P:2.894733 B:5.003618 --- vb_strategy=1 pass 1: PSNR: Y:40.07, Cb:44.25, Cr:44.54, All:41.09 user 41m23.155s pass 2: PSNR: Y:42.04, Cb:44.98, Cr:45.62, All:42.87 user 216m36.802s frame counts: I: 3370 1% P: 117916 60% B: 74780 38% average qps: I: 3.418101 P:3.483955 B:5.035330 --- vb_strategy=2:brd_scale=0 pass 1: PSNR: Y:39.69, Cb:43.88, Cr:44.10, All:40.71 user 301m18.478s pass 2: PSNR: Y:41.80, Cb:44.69, Cr:45.25, All:42.62 user 175m5.316s frame counts: I: 3461 1% P: 191454 97% B: 1151 0% average qps: I: 3.871424 P:3.598206 B:7.099101 --- vb_strategy=2:brd_scale=1 pass 1: PSNR: Y:39.87, Cb:44.02, Cr:44.24, All:40.89 user 114m29.259s pass 2: PSNR: Y:41.83, Cb:44.75, Cr:45.33, All:42.66 user 177m19.750s frame counts: I: 3464 1% P: 188111 95% B: 4491 2% average qps: I: 3.844111 P:3.525535 B:7.184146 --- vb_strategy=2:brd_scale=2 pass 1: PSNR: Y:39.88, Cb:44.03, Cr:44.24, All:40.89 user 64m21.692s pass 2: PSNR: Y:41.83, Cb:44.76, Cr:45.34, All:42.66 user 178m6.951s frame counts: I: 3463 1% P: 184997 94% B: 7606 3% average qps: I: 3.817499 P:3.503954 B:6.764923 --- vb_strategy=2:brd_scale=3 pass 1: PSNR: Y:39.83, Cb:44.02, Cr:44.26, All:40.86 user 47m23.826s pass 2: PSNR: Y:41.82, Cb:44.77, Cr:45.34, All:42.65 user 181m26.558s frame counts: I: 3435 1% P: 178333 90% B: 14298 7% average qps: I: 3.813683 P:3.504388 B:6.178067 --- vb_strategy=2:brd_scale=4 pass 1: PSNR: Y:39.96, Cb:44.16, Cr:44.42, All:40.99 user 43m28.237s pass 2: PSNR: Y:41.92, Cb:44.89, Cr:45.50, All:42.76 user 199m49.870s frame counts: I: 3390 1% P: 145493 74% B: 47183 24% average qps: I: 3.579056 P:3.429801 B:5.470042 -------------------------------------------------------- I can't test brd_scale=5 and higher now, due to the bug I just reported; however, the results aren't good for lower values of brd_scale. The PSNR values are fairly representative of the relative qualities of the different encodes -- all the ones with vb_strategy=2 look much worse in high-motion scenes than the encode with vb_strategy=0. brd_scale=4 looks a bit better than brd_scale=0. From the frame counts, it seems that there just aren't enough B-frames being used to make an improvement.... It's possible that vb_strategy=2 with a high brd_scale is an improvement, and perhaps using a higher vmax_b_frames will help. I won't test that until the bug with brd_scale is fixed, though, in case it could alter my results. -Corey
participants (5)
-
bugfood-ml@fatooh.org -
dalias@aerifal.cx -
michael@mplayerhq.hu -
michaelni@gmx.at -
poirierg@gmail.com