[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, Sat Feb 15 15:22:19 2003 UTC revision 1.21.2.15, Thu Jul 24 13:09:14 2003 UTC
# Line 1  Line 1 
1   /******************************************************************************  /*****************************************************************************
2    *                                                                            *   *
3    *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *   *  XVID MPEG-4 VIDEO CODEC
4    *                                                                            *   *  - MB Transfert/Quantization functions -
5    *  XviD is an implementation of a part of one or more MPEG-4 Video tools     *   *
6    *  as specified in ISO/IEC 14496-2 standard.  Those intending to use this    *   *  Copyright(C) 2001-2003  Peter Ross <pross@xvid.org>
7    *  software module in hardware or software products are advised that its     *   *               2001-2003  Michael Militzer <isibaar@xvid.org>
8    *  use may infringe existing patents or copyrights, and any such use         *   *               2003       Edouard Gomez <ed.gomez@free.fr>
9    *  would be at such party's own risk.  The original developer of this        *   *
10    *  software module and his/her company, and subsequent editors and their     *   *  This program is free software ; you can redistribute it and/or modify
11    *  companies, will have no liability for use of this software or             *   *  it under the terms of the GNU General Public License as published by
12    *  modifications or derivatives thereof.                                     *   *  the Free Software Foundation ; either version 2 of the License, or
13    *                                                                            *   *  (at your option) any later version.
14    *  XviD is free software; you can redistribute it and/or modify it           *   *
15    *  under the terms of the GNU General Public License as published by         *   *  This program is distributed in the hope that it will be useful,
16    *  the Free Software Foundation; either version 2 of the License, or         *   *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
17    *  (at your option) any later version.                                       *   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    *                                                                            *   *  GNU General Public License for more details.
19    *  XviD is distributed in the hope that it will be useful, but               *   *
20    *  WITHOUT ANY WARRANTY; without even the implied warranty of                *   *  You should have received a copy of the GNU General Public License
21    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *   *  along with this program ; if not, write to the Free Software
22    *  GNU General Public License for more details.                              *   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23    *                                                                            *   *
24    *  You should have received a copy of the GNU General Public License         *   * $Id$
25    *  along with this program; if not, write to the Free Software               *   *
26    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *   ****************************************************************************/
   *                                                                            *  
   ******************************************************************************/  
   
  /******************************************************************************  
   *                                                                            *  
   *  mbtransquant.c                                                            *  
   *                                                                            *  
   *  Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au>                    *  
   *  Copyright (C) 2001 - Michael Militzer <isibaar@xvid.org>                  *  
   *                                                                            *  
   *  For more information visit the XviD homepage: http://www.xvid.org         *  
   *                                                                            *  
   ******************************************************************************/  
   
  /******************************************************************************  
   *                                                                            *  
   *  Revision history:                                                         *  
   *                                                                            *  
   *  29.03.2002 interlacing speedup - used transfer strides instead of             *  
   *             manual field-to-frame conversion                                                           *  
   *  26.03.2002 interlacing support - moved transfers outside loops                        *  
   *  22.12.2001 get_dc_scaler() moved to common.h                                                          *  
   *  19.11.2001 introduced coefficient thresholding (Isibaar)                  *  
   *  17.11.2001 initial version                                                *  
   *                                                                            *  
   ******************************************************************************/  
27    
28    #include <stdio.h>
29    #include <stdlib.h>
30  #include <string.h>  #include <string.h>
31    
32  #include "../portab.h"  #include "../portab.h"
# Line 59  Line 35 
35  #include "../global.h"  #include "../global.h"
36  #include "mem_transfer.h"  #include "mem_transfer.h"
37  #include "timer.h"  #include "timer.h"
38    #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 69  Line 47 
47    
48  MBFIELDTEST_PTR MBFieldTest;  MBFIELDTEST_PTR MBFieldTest;
49    
 #define TOOSMALL_LIMIT  1       /* skip blocks having a coefficient sum below this value */  
   
 void  
 MBTransQuantIntra(const MBParam * pParam,  
                                   FRAMEINFO * frame,  
                                   MACROBLOCK * pMB,  
                                   const uint32_t x_pos,  
                                   const uint32_t y_pos,  
                                   int16_t data[6 * 64],  
                                   int16_t qcoeff[6 * 64])  
 {  
   
         uint32_t stride = pParam->edged_width;  
         uint32_t stride2 = stride / 2;  
         uint32_t next_block = stride * ((frame->global_flags & XVID_REDUCED)?16:8);  
         uint32_t i;  
         uint32_t iQuant = frame->quant;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
         IMAGE *pCurrent = &frame->image;  
   
         start_timer();  
         if ((frame->global_flags & XVID_REDUCED))  
         {  
                 pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5);  
                 pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4);  
                 pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4);  
   
                 filter_18x18_to_8x8(&data[0 * 64], pY_Cur, stride);  
                 filter_18x18_to_8x8(&data[1 * 64], pY_Cur + 16, stride);  
                 filter_18x18_to_8x8(&data[2 * 64], pY_Cur + next_block, stride);  
                 filter_18x18_to_8x8(&data[3 * 64], pY_Cur + next_block + 16, stride);  
                 filter_18x18_to_8x8(&data[4 * 64], pU_Cur, stride2);  
                 filter_18x18_to_8x8(&data[5 * 64], pV_Cur, stride2);  
         }else{  
                 pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);  
                 pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);  
                 pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);  
   
                 transfer_8to16copy(&data[0 * 64], pY_Cur, stride);  
                 transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride);  
                 transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride);  
                 transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride);  
                 transfer_8to16copy(&data[4 * 64], pU_Cur, stride2);  
                 transfer_8to16copy(&data[5 * 64], pV_Cur, stride2);  
         }  
         stop_transfer_timer();  
   
         /* XXX: rrv+interlacing is buggy */  
         start_timer();  
         pMB->field_dct = 0;  
         if ((frame->global_flags & XVID_INTERLACING) &&  
                 (x_pos>0) && (x_pos<pParam->mb_width-1) &&  
                 (y_pos>0) && (y_pos<pParam->mb_height-1)) {  
                 pMB->field_dct = MBDecideFieldDCT(data);  
         }  
         stop_interlacing_timer();  
   
         for (i = 0; i < 6; i++) {  
                 uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);  
   
                 start_timer();  
                 fdct(&data[i * 64]);  
                 stop_dct_timer();  
   
                 if (pParam->m_quant_type == H263_QUANT) {  
                         start_timer();  
                         quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);  
                         stop_quant_timer();  
                 } else {  
                         start_timer();  
                         quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);  
                         stop_quant_timer();  
                 }  
   
                 /* speedup: dont decode when encoding only ivops */  
                 if (pParam->iMaxKeyInterval != 1 || pParam->max_bframes > 0)  
                 {  
                         if (pParam->m_quant_type == H263_QUANT) {  
                                 start_timer();  
                                 dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);  
                                 stop_iquant_timer();  
                         } else {  
                                 start_timer();  
                                 dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);  
                                 stop_iquant_timer();  
                         }  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
   
         /* speedup: dont decode when encoding only ivops */  
         if (pParam->iMaxKeyInterval != 1 || pParam->max_bframes > 0)  
         {  
   
                 if (pMB->field_dct) {  
                         next_block = stride;  
                         stride *= 2;  
                 }  
   
                 start_timer();  
                 if ((frame->global_flags & XVID_REDUCED))  
                 {  
                         copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);  
                         copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);  
                         copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);  
                         copy_upsampled_8x8_16to8(pY_Cur + next_block + 16, &data[3 * 64], stride);  
                         copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);  
                         copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);  
   
                 }else{  
                         transfer_16to8copy(pY_Cur, &data[0 * 64], stride);  
                         transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);  
                         transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);  
                         transfer_16to8copy(pY_Cur + next_block + 8, &data[3 * 64], stride);  
                         transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);  
                         transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);  
                 }  
                 stop_transfer_timer();  
         }  
   
 }  
   
   
 uint8_t  
 MBTransQuantInter(const MBParam * pParam,  
                                   FRAMEINFO * frame,  
                                   MACROBLOCK * pMB,  
                                   const uint32_t x_pos,  
                                   const uint32_t y_pos,  
                                   int16_t data[6 * 64],  
                                   int16_t qcoeff[6 * 64])  
 {  
   
         uint32_t stride = pParam->edged_width;  
         uint32_t stride2 = stride / 2;  
         uint32_t next_block = stride * ((frame->global_flags & XVID_REDUCED)?16:8);  
         uint32_t i;  
         uint32_t iQuant = frame->quant;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
         uint8_t cbp = 0;  
         uint32_t sum;  
         IMAGE *pCurrent = &frame->image;  
   
         if ((frame->global_flags & XVID_REDUCED))  
         {  
                 pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5);  
                 pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4);  
                 pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4);  
         }else{  
                 pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);  
                 pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);  
                 pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);  
         }  
   
         start_timer();  
         pMB->field_dct = 0;  
         if ((frame->global_flags & XVID_INTERLACING) &&  
                 (x_pos>0) && (x_pos<pParam->mb_width-1) &&  
                 (y_pos>0) && (y_pos<pParam->mb_height-1)) {  
                 pMB->field_dct = MBDecideFieldDCT(data);  
         }  
         stop_interlacing_timer();  
   
         for (i = 0; i < 6; i++) {  
                 uint32_t increase_limit = (iQuant == 1) ? 1 : 0;  
   
50                  /*                  /*
51                   *  no need to transfer 8->16-bit   * Skip blocks having a coefficient sum below this value. This value will be
52                   * (this is performed already in motion compensation)   * corrected according to the MB quantizer to avoid artifacts for quant==1
53                   */                   */
54                  start_timer();  #define PVOP_TOOSMALL_LIMIT 1
55                  fdct(&data[i * 64]);  #define BVOP_TOOSMALL_LIMIT 3
                 stop_dct_timer();  
56    
57                  if (pParam->m_quant_type == 0) {  /*****************************************************************************
58                          start_timer();   * Local functions
59                          sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant);   ****************************************************************************/
                         stop_quant_timer();  
                 } else {  
                         start_timer();  
                         sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant);  
                         stop_quant_timer();  
                 }  
   
                 if ((sum >= TOOSMALL_LIMIT + increase_limit) || (qcoeff[i*64] != 0) ||  
                         (qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) {  
   
                         if (pParam->m_quant_type == H263_QUANT) {  
                                 start_timer();  
                                 dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant);  
                                 stop_iquant_timer();  
                         } else {  
                                 start_timer();  
                                 dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant);  
                                 stop_iquant_timer();  
                         }  
   
                         cbp |= 1 << (5 - i);  
