[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 1014, Mon May 12 12:33:16 2003 UTC revision 1053, Mon Jun 9 01:25:19 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.12 2003-05-12 12:33:16 suxen_drol Exp $   * $Id: mbtransquant.c,v 1.21.2.13 2003-06-09 01:25:05 edgomez Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 162  Line 162 
162    
163    
164  static int  static int
165  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_h263_c(int16_t *const Out,
166                                                            const int16_t *const In,
167                                                            int Q,
168                                                            const uint16_t * const Zigzag,
169                                                            int Non_Zero);
170    
171    #if 0
172  static int  static int
173  dct_quantize_trellis_mpeg_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero);  dct_quantize_trellis_mpeg_c(int16_t *const Out,
174                                                            const int16_t *const In,
175                                                            int Q,
176                                                            const uint16_t * const Zigzag,
177                                                            int Non_Zero);
178    #endif
179    
180  /* Quantize all blocks -- Inter mode */  /* Quantize all blocks -- Inter mode */
181  static __inline uint8_t  static __inline uint8_t
# Line 196  Line 205 
205                          }                          }
206                  } else {                  } else {
207                          sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant);                          sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant);
208  //                      if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) )  #if 0
209  //                              sum = dct_quantize_trellis_mpeg_c (&qcoeff[i*64], &data[i*64], pMB->quant)+1;                          if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) )
210                                    sum = dct_quantize_trellis_mpeg_c (&qcoeff[i*64], &data[i*64], pMB->quant)+1;
211    #endif
212                  }                  }
213                  stop_quant_timer();                  stop_quant_timer();
214    
# Line 546  Line 557 
557    
558          /* left blocks */          /* left blocks */
559    
560          // 1=2, 2=4, 4=8, 8=1          /* 1=2, 2=4, 4=8, 8=1 */
561          MOVLINE(tmp, LINE(0, 1));          MOVLINE(tmp, LINE(0, 1));
562          MOVLINE(LINE(0, 1), LINE(0, 2));          MOVLINE(LINE(0, 1), LINE(0, 2));
563          MOVLINE(LINE(0, 2), LINE(0, 4));          MOVLINE(LINE(0, 2), LINE(0, 4));
564          MOVLINE(LINE(0, 4), LINE(2, 0));          MOVLINE(LINE(0, 4), LINE(2, 0));
565          MOVLINE(LINE(2, 0), tmp);          MOVLINE(LINE(2, 0), tmp);
566    
567          // 3=6, 6=12, 12=9, 9=3          /* 3=6, 6=12, 12=9, 9=3 */
568          MOVLINE(tmp, LINE(0, 3));          MOVLINE(tmp, LINE(0, 3));
569          MOVLINE(LINE(0, 3), LINE(0, 6));          MOVLINE(LINE(0, 3), LINE(0, 6));
570          MOVLINE(LINE(0, 6), LINE(2, 4));          MOVLINE(LINE(0, 6), LINE(2, 4));
571          MOVLINE(LINE(2, 4), LINE(2, 1));          MOVLINE(LINE(2, 4), LINE(2, 1));
572          MOVLINE(LINE(2, 1), tmp);          MOVLINE(LINE(2, 1), tmp);
573    
574          // 5=10, 10=5          /* 5=10, 10=5 */
575          MOVLINE(tmp, LINE(0, 5));          MOVLINE(tmp, LINE(0, 5));
576          MOVLINE(LINE(0, 5), LINE(2, 2));          MOVLINE(LINE(0, 5), LINE(2, 2));
577          MOVLINE(LINE(2, 2), tmp);          MOVLINE(LINE(2, 2), tmp);
578    
579          // 7=14, 14=13, 13=11, 11=7          /* 7=14, 14=13, 13=11, 11=7 */
580          MOVLINE(tmp, LINE(0, 7));          MOVLINE(tmp, LINE(0, 7));
581          MOVLINE(LINE(0, 7), LINE(2, 6));          MOVLINE(LINE(0, 7), LINE(2, 6));
582          MOVLINE(LINE(2, 6), LINE(2, 5));          MOVLINE(LINE(2, 6), LINE(2, 5));
# Line 574  Line 585 
585    
586          /* right blocks */          /* right blocks */
587    
588          // 1=2, 2=4, 4=8, 8=1          /* 1=2, 2=4, 4=8, 8=1 */
589          MOVLINE(tmp, LINE(1, 1));          MOVLINE(tmp, LINE(1, 1));
590          MOVLINE(LINE(1, 1), LINE(1, 2));          MOVLINE(LINE(1, 1), LINE(1, 2));
591          MOVLINE(LINE(1, 2), LINE(1, 4));          MOVLINE(LINE(1, 2), LINE(1, 4));
592          MOVLINE(LINE(1, 4), LINE(3, 0));          MOVLINE(LINE(1, 4), LINE(3, 0));
593          MOVLINE(LINE(3, 0), tmp);          MOVLINE(LINE(3, 0), tmp);
594    
595          // 3=6, 6=12, 12=9, 9=3          /* 3=6, 6=12, 12=9, 9=3 */
596          MOVLINE(tmp, LINE(1, 3));          MOVLINE(tmp, LINE(1, 3));
597          MOVLINE(LINE(1, 3), LINE(1, 6));          MOVLINE(LINE(1, 3), LINE(1, 6));
598          MOVLINE(LINE(1, 6), LINE(3, 4));          MOVLINE(LINE(1, 6), LINE(3, 4));
599          MOVLINE(LINE(3, 4), LINE(3, 1));          MOVLINE(LINE(3, 4), LINE(3, 1));
600          MOVLINE(LINE(3, 1), tmp);          MOVLINE(LINE(3, 1), tmp);
601    
602          // 5=10, 10=5          /* 5=10, 10=5 */
603          MOVLINE(tmp, LINE(1, 5));          MOVLINE(tmp, LINE(1, 5));
604          MOVLINE(LINE(1, 5), LINE(3, 2));          MOVLINE(LINE(1, 5), LINE(3, 2));
605          MOVLINE(LINE(3, 2), tmp);          MOVLINE(LINE(3, 2), tmp);
606    
607          // 7=14, 14=13, 13=11, 11=7          /* 7=14, 14=13, 13=11, 11=7 */
608          MOVLINE(tmp, LINE(1, 7));          MOVLINE(tmp, LINE(1, 7));
609          MOVLINE(LINE(1, 7), LINE(3, 6));          MOVLINE(LINE(1, 7), LINE(3, 6));
610          MOVLINE(LINE(3, 6), LINE(3, 5));          MOVLINE(LINE(3, 6), LINE(3, 5));
# Line 605  Line 616 
616    
617    
618    
619  /************************************************************************  /*****************************************************************************
620   *               Trellis based R-D optimal quantization                 *   *               Trellis based R-D optimal quantization
621   *                                                                      *   *
622   *   Trellis Quant code (C) 2003 Pascal Massimino skal(at)planet-d.net  *   *   Trellis Quant code (C) 2003 Pascal Massimino skal(at)planet-d.net
623   *                                                                      *   *
624   ************************************************************************/   ****************************************************************************/
625    
626    
627    #if 0
628  static int  static int
629  dct_quantize_trellis_mpeg_c(int16_t *const Out, const int16_t *const In, int Q,  dct_quantize_trellis_mpeg_c(int16_t *const Out,
630                  const uint16_t * const Zigzag, int Non_Zero)                                                          const int16_t *const In,
631  { return 63; }                                                          int Q,
632                                                            const uint16_t * const Zigzag,
633                                                            int Non_Zero)
634  //////////////////////////////////////////////////////////  {
635  //          return 63;
636  //        Trellis-Based quantization  }
637  //  #endif
 // So far I understand this paper:  
 //  
 //  "Trellis-Based R-D Optimal Quantization in H.263+"  
 //    J.Wen, M.Luttrell, J.Villasenor  
 //    IEEE Transactions on Image Processing, Vol.9, No.8, Aug. 2000.  
 //  
 // we are at stake with a simplified Bellmand-Ford / Dijkstra Single  
 // Source Shorted Path algo. But due to the underlying graph structure  
 // ("Trellis"), it can be turned into a dynamic programming algo,  
 // partially saving the explicit graph's nodes representation. And  
 // without using a heap, since the open frontier of the DAG is always  
 // known, and of fixed sized.  
 //  
 //////////////////////////////////////////////////////////  
