[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.8, Sun Apr 13 16:18:09 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 <string.h>  #include <string.h>
29    #include <stdlib.h>
30    
31  #include "../portab.h"  #include "../portab.h"
32  #include "mbfunctions.h"  #include "mbfunctions.h"
# Line 69  Line 44 
44    
45  MBFIELDTEST_PTR MBFieldTest;  MBFIELDTEST_PTR MBFieldTest;
46    
 #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;  
   
47                  /*                  /*
48                   *  no need to transfer 8->16-bit   * Skip blocks having a coefficient sum below this value. This value will be
49                   * (this is performed already in motion compensation)   * corrected according to the MB quantizer to avoid artifacts for quant==1
50                   */                   */
51                  start_timer();  #define PVOP_TOOSMALL_LIMIT 1
52                  fdct(&data[i * 64]);  #define BVOP_TOOSMALL_LIMIT 3
                 stop_dct_timer();  
   
                 if (pParam->m_quant_type == 0) {  
                         start_timer();  
                         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);  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
   
         if (pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
53    
54          start_timer();  /*****************************************************************************
55          if ((frame->global_flags & XVID_REDUCED))   * Local functions
56          {   ****************************************************************************/
                 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;  
57    
58  /* there is no MBTrans for Inter block, that's done in motion compensation already */  /* permute block and return field dct choice */
59    static __inline uint32_t
60          MBfDCT(pParam,frame,pMB,data);  MBDecideFieldDCT(int16_t data[6 * 64])
         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])  
61  {  {
62          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);  
63    
64  /* we don't have to DeQuant, iDCT and Transfer back data for B-frames */          if (field)
65                    MBFrameToField(data);
66    
67          return cbp;          return field;
68  }  }
69    
70    /* Performs Forward DCT on all blocks */
71  void  static __inline void
72  MBfDCT(const MBParam * pParam,  MBfDCT(const MBParam * const pParam,
73                                    FRAMEINFO * frame,             const FRAMEINFO * const frame,
74                                    MACROBLOCK * pMB,             MACROBLOCK * const pMB,
75               uint32_t x_pos,
76               uint32_t y_pos,
77                                    int16_t data[6 * 64])                                    int16_t data[6 * 64])
78  {  {
79          int i;          /* Handles interlacing */
   
80          start_timer();          start_timer();
81          pMB->field_dct = 0;          pMB->field_dct = 0;
82          if ((frame->global_flags & XVID_INTERLACING)) {          if ((frame->vol_flags & XVID_VOL_INTERLACING) &&
83                    (x_pos>0) && (x_pos<pParam->mb_width-1) &&
84                    (y_pos>0) && (y_pos<pParam->mb_height-1)) {
85                  pMB->field_dct = MBDecideFieldDCT(data);                  pMB->field_dct = MBDecideFieldDCT(data);
86          }          }
87          stop_interlacing_timer();          stop_interlacing_timer();
88    
89          for (i = 0; i < 6; i++) {          /* Perform DCT */
90                  start_timer();                  start_timer();
91                  fdct(&data[i * 64]);          fdct(&data[0 * 64]);
92            fdct(&data[1 * 64]);
93            fdct(&data[2 * 64]);
94            fdct(&data[3 * 64]);
95            fdct(&data[4 * 64]);
96            fdct(&data[5 * 64]);
97                  stop_dct_timer();                  stop_dct_timer();
98          }          }
 }  
99    
100  void  /* Performs Inverse DCT on all blocks */
101  MBQuantDeQuantIntra(const MBParam * pParam,  static __inline void
102                                          FRAMEINFO * frame,  MBiDCT(int16_t data[6 * 64],
103                                          MACROBLOCK * pMB,             const uint8_t cbp)
                                         int16_t qcoeff[6 * 64],  
                                         int16_t data[6*64])  