60    
61                          start_timer();  /* permute block and return field dct choice */
62                          idct(&data[i * 64]);  static __inline uint32_t
63                          stop_idct_timer();  MBDecideFieldDCT(int16_t data[6 * 64])
                 }  
         }  
   
         if (pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
   
         start_timer();  
         if ((frame->global_flags & XVID_REDUCED))  
         {  
                 if (cbp & 32)  
                         add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);  
                 if (cbp & 16)  
                         add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);  
                 if (cbp & 8)  
                         add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);  
                 if (cbp & 4)  
                         add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);  
                 if (cbp & 2)  
                         add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);  
                 if (cbp & 1)  
                         add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);  
         }else{  
                 if (cbp & 32)  
                         transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
                 if (cbp & 16)  
                         transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
                 if (cbp & 8)  
                         transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
                 if (cbp & 4)  
                         transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride);  
                 if (cbp & 2)  
                         transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
                 if (cbp & 1)  
                         transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         }  
         stop_transfer_timer();  
   
         return cbp;  
   
 }  
   
 void  
 MBTransQuantIntra2(const MBParam * pParam,  
                                   FRAMEINFO * frame,  
                                   MACROBLOCK * pMB,  
                                   const uint32_t x_pos,  
                                   const uint32_t y_pos,  
                                   int16_t data[6 * 64],  
                                   int16_t qcoeff[6 * 64])  
 {  
         MBTrans(pParam,frame,pMB,x_pos,y_pos,data);  
         MBfDCT(pParam,frame,pMB,data);  
         MBQuantIntra(pParam,frame,pMB,data,qcoeff);  
         MBDeQuantIntra(pParam,frame->quant,data,qcoeff);  
         MBiDCT(data,0x3F);  
         MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,0x3F);  
 }  
   
   
 uint8_t  
 MBTransQuantInter2(const MBParam * pParam,  
                                   FRAMEINFO * frame,  
                                   MACROBLOCK * pMB,  
                                   const uint32_t x_pos,  
                                   const uint32_t y_pos,  
                                   int16_t data[6 * 64],  
                                   int16_t qcoeff[6 * 64])  
 {  
         uint8_t cbp;  
   
 /* there is no MBTrans for Inter block, that's done in motion compensation already */  
   
         MBfDCT(pParam,frame,pMB,data);  
         cbp = MBQuantInter(pParam,frame->quant,data,qcoeff);  
         MBDeQuantInter(pParam,frame->quant,data,qcoeff,cbp);  
         MBiDCT(data,cbp);  
         MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,cbp);  
   
         return cbp;  
 }  
   
 uint8_t  
 MBTransQuantInterBVOP(const MBParam * pParam,  
                                   FRAMEINFO * frame,  
                                   MACROBLOCK * pMB,  
                                   int16_t data[6 * 64],  
                                   int16_t qcoeff[6 * 64])  