638    
639    /*----------------------------------------------------------------------------
640     *
641     *        Trellis-Based quantization
642     *
643     * So far I understand this paper:
644     *
645     *  "Trellis-Based R-D Optimal Quantization in H.263+"
646     *    J.Wen, M.Luttrell, J.Villasenor
647     *    IEEE Transactions on Image Processing, Vol.9, No.8, Aug. 2000.
648     *
649     * we are at stake with a simplified Bellmand-Ford / Dijkstra Single
650     * Source Shorted Path algo. But due to the underlying graph structure
651     * ("Trellis"), it can be turned into a dynamic programming algo,
652     * partially saving the explicit graph's nodes representation. And
653     * without using a heap, since the open frontier of the DAG is always
654     * known, and of fixed sized.
655     *--------------------------------------------------------------------------*/
656    
 //////////////////////////////////////////////////////////  
 // Codes lengths for relevant levels.  
657    
658    // let's factorize:  
659    /* Codes lengths for relevant levels. */
660    
661      /* let's factorize: */
662  static const uint8_t Code_Len0[64] = {  static const uint8_t Code_Len0[64] = {
663    30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,    30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
664    30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };    30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
# Line 707  Line 723 
723     3, 4, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9,10,10,10,10,10,10,10,10,12,12,13,13,12,13,14,15,15,     3, 4, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9,10,10,10,10,10,10,10,10,12,12,13,13,12,13,14,15,15,
724    15,16,16,16,16,17,17,17,18,18,19,19,19,19,19,19,19,19,21,21,22,22,30,30,30,30,30,30,30,30,30,30 };    15,16,16,16,16,17,17,17,18,18,19,19,19,19,19,19,19,19,21,21,22,22,30,30,30,30,30,30,30,30,30,30 };
725    
726    // a few more table for LAST table:    /* a few more table for LAST table: */
727  static const uint8_t Code_Len21[64] = {  static const uint8_t Code_Len21[64] = {
728    13,20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,    13,20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
729    30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30};    30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30};
# Line 722  Line 738 
738    12,13,13,13,13,13,13,13,13,14,16,16,16,16,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,19,19};    12,13,13,13,13,13,13,13,13,14,16,16,16,16,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,19,19};
739    
740    
741  static const uint8_t * const B16_17_Code_Len[24] = { // levels [1..24]  static const uint8_t * const B16_17_Code_Len[24] = { /* levels [1..24] */
742    Code_Len20,Code_Len19,Code_Len18,Code_Len17,    Code_Len20,Code_Len19,Code_Len18,Code_Len17,
743    Code_Len16,Code_Len15,Code_Len14,Code_Len13,    Code_Len16,Code_Len15,Code_Len14,Code_Len13,
744    Code_Len12,Code_Len11,Code_Len10,Code_Len9,    Code_Len12,Code_Len11,Code_Len10,Code_Len9,
# Line 731  Line 747 
747    Code_Len2, Code_Len1, Code_Len1, Code_Len1,    Code_Len2, Code_Len1, Code_Len1, Code_Len1,
748  };  };
749    
750  static const uint8_t * const B16_17_Code_Len_Last[6] = { // levels [1..6]  static const uint8_t * const B16_17_Code_Len_Last[6] = { /* levels [1..6] */
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    
# Line 754  Line 770 
770    return -1;    return -1;
771  }  }
772    
773  //////////////////////////////////////////////////////////  /* this routine has been strippen of all debug code */
 // this routine has been strippen of all debug code  
 //////////////////////////////////////////////////////////  
