[svn] / branches / dev-api-4 / xvidcore / src / utils / mbtransquant.c Repository:
ViewVC logotype

Diff of /branches/dev-api-4/xvidcore/src/utils/mbtransquant.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1174, Tue Oct 7 13:02:35 2003 UTC revision 1223, Sun Nov 23 17:01:08 2003 UTC
# Line 21  Line 21 
21   *  along with this program ; if not, write to the Free Software   *  along with this program ; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23   *   *
24   * $Id: mbtransquant.c,v 1.21.2.18 2003-10-07 13:02:35 edgomez Exp $   * $Id: mbtransquant.c,v 1.21.2.19 2003-11-23 17:01:08 edgomez Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 43  Line 43 
43  #include "../encoder.h"  #include "../encoder.h"
44    
45  #include "../image/reduced.h"  #include "../image/reduced.h"
46    #include  "../quant/quant_matrix.h"
47    
48  MBFIELDTEST_PTR MBFieldTest;  MBFIELDTEST_PTR MBFieldTest;
49    
# Line 176  Line 177 
177          stop_iquant_timer();          stop_iquant_timer();
178  }  }
179    
   
 typedef int (*trellis_func_ptr_t)(int16_t *const Out,  
                                                                   const int16_t *const In,  
                                                                   int Q,  
                                                                   const uint16_t * const Zigzag,  
                                                                   int Non_Zero);  
   
 static int  
 dct_quantize_trellis_h263_c(int16_t *const Out,  
                                                         const int16_t *const In,  
                                                         int Q,  
                                                         const uint16_t * const Zigzag,  
                                                         int Non_Zero);  
   
180  static int  static int
181  dct_quantize_trellis_mpeg_c(int16_t *const Out,  dct_quantize_trellis_c(int16_t *const Out,
182                                                          const int16_t *const In,                                                          const int16_t *const In,
183                                                          int Q,                                                          int Q,
184                                                          const uint16_t * const Zigzag,                                                          const uint16_t * const Zigzag,
185                                               const uint16_t * const QuantMatrix,
186                                                          int Non_Zero);                                                          int Non_Zero);
187    
188  /* Quantize all blocks -- Inter mode */  /* Quantize all blocks -- Inter mode */
# Line 219  Line 207 
207                          quant_mpeg_inter                          quant_mpeg_inter
208                  };                  };
209    
         trellis_func_ptr_t const trellis[2] =  
                 {  
                         dct_quantize_trellis_h263_c,  
                         dct_quantize_trellis_mpeg_c  
                 };  
   