64  {  {
65          uint8_t cbp;          uint32_t field = MBFieldTest(data);
   
 /* there is no MBTrans for Inter block, that's done in motion compensation already */  
   
         MBfDCT(pParam,frame,pMB,data);  
         cbp = MBQuantInter(pParam,frame->quant,data,qcoeff);  
66    
67  /* we don't have to DeQuant, iDCT and Transfer back data for B-frames */          if (field)
68                    MBFrameToField(data);
69    
70          return cbp;          return field;
71  }  }
72    
73    /* Performs Forward DCT on all blocks */
74  void  static __inline void
75  MBfDCT(const MBParam * pParam,  MBfDCT(const MBParam * const pParam,
76                                    FRAMEINFO * frame,             const FRAMEINFO * const frame,
77                                    MACROBLOCK * pMB,             MACROBLOCK * const pMB,
78               uint32_t x_pos,
79               uint32_t y_pos,
80                                    int16_t data[6 * 64])                                    int16_t data[6 * 64])
81  {  {
82          int i;          /* Handles interlacing */
   
83          start_timer();          start_timer();
84          pMB->field_dct = 0;          pMB->field_dct = 0;
85          if ((frame->global_flags & XVID_INTERLACING)) {          if ((frame->vol_flags & XVID_VOL_INTERLACING) &&
86                    (x_pos>0) && (x_pos<pParam->mb_width-1) &&
87                    (y_pos>0) && (y_pos<pParam->mb_height-1)) {
88                  pMB->field_dct = MBDecideFieldDCT(data);                  pMB->field_dct = MBDecideFieldDCT(data);
89          }          }
90          stop_interlacing_timer();          stop_interlacing_timer();
91    
92          for (i = 0; i < 6; i++) {          /* Perform DCT */
93                  start_timer();                  start_timer();
94                  fdct(&data[i * 64]);          fdct(&data[0 * 64]);
95            fdct(&data[1 * 64]);
96            fdct(&data[2 * 64]);
97            fdct(&data[3 * 64]);
98            fdct(&data[4 * 64]);
99            fdct(&data[5 * 64]);
100                  stop_dct_timer();                  stop_dct_timer();
101          }          }
 }  
