[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.14, Thu Sep 26 04:52:16 2002 UTC revision 1.21.2.15, Thu Jul 24 13:09:14 2003 UTC
# Line 1  Line 1 
1  /*****************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - MacroBlock transfer and quantization -   *  - MB Transfert/Quantization functions -
5   *   *
6   *  Copyright(C) 2002-2001 Michael Militzer <isibaar@xvid.org>   *  Copyright(C) 2001-2003  Peter Ross <pross@xvid.org>
7   *               2002-2001 Peter Ross <pross@xvid.org>   *               2001-2003  Michael Militzer <isibaar@xvid.org>
8   *   *               2003       Edouard Gomez <ed.gomez@free.fr>
  *  This program is an implementation of a part of one or more MPEG-4  
  *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending  
  *  to use this software module in hardware or software products are  
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
9   *   *
10   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
11   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 33  Line 25 
25   *   *
26   ****************************************************************************/   ****************************************************************************/
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 41  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"
43  #include "../quant/quant_h263.h"  #include "../quant/quant_h263.h"
44  #include "../encoder.h"  #include "../encoder.h"
45    
46  #define MIN(X, Y) ((X)<(Y)?(X):(Y))  #include "../image/reduced.h"
 #define MAX(X, Y) ((X)>(Y)?(X):(Y))  
47    
48  #define TOOSMALL_LIMIT 3                /* skip blocks having a coefficient sum below this value */  MBFIELDTEST_PTR MBFieldTest;
49    
50  /* this isnt pretty, but its better than 20 ifdefs */  /*
51     * Skip blocks having a coefficient sum below this value. This value will be
52     * corrected according to the MB quantizer to avoid artifacts for quant==1
53     */
54    #define PVOP_TOOSMALL_LIMIT 1
55    #define BVOP_TOOSMALL_LIMIT 3
56    
57  void  /*****************************************************************************
58  MBTransQuantIntra(const MBParam * pParam,   * Local functions
59                                    FRAMEINFO * frame,   ****************************************************************************/
                                   MACROBLOCK * pMB,  
                                   const uint32_t x_pos,  
                                   const uint32_t y_pos,  
                                   int16_t data[6 * 64],  
                                   int16_t qcoeff[6 * 64])  
 {  
60    
61          uint32_t stride = pParam->edged_width;  /* permute block and return field dct choice */
62          uint32_t stride2 = stride / 2;  static __inline uint32_t
63          uint32_t next_block = stride * 8;  MBDecideFieldDCT(int16_t data[6 * 64])
64          uint32_t i;  {
65          uint32_t iQuant = frame->quant;          uint32_t field = MBFieldTest(data);
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
         IMAGE *pCurrent = &frame->image;  
66    
67          pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);          if (field)
68          pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);                  MBFrameToField(data);
         pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);  
69    
70          start_timer();          return field;
71          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();  
72    
73    /* Performs Forward DCT on all blocks */
74    static __inline void
75    MBfDCT(const MBParam * const pParam,
76               const FRAMEINFO * const frame,
77               MACROBLOCK * const pMB,
78               uint32_t x_pos,
79               uint32_t y_pos,
80               int16_t data[6 * 64])
81    {
82            /* 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) &&                  (x_pos>0) && (x_pos<pParam->mb_width-1) &&
87                  (y_pos>0) && (y_pos<pParam->mb_height-1)) {                  (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 */
                 uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);  
   
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                  if (pParam->m_quant_type == H263_QUANT) {  /* Performs Inverse DCT on all blocks */
104    static __inline void
105    MBiDCT(int16_t data[6 * 64],
106               const uint8_t cbp)
107    {
108                          start_timer();                          start_timer();
109                          quant_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            if(cbp & (1 << (5 - 3))) idct(&data[3 * 64]);
113            if(cbp & (1 << (5 - 4))) idct(&data[4 * 64]);
114            if(cbp & (1 << (5 - 5))) idct(&data[5 * 64]);
115            stop_idct_timer();
116    }
117    
118    /* Quantize all blocks -- Intra mode */
119    static __inline void
120    MBQuantIntra(const MBParam * pParam,
121                             const FRAMEINFO * const frame,
122                             const MACROBLOCK * pMB,
123                             int16_t qcoeff[6 * 64],
124                             int16_t data[6*64])
125    {
126            int i;
127    
128            for (i = 0; i < 6; i++) {
129                    uint32_t iDcScaler = get_dc_scaler(pMB->quant, i < 4);
130    
131                    /* Quantize the block */
132                          start_timer();                          start_timer();
133                          dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);                  if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) {
134                          stop_iquant_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);
137                          quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);                  }
138                          stop_quant_timer();                          stop_quant_timer();
   
                         start_timer();  
                         dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);  
                         stop_iquant_timer();  