210          mpeg = !!(pParam->vol_flags & XVID_VOL_MPEGQUANT);          mpeg = !!(pParam->vol_flags & XVID_VOL_MPEGQUANT);
211    
212          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
# Line 235  Line 217 
217                  sum = quant[mpeg](&qcoeff[i*64], &data[i*64], pMB->quant);                  sum = quant[mpeg](&qcoeff[i*64], &data[i*64], pMB->quant);
218    
219                  if(sum && (frame->vop_flags & XVID_VOP_TRELLISQUANT)) {                  if(sum && (frame->vop_flags & XVID_VOP_TRELLISQUANT)) {
220                          sum = trellis[mpeg](&qcoeff[i*64], &data[i*64], pMB->quant, &scan_tables[0][0], 63);                          const static uint16_t h263matrix[] =
221                                    {
222                                            16, 16, 16, 16, 16, 16, 16, 16,
223                                            16, 16, 16, 16, 16, 16, 16, 16,
224                                            16, 16, 16, 16, 16, 16, 16, 16,
225                                            16, 16, 16, 16, 16, 16, 16, 16,
226                                            16, 16, 16, 16, 16, 16, 16, 16,
227                                            16, 16, 16, 16, 16, 16, 16, 16,
228                                            16, 16, 16, 16, 16, 16, 16, 16,
229                                            16, 16, 16, 16, 16, 16, 16, 16
230                                    };
231                            sum = dct_quantize_trellis_c(&qcoeff[i*64], &data[i*64],
232                                                                                     pMB->quant, &scan_tables[0][0],
233                                                                                     (mpeg)?(uint16_t*)get_inter_matrix():h263matrix,
234                                                                                     63);
235                  }                  }
236                  stop_quant_timer();                  stop_quant_timer();
237    
# Line 755  Line 751 
751          Code_Len24,Code_Len23,Code_Len22,Code_Len21, Code_Len3, Code_Len1,          Code_Len24,Code_Len23,Code_Len22,Code_Len21, Code_Len3, Code_Len1,
752  };  };
753    
754  #define TL(q) 0xfe00/(q*q)  /* TL_SHIFT controls the precision of the RD optimizations in trellis
755     * valid range is [10..16]. The bigger, the more trellis is vulnerable
756     * to overflows in cost formulas.
757     *  - 10 allows ac values up to 2^11 == 2048
758     *  - 16 allows ac values up to 2^8 == 256
759     */
760    #define TL_SHIFT 11
761    #define TL(q) ((0xfe00>>(16-TL_SHIFT))/(q*q))
762    
763  static const int Trellis_Lambda_Tabs[31] = {  static const int Trellis_Lambda_Tabs[31] = {
764          TL( 1),TL( 2),TL( 3),TL( 4),TL( 5),TL( 6), TL( 7),          TL( 1),TL( 2),TL( 3),TL( 4),TL( 5),TL( 6), TL( 7),
# Line 785  Line 788 
788    
789          return(sum);          return(sum);
790  }  }
 /* this routine has been strippen of all debug code */  
