comparison mpegvideo.c @ 329:5cc47d0ba53e libavcodec

fixed ratecontrol & b-frames 2pass ratecontrol fixed hq with 2pass inceased build num (a few more vars for the rc stuff) hopefully no new bugs
author michaelni
date Sun, 21 Apr 2002 21:18:17 +0000
parents d359db02fc90
children 8aa87f1dfc52
comparison
equal deleted inserted replaced
328:025825084364 329:5cc47d0ba53e
29 #ifdef USE_FASTMEMCPY 29 #ifdef USE_FASTMEMCPY
30 #include "fastmemcpy.h" 30 #include "fastmemcpy.h"
31 #endif 31 #endif
32 32
33 static void encode_picture(MpegEncContext *s, int picture_number); 33 static void encode_picture(MpegEncContext *s, int picture_number);
34 static void rate_control_init(MpegEncContext *s);
35 static int rate_estimate_qscale(MpegEncContext *s);
36 static void dct_unquantize_mpeg1_c(MpegEncContext *s, 34 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
37 DCTELEM *block, int n, int qscale); 35 DCTELEM *block, int n, int qscale);
38 static void dct_unquantize_mpeg2_c(MpegEncContext *s, 36 static void dct_unquantize_mpeg2_c(MpegEncContext *s,
39 DCTELEM *block, int n, int qscale); 37 DCTELEM *block, int n, int qscale);
40 static void dct_unquantize_h263_c(MpegEncContext *s, 38 static void dct_unquantize_h263_c(MpegEncContext *s,
375 s->qmin= avctx->qmin; 373 s->qmin= avctx->qmin;
376 s->qmax= avctx->qmax; 374 s->qmax= avctx->qmax;
377 s->max_qdiff= avctx->max_qdiff; 375 s->max_qdiff= avctx->max_qdiff;
378 s->qcompress= avctx->qcompress; 376 s->qcompress= avctx->qcompress;
379 s->qblur= avctx->qblur; 377 s->qblur= avctx->qblur;
378 s->b_quant_factor= avctx->b_quant_factor;
380 s->avctx = avctx; 379 s->avctx = avctx;
381 s->aspect_ratio_info= avctx->aspect_ratio_info; 380 s->aspect_ratio_info= avctx->aspect_ratio_info;
382 s->flags= avctx->flags; 381 s->flags= avctx->flags;
383 s->max_b_frames= avctx->max_b_frames; 382 s->max_b_frames= avctx->max_b_frames;
383 s->rc_strategy= avctx->rc_strategy;
384 s->b_frame_strategy= avctx->b_frame_strategy;
384 385
385 if (s->gop_size <= 1) { 386 if (s->gop_size <= 1) {
386 s->intra_only = 1; 387 s->intra_only = 1;
387 s->gop_size = 12; 388 s->gop_size = 12;
388 } else { 389 } else {
508 for(i=0;i<64;i++) { 509 for(i=0;i<64;i++) {
509 s->intra_matrix[i] = default_intra_matrix[i]; 510 s->intra_matrix[i] = default_intra_matrix[i];
510 s->non_intra_matrix[i] = default_non_intra_matrix[i]; 511 s->non_intra_matrix[i] = default_non_intra_matrix[i];
511 } 512 }
512 513
513 /* rate control init */ 514 if(ff_rate_control_init(s) < 0)
514 rate_control_init(s); 515 return -1;
515 516
516 s->picture_number = 0; 517 s->picture_number = 0;
517 s->picture_in_gop_number = 0; 518 s->picture_in_gop_number = 0;
518 s->fake_picture_number = 0; 519 s->fake_picture_number = 0;
519 /* motion detector init */ 520 /* motion detector init */
528 MpegEncContext *s = avctx->priv_data; 529 MpegEncContext *s = avctx->priv_data;
529 530
530 #ifdef STATS 531 #ifdef STATS
531 print_stats(); 532 print_stats();
532 #endif 533 #endif
534
535 ff_rate_control_uninit(s);
536
533 MPV_common_end(s); 537 MPV_common_end(s);
534 if (s->out_format == FMT_MJPEG) 538 if (s->out_format == FMT_MJPEG)
535 mjpeg_close(s); 539 mjpeg_close(s);
536 540
537 return 0; 541 return 0;
575 if (s->pict_type == B_TYPE) { 579 if (s->pict_type == B_TYPE) {
576 for(i=0;i<3;i++) { 580 for(i=0;i<3;i++) {
577 s->current_picture[i] = s->aux_picture[i]; 581 s->current_picture[i] = s->aux_picture[i];
578 } 582 }
579 } else { 583 } else {
580 s->last_non_b_pict_type= s->pict_type;
581 for(i=0;i<3;i++) { 584 for(i=0;i<3;i++) {
582 /* swap next and last */ 585 /* swap next and last */
583 tmp = s->last_picture[i]; 586 tmp = s->last_picture[i];
584 s->last_picture[i] = s->next_picture[i]; 587 s->last_picture[i] = s->next_picture[i];
585 s->next_picture[i] = tmp; 588 s->next_picture[i] = tmp;
603 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2); 606 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
604 draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2); 607 draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
605 } 608 }
606 } 609 }
607 emms_c(); 610 emms_c();
611
612 if(s->pict_type!=B_TYPE){
613 s->last_non_b_pict_type= s->pict_type;
614 s->last_non_b_qscale= s->qscale;
615 s->last_non_b_mc_mb_var= s->mc_mb_var;
616 }
608 } 617 }
609 618
610 /* reorder input for encoding */ 619 /* reorder input for encoding */
611 void reorder_input(MpegEncContext *s, AVPicture *pict) 620 void reorder_input(MpegEncContext *s, AVPicture *pict)
612 { 621 {
693 702
694 s->input_qscale = avctx->quality; 703 s->input_qscale = avctx->quality;
695 704
696 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); 705 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
697 706
698 s->force_input_type= (avctx->flags&CODEC_FLAG_TYPE) ? 707 if(avctx->flags&CODEC_FLAG_TYPE){
699 (avctx->key_frame ? I_TYPE : P_TYPE) : 0; 708 s->input_pict_type=
700 if (!s->intra_only) { 709 s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
701 /* first picture of GOP is intra */ 710 }else if(s->flags&CODEC_FLAG_PASS2){
702 if (s->input_picture_in_gop_number % s->gop_size==0 || s->force_input_type==I_TYPE){ 711 s->input_pict_type=
703 s->input_picture_in_gop_number=0; 712 s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
713 }else{
714 s->force_input_type=0;
715 if (!s->intra_only) {
716 /* first picture of GOP is intra */
717 if (s->input_picture_in_gop_number % s->gop_size==0){
718 s->input_pict_type = I_TYPE;
719 }else if(s->max_b_frames==0){
720 s->input_pict_type = P_TYPE;
721 }else{
722 if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
723 s->input_pict_type = B_TYPE;
724 else
725 s->input_pict_type = P_TYPE;
726 }
727 } else {
704 s->input_pict_type = I_TYPE; 728 s->input_pict_type = I_TYPE;
705 }else if(s->max_b_frames==0){ 729 }
706 s->input_pict_type = P_TYPE; 730 }
707 }else{ 731
708 if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ 732 if(s->input_pict_type==I_TYPE)
709 s->input_pict_type = B_TYPE; 733 s->input_picture_in_gop_number=0;
710 else 734
711 s->input_pict_type = P_TYPE;
712 }
713 } else {
714 s->input_pict_type = I_TYPE;
715 }
716
717 reorder_input(s, pict); 735 reorder_input(s, pict);
718 736
719 /* output? */ 737 /* output? */
720 if(s->coded_order[0].picture[0]){ 738 if(s->coded_order[0].picture[0]){
721 739
743 761
744 if (s->out_format == FMT_MJPEG) 762 if (s->out_format == FMT_MJPEG)
745 mjpeg_picture_trailer(s); 763 mjpeg_picture_trailer(s);
746 764
747 avctx->quality = s->qscale; 765 avctx->quality = s->qscale;
766
767 if(s->flags&CODEC_FLAG_PASS1)
768 ff_write_pass1_stats(s);
748 } 769 }
749 770
750 s->input_picture_number++; 771 s->input_picture_number++;
751 s->input_picture_in_gop_number++; 772 s->input_picture_in_gop_number++;
752 773
753 flush_put_bits(&s->pb); 774 flush_put_bits(&s->pb);
754 s->last_frame_bits= s->frame_bits;
755 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; 775 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
776 if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits;
777 else s->pb_frame_bits= s->frame_bits;
778
756 s->total_bits += s->frame_bits; 779 s->total_bits += s->frame_bits;
757 avctx->frame_bits = s->frame_bits; 780 avctx->frame_bits = s->frame_bits;
758 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", 781 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
759 //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); 782 //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);
760 783
1423 d->p_tex_bits= s->p_tex_bits; 1446 d->p_tex_bits= s->p_tex_bits;
1424 d->i_count= s->i_count; 1447 d->i_count= s->i_count;
1425 d->p_count= s->p_count; 1448 d->p_count= s->p_count;
1426 d->skip_count= s->skip_count; 1449 d->skip_count= s->skip_count;
1427 d->misc_bits= s->misc_bits; 1450 d->misc_bits= s->misc_bits;
1428 d->last_bits= s->last_bits; 1451 d->last_bits= 0;
1429 1452
1430 d->mb_skiped= s->mb_skiped; 1453 d->mb_skiped= s->mb_skiped;
1431 } 1454 }
1432 1455
1433 static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){ 1456 static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1447 d->p_tex_bits= s->p_tex_bits; 1470 d->p_tex_bits= s->p_tex_bits;
1448 d->i_count= s->i_count; 1471 d->i_count= s->i_count;
1449 d->p_count= s->p_count; 1472 d->p_count= s->p_count;
1450 d->skip_count= s->skip_count; 1473 d->skip_count= s->skip_count;
1451 d->misc_bits= s->misc_bits; 1474 d->misc_bits= s->misc_bits;
1452 d->last_bits= s->last_bits;
1453 1475
1454 d->mb_intra= s->mb_intra; 1476 d->mb_intra= s->mb_intra;
1455 d->mb_skiped= s->mb_skiped; 1477 d->mb_skiped= s->mb_skiped;
1456 d->mv_type= s->mv_type; 1478 d->mv_type= s->mv_type;
1457 d->mv_dir= s->mv_dir; 1479 d->mv_dir= s->mv_dir;
1477 s->block_wrap[2]= 1499 s->block_wrap[2]=
1478 s->block_wrap[3]= s->mb_width*2 + 2; 1500 s->block_wrap[3]= s->mb_width*2 + 2;
1479 s->block_wrap[4]= 1501 s->block_wrap[4]=
1480 s->block_wrap[5]= s->mb_width + 2; 1502 s->block_wrap[5]= s->mb_width + 2;
1481 1503
1482 s->last_mc_mb_var = s->mc_mb_var;
1483 /* Reset the average MB variance */ 1504 /* Reset the average MB variance */
1484 s->avg_mb_var = 0; 1505 s->avg_mb_var = 0;
1485 s->mc_mb_var = 0; 1506 s->mc_mb_var = 0;
1486 1507
1487 /* we need to initialize some time vars before we can encode b-frames */ 1508 /* we need to initialize some time vars before we can encode b-frames */
1522 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2); 1543 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
1523 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2); 1544 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
1524 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); 1545 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1525 } 1546 }
1526 1547
1527 if(s->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE && (!s->force_type) && s->max_b_frames==0){ //FIXME subtract MV bits 1548 if(s->avg_mb_var < s->mc_mb_var && s->pict_type == P_TYPE){ //FIXME subtract MV bits
1528 // FIXME b-frames & scene change detection
1529 s->input_pict_type= I_TYPE;
1530 s->pict_type= I_TYPE; 1549 s->pict_type= I_TYPE;
1531 s->input_picture_in_gop_number=0;
1532 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); 1550 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1551 if(s->max_b_frames==0){
1552 s->input_pict_type= I_TYPE;
1553 s->input_picture_in_gop_number=0;
1554 }
1533 //printf("Scene change detected, encoding as I Frame\n"); 1555 //printf("Scene change detected, encoding as I Frame\n");
1534 } 1556 }
1535 1557
1536 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) 1558 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
1537 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER); 1559 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
1548 1570
1549 //printf("f_code %d ///\n", s->f_code); 1571 //printf("f_code %d ///\n", s->f_code);
1550 1572
1551 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var); 1573 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
1552 1574
1553 if (!s->fixed_qscale) 1575 if(s->flags&CODEC_FLAG_PASS2)
1554 s->qscale = rate_estimate_qscale(s); 1576 s->qscale = ff_rate_estimate_qscale_pass2(s);
1577 else if (!s->fixed_qscale)
1578 s->qscale = ff_rate_estimate_qscale(s);
1579
1555 1580
1556 /* precompute matrix */ 1581 /* precompute matrix */
1557 if (s->out_format == FMT_MJPEG) { 1582 if (s->out_format == FMT_MJPEG) {
1558 /* for mjpeg, we do include qscale in the matrix */ 1583 /* for mjpeg, we do include qscale in the matrix */
1559 s->intra_matrix[0] = default_intra_matrix[0]; 1584 s->intra_matrix[0] = default_intra_matrix[0];
1665 s->mb_intra= 0; 1690 s->mb_intra= 0;
1666 s->mv[0][0][0] = s->p_mv_table[xy][0]; 1691 s->mv[0][0][0] = s->p_mv_table[xy][0];
1667 s->mv[0][0][1] = s->p_mv_table[xy][1]; 1692 s->mv[0][0][1] = s->p_mv_table[xy][1];
1668 init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL); 1693 init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);
1669 s->block= s->blocks[next_block]; 1694 s->block= s->blocks[next_block];
1695 s->last_bits= 0; //done in copy_context_before_encode but we skip that here
1670 1696
1671 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]); 1697 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1672 d= get_bit_count(&s->pb); 1698 d= get_bit_count(&s->pb);
1673 if(d<dmin){ 1699 if(d<dmin){
1674 flush_put_bits(&s->pb); 1700 flush_put_bits(&s->pb);
1808 s->mbintra_table[mb_x + mb_y*s->mb_width]=1; 1834 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1809 } 1835 }
1810 copy_context_after_encode(s, &best_s, -1); 1836 copy_context_after_encode(s, &best_s, -1);
1811 copy_bits(&pb, bit_buf[best], dmin); 1837 copy_bits(&pb, bit_buf[best], dmin);
1812 s->pb= pb; 1838 s->pb= pb;
1839 s->last_bits= get_bit_count(&s->pb);
1813 } else { 1840 } else {
1814 int motion_x, motion_y; 1841 int motion_x, motion_y;
1815 s->mv_type=MV_TYPE_16X16; 1842 s->mv_type=MV_TYPE_16X16;
1816 // only one MB-Type possible 1843 // only one MB-Type possible
1817 switch(mb_type){ 1844 switch(mb_type){
2188 block[i] = level; 2215 block[i] = level;
2189 } 2216 }
2190 } 2217 }
2191 } 2218 }
2192 2219
2193 /* rate control */
2194
2195 static void rate_control_init(MpegEncContext *s)
2196 {
2197 emms_c();
2198
2199 //initial values, they dont really matter as they will be totally different within a few frames
2200 s->i_pred.coeff= s->p_pred.coeff= 7.0;
2201 s->i_pred.count= s->p_pred.count= 1.0;
2202
2203 s->i_pred.decay= s->p_pred.decay= 0.4;
2204
2205 // use more bits at the beginning, otherwise high motion at the begin will look like shit
2206 s->qsum=100;
2207 s->qcount=100;
2208
2209 s->short_term_qsum=0.001;
2210 s->short_term_qcount=0.001;
2211 }
2212
2213 static double predict(Predictor *p, double q, double var)
2214 {
2215 return p->coeff*var / (q*p->count);
2216 }
2217
2218 static void update_predictor(Predictor *p, double q, double var, double size)
2219 {
2220 double new_coeff= size*q / (var + 1);
2221 if(var<1000) return;
2222 /*{
2223 int pred= predict(p, q, var);
2224 int error= abs(pred-size);
2225 static double sum=0;
2226 static int count=0;
2227 if(count>5) sum+=error;
2228 count++;
2229 if(256*256*256*64%count==0){
2230 printf("%d %f %f\n", count, sum/count, p->coeff);
2231 }
2232 }*/
2233 p->count*= p->decay;
2234 p->coeff*= p->decay;
2235 p->count++;
2236 p->coeff+= new_coeff;
2237 }
2238
2239 static int rate_estimate_qscale(MpegEncContext *s)
2240 {
2241 int qmin= s->qmin;
2242 int qmax= s->qmax;
2243 int rate_q=5;
2244 float q;
2245 int qscale;
2246 float br_compensation;
2247 double diff;
2248 double short_term_q;
2249 double long_term_q;
2250 int last_qscale= s->qscale;
2251 double fps;
2252 INT64 wanted_bits;
2253 emms_c();
2254
2255 fps= (double)s->frame_rate / FRAME_RATE_BASE;
2256 wanted_bits= s->bit_rate*(double)s->picture_number/fps;
2257
2258
2259 if(s->picture_number>2){
2260 /* update predictors */
2261 if(s->last_pict_type == I_TYPE){
2262 //FIXME
2263 }else{ //P Frame
2264 //printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff);
2265 update_predictor(&s->p_pred, s->qscale, s->last_mc_mb_var, s->frame_bits);
2266 }
2267 }
2268
2269 if(s->pict_type == I_TYPE){
2270 //FIXME
2271 rate_q= s->qsum/s->qcount;
2272 }else{ //P Frame
2273 int i;
2274 int diff, best_diff=1000000000;
2275 for(i=1; i<=31; i++){
2276 diff= predict(&s->p_pred, i, s->mc_mb_var) - (double)s->bit_rate/fps;
2277 if(diff<0) diff= -diff;
2278 if(diff<best_diff){
2279 best_diff= diff;
2280 rate_q= i;
2281 }
2282 }
2283 }
2284
2285 s->short_term_qsum*=s->qblur;
2286 s->short_term_qcount*=s->qblur;
2287
2288 s->short_term_qsum+= rate_q;
2289 s->short_term_qcount++;
2290 short_term_q= s->short_term_qsum/s->short_term_qcount;
2291
2292 long_term_q= s->qsum/s->qcount*(s->total_bits+1)/(wanted_bits+1); //+1 to avoid nan & 0
2293
2294 // q= (long_term_q - short_term_q)*s->qcompress + short_term_q;
2295 q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);
2296
2297 diff= s->total_bits - wanted_bits;
2298 br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;
2299 if(br_compensation<=0.0) br_compensation=0.001;
2300 q/=br_compensation;
2301
2302 qscale= (int)(q + 0.5);
2303 if (qscale<qmin) qscale=qmin;
2304 else if(qscale>qmax) qscale=qmax;
2305
2306 if (qscale<last_qscale-s->max_qdiff) qscale=last_qscale-s->max_qdiff;
2307 else if(qscale>last_qscale+s->max_qdiff) qscale=last_qscale+s->max_qdiff;
2308
2309 s->qsum+= qscale;
2310 s->qcount++;
2311
2312 s->last_pict_type= s->pict_type;
2313 //printf("q:%d diff:%d comp:%f rate_q:%d st_q:%f fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation,
2314 // rate_q, short_term_q, s->mc_mb_var, s->frame_bits);
2315 //printf("%d %d\n", s->bit_rate, (int)fps);
2316 return qscale;
2317 }
2318
2319 AVCodec mpeg1video_encoder = { 2220 AVCodec mpeg1video_encoder = {
2320 "mpeg1video", 2221 "mpeg1video",
2321 CODEC_TYPE_VIDEO, 2222 CODEC_TYPE_VIDEO,
2322 CODEC_ID_MPEG1VIDEO, 2223 CODEC_ID_MPEG1VIDEO,
2323 sizeof(MpegEncContext), 2224 sizeof(MpegEncContext),