102    
103  void  /* Performs Inverse DCT on all blocks */
104  MBQuantDeQuantIntra(const MBParam * pParam,  static __inline void
105                                          FRAMEINFO * frame,  MBiDCT(int16_t data[6 * 64],
106                                          MACROBLOCK * pMB,             const uint8_t cbp)
                                         int16_t qcoeff[6 * 64],  
                                         int16_t data[6*64])  
107  {  {
         int i;  
         int iQuant = frame->quant;  
   
         start_timer();  
         pMB->field_dct = 0;  
         if ((frame->global_flags & XVID_INTERLACING)) {  
                 pMB->field_dct = MBDecideFieldDCT(data);  
         }  
         stop_interlacing_timer();  
   
         for (i = 0; i < 6; i++) {  
                 uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);  
   
                 if (pParam->m_quant_type == H263_QUANT) {  
                         start_timer();  
                         quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);  
                         stop_quant_timer();  
   
                         start_timer();  
                         dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);  
                         stop_iquant_timer();  
                 } else {  
108                          start_timer();                          start_timer();
109                          quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);          if(cbp & (1 << (5 - 0))) idct(&data[0 * 64]);
110                          stop_quant_timer();          if(cbp & (1 << (5 - 1))) idct(&data[1 * 64]);
111            if(cbp & (1 << (5 - 2))) idct(&data[2 * 64]);
112                          start_timer();          if(cbp & (1 << (5 - 3))) idct(&data[3 * 64]);
113                          dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);          if(cbp & (1 << (5 - 4))) idct(&data[4 * 64]);
114                          stop_iquant_timer();          if(cbp & (1 << (5 - 5))) idct(&data[5 * 64]);
115                  }          stop_idct_timer();
         }  
116  }  }
117    
118  void  /* Quantize all blocks -- Intra mode */
119    static __inline void
120  MBQuantIntra(const MBParam * pParam,  MBQuantIntra(const MBParam * pParam,
121                           FRAMEINFO * frame,                           const FRAMEINFO * const frame,
122                           MACROBLOCK *pMB,                           const MACROBLOCK * pMB,
123                       int16_t qcoeff[6 * 64],                       int16_t qcoeff[6 * 64],
124                           int16_t data[6*64])                           int16_t data[6*64])
125  {  {
126          int i;          int i;
         int iQuant = frame->quant;  
   
         start_timer();  
         pMB->field_dct = 0;  
         if ((frame->global_flags & XVID_INTERLACING)) {  
                 pMB->field_dct = MBDecideFieldDCT(data);  
         }  
         stop_interlacing_timer();  
127    
128          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
129                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(pMB->quant, i < 4);
130    
131                  if (pParam->m_quant_type == H263_QUANT) {                  /* Quantize the block */
132                          start_timer();                          start_timer();
133                          quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);                  if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) {
134                          stop_quant_timer();                          quant_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler);
135                  } else {                  } else {
136                          start_timer();                          quant4_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler);
                         quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);  
                         stop_quant_timer();  