139                  }                  }
   
                 start_timer();  
                 idct(&data[i * 64]);  
                 stop_idct_timer();  
140          }          }
141    
142          if (pMB->field_dct) {  /* DeQuantize all blocks -- Intra mode */
143                  next_block = stride;  static __inline void
144                  stride *= 2;  MBDeQuantIntra(const MBParam * pParam,
145          }                             const int iQuant,
146                               int16_t qcoeff[6 * 64],
147                               int16_t data[6*64])
148    {
149            int i;
150    
151          start_timer();          for (i = 0; i < 6; i++) {
152          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
         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();  
153    
154                    start_timer();
155                    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();
160            }
161  }  }
162    
163    
164  uint8_t  static int
165  MBTransQuantInter(const MBParam * pParam,  dct_quantize_trellis_h263_c(int16_t *const Out,
166                                    FRAMEINFO * frame,                                                          const int16_t *const In,
167                                    MACROBLOCK * pMB,                                                          int Q,
168                                    const uint32_t x_pos,                                                          const uint16_t * const Zigzag,
169                                    const uint32_t y_pos,                                                          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,
183                             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          uint32_t stride = pParam->edged_width;          int i;
         uint32_t stride2 = stride / 2;  
         uint32_t next_block = stride * 8;  
         uint32_t i;  
         uint32_t iQuant = frame->quant;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
192          uint8_t cbp = 0;          uint8_t cbp = 0;
193          uint32_t sum;          int sum;
194          IMAGE *pCurrent = &frame->image;          int code_block;
195    
196          pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);          for (i = 0; i < 6; i++) {
         pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);  
         pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);  