791    
792    /* this routine has been strippen of all debug code */
793  static int  static int
794  dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero)  dct_quantize_trellis_c(int16_t *const Out,
795                                               const int16_t *const In,
796                                               int Q,
797                                               const uint16_t * const Zigzag,
798                                               const uint16_t * const QuantMatrix,
799                                               int Non_Zero)
800  {  {
801    
802      /*      /*
# Line 802  Line 810 
810          NODE Nodes[65], Last;          NODE Nodes[65], Last;
811          uint32_t Run_Costs0[64+1];          uint32_t Run_Costs0[64+1];
812          uint32_t * const Run_Costs = Run_Costs0 + 1;          uint32_t * const Run_Costs = Run_Costs0 + 1;
813          const int Mult = 2*Q;  
         const int Bias = (Q-1) | 1;  
         const int Lev0 = Mult + Bias;  
814          const int Lambda = Trellis_Lambda_Tabs[Q-1];    /* it's 1/lambda, actually */          const int Lambda = Trellis_Lambda_Tabs[Q-1];    /* it's 1/lambda, actually */
815    
816          int Run_Start = -1;          int Run_Start = -1;
817          uint32_t Min_Cost = 2<<16;          uint32_t Min_Cost = 2<<TL_SHIFT;
818    
819          int Last_Node = -1;          int Last_Node = -1;
820          uint32_t Last_Cost = 0;          uint32_t Last_Cost = 0;
821    
822          int i, j, sum;          int i, j, sum;
823          Run_Costs[-1] = 2<<16;                          /* source (w/ CBP penalty) */          Run_Costs[-1] = 2<<TL_SHIFT;                          /* source (w/ CBP penalty) */
824    
825          Non_Zero = Find_Last(Out, Zigzag, Non_Zero);          Non_Zero = Find_Last(Out, Zigzag, Non_Zero);
826          if (Non_Zero<0)          if (Non_Zero<0)
827                  return 0; /* Sum is zero if there are only zero coeffs */                  return 0; /* Sum is zero if there are only zero coeffs */
828    
829          for(i=0; i<=Non_Zero; i++) {          for(i=0; i<=Non_Zero; i++) {
830                    const int q = ((Q*QuantMatrix[Zigzag[i]])>>4);
831                    const int Mult = 2*q;
832                    const int Bias = (q-1) | 1;
833                    const int Lev0 = Mult + Bias;
834    
835                  const int AC = In[Zigzag[i]];                  const int AC = In[Zigzag[i]];
836                  const int Level1 = Out[Zigzag[i]];                  const int Level1 = Out[Zigzag[i]];
837                  const int Dist0 = Lambda* AC*AC;                  const unsigned int Dist0 = Lambda* AC*AC;
838                  uint32_t Best_Cost = 0xf0000000;                  uint32_t Best_Cost = 0xf0000000;
839                  Last_Cost += Dist0;                  Last_Cost += Dist0;
840    
# Line 843  Line 854 
854                          Cost0 = Lambda*dQ*dQ;                          Cost0 = Lambda*dQ*dQ;
855    
856                          Nodes[i].Run = 1;                          Nodes[i].Run = 1;
857                          Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0;                          Best_Cost = (Code_Len20[0]<<TL_SHIFT) + Run_Costs[i-1]+Cost0;
858                          for(Run=i-Run_Start; Run>0; --Run) {                          for(Run=i-Run_Start; Run>0; --Run) {
859                                  const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run];                                  const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run];
860                                  const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16);                                  const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<TL_SHIFT);
861                                  const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16);                                  const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<TL_SHIFT);
862    
863                                  /*                                  /*
864                                   * TODO: what about tie-breaks? Should we favor short runs or                                   * TODO: what about tie-breaks? Should we favor short runs or
# Line 912  Line 923 
923                                   * (? doesn't seem to have any effect -- gruel )                                   * (? doesn't seem to have any effect -- gruel )
924                                   */                                   */
925    
926                                  Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16);                                  Cost1 = Cost_Base + (Tbl_L1[Run-1]<<TL_SHIFT);
927                                  Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21;                                  Cost2 = Cost_Base + (Tbl_L2[Run-1]<<TL_SHIFT) + dDist21;
928    
929                                  if (Cost2<Cost1) {                                  if (Cost2<Cost1) {
930                                          Cost1 = Cost2;                                          Cost1 = Cost2;
# Line 928  Line 939 
939                                          Nodes[i].Level = bLevel;                                          Nodes[i].Level = bLevel;
940                                  }                                  }
941    
942                                  Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16);                                  Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<TL_SHIFT);
943                                  Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21;                                  Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<TL_SHIFT) + dDist21;
944    
945                                  if (Cost2<Cost1) {                                  if (Cost2<Cost1) {
946                                          Cost1 = Cost2;                                          Cost1 = Cost2;
# Line 960  Line 971 
971                           * it a chance by not moving the left barrier too much.                           * it a chance by not moving the left barrier too much.
972                           */                           */
973    
974                          while( Run_Costs[Run_Start]>Min_Cost+(1<<16) )                          while( Run_Costs[Run_Start]>Min_Cost+(1<<TL_SHIFT) )
975                                  Run_Start++;                                  Run_Start++;
976    
977                          /* spread on preceding coeffs the cost incurred by skipping this one */                          /* spread on preceding coeffs the cost incurred by skipping this one */
# Line 988  Line 999 
999          return sum;          return sum;
1000  }  }
1001    
 static int  
 dct_quantize_trellis_mpeg_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero)  
 {  
         /* ToDo: Ok ok it's just a place holder for Gruel -- damn write this one :-) */  
         return Compute_Sum(Out, 63);  
 }  
   
