comparison h263.c @ 1490:0355f2b3519a libavcodec

rate distortion optimal cbp support (h263/mpeg4 non intra only)
author michaelni
date Tue, 30 Sep 2003 22:18:43 +0000
parents 8e7d8112de29
children 3518325146fc
comparison
equal deleted inserted replaced
1489:337d13aee605 1490:0355f2b3519a
27 * @file h263.c 27 * @file h263.c
28 * h263/mpeg4 codec. 28 * h263/mpeg4 codec.
29 */ 29 */
30 30
31 //#define DEBUG 31 //#define DEBUG
32 #include <limits.h>
33
32 #include "common.h" 34 #include "common.h"
33 #include "dsputil.h" 35 #include "dsputil.h"
34 #include "avcodec.h" 36 #include "avcodec.h"
35 #include "mpegvideo.h" 37 #include "mpegvideo.h"
36 #include "h263data.h" 38 #include "h263data.h"
558 s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_16x16; 560 s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_16x16;
559 } 561 }
560 } 562 }
561 563
562 #ifdef CONFIG_ENCODERS 564 #ifdef CONFIG_ENCODERS
565
566 static inline int get_p_cbp(MpegEncContext * s,
567 DCTELEM block[6][64],
568 int motion_x, int motion_y){
569 int cbp, i;
570
571 if(s->flags & CODEC_FLAG_CBP_RD){
572 int best_cbpy_score= INT_MAX;
573 int best_cbpc_score= INT_MAX;
574 int cbpc, cbpy;
575 const int offset= (s->mv_type==MV_TYPE_16X16 ? 0 : 16) + (s->dquant ? 8 : 0);
576 const int lambda= (s->qscale*s->qscale*64*105 + 64)>>7;
577
578 for(i=0; i<4; i++){
579 int score= inter_MCBPC_bits[i + offset] * lambda;
580 if(i&1) score += s->coded_score[5];
581 if(i&2) score += s->coded_score[4];
582
583 if(score < best_cbpc_score){
584 best_cbpc_score= score;
585 cbpc= i;
586 }
587 }
588
589 for(i=0; i<16; i++){
590 int score= cbpy_tab[i ^ 0xF][1] * lambda;
591 if(i&1) score += s->coded_score[3];
592 if(i&2) score += s->coded_score[2];
593 if(i&4) score += s->coded_score[1];
594 if(i&8) score += s->coded_score[0];
595
596 if(score < best_cbpy_score){
597 best_cbpy_score= score;
598 cbpy= i;
599 }
600 }
601 cbp= cbpc + 4*cbpy;
602 if ((motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16){
603 if(best_cbpy_score + best_cbpc_score + 2*lambda >= 0)
604 cbp= 0;
605 }
606
607 for (i = 0; i < 6; i++) {
608 if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
609 s->block_last_index[i]= -1;
610 memset(s->block[i], 0, sizeof(DCTELEM)*64);
611 }
612 }
613 }else{
614 cbp= 0;
615 for (i = 0; i < 6; i++) {
616 if (s->block_last_index[i] >= 0)
617 cbp |= 1 << (5 - i);
618 }
619 }
620 return cbp;
621 }
622
623 static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64],
624 int motion_x, int motion_y, int mb_type){
625 int cbp=0, i;
626
627 if(s->flags & CODEC_FLAG_CBP_RD){
628 int score=0;
629 const int lambda= (s->qscale*s->qscale*64*105 + 64)>>7;
630
631 for(i=0; i<6; i++){
632 if(s->coded_score[i] < 0){
633 score += s->coded_score[i];
634 cbp |= 1 << (5 - i);
635 }
636 }
637
638 if(cbp){
639 int zero_score= -6;
640 if ((motion_x | motion_y | s->dquant | mb_type) == 0){
641 zero_score-= 4; //2*MV + mb_type + cbp bit
642 }
643
644 zero_score*= lambda;
645 if(zero_score <= score){
646 cbp=0;
647 }
648 }
649
650 for (i = 0; i < 6; i++) {
651 if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
652 s->block_last_index[i]= -1;
653 memset(s->block[i], 0, sizeof(DCTELEM)*64);
654 }
655 }
656 }else{
657 for (i = 0; i < 6; i++) {
658 if (s->block_last_index[i] >= 0)
659 cbp |= 1 << (5 - i);
660 }
661 }
662 return cbp;
663 }
664
563 void mpeg4_encode_mb(MpegEncContext * s, 665 void mpeg4_encode_mb(MpegEncContext * s,
564 DCTELEM block[6][64], 666 DCTELEM block[6][64],
565 int motion_x, int motion_y) 667 int motion_x, int motion_y)
566 { 668 {
567 int cbpc, cbpy, pred_x, pred_y; 669 int cbpc, cbpy, pred_x, pred_y;
572 const int dquant_code[5]= {1,0,9,2,3}; 674 const int dquant_code[5]= {1,0,9,2,3};
573 675
574 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); 676 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
575 if (!s->mb_intra) { 677 if (!s->mb_intra) {
576 /* compute cbp */ 678 /* compute cbp */
577 int i, cbp = 0; 679 int i, cbp;
578 for (i = 0; i < 6; i++) { 680
579 if (s->block_last_index[i] >= 0)
580 cbp |= 1 << (5 - i);
581 }
582
583 if(s->pict_type==B_TYPE){ 681 if(s->pict_type==B_TYPE){
584 static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */ 682 static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
585 int mb_type= mb_type_table[s->mv_dir]; 683 int mb_type= mb_type_table[s->mv_dir];
586 684
587 if(s->mb_x==0){ 685 if(s->mb_x==0){
606 s->qscale -= s->dquant; 704 s->qscale -= s->dquant;
607 // s->mb_skiped=1; 705 // s->mb_skiped=1;
608 706
609 return; 707 return;
610 } 708 }
709
710 cbp= get_b_cbp(s, block, motion_x, motion_y, mb_type);
611 711
612 if ((cbp | motion_x | motion_y | mb_type) ==0) { 712 if ((cbp | motion_x | motion_y | mb_type) ==0) {
613 /* direct MB with MV={0,0} */ 713 /* direct MB with MV={0,0} */
614 assert(s->dquant==0); 714 assert(s->dquant==0);
615 715
697 797
698 if(interleaved_stats){ 798 if(interleaved_stats){
699 s->p_tex_bits+= get_bits_diff(s); 799 s->p_tex_bits+= get_bits_diff(s);
700 } 800 }
701 }else{ /* s->pict_type==B_TYPE */ 801 }else{ /* s->pict_type==B_TYPE */
802 cbp= get_p_cbp(s, block, motion_x, motion_y);
803
702 if ((cbp | motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16) { 804 if ((cbp | motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16) {
703 /* check if the B frames can skip it too, as we must skip it if we skip here 805 /* check if the B frames can skip it too, as we must skip it if we skip here
704 why didnt they just compress the skip-mb bits instead of reusing them ?! */ 806 why didnt they just compress the skip-mb bits instead of reusing them ?! */
705 if(s->max_b_frames>0){ 807 if(s->max_b_frames>0){
706 int i; 808 int i;
936 const int dquant_code[5]= {1,0,9,2,3}; 1038 const int dquant_code[5]= {1,0,9,2,3};
937 1039
938 //printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); 1040 //printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
939 if (!s->mb_intra) { 1041 if (!s->mb_intra) {
940 /* compute cbp */ 1042 /* compute cbp */
941 cbp = 0; 1043 cbp= get_p_cbp(s, block, motion_x, motion_y);
942 for (i = 0; i < 6; i++) { 1044
943 if (s->block_last_index[i] >= 0)
944 cbp |= 1 << (5 - i);
945 }
946 if ((cbp | motion_x | motion_y | s->dquant) == 0) { 1045 if ((cbp | motion_x | motion_y | s->dquant) == 0) {
947 /* skip macroblock */ 1046 /* skip macroblock */
948 put_bits(&s->pb, 1, 1); 1047 put_bits(&s->pb, 1, 1);
949 if(interleaved_stats){ 1048 if(interleaved_stats){
950 s->misc_bits++; 1049 s->misc_bits++;