104  {  {
         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 {  
                         start_timer();  
                         quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);  
                         stop_quant_timer();  
   
105                          start_timer();                          start_timer();
106                          dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);          if(cbp & (1 << (5 - 0))) idct(&data[0 * 64]);
107                          stop_iquant_timer();          if(cbp & (1 << (5 - 1))) idct(&data[1 * 64]);
108                  }          if(cbp & (1 << (5 - 2))) idct(&data[2 * 64]);
109          }          if(cbp & (1 << (5 - 3))) idct(&data[3 * 64]);
110            if(cbp & (1 << (5 - 4))) idct(&data[4 * 64]);
111            if(cbp & (1 << (5 - 5))) idct(&data[5 * 64]);
112            stop_idct_timer();
113  }  }
114    
115  void  /* Quantize all blocks -- Intra mode */
116    static __inline void
117  MBQuantIntra(const MBParam * pParam,  MBQuantIntra(const MBParam * pParam,
118                           FRAMEINFO * frame,                           const MACROBLOCK * pMB,
                          MACROBLOCK *pMB,  
119                       int16_t qcoeff[6 * 64],                       int16_t qcoeff[6 * 64],
120                           int16_t data[6*64])                           int16_t data[6*64])
121  {  {
122          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();  
123    
124          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
125                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(pMB->quant, i < 4);
126    
127                  if (pParam->m_quant_type == H263_QUANT) {                  /* Quantize the block */
                         start_timer();  
                         quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);  
                         stop_quant_timer();  
                 } else {  
128                          start_timer();                          start_timer();
129                          quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);                  if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT))
130                            quant_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler);
131                    else
132                            quant4_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler);
133                          stop_quant_timer();                          stop_quant_timer();
134                  }                  }
135          }          }
 }  
136    
137  void  /* DeQuantize all blocks -- Intra mode */
138    static __inline void
139  MBDeQuantIntra(const MBParam * pParam,  MBDeQuantIntra(const MBParam * pParam,
140                             const int iQuant,                             const int iQuant,
141                                    int16_t qcoeff[6 * 64],                                    int16_t qcoeff[6 * 64],
# Line 482  Line 146 
146          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
147                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
148    
                 if (pParam->m_quant_type == H263_QUANT) {  
                         start_timer();  
                         dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);  
                         stop_iquant_timer();  
                 } else {  
149                          start_timer();                          start_timer();
150                          dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);                  if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT))
151                            dequant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);
152                    else
153                            dequant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);
154                          stop_iquant_timer();                          stop_iquant_timer();
155                  }                  }
156          }          }
 }  
157    
158  uint8_t  /* Quantize all blocks -- Inter mode */
159    static __inline uint8_t
160  MBQuantInter(const MBParam * pParam,  MBQuantInter(const MBParam * pParam,
161                           const int iQuant,                           const MACROBLOCK * pMB,
162                                    int16_t data[6 * 64],                                    int16_t data[6 * 64],
163                                    int16_t qcoeff[6 * 64])                           int16_t qcoeff[6 * 64],
164                             int bvop,
165                             int limit)
166  {  {
167    
168          int i;          int i;
169          uint8_t cbp = 0;          uint8_t cbp = 0;
170          int sum;          int sum;
171            int code_block;
172    
173          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
174    
175                  if (pParam->m_quant_type == 0) {                  /* Quantize the block */
176                          start_timer();                          start_timer();
177                          sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant);                  if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT))
178                            sum = quant_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant);
179                    else
180                            sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant);
181                          stop_quant_timer();                          stop_quant_timer();
182    
183                    /*
184                     * We code the block if the sum is higher than the limit and if the first
185                     * two AC coefficients in zig zag order are not zero.
186                     */
187                    code_block = 0;
188                    if ((sum >= limit) || (qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) {
189                            code_block = 1;
190                  } else {                  } else {
                         start_timer();  
                         sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant);  
                         stop_quant_timer();  
                 }  
191    
192                  if (sum >= TOOSMALL_LIMIT) {    // skip block ?                          if (bvop && (pMB->mode == MODE_DIRECT || pMB->mode == MODE_DIRECT_NO4V)) {
193                          cbp |= 1 << (5 - i);                                  /* dark blocks prevention for direct mode */
194                                    if ((qcoeff[i*64] < -1) || (qcoeff[i*64] > 0))
195                                            code_block = 1;
196                            } else {
197                                    /* not direct mode */
198                                    if (qcoeff[i*64] != 0)
199                                            code_block = 1;
200                  }                  }
201          }          }
202          return cbp;  
203                    /* Set the corresponding cbp bit */
204                    cbp |= code_block << (5 - i);
205    
206  }  }
207    
208  void          return(cbp);
209    }
210    
211    /* DeQuantize all blocks -- Inter mode */
212    static __inline void
213  MBDeQuantInter( const MBParam * pParam,  MBDeQuantInter( const MBParam * pParam,
214                                  const int iQuant,                                  const int iQuant,
215                                    int16_t data[6 * 64],                                    int16_t data[6 * 64],
# Line 534  Line 219 
219          int i;          int i;
220    
221          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
222                  if (cbp & (1 << (5 - i)))                  if (cbp & (1 << (5 - i))) {
                 {  
                         if (pParam->m_quant_type == H263_QUANT) {  
223                                  start_timer();                                  start_timer();
224                            if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT))
225                                  dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant);                                  dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant);
226                                  stop_iquant_timer();                          else
                         } else {  
                                 start_timer();  
227                                  dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant);                                  dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant);
228                                  stop_iquant_timer();                                  stop_iquant_timer();
229                          }                          }
230                  }                  }
231          }          }
 }  
   
 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();  