137                  }                  }
138                    stop_quant_timer();
139          }          }
140  }  }
141    
142  void  /* DeQuantize all blocks -- Intra mode */
143    static __inline void
144  MBDeQuantIntra(const MBParam * pParam,  MBDeQuantIntra(const MBParam * pParam,
145                             const int iQuant,                             const int iQuant,
146                                    int16_t qcoeff[6 * 64],                                    int16_t qcoeff[6 * 64],
# Line 482  Line 151 
151          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
152                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
153    
                 if (pParam->m_quant_type == H263_QUANT) {  
                         start_timer();  
                         dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);  
                         stop_iquant_timer();  
                 } else {  
154                          start_timer();                          start_timer();
155                          dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);                  if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT))
156                            dequant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);
157                    else
158                            dequant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);
159                          stop_iquant_timer();                          stop_iquant_timer();
160                  }                  }
161          }          }
 }  
162    
163  uint8_t  
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 */
181    static __inline uint8_t
182  MBQuantInter(const MBParam * pParam,  MBQuantInter(const MBParam * pParam,
183                           const int iQuant,                           const FRAMEINFO * const frame,
184                             const MACROBLOCK * pMB,
185                                    int16_t data[6 * 64],                                    int16_t data[6 * 64],
186                                    int16_t qcoeff[6 * 64])                           int16_t qcoeff[6 * 64],
187                             int bvop,
188                             int limit)
189  {  {
190    
191          int i;          int i;
192          uint8_t cbp = 0;          uint8_t cbp = 0;
193          int sum;          int sum;
194            int code_block;
195    
196          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
197    
198                  if (pParam->m_quant_type == 0) {                  /* Quantize the block */
199                          start_timer();                  start_timer();
200                          sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant);                  if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) {
201                          stop_quant_timer();                          sum = quant_inter(&qcoeff[i*64], &data[i*64], pMB->quant);
202                            if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) {
203                                    sum = dct_quantize_trellis_h263_c(&qcoeff[i*64], &data[i*64], pMB->quant, &scan_tables[0][0], 63)+1;
204    /*                              limit = 1; // Isibaar: why? deactivated so far - so please complain! ;-) */
205                            }
206                    } else {
207                            sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant);
208    #if 0
209                            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();
214    
215                    /*
216                     * We code the block if the sum is higher than the limit and if the first
217                     * two AC coefficients in zig zag order are not zero.
218                     */
219                    code_block = 0;
220                    if ((sum >= limit) || (qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) {
221                            code_block = 1;
222                    } else {
223    
224                            if (bvop && (pMB->mode == MODE_DIRECT || pMB->mode == MODE_DIRECT_NO4V)) {
225                                    /* dark blocks prevention for direct mode */
226                                    if ((qcoeff[i*64] < -1) || (qcoeff[i*64] > 0))
227                                            code_block = 1;
228                  } else {                  } else {
229                          start_timer();                                  /* not direct mode */
230                          sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant);                                  if (qcoeff[i*64] != 0)
231                          stop_quant_timer();                                          code_block = 1;
232                  }                  }
   
                 if (sum >= TOOSMALL_LIMIT) {    // skip block ?  
                         cbp |= 1 << (5 - i);  
233                  }                  }
234    
235                    /* Set the corresponding cbp bit */
236                    cbp |= code_block << (5 - i);
237          }          }
238          return cbp;  
239            return(cbp);
240  }  }
241    
242  void  /* DeQuantize all blocks -- Inter mode */
243    static __inline void
244  MBDeQuantInter( const MBParam * pParam,  MBDeQuantInter( const MBParam * pParam,
245                                  const int iQuant,                                  const int iQuant,
246                                    int16_t data[6 * 64],                                    int16_t data[6 * 64],
# Line 534  Line 250 
250          int i;          int i;
251    
252          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
253                  if (cbp & (1 << (5 - i)))                  if (cbp & (1 << (5 - i))) {
                 {  
                         if (pParam->m_quant_type == H263_QUANT) {  
254                                  start_timer();                                  start_timer();
255                            if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT))
256                                  dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant);                                  dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant);
257                                  stop_iquant_timer();                          else
                         } else {  
                                 start_timer();  
258                                  dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant);                                  dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant);
259                                  stop_iquant_timer();                                  stop_iquant_timer();
260                          }                          }
261                  }                  }
262          }          }
 }  
   
 void  
 MBiDCT( int16_t data[6 * 64],  
                 const uint8_t cbp)  
 {  
         int i;  
   
         for (i = 0; i < 6; i++) {  
                 if (cbp & (1 << (5 - i)))  
                 {  
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
263    
264                  }  typedef void (transfer_operation_8to16_t) (int16_t *Dst, const uint8_t *Src, int BpS);
265          }  typedef void (transfer_operation_16to8_t) (uint8_t *Dst, const int16_t *Src, int BpS);
 }  