774    
775  static int  static int
776  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_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero)
777  {  {
778    
779      // Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]),      /*
780      // not quantized one (Out[]). However, it only improves the result *very*           * Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]),
781      // slightly (~0.01dB), whereas speed drops to crawling level :)           * not quantized one (Out[]). However, it only improves the result *very*
782      // Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps,           * slightly (~0.01dB), whereas speed drops to crawling level :)
783             * Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps.
784             */
785    typedef struct { int16_t Run, Level; } NODE;    typedef struct { int16_t Run, Level; } NODE;
786    
787    NODE Nodes[65], Last;    NODE Nodes[65], Last;
# Line 775  Line 790 
790    const int Mult = 2*Q;    const int Mult = 2*Q;
791    const int Bias = (Q-1) | 1;    const int Bias = (Q-1) | 1;
792    const int Lev0 = Mult + Bias;    const int Lev0 = Mult + Bias;
793    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 */
794    
795    int Run_Start = -1;    int Run_Start = -1;
796    uint32_t Min_Cost = 2<<16;    uint32_t Min_Cost = 2<<16;
# Line 784  Line 799 
799    uint32_t Last_Cost = 0;    uint32_t Last_Cost = 0;
800    
801    int i, j;    int i, j;
802    Run_Costs[-1] = 2<<16;                          // source (w/ CBP penalty)    Run_Costs[-1] = 2<<16;                          /* source (w/ CBP penalty) */
803    
804    Non_Zero = Find_Last(Out, Zigzag, Non_Zero);    Non_Zero = Find_Last(Out, Zigzag, Non_Zero);
805    if (Non_Zero<0)    if (Non_Zero<0)
# Line 798  Line 813 
813      uint32_t Best_Cost = 0xf0000000;      uint32_t Best_Cost = 0xf0000000;
814      Last_Cost += Dist0;      Last_Cost += Dist0;
815    
816      if ((uint32_t)(Level1+1)<3)                 // very specialized loop for -1,0,+1      if ((uint32_t)(Level1+1)<3)                 /* very specialized loop for -1,0,+1 */
817      {      {
818          int dQ;          int dQ;
819                  int Run;                  int Run;
# Line 821  Line 836 
836          const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16);          const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16);
837          const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16);          const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16);
838    
839            // TODO: what about tie-breaks? Should we favor short runs or            /*
840            // long runs? Although the error is the same, it would not be                     * TODO: what about tie-breaks? Should we favor short runs or
841            // spread the same way along high and low frequencies...                     * long runs? Although the error is the same, it would not be
842                       * spread the same way along high and low frequencies...
843                       */
844    
845                          // (I'd say: favour short runs => hifreq errors (HVS) -- gruel )                          /* (I'd say: favour short runs => hifreq errors (HVS) -- gruel ) */
846    
847          if (Cost<Best_Cost) {          if (Cost<Best_Cost) {
848            Best_Cost    = Cost;            Best_Cost    = Cost;
# Line 841  Line 858 
858        if (Last_Node==i)        if (Last_Node==i)
859                          Last.Level = Nodes[i].Level;                          Last.Level = Nodes[i].Level;
860      }      }
861      else                      // "big" levels      else                      /* "big" levels */
862      {      {
863        const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last;        const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last;
864        int Level2;        int Level2;
# Line 858  Line 875 
875          Tbl_L2      = (Level2<=24) ? B16_17_Code_Len[Level2-1]     : Code_Len0;          Tbl_L2      = (Level2<=24) ? B16_17_Code_Len[Level2-1]     : Code_Len0;
876          Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0;          Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0;
877          Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0;          Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0;
878        } else { // Level1<-1        } else { /* Level1<-1 */
879          dQ1 = Level1*Mult-AC - Bias;          dQ1 = Level1*Mult-AC - Bias;
880          dQ2 = dQ1 + Mult;          dQ2 = dQ1 + Mult;
881          Level2 = Level1 + 1;          Level2 = Level1 + 1;
# Line 877  Line 894 
894          uint32_t Cost1, Cost2;          uint32_t Cost1, Cost2;
895          int bLevel;          int bLevel;
896    
897  // for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following:  /*
898  //        if (Cost_Base>=Best_Cost) continue;   * for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following:
899  // (? doesn't seem to have any effect -- gruel )   *      if (Cost_Base>=Best_Cost) continue;
900     * (? doesn't seem to have any effect -- gruel )
901     */
902    
903          Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16);          Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16);
904          Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21;          Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21;
# Line 911  Line 930 
930            Last.Level = bLevel;            Last.Level = bLevel;
931            Last_Node  = i;            Last_Node  = i;
932          }          }
933        } //end of "for Run"        } /* end of "for Run" */
934    
935      }      }
936    
# Line 923  Line 942 
942      }      }
943      else      else
944      {      {
945          // as noticed by Michael Niedermayer (michaelni at gmx.at), there's          /*
946          // a code shorter by 1 bit for a larger run (!), same level. We give                   * as noticed by Michael Niedermayer (michaelni at gmx.at), there's
947          // it a chance by not moving the left barrier too much.                   * a code shorter by 1 bit for a larger run (!), same level. We give
948                     * it a chance by not moving the left barrier too much.
949                     */
950    
951        while( Run_Costs[Run_Start]>Min_Cost+(1<<16) )        while( Run_Costs[Run_Start]>Min_Cost+(1<<16) )
952          Run_Start++;          Run_Start++;
953    
954          // spread on preceding coeffs the cost incurred by skipping this one          /* spread on preceding coeffs the cost incurred by skipping this one */
955        for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0;        for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0;
956        Min_Cost += Dist0;        Min_Cost += Dist0;
957      }      }
# Line 939  Line 960 
960    if (Last_Node<0)    if (Last_Node<0)
961      return -1;      return -1;
962    
963         // reconstruct optimal sequence backward with surviving paths         /* reconstruct optimal sequence backward with surviving paths */
964    memset(Out, 0x00, 64*sizeof(*Out));    memset(Out, 0x00, 64*sizeof(*Out));
965    Out[Zigzag[Last_Node]] = Last.Level;    Out[Zigzag[Last_Node]] = Last.Level;
966    i = Last_Node - Last.Run;    i = Last_Node - Last.Run;
# Line 960  Line 981 
981    
982    
983    
984  //////////////////////////////////////////////////////////  /* original version including heavy debugging info */
 // original version including heavy debugging info  
 //////////////////////////////////////////////////////////  
   