232    
233                  }  typedef void (transfer_operation_8to16_t) (int16_t *Dst, const uint8_t *Src, int BpS);
234          }  typedef void (transfer_operation_16to8_t) (uint8_t *Dst, const int16_t *Src, int BpS);
 }  
235    
236    
237  void  static __inline void
238  MBTrans(const MBParam * pParam,  MBTrans8to16(const MBParam * const pParam,
239                                    FRAMEINFO * frame,                           const FRAMEINFO * const frame,
240                                    MACROBLOCK * pMB,                           const MACROBLOCK * const pMB,
241                                    const uint32_t x_pos,                                    const uint32_t x_pos,
242                                    const uint32_t y_pos,                                    const uint32_t y_pos,
243                                    int16_t data[6 * 64])                                    int16_t data[6 * 64])
# Line 578  Line 245 
245          uint32_t stride = pParam->edged_width;          uint32_t stride = pParam->edged_width;
246          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
247          uint32_t next_block = stride * 8;          uint32_t next_block = stride * 8;
248            int32_t cst;
249          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
250          IMAGE *pCurrent = &frame->image;          const IMAGE * const pCurrent = &frame->image;
251            transfer_operation_8to16_t *transfer_op = NULL;
252    
253            if ((frame->vop_flags & XVID_VOP_REDUCED)) {
254    
255                    /* Image pointers */
256                    pY_Cur = pCurrent->y + (y_pos << 5) * stride  + (x_pos << 5);
257                    pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4);
258                    pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4);
259    
260                    /* Block size */
261                    cst = 16;
262    
263                    /* Operation function */
264                    transfer_op = (transfer_operation_8to16_t*)filter_18x18_to_8x8;
265            } else {
266    
267                    /* Image pointers */
268          pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);
269          pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);
270          pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);
271    
272                    /* Block size */
273                    cst = 8;
274    
275                    /* Operation function */
276                    transfer_op = (transfer_operation_8to16_t*)transfer_8to16copy;
277            }
278    
279            /* Do the transfer */
280          start_timer();          start_timer();
281          transfer_8to16copy(&data[0 * 64], pY_Cur, stride);          transfer_op(&data[0 * 64], pY_Cur, stride);
282          transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride);          transfer_op(&data[1 * 64], pY_Cur + cst, stride);
283          transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride);          transfer_op(&data[2 * 64], pY_Cur + next_block, stride);
284          transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride);          transfer_op(&data[3 * 64], pY_Cur + next_block + cst, stride);
285          transfer_8to16copy(&data[4 * 64], pU_Cur, stride2);          transfer_op(&data[4 * 64], pU_Cur, stride2);
286          transfer_8to16copy(&data[5 * 64], pV_Cur, stride2);          transfer_op(&data[5 * 64], pV_Cur, stride2);
287          stop_transfer_timer();          stop_transfer_timer();
288  }  }
289    
290  void  static __inline void
291  MBTransAdd(const MBParam * pParam,  MBTrans16to8(const MBParam * const pParam,
292                                    FRAMEINFO * frame,                           const FRAMEINFO * const frame,
293                                    MACROBLOCK * pMB,                           const MACROBLOCK * const pMB,
294                                    const uint32_t x_pos,                                    const uint32_t x_pos,
295                                    const uint32_t y_pos,                                    const uint32_t y_pos,
296                                    int16_t data[6 * 64],                                    int16_t data[6 * 64],
297                             const uint32_t add,
298                                    const uint8_t cbp)                                    const uint8_t cbp)
299  {  {
300          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
301          uint32_t stride = pParam->edged_width;          uint32_t stride = pParam->edged_width;
302          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
303          uint32_t next_block = stride * 8;          uint32_t next_block = stride * 8;
304          IMAGE *pCurrent = &frame->image;          uint32_t cst;
305            const IMAGE * const pCurrent = &frame->image;
306          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);  
307    
308          if (pMB->field_dct) {          if (pMB->field_dct) {
309                  next_block = stride;                  next_block = stride;
310                  stride *= 2;                  stride *= 2;
311          }          }
312    
313          start_timer();          if ((frame->vop_flags & XVID_VOP_REDUCED)) {
314          if (cbp & 32)  
315                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  /* Image pointers */
316          if (cbp & 16)                  pY_Cur = pCurrent->y + (y_pos << 5) * stride  + (x_pos << 5);
317                  transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);                  pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4);
318          if (cbp & 8)                  pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4);
319                  transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
320          if (cbp & 4)                  /* Block size */
321                  transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride);                  cst = 16;
322          if (cbp & 2)  
323                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);                  /* Operation function */
324          if (cbp & 1)                  if(add)
325                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);                          transfer_op = (transfer_operation_16to8_t*)add_upsampled_8x8_16to8;
326                    else
327                            transfer_op = (transfer_operation_16to8_t*)copy_upsampled_8x8_16to8;
328            } else {
329    
330                    /* Image pointers */
331                    pY_Cur = pCurrent->y + (y_pos << 4) * stride  + (x_pos << 4);
332                    pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);
333                    pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);
334    
335                    /* Block size */
336                    cst = 8;
337    
338                    /* Operation function */
339                    if(add)
340                            transfer_op = (transfer_operation_16to8_t*)transfer_16to8add;
341                    else
342                            transfer_op = (transfer_operation_16to8_t*)transfer_16to8copy;
343            }
344    
345            /* Do the operation */
346            start_timer();
347            if (cbp&32) transfer_op(pY_Cur, &data[0 * 64], stride);
348            if (cbp&16) transfer_op(pY_Cur + cst, &data[1 * 64], stride);
349            if (cbp& 8) transfer_op(pY_Cur + next_block, &data[2 * 64], stride);
350            if (cbp& 4) transfer_op(pY_Cur + next_block + cst, &data[3 * 64], stride);
351            if (cbp& 2) transfer_op(pU_Cur, &data[4 * 64], stride2);
352            if (cbp& 1) transfer_op(pV_Cur, &data[5 * 64], stride2);
353          stop_transfer_timer();          stop_transfer_timer();
354  }  }
355    
356    /*****************************************************************************
357     * Module functions
358     ****************************************************************************/
359    
360    void
361    MBTransQuantIntra(const MBParam * const pParam,
362                                      const FRAMEINFO * const frame,
363                                      MACROBLOCK * const pMB,
364                                      const uint32_t x_pos,
365                                      const uint32_t y_pos,
366                                      int16_t data[6 * 64],
367                                      int16_t qcoeff[6 * 64])
368    {
369    
370            /* Transfer data */
371            MBTrans8to16(pParam, frame, pMB, x_pos, y_pos, data);
372    
373  /* permute block and return field dct choice */          /* Perform DCT (and field decision) */
374            MBfDCT(pParam, frame, pMB, x_pos, y_pos, data);
375    
376            /* Quantize the block */
377            MBQuantIntra(pParam, pMB, data, qcoeff);
378    
379  uint32_t          /* DeQuantize the block */
380  MBDecideFieldDCT(int16_t data[6 * 64])          MBDeQuantIntra(pParam, pMB->quant, data, qcoeff);
381    
382            /* Perform inverse DCT*/
383            MBiDCT(data, 0x3F);
384    
385            /* Transfer back the data -- Don't add data */
386            MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 0, 0x3F);
387    }
388    
389    
390    uint8_t
391    MBTransQuantInter(const MBParam * const pParam,
392                                      const FRAMEINFO * const frame,
393                                      MACROBLOCK * const pMB,
394                                      const uint32_t x_pos,
395                                      const uint32_t y_pos,
396                                      int16_t data[6 * 64],
397                                      int16_t qcoeff[6 * 64])
398  {  {
399          uint32_t field = MBFieldTest(data);          uint8_t cbp;
400            uint32_t limit;
401    
402          if (field) {          /*
403                  MBFrameToField(data);           * There is no MBTrans8to16 for Inter block, that's done in motion compensation
404             * already
405             */
406    
407            /* Perform DCT (and field decision) */
408            MBfDCT(pParam, frame, pMB, x_pos, y_pos, data);
409    
410            /* Set the limit threshold */
411            limit = PVOP_TOOSMALL_LIMIT + ((pMB->quant == 1)? 1 : 0);
412    
413            /* Quantize the block */
414            cbp = MBQuantInter(pParam, pMB, data, qcoeff, 0, limit);
415    
416            /* DeQuantize the block */
417            MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp);
418    
419            /* Perform inverse DCT*/
420            MBiDCT(data, cbp);
421    
422            /* Transfer back the data -- Add the data */
423            MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp);
424    
425            return(cbp);
426          }          }
427    
428          return field;  uint8_t
429    MBTransQuantInterBVOP(const MBParam * pParam,
430                                      FRAMEINFO * frame,
431                                      MACROBLOCK * pMB,
432                                      const uint32_t x_pos,
433                                      const uint32_t y_pos,
434                                      int16_t data[6 * 64],
435                                      int16_t qcoeff[6 * 64])
436    {
437            uint8_t cbp;
438            uint32_t limit;
439    
440            /*
441             * There is no MBTrans8to16 for Inter block, that's done in motion compensation
442             * already
443             */
444    
445            /* Perform DCT (and field decision) */
446            MBfDCT(pParam, frame, pMB, x_pos, y_pos, data);
447    
448            /* Set the limit threshold */
449            limit = BVOP_TOOSMALL_LIMIT;
450    
451            /* Quantize the block */
452            cbp = MBQuantInter(pParam, pMB, data, qcoeff, 1, limit);
453    
454            /*
455             * History comment:
456             * We don't have to DeQuant, iDCT and Transfer back data for B-frames.
457             *
458             * BUT some plugins require the original frame to be passed so we have
459             * to take care of that here
460             */
461            if((pParam->plugin_flags & XVID_REQORIGINAL)) {
462    
463                    /* DeQuantize the block */
464                    MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp);
465    
466                    /* Perform inverse DCT*/
467                    MBiDCT(data, cbp);
468    
469                    /* Transfer back the data -- Add the data */
470                    MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp);
471  }  }
472    
473            return(cbp);
474    }
475    
476  /* 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 */
   