266    
267    
268  void  static __inline void
269  MBTrans(const MBParam * pParam,  MBTrans8to16(const MBParam * const pParam,
270                                    FRAMEINFO * frame,                           const FRAMEINFO * const frame,
271                                    MACROBLOCK * pMB,                           const MACROBLOCK * const pMB,
272                                    const uint32_t x_pos,                                    const uint32_t x_pos,
273                                    const uint32_t y_pos,                                    const uint32_t y_pos,
274                                    int16_t data[6 * 64])                                    int16_t data[6 * 64])
# Line 578  Line 276 
276          uint32_t stride = pParam->edged_width;          uint32_t stride = pParam->edged_width;
277          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
278          uint32_t next_block = stride * 8;          uint32_t next_block = stride * 8;
279            int32_t cst;
280          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
281          IMAGE *pCurrent = &frame->image;          const IMAGE * const pCurrent = &frame->image;
282            transfer_operation_8to16_t *transfer_op = NULL;
283    
284            if ((frame->vop_flags & XVID_VOP_REDUCED)) {
285    
286                    /* Image pointers */
287                    pY_Cur = pCurrent->y + (y_pos << 5) * stride  + (x_pos << 5);
288                    pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4);
289                    pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4);
290    
291                    /* Block size */
292                    cst = 16;
293    
294                    /* Operation function */
295                    transfer_op = (transfer_operation_8to16_t*)filter_18x18_to_8x8;
296            } else {
297    
298                    /* Image pointers */
299          pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);
300          pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);
301          pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);
302    
303                    /* Block size */
304                    cst = 8;
305    
306                    /* Operation function */
307                    transfer_op = (transfer_operation_8to16_t*)transfer_8to16copy;
308            }
309    
310            /* Do the transfer */
311          start_timer();          start_timer();
312          transfer_8to16copy(&data[0 * 64], pY_Cur, stride);          transfer_op(&data[0 * 64], pY_Cur, stride);
313          transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride);          transfer_op(&data[1 * 64], pY_Cur + cst, stride);
314          transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride);          transfer_op(&data[2 * 64], pY_Cur + next_block, stride);
315          transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride);          transfer_op(&data[3 * 64], pY_Cur + next_block + cst, stride);
316          transfer_8to16copy(&data[4 * 64], pU_Cur, stride2);          transfer_op(&data[4 * 64], pU_Cur, stride2);
317          transfer_8to16copy(&data[5 * 64], pV_Cur, stride2);          transfer_op(&data[5 * 64], pV_Cur, stride2);
318          stop_transfer_timer();          stop_transfer_timer();
319  }  }
320    
321  void  static __inline void
322  MBTransAdd(const MBParam * pParam,  MBTrans16to8(const MBParam * const pParam,
323                                    FRAMEINFO * frame,                           const FRAMEINFO * const frame,
324                                    MACROBLOCK * pMB,                           const MACROBLOCK * const pMB,
325                                    const uint32_t x_pos,                                    const uint32_t x_pos,
326                                    const uint32_t y_pos,                                    const uint32_t y_pos,
327                                    int16_t data[6 * 64],                                    int16_t data[6 * 64],
328                             const uint32_t add,
329                                    const uint8_t cbp)                                    const uint8_t cbp)
330  {  {
331          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
332          uint32_t stride = pParam->edged_width;          uint32_t stride = pParam->edged_width;
333          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
334          uint32_t next_block = stride * 8;          uint32_t next_block = stride * 8;
335          IMAGE *pCurrent = &frame->image;          uint32_t cst;
336            const IMAGE * const pCurrent = &frame->image;
337          pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);          transfer_operation_16to8_t *transfer_op = NULL;
         pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);  
         pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);  