197    
198                    /* Quantize the block */
199          start_timer();          start_timer();
200          pMB->field_dct = 0;                  if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) {
201          if ((frame->global_flags & XVID_INTERLACING) &&                          sum = quant_inter(&qcoeff[i*64], &data[i*64], pMB->quant);
202                  (x_pos>0) && (x_pos<pParam->mb_width-1) &&                          if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) {
203                  (y_pos>0) && (y_pos<pParam->mb_height-1)) {                                  sum = dct_quantize_trellis_h263_c(&qcoeff[i*64], &data[i*64], pMB->quant, &scan_tables[0][0], 63)+1;
204                  pMB->field_dct = MBDecideFieldDCT(data);  /*                              limit = 1; // Isibaar: why? deactivated so far - so please complain! ;-) */
205          }          }
206          stop_interlacing_timer();                  } 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    
         for (i = 0; i < 6; i++) {  
215                  /*                  /*
216                   *  no need to transfer 8->16-bit                   * We code the block if the sum is higher than the limit and if the first
217                   * (this is performed already in motion compensation)                   * two AC coefficients in zig zag order are not zero.
218                   */                   */
219                  start_timer();                  code_block = 0;
220                  fdct(&data[i * 64]);                  if ((sum >= limit) || (qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) {
221                  stop_dct_timer();                          code_block = 1;
222                    } else {
223    
224                  if (pParam->m_quant_type == 0) {                          if (bvop && (pMB->mode == MODE_DIRECT || pMB->mode == MODE_DIRECT_NO4V)) {
225                          start_timer();                                  /* dark blocks prevention for direct mode */
226                          sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant);                                  if ((qcoeff[i*64] < -1) || (qcoeff[i*64] > 0))
227                          stop_quant_timer();                                          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                            }
233                    }
234    
235                    /* Set the corresponding cbp bit */
236                    cbp |= code_block << (5 - i);
237            }
238    
239            return(cbp);
240                  }                  }
241    
242                  if ((sum >= TOOSMALL_LIMIT) || (qcoeff[i*64] != 0) ||  /* DeQuantize all blocks -- Inter mode */
243                          (qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) {  static __inline void
244    MBDeQuantInter(const MBParam * pParam,
245                               const int iQuant,
246                               int16_t data[6 * 64],
247                               int16_t qcoeff[6 * 64],
248                               const uint8_t cbp)
249    {
250            int i;
251    
252                          if (pParam->m_quant_type == H263_QUANT) {          for (i = 0; i < 6; i++) {
253                    if (cbp & (1 << (5 - i))) {
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    }
263    
264                          cbp |= 1 << (5 - i);  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                          start_timer();  
268                          idct(&data[i * 64]);  static __inline void
269                          stop_idct_timer();  MBTrans8to16(const MBParam * const pParam,
270                             const FRAMEINFO * const frame,
271                             const MACROBLOCK * const pMB,
272                             const uint32_t x_pos,
273                             const uint32_t y_pos,
274                             int16_t data[6 * 64])
275    {
276            uint32_t stride = pParam->edged_width;
277            uint32_t stride2 = stride / 2;
278            uint32_t next_block = stride * 8;
279            int32_t cst;
280            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
281            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);
300                    pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);
301                    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();
312            transfer_op(&data[0 * 64], pY_Cur, stride);
313            transfer_op(&data[1 * 64], pY_Cur + cst, stride);
314            transfer_op(&data[2 * 64], pY_Cur + next_block, stride);
315            transfer_op(&data[3 * 64], pY_Cur + next_block + cst, stride);
316            transfer_op(&data[4 * 64], pU_Cur, stride2);
317            transfer_op(&data[5 * 64], pV_Cur, stride2);
318            stop_transfer_timer();
319          }          }
320    
321    static __inline void
322    MBTrans16to8(const MBParam * const pParam,
323                             const FRAMEINFO * const frame,
324                             const MACROBLOCK * const pMB,
325                             const uint32_t x_pos,
326                             const uint32_t y_pos,
327                             int16_t data[6 * 64],
328                             const uint32_t add,
329                             const uint8_t cbp)
330    {
331            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
332            uint32_t stride = pParam->edged_width;
333            uint32_t stride2 = stride / 2;
334            uint32_t next_block = stride * 8;
335            uint32_t cst;
336            const IMAGE * const pCurrent = &frame->image;
337            transfer_operation_16to8_t *transfer_op = NULL;
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          stop_transfer_timer();                  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          return cbp;                  /* 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();
385  }  }
386    
387    /*****************************************************************************
388     * Module functions
389     ****************************************************************************/
390    
391  void  void
392  MBTransQuantIntra2(const MBParam * pParam,  MBTransQuantIntra(const MBParam * const pParam,
393                                    FRAMEINFO * frame,                                    const FRAMEINFO * const frame,
394                                    MACROBLOCK * pMB,                                    MACROBLOCK * const pMB,
395                                    const uint32_t x_pos,                                    const uint32_t x_pos,
396                                    const uint32_t y_pos,                                    const uint32_t y_pos,
397                                    int16_t data[6 * 64],                                    int16_t data[6 * 64],
398                                    int16_t qcoeff[6 * 64])                                    int16_t qcoeff[6 * 64])
399  {  {
400          MBTrans(pParam,frame,pMB,x_pos,y_pos,data);  
401          MBfDCT(pParam,frame,pMB,data);          /* Transfer data */
402            MBTrans8to16(pParam, frame, pMB, x_pos, y_pos, data);
403    
404            /* 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);          MBQuantIntra(pParam,frame,pMB,data,qcoeff);
409          MBDeQuantIntra(pParam,frame->quant,data,qcoeff);  
410            /* DeQuantize the block */
411            MBDeQuantIntra(pParam, pMB->quant, data, qcoeff);
412    
413            /* Perform inverse DCT*/
414          MBiDCT(data,0x3F);          MBiDCT(data,0x3F);
415          MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,0x3F);  
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  uint8_t
422  MBTransQuantInter2(const MBParam * pParam,  MBTransQuantInter(const MBParam * const pParam,
423                                    FRAMEINFO * frame,                                    const FRAMEINFO * const frame,
424                                    MACROBLOCK * pMB,                                    MACROBLOCK * const pMB,
425                                    const uint32_t x_pos,                                    const uint32_t x_pos,
426                                    const uint32_t y_pos,                                    const uint32_t y_pos,
427                                    int16_t data[6 * 64],                                    int16_t data[6 * 64],
428                                    int16_t qcoeff[6 * 64])                                    int16_t qcoeff[6 * 64])
429  {  {
430          uint8_t cbp;          uint8_t cbp;
431            uint32_t limit;
432    
433            /*
434             * 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  /* there is no MBTrans for Inter block, that's done in motion compensation already */          /* Quantize the block */
448            cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 0, limit);
449    
450          MBfDCT(pParam,frame,pMB,data);          /* DeQuantize the block */
451          cbp = MBQuantInter(pParam,frame->quant,data,qcoeff);          MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp);
452          MBDeQuantInter(pParam,frame->quant,data,qcoeff,cbp);  
453            /* Perform inverse DCT*/
454          MBiDCT(data,cbp);          MBiDCT(data,cbp);
         MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,cbp);  
455    
456          return cbp;          /* 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  uint8_t  uint8_t
463  MBTransQuantInterBVOP(const MBParam * pParam,  MBTransQuantInterBVOP(const MBParam * pParam,
464                                    FRAMEINFO * frame,                                    FRAMEINFO * frame,
465                                    MACROBLOCK * pMB,                                    MACROBLOCK * pMB,
466                                      const uint32_t x_pos,
467                                      const uint32_t y_pos,
468                                    int16_t data[6 * 64],                                    int16_t data[6 * 64],
469                                    int16_t qcoeff[6 * 64])                                    int16_t qcoeff[6 * 64])
470  {  {
471          uint8_t cbp;          uint8_t cbp;
472            uint32_t limit;
473    
474  /* there is no MBTrans for Inter block, that's done in motion compensation already */          /*
475             * There is no MBTrans8to16 for Inter block, that's done in motion compensation
476             * already
477             */
478    
479          MBfDCT(pParam,frame,pMB,data);          /* Perform DCT (and field decision) */
480          cbp = MBQuantInter(pParam,frame->quant,data,qcoeff);          MBfDCT(pParam, frame, pMB, x_pos, y_pos, data);
481    
482  /* we don't have to DeQuant, iDCT and Transfer back data for B-frames */          /* Set the limit threshold */
483            limit = BVOP_TOOSMALL_LIMIT;
484    
485          return cbp;          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  void          /*
492  MBfDCT(const MBParam * pParam,           * History comment:
493                                    FRAMEINFO * frame,           * We don't have to DeQuant, iDCT and Transfer back data for B-frames.
494                                    MACROBLOCK * pMB,           *
495                                    int16_t data[6 * 64])           * BUT some plugins require the original frame to be passed so we have
496  {           * to take care of that here
497          int i;           */
498            if((pParam->plugin_flags & XVID_REQORIGINAL)) {
499    
500          start_timer();                  /* DeQuantize the block */
501          pMB->field_dct = 0;                  MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp);
         if ((frame->global_flags & XVID_INTERLACING)) {  
                 pMB->field_dct = MBDecideFieldDCT(data);  
         }  
         stop_interlacing_timer();  
502    
503          for (i = 0; i < 6; i++) {                  /* Perform inverse DCT*/
504                  start_timer();                  MBiDCT(data, cbp);
505                  fdct(&data[i * 64]);  
506                  stop_dct_timer();                  /* 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  void  /* if sum(diff between field lines) < sum(diff between frame lines), use field dct */
514  MBQuantDeQuantIntra(const MBParam * pParam,  uint32_t
515                                          FRAMEINFO * frame,  MBFieldTest_c(int16_t data[6 * 64])
                                         MACROBLOCK * pMB,  
                                         int16_t qcoeff[6 * 64],  
                                         int16_t data[6*64])  
516  {  {
517          int i;          const uint8_t blocks[] =
518          int iQuant = frame->quant;                  { 0 * 64, 0 * 64, 0 * 64, 0 * 64, 2 * 64, 2 * 64, 2 * 64, 2 * 64 };
519            const uint8_t lines[] = { 0, 16, 32, 48, 0, 16, 32, 48 };
520    
521          start_timer();          int frame = 0, field = 0;
522          pMB->field_dct = 0;          int i, j;
523          if ((frame->global_flags & XVID_INTERLACING)) {  
524                  pMB->field_dct = MBDecideFieldDCT(data);          for (i = 0; i < 7; ++i) {
525                    for (j = 0; j < 8; ++j) {
526                            frame +=
527                                    abs(data[0 * 64 + (i + 1) * 8 + j] - data[0 * 64 + i * 8 + j]);
528                            frame +=
529                                    abs(data[1 * 64 + (i + 1) * 8 + j] - data[1 * 64 + i * 8 + j]);
530                            frame +=
531                                    abs(data[2 * 64 + (i + 1) * 8 + j] - data[2 * 64 + i * 8 + j]);
532                            frame +=
533                                    abs(data[3 * 64 + (i + 1) * 8 + j] - data[3 * 64 + i * 8 + j]);
534    
535                            field +=
536                                    abs(data[blocks[i + 1] + lines[i + 1] + j] -
537                                            data[blocks[i] + lines[i] + j]);
538                            field +=
539                                    abs(data[blocks[i + 1] + lines[i + 1] + 8 + j] -
540                                            data[blocks[i] + lines[i] + 8 + j]);
541                            field +=
542                                    abs(data[blocks[i + 1] + 64 + lines[i + 1] + j] -
543                                            data[blocks[i] + 64 + lines[i] + j]);
544                            field +=
545                                    abs(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] -
546                                            data[blocks[i] + 64 + lines[i] + 8 + j]);
547                    }
548          }          }
         stop_interlacing_timer();  
549    
550          for (i = 0; i < 6; i++) {          return (frame >= (field + 350));
551                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);  }
552    
                 if (pParam->m_quant_type == H263_QUANT) {  
                         start_timer();  
                         quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);  
                         stop_quant_timer();  
553    
554                          start_timer();  /* deinterlace Y blocks vertically */
                         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();  
555    
556                          start_timer();  #define MOVLINE(X,Y) memcpy(X, Y, sizeof(tmp))
557                          dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);  #define LINE(X,Y)       &data[X*64 + Y*8]
                         stop_iquant_timer();  
                 }  
         }  
 }  