1002  /* original version including heavy debugging info */  /* original version including heavy debugging info */
1003    
1004  #ifdef DBGTRELL  #ifdef DBGTRELL
# Line 1051  Line 1055 
1055                  V -= Ref[Zigzag[i]];                  V -= Ref[Zigzag[i]];
1056                  Dist += V*V;                  Dist += V*V;
1057          }          }
1058          Cost = Lambda*Dist + (Bits<<16);          Cost = Lambda*Dist + (Bits<<TL_SHIFT);
1059          if (DBG==1)          if (DBG==1)
1060                  printf( " Last:%2d/%2d Cost = [(Bits=%5.0d) + Lambda*(Dist=%6.0d) = %d ] >>12= %d ", Last,Max, Bits, Dist, Cost, Cost>>12 );                  printf( " Last:%2d/%2d Cost = [(Bits=%5.0d) + Lambda*(Dist=%6.0d) = %d ] >>12= %d ", Last,Max, Bits, Dist, Cost, Cost>>12 );
1061          return Cost;          return Cost;
# Line 1083  Line 1087 
1087          const int Lambda = Trellis_Lambda_Tabs[Q-1];    /* it's 1/lambda, actually */          const int Lambda = Trellis_Lambda_Tabs[Q-1];    /* it's 1/lambda, actually */
1088    
1089          int Run_Start = -1;          int Run_Start = -1;
1090          Run_Costs[-1] = 2<<16;                          /* source (w/ CBP penalty) */          Run_Costs[-1] = 2<<TL_SHIFT;                          /* source (w/ CBP penalty) */
1091          uint32_t Min_Cost = 2<<16;          uint32_t Min_Cost = 2<<TL_SHIFT;
1092    
1093          int Last_Node = -1;          int Last_Node = -1;
1094          uint32_t Last_Cost = 0;          uint32_t Last_Cost = 0;
# Line 1123  Line 1127 
1127                          Cost0 = Lambda*dQ*dQ;                          Cost0 = Lambda*dQ*dQ;
1128    
1129                          Nodes[i].Run = 1;                          Nodes[i].Run = 1;
1130                          Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0;                          Best_Cost = (Code_Len20[0]<<TL_SHIFT) + Run_Costs[i-1]+Cost0;
1131                          for(Run=i-Run_Start; Run>0; --Run)                          for(Run=i-Run_Start; Run>0; --Run)
1132                          {                          {
1133                                  const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run];                                  const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run];
1134                                  const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16);                                  const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<TL_SHIFT);
1135                                  const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16);                                  const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<TL_SHIFT);
1136    
1137                                  /*                                  /*
1138                                   * TODO: what about tie-breaks? Should we favor short runs or                                   * TODO: what about tie-breaks? Should we favor short runs or
# Line 1204  Line 1208 
1208   * for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following:   * for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following:
1209   *        if (Cost_Base>=Best_Cost) continue;   *        if (Cost_Base>=Best_Cost) continue;
1210   */   */
1211                                  Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16);                                  Cost1 = Cost_Base + (Tbl_L1[Run-1]<<TL_SHIFT);
1212                                  Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21;                                  Cost2 = Cost_Base + (Tbl_L2[Run-1]<<TL_SHIFT) + dDist21;
1213    
1214                                  if (Cost2<Cost1) {                                  if (Cost2<Cost1) {
1215                                          Cost1 = Cost2;                                          Cost1 = Cost2;
# Line 1219  Line 1223 
1223                                          Nodes[i].Level = bLevel;                                          Nodes[i].Level = bLevel;
1224                                  }                                  }
1225    
1226                                  Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16);                                  Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<TL_SHIFT);
1227                                  Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21;                                  Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<TL_SHIFT) + dDist21;
1228    
1229                                  if (Cost2<Cost1) {                                  if (Cost2<Cost1) {
1230                                          Cost1 = Cost2;                                          Cost1 = Cost2;
# Line 1266  Line 1270 
1270                           * it a chance by not moving the left barrier too much.                           * it a chance by not moving the left barrier too much.
1271                           */                           */
1272    
1273                          while( Run_Costs[Run_Start]>Min_Cost+(1<<16) )                          while( Run_Costs[Run_Start]>Min_Cost+(1<<TL_SHIFT) )
1274                                  Run_Start++;                                  Run_Start++;
1275    
1276                          /* spread on preceding coeffs the cost incurred by skipping this one */                          /* spread on preceding coeffs the cost incurred by skipping this one */

Legend:
Removed from v.1174  
changed lines
  Added in v.1223

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4