338    
339          if (pMB->field_dct) {          if (pMB->field_dct) {
340                  next_block = stride;                  next_block = stride;
341                  stride *= 2;                  stride *= 2;
342          }          }
343    
344          start_timer();          if ((frame->vop_flags & XVID_VOP_REDUCED)) {
345          if (cbp & 32)  
346                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  /* Image pointers */
347          if (cbp & 16)                  pY_Cur = pCurrent->y + (y_pos << 5) * stride  + (x_pos << 5);
348                  transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);                  pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4);
349          if (cbp & 8)                  pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4);
350                  transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
351          if (cbp & 4)                  /* Block size */
352                  transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride);                  cst = 16;
353          if (cbp & 2)  
354                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);                  /* Operation function */
355          if (cbp & 1)                  if(add)
356                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);                          transfer_op = (transfer_operation_16to8_t*)add_upsampled_8x8_16to8;
357                    else
358                            transfer_op = (transfer_operation_16to8_t*)copy_upsampled_8x8_16to8;
359            } else {
360    
361                    /* Image pointers */
362                    pY_Cur = pCurrent->y + (y_pos << 4) * stride  + (x_pos << 4);
363                    pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);
364                    pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);
365    
366                    /* Block size */
367                    cst = 8;
368    
369                    /* Operation function */
370                    if(add)
371                            transfer_op = (transfer_operation_16to8_t*)transfer_16to8add;
372                    else
373                            transfer_op = (transfer_operation_16to8_t*)transfer_16to8copy;
374            }
375    
376            /* Do the operation */
377            start_timer();
378            if (cbp&32) transfer_op(pY_Cur, &data[0 * 64], stride);
379            if (cbp&16) transfer_op(pY_Cur + cst, &data[1 * 64], stride);
380            if (cbp& 8) transfer_op(pY_Cur + next_block, &data[2 * 64], stride);
381            if (cbp& 4) transfer_op(pY_Cur + next_block + cst, &data[3 * 64], stride);
382            if (cbp& 2) transfer_op(pU_Cur, &data[4 * 64], stride2);
383            if (cbp& 1) transfer_op(pV_Cur, &data[5 * 64], stride2);
384          stop_transfer_timer();          stop_transfer_timer();
385  }  }
386    
387    /*****************************************************************************
388     * Module functions
389     ****************************************************************************/
390    
391    void
392    MBTransQuantIntra(const MBParam * const pParam,
393                                      const FRAMEINFO * const frame,
394                                      MACROBLOCK * const pMB,
395                                      const uint32_t x_pos,
396                                      const uint32_t y_pos,
397                                      int16_t data[6 * 64],
398                                      int16_t qcoeff[6 * 64])
399    {
400    
401            /* Transfer data */
402            MBTrans8to16(pParam, frame, pMB, x_pos, y_pos, data);
403    
404  /* permute block and return field dct choice */          /* Perform DCT (and field decision) */
405            MBfDCT(pParam, frame, pMB, x_pos, y_pos, data);
406    
407            /* Quantize the block */
408            MBQuantIntra(pParam, frame, pMB, data, qcoeff);
409    
410            /* DeQuantize the block */
411            MBDeQuantIntra(pParam, pMB->quant, data, qcoeff);
412    
413  uint32_t          /* Perform inverse DCT*/
414  MBDecideFieldDCT(int16_t data[6 * 64])          MBiDCT(data, 0x3F);
415    
416            /* Transfer back the data -- Don't add data */
417            MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 0, 0x3F);
418    }
419    
420    
421    uint8_t
422    MBTransQuantInter(const MBParam * const pParam,
423                                      const FRAMEINFO * const frame,
424                                      MACROBLOCK * const pMB,
425                                      const uint32_t x_pos,
426                                      const uint32_t y_pos,
427                                      int16_t data[6 * 64],
428                                      int16_t qcoeff[6 * 64])
429  {  {
430          uint32_t field = MBFieldTest(data);          uint8_t cbp;
431            uint32_t limit;
432    
433          if (field) {          /*
434                  MBFrameToField(data);           * There is no MBTrans8to16 for Inter block, that's done in motion compensation
435             * already
436             */
437    
438            /* Perform DCT (and field decision) */
439            MBfDCT(pParam, frame, pMB, x_pos, y_pos, data);
440    
441            /* Set the limit threshold */
442            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 */
448            cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 0, limit);
449    
450            /* DeQuantize the block */
451            MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp);
452    
453            /* Perform inverse DCT*/
454            MBiDCT(data, cbp);
455    
456            /* Transfer back the data -- Add the data */
457            MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp);
458    
459            return(cbp);
460          }          }
461    
462          return field;  uint8_t
463    MBTransQuantInterBVOP(const MBParam * pParam,
464                                      FRAMEINFO * frame,
465                                      MACROBLOCK * pMB,
466                                      const uint32_t x_pos,
467                                      const uint32_t y_pos,
468                                      int16_t data[6 * 64],
469                                      int16_t qcoeff[6 * 64])
470    {
471            uint8_t cbp;
472            uint32_t limit;
473    
474            /*
475             * There is no MBTrans8to16 for Inter block, that's done in motion compensation
476             * already
477             */
478    
479            /* Perform DCT (and field decision) */
480            MBfDCT(pParam, frame, pMB, x_pos, y_pos, data);
481    
482            /* Set the limit threshold */
483            limit = BVOP_TOOSMALL_LIMIT;
484    
485            if (frame->vop_flags & XVID_VOP_CARTOON)
486                    limit *= 2;
487    
488            /* Quantize the block */
489            cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 1, limit);
490    
491            /*
492             * History comment:
493             * We don't have to DeQuant, iDCT and Transfer back data for B-frames.
494             *
495             * BUT some plugins require the original frame to be passed so we have
496             * to take care of that here
497             */
498            if((pParam->plugin_flags & XVID_REQORIGINAL)) {
499    
500                    /* DeQuantize the block */
501                    MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp);
502    
503                    /* Perform inverse DCT*/
504                    MBiDCT(data, cbp);
505    
506                    /* Transfer back the data -- Add the data */
507                    MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp);
508  }  }
509    
510            return(cbp);
511    }
512    
513  /* if sum(diff between field lines) < sum(diff between frame lines), use field dct */  /* if sum(diff between field lines) < sum(diff between frame lines), use field dct */
   
