[cvs] / xvidcore / src / utils / mbtransquant.c Repository:
ViewVC logotype

Diff of /xvidcore/src/utils/mbtransquant.c

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

revision 1.21.2.9, Sun Apr 27 19:47:48 2003 UTC revision 1.21.2.15, Thu Jul 24 13:09:14 2003 UTC
# Line 25  Line 25 
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
28  #include <string.h>  #include <stdio.h>
29  #include <stdlib.h>  #include <stdlib.h>
30    #include <string.h>
31    
32  #include "../portab.h"  #include "../portab.h"
33  #include "mbfunctions.h"  #include "mbfunctions.h"
# Line 35  Line 36 
36  #include "mem_transfer.h"  #include "mem_transfer.h"
37  #include "timer.h"  #include "timer.h"
38  #include "../bitstream/mbcoding.h"  #include "../bitstream/mbcoding.h"
39    #include "../bitstream/zigzag.h"
40  #include "../dct/fdct.h"  #include "../dct/fdct.h"
41  #include "../dct/idct.h"  #include "../dct/idct.h"
42  #include "../quant/quant_mpeg4.h"  #include "../quant/quant_mpeg4.h"
# Line 158  Line 160 
160          }          }
161  }  }
162    
163    
164    static int
165    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
173    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
182  MBQuantInter(const MBParam * pParam,  MBQuantInter(const MBParam * pParam,
# Line 181  Line 200 
200                  if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) {                  if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) {
201                          sum = quant_inter(&qcoeff[i*64], &data[i*64], pMB->quant);                          sum = quant_inter(&qcoeff[i*64], &data[i*64], pMB->quant);
202                          if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) {                          if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) {
203                                  sum = dct_quantize_trellis_inter_h263_c (&qcoeff[i*64], &data[i*64], pMB->quant)+1;                                  sum = dct_quantize_trellis_h263_c(&qcoeff[i*64], &data[i*64], pMB->quant, &scan_tables[0][0], 63)+1;
204                                  limit = 1;  /*                              limit = 1; // Isibaar: why? deactivated so far - so please complain! ;-) */
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_inter_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 420  Line 441 
441          /* Set the limit threshold */          /* Set the limit threshold */
442          limit = PVOP_TOOSMALL_LIMIT + ((pMB->quant == 1)? 1 : 0);          limit = PVOP_TOOSMALL_LIMIT + ((pMB->quant == 1)? 1 : 0);
443    
444            if (frame->vop_flags & XVID_VOP_CARTOON)
445                    limit *= 3;
446    
447          /* Quantize the block */          /* Quantize the block */
448          cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 0, limit);          cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 0, limit);
449    
# Line 458  Line 482 
482          /* Set the limit threshold */          /* Set the limit threshold */
483          limit = BVOP_TOOSMALL_LIMIT;          limit = BVOP_TOOSMALL_LIMIT;
484    
485            if (frame->vop_flags & XVID_VOP_CARTOON)
486                    limit *= 2;
487    
488          /* Quantize the block */          /* Quantize the block */
489          cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 1, limit);          cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 1, limit);
490    
# Line 536  Line 563 
563    
564          /* left blocks */          /* left blocks */
565    
566          // 1=2, 2=4, 4=8, 8=1          /* 1=2, 2=4, 4=8, 8=1 */
567          MOVLINE(tmp, LINE(0, 1));          MOVLINE(tmp, LINE(0, 1));
568          MOVLINE(LINE(0, 1), LINE(0, 2));          MOVLINE(LINE(0, 1), LINE(0, 2));
569          MOVLINE(LINE(0, 2), LINE(0, 4));          MOVLINE(LINE(0, 2), LINE(0, 4));
570          MOVLINE(LINE(0, 4), LINE(2, 0));          MOVLINE(LINE(0, 4), LINE(2, 0));
571          MOVLINE(LINE(2, 0), tmp);          MOVLINE(LINE(2, 0), tmp);
572    
573          // 3=6, 6=12, 12=9, 9=3          /* 3=6, 6=12, 12=9, 9=3 */
574          MOVLINE(tmp, LINE(0, 3));          MOVLINE(tmp, LINE(0, 3));
575          MOVLINE(LINE(0, 3), LINE(0, 6));          MOVLINE(LINE(0, 3), LINE(0, 6));
576          MOVLINE(LINE(0, 6), LINE(2, 4));          MOVLINE(LINE(0, 6), LINE(2, 4));
577          MOVLINE(LINE(2, 4), LINE(2, 1));          MOVLINE(LINE(2, 4), LINE(2, 1));
578          MOVLINE(LINE(2, 1), tmp);          MOVLINE(LINE(2, 1), tmp);
579    
580          // 5=10, 10=5          /* 5=10, 10=5 */
581          MOVLINE(tmp, LINE(0, 5));          MOVLINE(tmp, LINE(0, 5));
582          MOVLINE(LINE(0, 5), LINE(2, 2));          MOVLINE(LINE(0, 5), LINE(2, 2));
583          MOVLINE(LINE(2, 2), tmp);          MOVLINE(LINE(2, 2), tmp);
584    
585          // 7=14, 14=13, 13=11, 11=7          /* 7=14, 14=13, 13=11, 11=7 */
586          MOVLINE(tmp, LINE(0, 7));          MOVLINE(tmp, LINE(0, 7));
587          MOVLINE(LINE(0, 7), LINE(2, 6));          MOVLINE(LINE(0, 7), LINE(2, 6));
588          MOVLINE(LINE(2, 6), LINE(2, 5));          MOVLINE(LINE(2, 6), LINE(2, 5));
# Line 564  Line 591 
591    
592          /* right blocks */          /* right blocks */
593    
594          // 1=2, 2=4, 4=8, 8=1          /* 1=2, 2=4, 4=8, 8=1 */
595          MOVLINE(tmp, LINE(1, 1));          MOVLINE(tmp, LINE(1, 1));
596          MOVLINE(LINE(1, 1), LINE(1, 2));          MOVLINE(LINE(1, 1), LINE(1, 2));
597          MOVLINE(LINE(1, 2), LINE(1, 4));          MOVLINE(LINE(1, 2), LINE(1, 4));
598          MOVLINE(LINE(1, 4), LINE(3, 0));          MOVLINE(LINE(1, 4), LINE(3, 0));
599          MOVLINE(LINE(3, 0), tmp);          MOVLINE(LINE(3, 0), tmp);
600    
601          // 3=6, 6=12, 12=9, 9=3          /* 3=6, 6=12, 12=9, 9=3 */
602          MOVLINE(tmp, LINE(1, 3));          MOVLINE(tmp, LINE(1, 3));
603          MOVLINE(LINE(1, 3), LINE(1, 6));          MOVLINE(LINE(1, 3), LINE(1, 6));
604          MOVLINE(LINE(1, 6), LINE(3, 4));          MOVLINE(LINE(1, 6), LINE(3, 4));
605          MOVLINE(LINE(3, 4), LINE(3, 1));          MOVLINE(LINE(3, 4), LINE(3, 1));
606          MOVLINE(LINE(3, 1), tmp);          MOVLINE(LINE(3, 1), tmp);
607    
608          // 5=10, 10=5          /* 5=10, 10=5 */
609          MOVLINE(tmp, LINE(1, 5));          MOVLINE(tmp, LINE(1, 5));
610          MOVLINE(LINE(1, 5), LINE(3, 2));          MOVLINE(LINE(1, 5), LINE(3, 2));
611          MOVLINE(LINE(3, 2), tmp);          MOVLINE(LINE(3, 2), tmp);
612    
613          // 7=14, 14=13, 13=11, 11=7          /* 7=14, 14=13, 13=11, 11=7 */
614          MOVLINE(tmp, LINE(1, 7));          MOVLINE(tmp, LINE(1, 7));
615          MOVLINE(LINE(1, 7), LINE(3, 6));          MOVLINE(LINE(1, 7), LINE(3, 6));
616          MOVLINE(LINE(3, 6), LINE(3, 5));          MOVLINE(LINE(3, 6), LINE(3, 5));
617          MOVLINE(LINE(3, 5), LINE(3, 3));          MOVLINE(LINE(3, 5), LINE(3, 3));
618          MOVLINE(LINE(3, 3), tmp);          MOVLINE(LINE(3, 3), tmp);
619  }  }
620    
621    
622    
623    
624    
625    /*****************************************************************************
626     *               Trellis based R-D optimal quantization
627     *
628     *   Trellis Quant code (C) 2003 Pascal Massimino skal(at)planet-d.net
629     *
630     ****************************************************************************/
631    
632    
633    #if 0
634    static int
635    dct_quantize_trellis_mpeg_c(int16_t *const Out,
636                                                            const int16_t *const In,
637                                                            int Q,
638                                                            const uint16_t * const Zigzag,
639                                                            int Non_Zero)
640    {
641            return 63;
642    }
643    #endif
644    
645    /*----------------------------------------------------------------------------
646     *
647     *        Trellis-Based quantization
648     *
649     * So far I understand this paper:
650     *
651     *  "Trellis-Based R-D Optimal Quantization in H.263+"
652     *    J.Wen, M.Luttrell, J.Villasenor
653     *    IEEE Transactions on Image Processing, Vol.9, No.8, Aug. 2000.
654     *
655     * we are at stake with a simplified Bellmand-Ford / Dijkstra Single
656     * Source Shorted Path algo. But due to the underlying graph structure
657     * ("Trellis"), it can be turned into a dynamic programming algo,
658     * partially saving the explicit graph's nodes representation. And
659     * without using a heap, since the open frontier of the DAG is always
660     * known, and of fixed sized.
661     *--------------------------------------------------------------------------*/
662    
663    
664    
665    /* Codes lengths for relevant levels. */
666    
667      /* let's factorize: */
668    static const uint8_t Code_Len0[64] = {
669      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,
670      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 };
671    static const uint8_t Code_Len1[64] = {
672      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,30,
673      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 };
674    static const uint8_t Code_Len2[64] = {
675      19,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,
676      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 };
677    static const uint8_t Code_Len3[64] = {
678      18,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,
679      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 };
680    static const uint8_t Code_Len4[64] = {
681      17,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,
682      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 };
683    static const uint8_t Code_Len5[64] = {
684      16,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,
685      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 };
686    static const uint8_t Code_Len6[64] = {
687      15,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,
688      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 };
689    static const uint8_t Code_Len7[64] = {
690      13,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,
691      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 };
692    static const uint8_t Code_Len8[64] = {
693      11,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,
694      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 };
695    static const uint8_t Code_Len9[64] = {
696      12,21,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,
697      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 };
698    static const uint8_t Code_Len10[64] = {
699      12,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,
700      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 };
701    static const uint8_t Code_Len11[64] = {
702      12,19,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,
703      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 };
704    static const uint8_t Code_Len12[64] = {
705      11,17,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,
706      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 };
707    static const uint8_t Code_Len13[64] = {
708      11,15,21,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,
709      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 };
710    static const uint8_t Code_Len14[64] = {
711      10,12,19,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,
712      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 };
713    static const uint8_t Code_Len15[64] = {
714      10,13,17,19,21,21,21,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,
715      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 };
716    static const uint8_t Code_Len16[64] = {
717       9,12,13,18,18,19,19,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,
718      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};
719    static const uint8_t Code_Len17[64] = {
720       8,11,13,14,14,14,15,19,19,19,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
721      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 };
722    static const uint8_t Code_Len18[64] = {
723       7, 9,11,11,13,13,13,15,15,15,16,22,22,22,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
724      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 };
725    static const uint8_t Code_Len19[64] = {
726       5, 7, 9,10,10,11,11,11,11,11,13,14,16,17,17,18,18,18,18,18,18,18,18,20,20,21,21,30,30,30,30,30,
727      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 };
728    static const uint8_t Code_Len20[64] = {
729       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,
730      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 };
731    
732      /* a few more table for LAST table: */
733    static const uint8_t Code_Len21[64] = {
734      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,
735      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};
736    static const uint8_t Code_Len22[64] = {
737      12,15,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,
738      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};
739    static const uint8_t Code_Len23[64] = {
740      10,12,15,15,15,16,16,16,16,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,20,20,20,
741      20,21,21,21,21,21,21,21,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30};
742    static const uint8_t Code_Len24[64] = {
743       5, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,11,11,11,11,12,12,12,
744      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};
745    
746    
747    static const uint8_t * const B16_17_Code_Len[24] = { /* levels [1..24] */
748      Code_Len20,Code_Len19,Code_Len18,Code_Len17,
749      Code_Len16,Code_Len15,Code_Len14,Code_Len13,
750      Code_Len12,Code_Len11,Code_Len10,Code_Len9,
751      Code_Len8, Code_Len7 ,Code_Len6 ,Code_Len5,
752      Code_Len4, Code_Len3, Code_Len3 ,Code_Len2,
753      Code_Len2, Code_Len1, Code_Len1, Code_Len1,
754    };
755    
756    static const uint8_t * const B16_17_Code_Len_Last[6] = { /* levels [1..6] */
757      Code_Len24,Code_Len23,Code_Len22,Code_Len21, Code_Len3, Code_Len1,
758    };
759    
760    #define TL(q) 0xfe00/(q*q)
761    
762    static const int Trellis_Lambda_Tabs[31] = {
763             TL( 1),TL( 2),TL( 3),TL( 4),TL( 5),TL( 6), TL( 7),
764      TL( 8),TL( 9),TL(10),TL(11),TL(12),TL(13),TL(14), TL(15),
765      TL(16),TL(17),TL(18),TL(19),TL(20),TL(21),TL(22), TL(23),
766      TL(24),TL(25),TL(26),TL(27),TL(28),TL(29),TL(30), TL(31)
767    };
768    #undef TL
769    
770    static __inline int Find_Last(const int16_t *C, const uint16_t *Zigzag, int i)
771    {
772      while(i>=0)
773        if (C[Zigzag[i]])
774          return i;
775        else i--;
776      return -1;
777    }
778    
779    /* this routine has been strippen of all debug code */
780    
781    static int
782    dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero)
783    {
784    
785        /*
786             * Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]),
787             * not quantized one (Out[]). However, it only improves the result *very*
788             * slightly (~0.01dB), whereas speed drops to crawling level :)
789             * Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps.
790             */
791      typedef struct { int16_t Run, Level; } NODE;
792    
793      NODE Nodes[65], Last;
794      uint32_t Run_Costs0[64+1];
795      uint32_t * const Run_Costs = Run_Costs0 + 1;
796      const int Mult = 2*Q;
797      const int Bias = (Q-1) | 1;
798      const int Lev0 = Mult + Bias;
799      const int Lambda = Trellis_Lambda_Tabs[Q-1];    /* it's 1/lambda, actually */
800    
801      int Run_Start = -1;
802      uint32_t Min_Cost = 2<<16;
803    
804      int Last_Node = -1;
805      uint32_t Last_Cost = 0;
806    
807      int i, j;
808      Run_Costs[-1] = 2<<16;                          /* source (w/ CBP penalty) */
809    
810      Non_Zero = Find_Last(Out, Zigzag, Non_Zero);
811      if (Non_Zero<0)
812          return -1;
813    
814      for(i=0; i<=Non_Zero; i++)
815      {
816        const int AC = In[Zigzag[i]];
817        const int Level1 = Out[Zigzag[i]];
818        const int Dist0 = Lambda* AC*AC;
819        uint32_t Best_Cost = 0xf0000000;
820        Last_Cost += Dist0;
821    
822        if ((uint32_t)(Level1+1)<3)                 /* very specialized loop for -1,0,+1 */
823        {
824            int dQ;
825                    int Run;
826          uint32_t Cost0;
827    
828          if (AC<0) {
829            Nodes[i].Level = -1;
830            dQ = Lev0 + AC;
831          } else {
832            Nodes[i].Level = 1;
833            dQ = Lev0 - AC;
834          }
835                    Cost0 = Lambda*dQ*dQ;
836    
837          Nodes[i].Run = 1;
838          Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0;
839          for(Run=i-Run_Start; Run>0; --Run)
840          {
841            const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run];
842            const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16);
843            const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16);
844    
845              /*
846                       * TODO: what about tie-breaks? Should we favor short runs or
847                       * long runs? Although the error is the same, it would not be
848                       * spread the same way along high and low frequencies...
849                       */
850    
851                            /* (I'd say: favour short runs => hifreq errors (HVS) -- gruel ) */
852    
853            if (Cost<Best_Cost) {
854              Best_Cost    = Cost;
855              Nodes[i].Run = Run;
856            }
857    
858            if (lCost<Last_Cost) {
859              Last_Cost  = lCost;
860              Last.Run   = Run;
861              Last_Node  = i;
862            }
863          }
864          if (Last_Node==i)
865                            Last.Level = Nodes[i].Level;
866        }
867        else                      /* "big" levels */
868        {
869          const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last;
870          int Level2;
871          int dQ1, dQ2;
872          int Run;
873                    uint32_t Dist1,Dist2;
874                    int dDist21;
875    
876              if (Level1>1) {
877            dQ1 = Level1*Mult-AC + Bias;
878            dQ2 = dQ1 - Mult;
879            Level2 = Level1-1;
880            Tbl_L1      = (Level1<=24) ? B16_17_Code_Len[Level1-1]     : Code_Len0;
881            Tbl_L2      = (Level2<=24) ? B16_17_Code_Len[Level2-1]     : Code_Len0;
882            Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0;
883            Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0;
884          } else { /* Level1<-1 */
885            dQ1 = Level1*Mult-AC - Bias;
886            dQ2 = dQ1 + Mult;
887            Level2 = Level1 + 1;
888            Tbl_L1      = (Level1>=-24) ? B16_17_Code_Len[Level1^-1]      : Code_Len0;
889            Tbl_L2      = (Level2>=-24) ? B16_17_Code_Len[Level2^-1]      : Code_Len0;
890            Tbl_L1_Last = (Level1>=- 6) ? B16_17_Code_Len_Last[Level1^-1] : Code_Len0;
891            Tbl_L2_Last = (Level2>=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0;
892          }
893          Dist1 = Lambda*dQ1*dQ1;
894          Dist2 = Lambda*dQ2*dQ2;
895          dDist21 = Dist2-Dist1;
896    
897          for(Run=i-Run_Start; Run>0; --Run)
898          {
899            const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run];
900            uint32_t Cost1, Cost2;
901            int bLevel;
902    
903    /*
904     * for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following:
905     *      if (Cost_Base>=Best_Cost) continue;
906     * (? doesn't seem to have any effect -- gruel )
907     */
908    
909            Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16);
910            Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21;
911    
912            if (Cost2<Cost1) {
913                             Cost1 = Cost2;
914                             bLevel = Level2;
915                      } else
916                             bLevel = Level1;
917    
918            if (Cost1<Best_Cost) {
919              Best_Cost = Cost1;
920              Nodes[i].Run   = Run;
921              Nodes[i].Level = bLevel;
922            }
923    
924            Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16);
925            Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21;
926    
927            if (Cost2<Cost1) {
928                             Cost1 = Cost2;
929                             bLevel = Level2;
930                      } else
931                             bLevel = Level1;
932    
933            if (Cost1<Last_Cost) {
934              Last_Cost  = Cost1;
935              Last.Run   = Run;
936              Last.Level = bLevel;
937              Last_Node  = i;
938            }
939          } /* end of "for Run" */
940    
941        }
942    
943        Run_Costs[i] = Best_Cost;
944    
945        if (Best_Cost < Min_Cost + Dist0) {
946          Min_Cost = Best_Cost;
947          Run_Start = i;
948        }
949        else
950        {
951            /*
952                     * as noticed by Michael Niedermayer (michaelni at gmx.at), there's
953                     * a code shorter by 1 bit for a larger run (!), same level. We give
954                     * it a chance by not moving the left barrier too much.
955                     */
956    
957          while( Run_Costs[Run_Start]>Min_Cost+(1<<16) )
958            Run_Start++;
959    
960            /* spread on preceding coeffs the cost incurred by skipping this one */
961          for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0;
962          Min_Cost += Dist0;
963        }
964      }
965    
966      if (Last_Node<0)
967        return -1;
968    
969           /* reconstruct optimal sequence backward with surviving paths */
970      memset(Out, 0x00, 64*sizeof(*Out));
971      Out[Zigzag[Last_Node]] = Last.Level;
972      i = Last_Node - Last.Run;
973      while(i>=0) {
974        Out[Zigzag[i]] = Nodes[i].Level;
975        i -= Nodes[i].Run;
976      }
977      return Last_Node;
978    }
979    
980    
981    
982    
983    
984    
985    
986    
987    
988    
989    
990    /* original version including heavy debugging info */
991    
992    #ifdef DBGTRELL
993    
994    #define DBG 0
995    
996    static __inline uint32_t Evaluate_Cost(const int16_t *C, int Mult, int Bias,
997                                    const uint16_t * Zigzag, int Max, int Lambda)
998    {
999    #if (DBG>0)
1000      const int16_t * const Ref = C + 6*64;
1001      int Last = Max;
1002      int Bits = 0;
1003      int Dist = 0;
1004      int i;
1005      uint32_t Cost;
1006    
1007      while(Last>=0 && C[Zigzag[Last]]==0)
1008            Last--;
1009    
1010      if (Last>=0) {
1011        int j=0, j0=0;
1012        int Run, Level;
1013    
1014        Bits = 2;   /* CBP */
1015        while(j<Last) {
1016          while(!C[Zigzag[j]])
1017                            j++;
1018          if (j==Last)
1019                            break;
1020          Level=C[Zigzag[j]];
1021          Run = j - j0;
1022          j0 = ++j;
1023          if (Level>=-24 && Level<=24)
1024                            Bits += B16_17_Code_Len[(Level<0) ? -Level-1 : Level-1][Run];
1025          else
1026                            Bits += 30;
1027        }
1028        Level = C[Zigzag[Last]];
1029        Run = j - j0;
1030        if (Level>=-6 && Level<=6)
1031                    Bits += B16_17_Code_Len_Last[(Level<0) ? -Level-1 : Level-1][Run];
1032        else
1033                    Bits += 30;
1034      }
1035    
1036      for(i=0; i<=Last; ++i) {
1037        int V = C[Zigzag[i]]*Mult;
1038        if (V>0)
1039                    V += Bias;
1040        else
1041                    if (V<0)
1042                            V -= Bias;
1043        V -= Ref[Zigzag[i]];
1044        Dist += V*V;
1045      }
1046      Cost = Lambda*Dist + (Bits<<16);
1047      if (DBG==1)
1048        printf( " Last:%2d/%2d Cost = [(Bits=%5.0d) + Lambda*(Dist=%6.0d) = %d ] >>12= %d ", Last,Max, Bits, Dist, Cost, Cost>>12 );
1049      return Cost;
1050    
1051    #else
1052      return 0;
1053    #endif
1054    }
1055    
1056    
1057    static int
1058    dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero)
1059    {
1060    
1061        /*
1062             * Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]),
1063             * not quantized one (Out[]). However, it only improves the result *very*
1064             * slightly (~0.01dB), whereas speed drops to crawling level :)
1065             * Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps.
1066             */
1067      typedef struct { int16_t Run, Level; } NODE;
1068    
1069      NODE Nodes[65], Last;
1070      uint32_t Run_Costs0[64+1];
1071      uint32_t * const Run_Costs = Run_Costs0 + 1;
1072      const int Mult = 2*Q;
1073      const int Bias = (Q-1) | 1;
1074      const int Lev0 = Mult + Bias;
1075      const int Lambda = Trellis_Lambda_Tabs[Q-1];    /* it's 1/lambda, actually */
1076    
1077      int Run_Start = -1;
1078      Run_Costs[-1] = 2<<16;                          /* source (w/ CBP penalty) */
1079      uint32_t Min_Cost = 2<<16;
1080    
1081      int Last_Node = -1;
1082      uint32_t Last_Cost = 0;
1083    
1084      int i, j;
1085    
1086    #if (DBG>0)
1087      Last.Level = 0; Last.Run = -1; /* just initialize to smthg */
1088    #endif
1089    
1090      Non_Zero = Find_Last(Out, Zigzag, Non_Zero);
1091      if (Non_Zero<0)
1092          return -1;
1093    
1094      for(i=0; i<=Non_Zero; i++)
1095      {
1096        const int AC = In[Zigzag[i]];
1097        const int Level1 = Out[Zigzag[i]];
1098        const int Dist0 = Lambda* AC*AC;
1099        uint32_t Best_Cost = 0xf0000000;
1100        Last_Cost += Dist0;
1101    
1102        if ((uint32_t)(Level1+1)<3)                 /* very specialized loop for -1,0,+1 */
1103        {
1104            int dQ;
1105                    int Run;
1106          uint32_t Cost0;
1107    
1108          if (AC<0) {
1109            Nodes[i].Level = -1;
1110            dQ = Lev0 + AC;
1111          } else {
1112            Nodes[i].Level = 1;
1113            dQ = Lev0 - AC;
1114          }
1115                    Cost0 = Lambda*dQ*dQ;
1116    
1117          Nodes[i].Run = 1;
1118          Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0;
1119          for(Run=i-Run_Start; Run>0; --Run)
1120          {
1121            const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run];
1122            const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16);
1123            const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16);
1124    
1125              /*
1126                       * TODO: what about tie-breaks? Should we favor short runs or
1127                       * long runs? Although the error is the same, it would not be
1128                       * spread the same way along high and low frequencies...
1129                       */
1130            if (Cost<Best_Cost) {
1131              Best_Cost    = Cost;
1132              Nodes[i].Run = Run;
1133            }
1134    
1135            if (lCost<Last_Cost) {
1136              Last_Cost  = lCost;
1137              Last.Run   = Run;
1138              Last_Node  = i;
1139            }
1140          }
1141          if (Last_Node==i)
1142                            Last.Level = Nodes[i].Level;
1143    
1144          if (DBG==1) {
1145            Run_Costs[i] = Best_Cost;
1146            printf( "Costs #%2d: ", i);
1147            for(j=-1;j<=Non_Zero;++j) {
1148              if (j==Run_Start)            printf( " %3.0d|", Run_Costs[j]>>12 );
1149              else if (j>Run_Start && j<i) printf( " %3.0d|", Run_Costs[j]>>12 );
1150              else if (j==i)               printf( "(%3.0d)", Run_Costs[j]>>12 );
1151              else                         printf( "  - |" );
1152            }
1153            printf( "<%3.0d %2d %d>", Min_Cost>>12, Nodes[i].Level, Nodes[i].Run );
1154            printf( "  Last:#%2d {%3.0d %2d %d}", Last_Node, Last_Cost>>12, Last.Level, Last.Run );
1155            printf( " AC:%3.0d Dist0:%3d Dist(%d)=%d", AC, Dist0>>12, Nodes[i].Level, Cost0>>12 );
1156            printf( "\n" );
1157          }
1158        }
1159        else                      /* "big" levels */
1160        {
1161          const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last;
1162          int Level2;
1163          int dQ1, dQ2;
1164          int Run;
1165                    uint32_t Dist1,Dist2;
1166                    int dDist21;
1167    
1168              if (Level1>1) {
1169            dQ1 = Level1*Mult-AC + Bias;
1170            dQ2 = dQ1 - Mult;
1171            Level2 = Level1-1;
1172            Tbl_L1      = (Level1<=24) ? B16_17_Code_Len[Level1-1]     : Code_Len0;
1173            Tbl_L2      = (Level2<=24) ? B16_17_Code_Len[Level2-1]     : Code_Len0;
1174            Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0;
1175            Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0;
1176          } else { /* Level1<-1 */
1177            dQ1 = Level1*Mult-AC - Bias;
1178            dQ2 = dQ1 + Mult;
1179            Level2 = Level1 + 1;
1180            Tbl_L1      = (Level1>=-24) ? B16_17_Code_Len[Level1^-1]      : Code_Len0;
1181            Tbl_L2      = (Level2>=-24) ? B16_17_Code_Len[Level2^-1]      : Code_Len0;
1182            Tbl_L1_Last = (Level1>=- 6) ? B16_17_Code_Len_Last[Level1^-1] : Code_Len0;
1183            Tbl_L2_Last = (Level2>=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0;
1184          }
1185          Dist1 = Lambda*dQ1*dQ1;
1186          Dist2 = Lambda*dQ2*dQ2;
1187          dDist21 = Dist2-Dist1;
1188    
1189          for(Run=i-Run_Start; Run>0; --Run)
1190          {
1191            const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run];
1192            uint32_t Cost1, Cost2;
1193            int bLevel;
1194    
1195    /*
1196     * for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following:
1197     *        if (Cost_Base>=Best_Cost) continue;
1198     */
1199            Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16);
1200            Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21;
1201    
1202            if (Cost2<Cost1) {
1203                             Cost1 = Cost2;
1204                             bLevel = Level2;
1205                      } else
1206                             bLevel = Level1;
1207    
1208            if (Cost1<Best_Cost) {
1209              Best_Cost = Cost1;
1210              Nodes[i].Run   = Run;
1211              Nodes[i].Level = bLevel;
1212            }
1213    
1214            Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16);
1215            Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21;
1216    
1217            if (Cost2<Cost1) {
1218                             Cost1 = Cost2;
1219                             bLevel = Level2;
1220                      } else
1221                             bLevel = Level1;
1222    
1223            if (Cost1<Last_Cost) {
1224              Last_Cost  = Cost1;
1225              Last.Run   = Run;
1226              Last.Level = bLevel;
1227              Last_Node  = i;
1228            }
1229          } /* end of "for Run" */
1230    
1231          if (DBG==1) {
1232            Run_Costs[i] = Best_Cost;
1233            printf( "Costs #%2d: ", i);
1234            for(j=-1;j<=Non_Zero;++j) {
1235              if (j==Run_Start)            printf( " %3.0d|", Run_Costs[j]>>12 );
1236              else if (j>Run_Start && j<i) printf( " %3.0d|", Run_Costs[j]>>12 );
1237              else if (j==i)               printf( "(%3.0d)", Run_Costs[j]>>12 );
1238              else                         printf( "  - |" );
1239            }
1240            printf( "<%3.0d %2d %d>", Min_Cost>>12, Nodes[i].Level, Nodes[i].Run );
1241            printf( "  Last:#%2d {%3.0d %2d %d}", Last_Node, Last_Cost>>12, Last.Level, Last.Run );
1242            printf( " AC:%3.0d Dist0:%3d Dist(%2d):%3d Dist(%2d):%3d", AC, Dist0>>12, Level1, Dist1>>12, Level2, Dist2>>12 );
1243            printf( "\n" );
1244          }
1245        }
1246    
1247        Run_Costs[i] = Best_Cost;
1248    
1249        if (Best_Cost < Min_Cost + Dist0) {
1250          Min_Cost = Best_Cost;
1251          Run_Start = i;
1252        }
1253        else
1254        {
1255            /*
1256                     * as noticed by Michael Niedermayer (michaelni at gmx.at), there's
1257                     * a code shorter by 1 bit for a larger run (!), same level. We give
1258                     * it a chance by not moving the left barrier too much.
1259                     */
1260    
1261          while( Run_Costs[Run_Start]>Min_Cost+(1<<16) )
1262            Run_Start++;
1263    
1264            /* spread on preceding coeffs the cost incurred by skipping this one */
1265          for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0;
1266          Min_Cost += Dist0;
1267        }
1268      }
1269    
1270      if (DBG) {
1271        Last_Cost = Evaluate_Cost(Out,Mult,Bias, Zigzag,Non_Zero, Lambda);
1272        if (DBG==1) {
1273          printf( "=> " );
1274          for(i=0; i<=Non_Zero; ++i) printf( "[%3.0d] ", Out[Zigzag[i]] );
1275          printf( "\n" );
1276       }
1277      }
1278    
1279      if (Last_Node<0)
1280        return -1;
1281    
1282           /* reconstruct optimal sequence backward with surviving paths */
1283      memset(Out, 0x00, 64*sizeof(*Out));
1284      Out[Zigzag[Last_Node]] = Last.Level;
1285      i = Last_Node - Last.Run;
1286      while(i>=0) {
1287        Out[Zigzag[i]] = Nodes[i].Level;
1288        i -= Nodes[i].Run;
1289      }
1290    
1291      if (DBG) {
1292        uint32_t Cost = Evaluate_Cost(Out,Mult,Bias, Zigzag,Non_Zero, Lambda);
1293        if (DBG==1) {
1294          printf( "<= " );
1295          for(i=0; i<=Last_Node; ++i) printf( "[%3.0d] ", Out[Zigzag[i]] );
1296          printf( "\n--------------------------------\n" );
1297        }
1298        if (Cost>Last_Cost) printf( "!!! %u > %u\n", Cost, Last_Cost );
1299      }
1300      return Last_Node;
1301    }
1302    
1303    #undef DBG
1304    
1305    #endif

Legend:
Removed from v.1.21.2.9  
changed lines
  Added in v.1.21.2.15

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