558    
559  void  void
560  MBQuantIntra(const MBParam * pParam,  MBFrameToField(int16_t data[6 * 64])
                          FRAMEINFO * frame,  
                          MACROBLOCK *pMB,  
                      int16_t qcoeff[6 * 64],  
                          int16_t data[6*64])  
561  {  {
562          int i;          int16_t tmp[8];
         int iQuant = frame->quant;  
563    
564          start_timer();          /* left blocks */
         pMB->field_dct = 0;  
         if ((frame->global_flags & XVID_INTERLACING)) {  
                 pMB->field_dct = MBDecideFieldDCT(data);  
         }  
         stop_interlacing_timer();  
565    
566          for (i = 0; i < 6; i++) {          /* 1=2, 2=4, 4=8, 8=1 */
567                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);          MOVLINE(tmp, LINE(0, 1));
568            MOVLINE(LINE(0, 1), LINE(0, 2));
569            MOVLINE(LINE(0, 2), LINE(0, 4));
570            MOVLINE(LINE(0, 4), LINE(2, 0));
571            MOVLINE(LINE(2, 0), tmp);
572    
573                  if (pParam->m_quant_type == H263_QUANT) {          /* 3=6, 6=12, 12=9, 9=3 */
574                          start_timer();          MOVLINE(tmp, LINE(0, 3));
575                          quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);          MOVLINE(LINE(0, 3), LINE(0, 6));
576                          stop_quant_timer();          MOVLINE(LINE(0, 6), LINE(2, 4));
577                  } else {          MOVLINE(LINE(2, 4), LINE(2, 1));
578                          start_timer();          MOVLINE(LINE(2, 1), tmp);
579                          quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);  
580                          stop_quant_timer();          /* 5=10, 10=5 */
581            MOVLINE(tmp, LINE(0, 5));
582            MOVLINE(LINE(0, 5), LINE(2, 2));
583            MOVLINE(LINE(2, 2), tmp);
584    
585            /* 7=14, 14=13, 13=11, 11=7 */
586            MOVLINE(tmp, LINE(0, 7));
587            MOVLINE(LINE(0, 7), LINE(2, 6));
588            MOVLINE(LINE(2, 6), LINE(2, 5));
589            MOVLINE(LINE(2, 5), LINE(2, 3));
590            MOVLINE(LINE(2, 3), tmp);
591    
592            /* right blocks */
593    
594            /* 1=2, 2=4, 4=8, 8=1 */
595            MOVLINE(tmp, LINE(1, 1));
596            MOVLINE(LINE(1, 1), LINE(1, 2));
597            MOVLINE(LINE(1, 2), LINE(1, 4));
598            MOVLINE(LINE(1, 4), LINE(3, 0));
599            MOVLINE(LINE(3, 0), tmp);
600    
601            /* 3=6, 6=12, 12=9, 9=3 */
602            MOVLINE(tmp, LINE(1, 3));
603            MOVLINE(LINE(1, 3), LINE(1, 6));
604            MOVLINE(LINE(1, 6), LINE(3, 4));
605            MOVLINE(LINE(3, 4), LINE(3, 1));
606            MOVLINE(LINE(3, 1), tmp);
607    
608            /* 5=10, 10=5 */
609            MOVLINE(tmp, LINE(1, 5));
610            MOVLINE(LINE(1, 5), LINE(3, 2));
611            MOVLINE(LINE(3, 2), tmp);
612    
613            /* 7=14, 14=13, 13=11, 11=7 */
614            MOVLINE(tmp, LINE(1, 7));
615            MOVLINE(LINE(1, 7), LINE(3, 6));
616            MOVLINE(LINE(3, 6), LINE(3, 5));
617            MOVLINE(LINE(3, 5), LINE(3, 3));
618            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  void  /* this routine has been strippen of all debug code */
780  MBDeQuantIntra(const MBParam * pParam,  
781                             const int iQuant,  static int
782                                    int16_t qcoeff[6 * 64],  dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero)
                                   int16_t data[6*64])  