477  uint32_t  uint32_t
478  MBFieldTest_c(int16_t data[6 * 64])  MBFieldTest_c(int16_t data[6 * 64])
479  {  {
# Line 668  Line 487 
487          for (i = 0; i < 7; ++i) {          for (i = 0; i < 7; ++i) {
488                  for (j = 0; j < 8; ++j) {                  for (j = 0; j < 8; ++j) {
489                          frame +=                          frame +=
490                                  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]);
491                          frame +=                          frame +=
492                                  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]);
493                          frame +=                          frame +=
494                                  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]);
495                          frame +=                          frame +=
496                                  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]);
497    
498                          field +=                          field +=
499                                  ABS(data[blocks[i + 1] + lines[i + 1] + j] -                                  abs(data[blocks[i + 1] + lines[i + 1] + j] -
500                                          data[blocks[i] + lines[i] + j]);                                          data[blocks[i] + lines[i] + j]);
501                          field +=                          field +=
502                                  ABS(data[blocks[i + 1] + lines[i + 1] + 8 + j] -                                  abs(data[blocks[i + 1] + lines[i + 1] + 8 + j] -
503                                          data[blocks[i] + lines[i] + 8 + j]);                                          data[blocks[i] + lines[i] + 8 + j]);
504                          field +=                          field +=
505                                  ABS(data[blocks[i + 1] + 64 + lines[i + 1] + j] -                                  abs(data[blocks[i + 1] + 64 + lines[i + 1] + j] -
506                                          data[blocks[i] + 64 + lines[i] + j]);                                          data[blocks[i] + 64 + lines[i] + j]);
507                          field +=                          field +=
508                                  ABS(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] -                                  abs(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] -
509                                          data[blocks[i] + 64 + lines[i] + 8 + j]);                                          data[blocks[i] + 64 + lines[i] + 8 + j]);
510                  }                  }
511          }          }

Legend:
Removed from v.1.21  
changed lines
  Added in v.1.21.2.8

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