514  uint32_t  uint32_t
515  MBFieldTest_c(int16_t data[6 * 64])  MBFieldTest_c(int16_t data[6 * 64])
516  {  {
# Line 668  Line 524 
524          for (i = 0; i < 7; ++i) {          for (i = 0; i < 7; ++i) {
525                  for (j = 0; j < 8; ++j) {                  for (j = 0; j < 8; ++j) {
526                          frame +=                          frame +=
527                                  ABS(data[0 * 64 + (i + 1) * 8 + j] - data[0 * 64 + i * 8 + j]);                                  abs(data[0 * 64 + (i + 1) * 8 + j] - data[0 * 64 + i * 8 + j]);
528                          frame +=                          frame +=
529                                  ABS(data[1 * 64 + (i + 1) * 8 + j] - data[1 * 64 + i * 8 + j]);                                  abs(data[1 * 64 + (i + 1) * 8 + j] - data[1 * 64 + i * 8 + j]);
530                          frame +=                          frame +=
531                                  ABS(data[2 * 64 + (i + 1) * 8 + j] - data[2 * 64 + i * 8 + j]);                                  abs(data[2 * 64 + (i + 1) * 8 + j] - data[2 * 64 + i * 8 + j]);
532                          frame +=                          frame +=
533                                  ABS(data[3 * 64 + (i + 1) * 8 + j] - data[3 * 64 + i * 8 + j]);                                  abs(data[3 * 64 + (i + 1) * 8 + j] - data[3 * 64 + i * 8 + j]);
534    
535                          field +=                          field +=
536                                  ABS(data[blocks[i + 1] + lines[i + 1] + j] -                                  abs(data[blocks[i + 1] + lines[i + 1] + j] -
537                                          data[blocks[i] + lines[i] + j]);                                          data[blocks[i] + lines[i] + j]);
538                          field +=                          field +=
539                                  ABS(data[blocks[i + 1] + lines[i + 1] + 8 + j] -                                  abs(data[blocks[i + 1] + lines[i + 1] + 8 + j] -
540                                          data[blocks[i] + lines[i] + 8 + j]);                                          data[blocks[i] + lines[i] + 8 + j]);
541                          field +=                          field +=
542                                  ABS(data[blocks[i + 1] + 64 + lines[i + 1] + j] -                                  abs(data[blocks[i + 1] + 64 + lines[i + 1] + j] -
543                                          data[blocks[i] + 64 + lines[i] + j]);                                          data[blocks[i] + 64 + lines[i] + j]);
544                          field +=                          field +=
545                                  ABS(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] -                                  abs(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] -
546                                          data[blocks[i] + 64 + lines[i] + 8 + j]);                                          data[blocks[i] + 64 + lines[i] + 8 + j]);
547                  }                  }
548          }          }
# Line 707  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 735  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  
changed lines
  Added in v.1.21.2.15

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