783  {  {
         int i;  
784    
785          for (i = 0; i < 6; i++) {      /*
786                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);           * 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                  if (pParam->m_quant_type == H263_QUANT) {    NODE Nodes[65], Last;
794                          start_timer();    uint32_t Run_Costs0[64+1];
795                          dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);    uint32_t * const Run_Costs = Run_Costs0 + 1;
796                          stop_iquant_timer();    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 {                  } else {
832                          start_timer();          Nodes[i].Level = 1;
833                          dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);          dQ = Lev0 - AC;
                         stop_iquant_timer();  
                 }  
         }  
834  }  }
835                    Cost0 = Lambda*dQ*dQ;
836    
837  uint8_t        Nodes[i].Run = 1;
838  MBQuantInter(const MBParam * pParam,        Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0;
839                           const int iQuant,        for(Run=i-Run_Start; Run>0; --Run)
                                   int16_t data[6 * 64],  
                                   int16_t qcoeff[6 * 64])  
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          int i;            /*
846          uint8_t cbp = 0;                     * TODO: what about tie-breaks? Should we favor short runs or
847          int sum;                     * 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          for (i = 0; i < 6; i++) {                          /* (I'd say: favour short runs => hifreq errors (HVS) -- gruel ) */
852    
853                  if (pParam->m_quant_type == 0) {          if (Cost<Best_Cost) {
854                          start_timer();            Best_Cost    = Cost;
855                          sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant);            Nodes[i].Run = Run;
                         stop_quant_timer();  
                 } else {  
                         start_timer();  
                         sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant);  
                         stop_quant_timer();  
856                  }                  }
857    
858                  if (sum >= TOOSMALL_LIMIT) {    // skip block ?          if (lCost<Last_Cost) {
859                          cbp |= 1 << (5 - i);            Last_Cost  = lCost;
860              Last.Run   = Run;
861              Last_Node  = i;
862                  }                  }
863          }          }
864          return cbp;        if (Last_Node==i)
865                            Last.Level = Nodes[i].Level;
866  }  }
867        else                      /* "big" levels */
 void  
 MBDeQuantInter( const MBParam * pParam,  
                                 const int iQuant,  
                                   int16_t data[6 * 64],  
                                   int16_t qcoeff[6 * 64],  
                                   const uint8_t cbp)  