985    
986  #ifdef DBGTRELL  #ifdef DBGTRELL
987    
# Line 987  Line 1005 
1005      int j=0, j0=0;      int j=0, j0=0;
1006      int Run, Level;      int Run, Level;
1007    
1008      Bits = 2;   // CBP      Bits = 2;   /* CBP */
1009      while(j<Last) {      while(j<Last) {
1010        while(!C[Zigzag[j]])        while(!C[Zigzag[j]])
1011                          j++;                          j++;
# Line 1034  Line 1052 
1052  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_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero)
1053  {  {
1054    
1055      // Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]),      /*
1056      // not quantized one (Out[]). However, it only improves the result *very*           * Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]),
1057      // slightly (~0.01dB), whereas speed drops to crawling level :)           * not quantized one (Out[]). However, it only improves the result *very*
1058      // Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps,           * slightly (~0.01dB), whereas speed drops to crawling level :)
1059             * Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps.
1060             */
1061    typedef struct { int16_t Run, Level; } NODE;    typedef struct { int16_t Run, Level; } NODE;
1062    
1063    NODE Nodes[65], Last;    NODE Nodes[65], Last;
# Line 1047  Line 1066 
1066    const int Mult = 2*Q;    const int Mult = 2*Q;
1067    const int Bias = (Q-1) | 1;    const int Bias = (Q-1) | 1;
1068    const int Lev0 = Mult + Bias;    const int Lev0 = Mult + Bias;
1069    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 */
1070    
1071    int Run_Start = -1;    int Run_Start = -1;
1072    Run_Costs[-1] = 2<<16;                          // source (w/ CBP penalty)    Run_Costs[-1] = 2<<16;                          /* source (w/ CBP penalty) */
1073    uint32_t Min_Cost = 2<<16;    uint32_t Min_Cost = 2<<16;
1074    
1075    int Last_Node = -1;    int Last_Node = -1;
# Line 1059  Line 1078 
1078    int i, j;    int i, j;
1079    
1080  #if (DBG>0)  #if (DBG>0)
1081    Last.Level = 0; Last.Run = -1; // just initialize to smthg    Last.Level = 0; Last.Run = -1; /* just initialize to smthg */
1082  #endif  #endif
1083    
1084    Non_Zero = Find_Last(Out, Zigzag, Non_Zero);    Non_Zero = Find_Last(Out, Zigzag, Non_Zero);
# Line 1074  Line 1093 
1093      uint32_t Best_Cost = 0xf0000000;      uint32_t Best_Cost = 0xf0000000;
1094      Last_Cost += Dist0;      Last_Cost += Dist0;
1095    
1096      if ((uint32_t)(Level1+1)<3)                 // very specialized loop for -1,0,+1      if ((uint32_t)(Level1+1)<3)                 /* very specialized loop for -1,0,+1 */
1097      {      {
1098          int dQ;          int dQ;
1099                  int Run;                  int Run;
# Line 1097  Line 1116 
1116          const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16);          const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16);
1117          const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16);          const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16);
1118    
1119            // TODO: what about tie-breaks? Should we favor short runs or            /*
1120            // long runs? Although the error is the same, it would not be                     * TODO: what about tie-breaks? Should we favor short runs or
1121            // spread the same way along high and low frequencies...                     * long runs? Although the error is the same, it would not be
1122                       * spread the same way along high and low frequencies...
1123                       */
1124          if (Cost<Best_Cost) {          if (Cost<Best_Cost) {
1125            Best_Cost    = Cost;            Best_Cost    = Cost;
1126            Nodes[i].Run = Run;            Nodes[i].Run = Run;
# Line 1129  Line 1150 
1150          printf( "\n" );          printf( "\n" );
1151        }        }
1152      }      }
1153      else                      // "big" levels      else                      /* "big" levels */
1154      {      {
1155        const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last;        const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last;
1156        int Level2;        int Level2;
# Line 1146  Line 1167 
1167          Tbl_L2      = (Level2<=24) ? B16_17_Code_Len[Level2-1]     : Code_Len0;          Tbl_L2      = (Level2<=24) ? B16_17_Code_Len[Level2-1]     : Code_Len0;
1168          Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0;          Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0;
1169          Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0;          Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0;
1170        } else { // Level1<-1        } else { /* Level1<-1 */
1171          dQ1 = Level1*Mult-AC - Bias;          dQ1 = Level1*Mult-AC - Bias;
1172          dQ2 = dQ1 + Mult;          dQ2 = dQ1 + Mult;
1173          Level2 = Level1 + 1;          Level2 = Level1 + 1;
# Line 1165  Line 1186 
1186          uint32_t Cost1, Cost2;          uint32_t Cost1, Cost2;
1187          int bLevel;          int bLevel;
1188    
1189  // for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following:  /*
1190  //        if (Cost_Base>=Best_Cost) continue;   * for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following:
1191     *        if (Cost_Base>=Best_Cost) continue;
1192     */
1193          Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16);          Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16);
1194          Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21;          Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21;
1195    
# Line 1198  Line 1220 
1220            Last.Level = bLevel;            Last.Level = bLevel;
1221            Last_Node  = i;            Last_Node  = i;
1222          }          }
1223        } //end of "for Run"        } /* end of "for Run" */
1224    
1225        if (DBG==1) {        if (DBG==1) {
1226          Run_Costs[i] = Best_Cost;          Run_Costs[i] = Best_Cost;
# Line 1224  Line 1246 
1246      }      }
1247      else      else
1248      {      {
1249          // as noticed by Michael Niedermayer (michaelni at gmx.at), there's          /*
1250          // a code shorter by 1 bit for a larger run (!), same level. We give                   * as noticed by Michael Niedermayer (michaelni at gmx.at), there's
1251          // it a chance by not moving the left barrier too much.                   * a code shorter by 1 bit for a larger run (!), same level. We give
1252                     * it a chance by not moving the left barrier too much.
1253                     */
1254    
1255        while( Run_Costs[Run_Start]>Min_Cost+(1<<16) )        while( Run_Costs[Run_Start]>Min_Cost+(1<<16) )
1256          Run_Start++;          Run_Start++;
1257    
1258          // spread on preceding coeffs the cost incurred by skipping this one          /* spread on preceding coeffs the cost incurred by skipping this one */
1259        for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0;        for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0;
1260        Min_Cost += Dist0;        Min_Cost += Dist0;
1261      }      }
# Line 1249  Line 1273 
1273    if (Last_Node<0)    if (Last_Node<0)
1274      return -1;      return -1;
1275    
1276         // reconstruct optimal sequence backward with surviving paths         /* reconstruct optimal sequence backward with surviving paths */
1277    memset(Out, 0x00, 64*sizeof(*Out));    memset(Out, 0x00, 64*sizeof(*Out));
1278    Out[Zigzag[Last_Node]] = Last.Level;    Out[Zigzag[Last_Node]] = Last.Level;
1279    i = Last_Node - Last.Run;    i = Last_Node - Last.Run;

Legend:
Removed from v.1014  
changed lines
  Added in v.1053

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