868  {  {
869          int i;        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 (i = 0; i < 6; i++) {        for(Run=i-Run_Start; Run>0; --Run)
                 if (cbp & (1 << (5 - i)))  
898                  {                  {
899                          if (pParam->m_quant_type == H263_QUANT) {          const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run];
900                                  start_timer();          uint32_t Cost1, Cost2;
901                                  dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant);          int bLevel;
902                                  stop_iquant_timer();  
903                          } else {  /*
904                                  start_timer();   * for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following:
905                                  dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant);   *      if (Cost_Base>=Best_Cost) continue;
906                                  stop_iquant_timer();   * (? 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  void      Run_Costs[i] = Best_Cost;
 MBiDCT( int16_t data[6 * 64],  
                 const uint8_t cbp)  
 {  
         int i;  
944    
945          for (i = 0; i < 6; i++) {      if (Best_Cost < Min_Cost + Dist0) {
946                  if (cbp & (1 << (5 - i)))        Min_Cost = Best_Cost;
947          Run_Start = i;
948        }
949        else
950                  {                  {
951                          start_timer();          /*
952                          idct(&data[i * 64]);                   * as noticed by Michael Niedermayer (michaelni at gmx.at), there's
953                          stop_idct_timer();                   * 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    
 void  
 MBTrans(const MBParam * pParam,  
                                   FRAMEINFO * frame,  
                                   MACROBLOCK * pMB,  
                                   const uint32_t x_pos,  
                                   const uint32_t y_pos,  
                                   int16_t data[6 * 64])  
 {  
         uint32_t stride = pParam->edged_width;  
         uint32_t stride2 = stride / 2;  
         uint32_t next_block = stride * 8;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
         IMAGE *pCurrent = &frame->image;  
981    
         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);  
982    
         start_timer();  
         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();  
 }  
983    
 void  
 MBTransAdd(const MBParam * pParam,  
                                   FRAMEINFO * frame,  
                                   MACROBLOCK * pMB,  
                                   const uint32_t x_pos,  
                                   const uint32_t y_pos,  
                                   int16_t data[6 * 64],  
                                   const uint8_t cbp)  
 {  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
         uint32_t stride = pParam->edged_width;  
         uint32_t stride2 = stride / 2;  
         uint32_t next_block = stride * 8;  
         IMAGE *pCurrent = &frame->image;  
984    
         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);  
985    
         if (pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
986    
         start_timer();  
         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();  
 }  
987    
988    
989    
990  /* if sum(diff between field lines) < sum(diff between frame lines), use field dct */  /* original version including heavy debugging info */
991    
992    #ifdef DBGTRELL
993    
994  uint32_t  #define DBG 0
995  MBDecideFieldDCT(int16_t data[6 * 64])  
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          const uint8_t blocks[] =    while(Last>=0 && C[Zigzag[Last]]==0)
1008                  { 0 * 64, 0 * 64, 0 * 64, 0 * 64, 2 * 64, 2 * 64, 2 * 64, 2 * 64 };          Last--;
1009          const uint8_t lines[] = { 0, 16, 32, 48, 0, 16, 32, 48 };  
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    
         int frame = 0, field = 0;  
1084          int i, j;          int i, j;
1085    
1086          for (i = 0; i < 7; ++i) {  #if (DBG>0)
1087                  for (j = 0; j < 8; ++j) {    Last.Level = 0; Last.Run = -1; /* just initialize to smthg */
1088                          frame +=  #endif
1089                                  ABS(data[0 * 64 + (i + 1) * 8 + j] - data[0 * 64 + i * 8 + j]);  
1090                          frame +=    Non_Zero = Find_Last(Out, Zigzag, Non_Zero);
1091                                  ABS(data[1 * 64 + (i + 1) * 8 + j] - data[1 * 64 + i * 8 + j]);    if (Non_Zero<0)
1092                          frame +=        return -1;
                                 ABS(data[2 * 64 + (i + 1) * 8 + j] - data[2 * 64 + i * 8 + j]);  
                         frame +=  
                                 ABS(data[3 * 64 + (i + 1) * 8 + j] - data[3 * 64 + i * 8 + j]);  
1093    
1094                          field +=    for(i=0; i<=Non_Zero; i++)
1095                                  ABS(data[blocks[i + 1] + lines[i + 1] + j] -    {
1096                                          data[blocks[i] + lines[i] + j]);      const int AC = In[Zigzag[i]];
1097                          field +=      const int Level1 = Out[Zigzag[i]];
1098                                  ABS(data[blocks[i + 1] + lines[i + 1] + 8 + j] -      const int Dist0 = Lambda* AC*AC;
1099                                          data[blocks[i] + lines[i] + 8 + j]);      uint32_t Best_Cost = 0xf0000000;
1100                          field +=      Last_Cost += Dist0;
1101                                  ABS(data[blocks[i + 1] + 64 + lines[i + 1] + j] -  
1102                                          data[blocks[i] + 64 + lines[i] + j]);      if ((uint32_t)(Level1+1)<3)                 /* very specialized loop for -1,0,+1 */
1103                          field +=      {
1104                                  ABS(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] -          int dQ;
1105                                          data[blocks[i] + 64 + lines[i] + 8 + j]);                  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 (frame > field) {          if (lCost<Last_Cost) {
1136                  MBFrameToField(data);            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          return (frame > (field + 350));        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  /* deinterlace Y blocks vertically */  /*
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  #define MOVLINE(X,Y) memcpy(X, Y, sizeof(tmp))          if (Cost2<Cost1) {
1203  #define LINE(X,Y)    &data[X*64 + Y*8]                           Cost1 = Cost2;
1204                             bLevel = Level2;
1205                      } else
1206                             bLevel = Level1;
1207    
1208  void          if (Cost1<Best_Cost) {
1209  MBFrameToField(int16_t data[6 * 64])            Best_Cost = Cost1;
1210  {            Nodes[i].Run   = Run;
1211          int16_t tmp[8];            Nodes[i].Level = bLevel;
1212            }
1213    
1214          /* left blocks */          Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16);
1215            Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21;
1216    
1217          // 1=2, 2=4, 4=8, 8=1          if (Cost2<Cost1) {
1218          MOVLINE(tmp, LINE(0, 1));                           Cost1 = Cost2;
1219          MOVLINE(LINE(0, 1), LINE(0, 2));                           bLevel = Level2;
1220          MOVLINE(LINE(0, 2), LINE(0, 4));                    } else
1221          MOVLINE(LINE(0, 4), LINE(2, 0));                           bLevel = Level1;
         MOVLINE(LINE(2, 0), tmp);  
1222    
1223          // 3=6, 6=12, 12=9, 9=3          if (Cost1<Last_Cost) {
1224          MOVLINE(tmp, LINE(0, 3));            Last_Cost  = Cost1;
1225          MOVLINE(LINE(0, 3), LINE(0, 6));            Last.Run   = Run;
1226          MOVLINE(LINE(0, 6), LINE(2, 4));            Last.Level = bLevel;
1227          MOVLINE(LINE(2, 4), LINE(2, 1));            Last_Node  = i;
1228          MOVLINE(LINE(2, 1), tmp);          }
1229          } /* end of "for Run" */
1230    
1231          // 5=10, 10=5        if (DBG==1) {
1232          MOVLINE(tmp, LINE(0, 5));          Run_Costs[i] = Best_Cost;
1233          MOVLINE(LINE(0, 5), LINE(2, 2));          printf( "Costs #%2d: ", i);
1234          MOVLINE(LINE(2, 2), tmp);          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          // 7=14, 14=13, 13=11, 11=7      Run_Costs[i] = Best_Cost;
         MOVLINE(tmp, LINE(0, 7));  
         MOVLINE(LINE(0, 7), LINE(2, 6));  
         MOVLINE(LINE(2, 6), LINE(2, 5));  
         MOVLINE(LINE(2, 5), LINE(2, 3));  
         MOVLINE(LINE(2, 3), tmp);  
1248    
1249          /* right blocks */      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          // 1=2, 2=4, 4=8, 8=1        while( Run_Costs[Run_Start]>Min_Cost+(1<<16) )
1262          MOVLINE(tmp, LINE(1, 1));          Run_Start++;
         MOVLINE(LINE(1, 1), LINE(1, 2));  
         MOVLINE(LINE(1, 2), LINE(1, 4));  
         MOVLINE(LINE(1, 4), LINE(3, 0));  
         MOVLINE(LINE(3, 0), tmp);  
1263    
1264          // 3=6, 6=12, 12=9, 9=3          /* spread on preceding coeffs the cost incurred by skipping this one */
1265          MOVLINE(tmp, LINE(1, 3));        for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0;
1266          MOVLINE(LINE(1, 3), LINE(1, 6));        Min_Cost += Dist0;
1267          MOVLINE(LINE(1, 6), LINE(3, 4));      }
1268          MOVLINE(LINE(3, 4), LINE(3, 1));    }
         MOVLINE(LINE(3, 1), tmp);  
1269    
1270          // 5=10, 10=5    if (DBG) {
1271          MOVLINE(tmp, LINE(1, 5));      Last_Cost = Evaluate_Cost(Out,Mult,Bias, Zigzag,Non_Zero, Lambda);
1272          MOVLINE(LINE(1, 5), LINE(3, 2));      if (DBG==1) {
1273          MOVLINE(LINE(3, 2), tmp);        printf( "=> " );
1274          for(i=0; i<=Non_Zero; ++i) printf( "[%3.0d] ", Out[Zigzag[i]] );
1275          printf( "\n" );
1276       }
1277      }
1278    
1279          // 7=14, 14=13, 13=11, 11=7    if (Last_Node<0)
1280          MOVLINE(tmp, LINE(1, 7));      return -1;
1281          MOVLINE(LINE(1, 7), LINE(3, 6));  
1282          MOVLINE(LINE(3, 6), LINE(3, 5));         /* reconstruct optimal sequence backward with surviving paths */
1283          MOVLINE(LINE(3, 5), LINE(3, 3));    memset(Out, 0x00, 64*sizeof(*Out));
1284          MOVLINE(LINE(3, 3), tmp);    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.14  
changed lines
  Added in v.1.21.2.15

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