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

Diff of /xvidcore/src/decoder.c

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

revision 1.6, Thu Mar 28 12:24:41 2002 UTC revision 1.32, Fri Jul 19 11:15:21 2002 UTC
# Line 1  Line 1 
1  /**************************************************************************  /**************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC   *      XVID MPEG-4 VIDEO CODEC
4   *      decoder main   *  -  Decoder main module  -
5   *   *
6   *      This program is an implementation of a part of one or more MPEG-4   *      This program is an implementation of a part of one or more MPEG-4
7   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
# Line 12  Line 12 
12   *      editors and their companies, will have no liability for use of this   *      editors and their companies, will have no liability for use of this
13   *      software or modifications or derivatives thereof.   *      software or modifications or derivatives thereof.
14   *   *
15   *      This program is xvid_free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
16   *      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
17   *      the xvid_free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation; either version 2 of the License, or
18   *      (at your option) any later version.   *      (at your option) any later version.
19   *   *
20   *      This program is distributed in the hope that it will be useful,   *      This program is distributed in the hope that it will be useful,
# Line 23  Line 23 
23   *      GNU General Public License for more details.   *      GNU General Public License for more details.
24   *   *
25   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
26   *      along with this program; if not, write to the xvid_free Software   *  along with this program; if not, write to the Free Software
27   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28   *   *
29   *************************************************************************/   *************************************************************************/
30    
# Line 32  Line 32 
32   *   *
33   *      History:   *      History:
34   *   *
35     *  15.07.2002  fix a bug in B-frame decode at DIRECT mode
36     *              MinChen <chenm001@163.com>
37     *  10.07.2002  added BFRAMES_DEC_DEBUG support
38     *              Fix a little bug for low_delay flage
39     *              MinChen <chenm001@163.com>
40     *  28.06.2002  added basic resync support to iframe/pframe_decode()
41     *  22.06.2002  added primative N_VOP support
42     *                              #define BFRAMES_DEC now enables Minchen's bframe decoder
43     *  08.05.2002  add low_delay support for B_VOP decode
44     *              MinChen <chenm001@163.com>
45     *  05.05.2002  fix some B-frame decode problem
46     *  02.05.2002  add B-frame decode support(have some problem);
47     *              MinChen <chenm001@163.com>
48     *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>
49     *  29.03.2002  interlacing fix - compensated block wasn't being used when
50     *              reconstructing blocks, thus artifacts
51     *              interlacing speedup - used transfers to re-interlace
52     *              interlaced decoding should be as fast as progressive now
53   *  26.03.2002  interlacing support - moved transfers outside decode loop   *  26.03.2002  interlacing support - moved transfers outside decode loop
54   *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block   *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block
55   *      22.12.2001      block based interpolation   *  22.12.2001  lock based interpolation
56   *      01.12.2001      inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>   *      01.12.2001      inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>
57   *   *
58     *  $Id$
59     *
60   *************************************************************************/   *************************************************************************/
61    
62  #include <stdlib.h>  #include <stdlib.h>
63  #include <string.h>  // memset  #include <string.h>
64    
65    #ifdef BFRAMES_DEC_DEBUG
66            #define BFRAMES_DEC
67    #endif
68    
69  #include "xvid.h"  #include "xvid.h"
70  #include "portab.h"  #include "portab.h"
# Line 55  Line 79 
79  #include "dct/fdct.h"  #include "dct/fdct.h"
80  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
81  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
 #include "utils/mbfunctions.h"  
82    
83  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
84  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
# Line 66  Line 89 
89  #include "image/colorspace.h"  #include "image/colorspace.h"
90  #include "utils/mem_align.h"  #include "utils/mem_align.h"
91    
92  int decoder_create(XVID_DEC_PARAM * param)  int
93    decoder_create(XVID_DEC_PARAM * param)
94  {  {
95          DECODER * dec;          DECODER * dec;
96    
97          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
98          if (dec == NULL)          if (dec == NULL) {
         {  
99                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
100          }          }
101          param->handle = dec;          param->handle = dec;
# Line 85  Line 108 
108    
109          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
110          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
111            dec->low_delay = 0;
112    
113          if (image_create(&dec->cur, dec->edged_width, dec->edged_height))          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
         {  
114                  xvid_free(dec);                  xvid_free(dec);
115                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
116          }          }
117    
118          if (image_create(&dec->refn, dec->edged_width, dec->edged_height))          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {
119          {                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
120                    xvid_free(dec);
121                    return XVID_ERR_MEMORY;
122            }
123            // add by chenm001 <chenm001@163.com>
124            // for support B-frame to reference last 2 frame
125            if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
126                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
127                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
128                    xvid_free(dec);
129                    return XVID_ERR_MEMORY;
130            }
131            if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {
132                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
133                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
134                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
135                  xvid_free(dec);                  xvid_free(dec);
136                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
137          }          }
138    
139          dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);          dec->mbs =
140          if (dec->mbs == NULL)                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
141          {                                          CACHE_LINE);
142            if (dec->mbs == NULL) {
143                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
144                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
145                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
146                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
147                    xvid_free(dec);
148                    return XVID_ERR_MEMORY;
149            }
150    
151            memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
152    
153            // add by chenm001 <chenm001@163.com>
154            // for skip MB flag
155            dec->last_mbs =
156                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
157                                            CACHE_LINE);
158            if (dec->last_mbs == NULL) {
159                    xvid_free(dec->mbs);
160                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
161                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
162                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
163                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
164                  xvid_free(dec);                  xvid_free(dec);
165                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
166          }          }
167    
168            memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
169    
170          init_timer();          init_timer();
171          create_vlc_tables();  
172            // add by chenm001 <chenm001@163.com>
173            // for support B-frame to save reference frame's time
174            dec->frames = -1;
175            dec->time = dec->time_base = dec->last_time_base = 0;
176    
177          return XVID_ERR_OK;          return XVID_ERR_OK;
178  }  }
179    
180    
181  int decoder_destroy(DECODER * dec)  int
182    decoder_destroy(DECODER * dec)
183  {  {
184            xvid_free(dec->last_mbs);
185          xvid_free(dec->mbs);          xvid_free(dec->mbs);
186          image_destroy(&dec->refn, dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
187            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
188            image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
189          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
190          xvid_free(dec);          xvid_free(dec);
191    
         destroy_vlc_tables();  
   
192          write_timer();          write_timer();
193          return XVID_ERR_OK;          return XVID_ERR_OK;
194  }  }
195    
196    
197    
198  static const int32_t dquant_table[4] =  static const int32_t dquant_table[4] = {
 {  
199          -1, -2, 1, 2          -1, -2, 1, 2
200  };  };
201    
202    
203    
204    
205  // decode an intra macroblock  // decode an intra macroblock
206    
207  void decoder_mbintra(DECODER * dec,  void
208    decoder_mbintra(DECODER * dec,
209                                           MACROBLOCK * pMB,                                           MACROBLOCK * pMB,
210                                           const uint32_t x_pos,                                           const uint32_t x_pos,
211                                           const uint32_t y_pos,                                           const uint32_t y_pos,
# Line 145  Line 213 
213                                           const uint32_t cbp,                                           const uint32_t cbp,
214                                           Bitstream * bs,                                           Bitstream * bs,
215                                           const uint32_t quant,                                           const uint32_t quant,
216                                           const uint32_t intra_dc_threshold)                                  const uint32_t intra_dc_threshold,
217                                    const unsigned int bound)
218  {  {
219  #ifdef LINUX  
220          DECLARE_ALIGNED_MATRIX(block,6,64,int16_t,16);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
221          DECLARE_ALIGNED_MATRIX(data,6,64,int16_t,16);          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
222  #else  
223          CACHE_ALIGN int16_t block[6][64];          uint32_t stride = dec->edged_width;
224          CACHE_ALIGN int16_t data[6][64];          uint32_t stride2 = stride / 2;
225  #endif          uint32_t next_block = stride * 8;
         const uint32_t stride = dec->edged_width;  
226          uint32_t i;          uint32_t i;
227          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
228          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
229    
230      pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);      pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
231      pU_Cur = dec->cur.u + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
232      pV_Cur = dec->cur.v + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
233    
234  #ifdef LINUX          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear
         memset(block,0,sizeof(int16_t)*6*64);  
 #else  
         memset(block, 0, sizeof(block));                // clear  
 #endif  
235    
236          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
237                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
238                  int16_t predictors[8];                  int16_t predictors[8];
239                  int start_coeff;                  int start_coeff;
240    
241                  start_timer();                  start_timer();
242                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, block[i], iQuant, iDcScaler, predictors);                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64],
243                  if (!acpred_flag)                                           iQuant, iDcScaler, predictors, bound);
244                  {                  if (!acpred_flag) {
245                          pMB->acpred_directions[i] = 0;                          pMB->acpred_directions[i] = 0;
246                  }                  }
247                  stop_prediction_timer();                  stop_prediction_timer();
248    
249                  if (quant < intra_dc_threshold)                  if (quant < intra_dc_threshold) {
                 {  
250                          int dc_size;                          int dc_size;
251                          int dc_dif;                          int dc_dif;
252    
253                          dc_size = i < 4 ?  get_dc_size_lum(bs) : get_dc_size_chrom(bs);                          dc_size = i < 4 ?  get_dc_size_lum(bs) : get_dc_size_chrom(bs);
254                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;
255    
256                          if (dc_size > 8)                          if (dc_size > 8) {
                         {  
257                                  BitstreamSkip(bs, 1);           // marker                                  BitstreamSkip(bs, 1);           // marker
258                          }                          }
259    
260                          block[i][0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
261                          start_coeff = 1;                          start_coeff = 1;
262                  }  
263                  else                          DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);
264                  {                  } else {
265                          start_coeff = 0;                          start_coeff = 0;
266                  }                  }
267    
268                  start_timer();                  start_timer();
269                  if (cbp & (1 << (5-i)))                 // coded                  if (cbp & (1 << (5-i)))                 // coded
270                  {                  {
271                          get_intra_block(bs, block[i], pMB->acpred_directions[i], start_coeff);                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],
272                                                            start_coeff);
273                  }                  }
274                  stop_coding_timer();                  stop_coding_timer();
275    
276                  start_timer();                  start_timer();
277                  add_acdc(pMB, i, block[i], iDcScaler, predictors);                  add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors);
278                  stop_prediction_timer();                  stop_prediction_timer();
279    
280                  start_timer();                  start_timer();
281                  if (dec->quant_type == 0)                  if (dec->quant_type == 0) {
282                  {                          dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
283                          dequant_intra(data[i], block[i], iQuant, iDcScaler);                  } else {
284                  }                          dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
                 else  
                 {  
                         dequant4_intra(data[i], block[i], iQuant, iDcScaler);  
285                  }                  }
286                  stop_iquant_timer();                  stop_iquant_timer();
287    
288                  start_timer();                  start_timer();
289                  idct(data[i]);                  idct(&data[i * 64]);
290                  stop_idct_timer();                  stop_idct_timer();
291          }          }
292    
293          start_timer();          if (dec->interlacing && pMB->field_dct) {
294          if (dec->interlacing && pMB->field_dct)                  next_block = stride;
295          {                  stride *= 2;
                 MBFieldToFrame(data);  
296          }          }
         stop_interlacing_timer();  
297    
298          start_timer();          start_timer();
299          transfer_16to8copy(pY_Cur, data[0], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
300          transfer_16to8copy(pY_Cur + 8, data[1], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
301          transfer_16to8copy(pY_Cur + 8 * stride, data[2], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
302          transfer_16to8copy(pY_Cur + 8 + 8 * stride, data[3], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
303          transfer_16to8copy(pU_Cur, data[4], stride / 2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
304          transfer_16to8copy(pV_Cur, data[5], stride / 2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
305          stop_transfer_timer();          stop_transfer_timer();
306  }  }
307    
# Line 260  Line 317 
317    
318  // decode an inter macroblock  // decode an inter macroblock
319    
320  void decoder_mbinter(DECODER * dec,  void
321    decoder_mbinter(DECODER * dec,
322                                           const MACROBLOCK * pMB,                                           const MACROBLOCK * pMB,
323                                           const uint32_t x_pos,                                           const uint32_t x_pos,
324                                           const uint32_t y_pos,                                           const uint32_t y_pos,
# Line 270  Line 328 
328                                           const uint32_t quant,                                           const uint32_t quant,
329                                           const uint32_t rounding)                                           const uint32_t rounding)
330  {  {
 #ifdef LINUX  
         DECLARE_ALIGNED_MATRIX(block,6,64,int16_t,16);  
         DECLARE_ALIGNED_MATRIX(data,6,64,int16_t,16);  
 #else  
         CACHE_ALIGN int16_t block[6][64];  
         CACHE_ALIGN int16_t data[6][64];  
 #endif  
331    
332          const uint32_t stride = dec->edged_width;          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
333          const uint32_t stride2 = dec->edged_width / 2;          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
334    
335            uint32_t stride = dec->edged_width;
336            uint32_t stride2 = stride / 2;
337            uint32_t next_block = stride * 8;
338      uint32_t i;      uint32_t i;
339      uint32_t iQuant = pMB->quant;      uint32_t iQuant = pMB->quant;
340          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
341          int uv_dx, uv_dy;          int uv_dx, uv_dy;
342    
343      pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);      pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
344      pU_Cur = dec->cur.u + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
345      pV_Cur = dec->cur.v + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
346    
347          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
         {  
348                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
349                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
350    
351                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
352                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
353          }          } else {
         else  
         {  
354                  int sum;                  int sum;
355    
356                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
357                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                  uv_dx =
358                            (sum ==
359                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
360                                                                      (ABS(sum) / 16) * 2));
361    
362                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
363                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                  uv_dy =
364                            (sum ==
365                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
366                                                                      (ABS(sum) / 16) * 2));
367          }          }
368    
369          start_timer();          start_timer();
370          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, 16 * y_pos,
371          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding);
372          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,
373          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride,
374          interpolate8x8_switch(dec->cur.u, dec->refn.u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);                                                    rounding);
375          interpolate8x8_switch(dec->cur.v, dec->refn.v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos,
376                                                      16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,
377                                                      rounding);
378            interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,
379                                                      16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
380                                                      rounding);
381            interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
382                                                      uv_dx, uv_dy, stride2, rounding);
383            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
384                                                      uv_dx, uv_dy, stride2, rounding);
385          stop_comp_timer();          stop_comp_timer();
386    
387          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
388                  if (cbp & (1 << (5-i)))                 // coded                  if (cbp & (1 << (5-i)))                 // coded
389                  {                  {
390                          memset(block[i], 0, 64 * sizeof(int16_t));              // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
391    
392                          start_timer();                          start_timer();
393                          get_inter_block(bs, block[i]);                          get_inter_block(bs, &block[i * 64]);
394                          stop_coding_timer();                          stop_coding_timer();
395    
396                          start_timer();                          start_timer();
397                          if (dec->quant_type == 0)                          if (dec->quant_type == 0) {
398                          {                                  dequant_inter(&data[i * 64], &block[i * 64], iQuant);
399                                  dequant_inter(data[i], block[i], iQuant);                          } else {
400                          }                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
                         else  
                         {  
                                 dequant4_inter(data[i], block[i], iQuant);  
401                          }                          }
402                          stop_iquant_timer();                          stop_iquant_timer();
403    
404                          start_timer();                          start_timer();
405                          idct(data[i]);                          idct(&data[i * 64]);
406                          stop_idct_timer();                          stop_idct_timer();
407                  }                  }
408          }          }
409    
410          start_timer();          if (dec->interlacing && pMB->field_dct) {
411          if (pMB->field_dct)                  next_block = stride;
412          {                  stride *= 2;
                 MBFieldToFrame(data);  
413          }          }
         stop_interlacing_timer();  
414    
415          start_timer();          start_timer();
416          if (cbp & 32)          if (cbp & 32)
417                  transfer_16to8add(pY_Cur, data[0], stride);                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);
418          if (cbp & 16)          if (cbp & 16)
419                  transfer_16to8add(pY_Cur + 8, data[1], stride);                  transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
420          if (cbp & 8)          if (cbp & 8)
421                  transfer_16to8add(pY_Cur + 8 * stride, data[2], stride);                  transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
422          if (cbp & 4)          if (cbp & 4)
423                  transfer_16to8add(pY_Cur + 8 + 8 * stride, data[3], stride);                  transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
424          if (cbp & 2)          if (cbp & 2)
425                  transfer_16to8add(pU_Cur, data[4], stride / 2);                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
426          if (cbp & 1)          if (cbp & 1)
427                  transfer_16to8add(pV_Cur, data[5], stride / 2);                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
428          stop_transfer_timer();          stop_transfer_timer();
429  }  }
430    
431    
432  void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold)  void
433    decoder_iframe(DECODER * dec,
434                               Bitstream * bs,
435                               int quant,
436                               int intra_dc_threshold)
437  {  {
438            uint32_t bound;
439          uint32_t x, y;          uint32_t x, y;
440    
441          for (y = 0; y < dec->mb_height; y++)          bound = 0;
         {  
                 for (x = 0; x < dec->mb_width; x++)  
                 {  
                         MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];  
442    
443            for (y = 0; y < dec->mb_height; y++) {
444                    for (x = 0; x < dec->mb_width; x++) {
445                            MACROBLOCK *mb;
446                          uint32_t mcbpc;                          uint32_t mcbpc;
447                          uint32_t cbpc;                          uint32_t cbpc;
448                          uint32_t acpred_flag;                          uint32_t acpred_flag;
449                          uint32_t cbpy;                          uint32_t cbpy;
450                          uint32_t cbp;                          uint32_t cbp;
451    
452                            while (BitstreamShowBits(bs, 9) == 1)
453                                    BitstreamSkip(bs, 9);
454    
455                            if (check_resync_marker(bs, 0))
456                            {
457                                    bound = read_video_packet_header(bs, 0, &quant);
458                                    x = bound % dec->mb_width;
459                                    y = bound / dec->mb_width;
460                            }
461                            mb = &dec->mbs[y * dec->mb_width + x];
462    
463                            DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
464    
465                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
466                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
467                          cbpc = (mcbpc >> 4);                          cbpc = (mcbpc >> 4);
468    
469                          acpred_flag = BitstreamGetBit(bs);                          acpred_flag = BitstreamGetBit(bs);
470    
                         if (mb->mode == MODE_STUFFING)  
                         {  
                                 DEBUG("-- STUFFING ?");  
                                 continue;  
                         }  
   
471                          cbpy = get_cbpy(bs, 1);                          cbpy = get_cbpy(bs, 1);
472                          cbp = (cbpy << 2) | cbpc;                          cbp = (cbpy << 2) | cbpc;
473    
474                          if (mb->mode == MODE_INTRA_Q)                          if (mb->mode == MODE_INTRA_Q) {
                         {  
475                                  quant += dquant_table[BitstreamGetBits(bs,2)];                                  quant += dquant_table[BitstreamGetBits(bs,2)];
476                                  if (quant > 31)                                  if (quant > 31) {
                                 {  
477                                          quant = 31;                                          quant = 31;
478                                  }                                  } else if (quant < 1) {
                                 else if (quant < 1)  
                                 {  
479                                          quant = 1;                                          quant = 1;
480                                  }                                  }
481                          }                          }
482                          mb->quant = quant;                          mb->quant = quant;
483                            mb->mvs[0].x = mb->mvs[0].y =
484                            mb->mvs[1].x = mb->mvs[1].y =
485                            mb->mvs[2].x = mb->mvs[2].y =
486                            mb->mvs[3].x = mb->mvs[3].y =0;
487    
488                          if (dec->interlacing)                          if (dec->interlacing) {
                         {  
489                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
490                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DEBUG1("deci: field_dct: ", mb->field_dct);
491                          }                          }
492    
493                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
494                                                            intra_dc_threshold, bound);
495                  }                  }
496                    if(dec->out_frm)
497                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);
498    
499          }          }
500    
501  }  }
502    
503    
504  void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode)  void
505    get_motion_vector(DECODER * dec,
506                                      Bitstream * bs,
507                                      int x,
508                                      int y,
509                                      int k,
510                                      VECTOR * mv,
511                                      int fcode,
512                                      const int bound)
513  {  {
514    
515          int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
516          int high = (32 * scale_fac) - 1;          int high = (32 * scale_fac) - 1;
517          int low = ((-32) * scale_fac);          int low = ((-32) * scale_fac);
518          int range = (64 * scale_fac);          int range = (64 * scale_fac);
519    
520          VECTOR pmv[4];          VECTOR pmv;
         uint32_t psad[4];  
   
521          int mv_x, mv_y;          int mv_x, mv_y;
         int pmv_x, pmv_y;  
   
522    
523          get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
   
         pmv_x = pmv[0].x;  
         pmv_y = pmv[0].y;  
524    
525          mv_x = get_mv(bs, fcode);          mv_x = get_mv(bs, fcode);
526          mv_y = get_mv(bs, fcode);          mv_y = get_mv(bs, fcode);
527    
528          mv_x += pmv_x;          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);
         mv_y += pmv_y;  
529    
530          if (mv_x < low)          mv_x += pmv.x;
531          {          mv_y += pmv.y;
532    
533            if (mv_x < low) {
534                  mv_x += range;                  mv_x += range;
535          }          } else if (mv_x > high) {
         else if (mv_x > high)  
         {  
536                  mv_x -= range;                  mv_x -= range;
537          }          }
538    
539          if (mv_y < low)          if (mv_y < low) {
         {  
540                  mv_y += range;                  mv_y += range;
541          }          } else if (mv_y > high) {
         else if (mv_y > high)  
         {  
542                  mv_y -= range;                  mv_y -= range;
543          }          }
544    
# Line 473  Line 548 
548  }  }
549    
550    
551  void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold)  void
552    decoder_pframe(DECODER * dec,
553                               Bitstream * bs,
554                               int rounding,
555                               int quant,
556                               int fcode,
557                               int intra_dc_threshold)
558  {  {
         uint32_t x, y;  
559    
560          image_swap(&dec->cur, &dec->refn);          uint32_t x, y;
561            uint32_t bound;
562            int cp_mb, st_mb;
563    
564          start_timer();          start_timer();
565          image_setedges(&dec->refn, dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
566                                       dec->width, dec->height, dec->interlacing);
567          stop_edges_timer();          stop_edges_timer();
568    
569          for (y = 0; y < dec->mb_height; y++)          bound = 0;
570          {  
571                  for (x = 0; x < dec->mb_width; x++)          for (y = 0; y < dec->mb_height; y++) {
572                    cp_mb = st_mb = 0;
573                    for (x = 0; x < dec->mb_width; x++) {
574                            MACROBLOCK *mb;
575    
576                            // skip stuffing
577                            while (BitstreamShowBits(bs, 10) == 1)
578                                    BitstreamSkip(bs, 10);
579    
580                            if (check_resync_marker(bs, fcode - 1))
581                  {                  {
582                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                                  bound = read_video_packet_header(bs, fcode - 1, &quant);
583                                    x = bound % dec->mb_width;
584                                    y = bound / dec->mb_width;
585                            }
586                            mb = &dec->mbs[y * dec->mb_width + x];
587    
588                            DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
589    
590                          if (!BitstreamGetBit(bs))                       // not_coded                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded
591                            if (!(BitstreamGetBit(bs)))     // not_coded
592                          {                          {
593                                  uint32_t mcbpc;                                  uint32_t mcbpc;
594                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 498  Line 597 
597                                  uint32_t cbp;                                  uint32_t cbp;
598                                  uint32_t intra;                                  uint32_t intra;
599    
600                                    cp_mb++;
601                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
602                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
603                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
604    
605                                    DPRINTF(DPRINTF_MB, "mode %i", mb->mode);
606                                    DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);
607                                  acpred_flag = 0;                                  acpred_flag = 0;
608    
609                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
610    
611                                  if (intra)                                  if (intra) {
                                 {  
612                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
613                                  }                                  }
614    
                                 if (mb->mode == MODE_STUFFING)  
                                 {  
                                         DEBUG("-- STUFFING ?");  
                                         continue;  
                                 }  
   
615                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
616                                    DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);
617    
618                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
619    
620                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q)                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
621                                  {                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
622                                          quant += dquant_table[BitstreamGetBits(bs,2)];                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);
623                                          if (quant > 31)                                          quant += dquant;
624                                          {                                          if (quant > 31) {
625                                                  quant = 31;                                                  quant = 31;
626                                          }                                          } else if (quant < 1) {
                                         else if (mb->quant < 1)  
                                         {  
627                                                  quant = 1;                                                  quant = 1;
628                                          }                                          }
629                                            DPRINTF(DPRINTF_MB, "quant %i", quant);
630                                  }                                  }
631                                  mb->quant = quant;                                  mb->quant = quant;
632    
633                                  if (dec->interlacing)                                  if (dec->interlacing) {
                                 {  
634                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
635                                          DEBUG1("decp: field_dct: ", mb->field_dct);                                          DEBUG1("decp: field_dct: ", mb->field_dct);
636    
637                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
                                         {  
638                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
639                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DEBUG1("decp: field_pred: ", mb->field_pred);
640    
641                                                  if (mb->field_pred)                                                  if (mb->field_pred) {
                                                 {  
642                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
643                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);
644                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
# Line 553  Line 647 
647                                          }                                          }
648                                  }                                  }
649    
650                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
651                                  {                                          if (dec->interlacing && mb->field_pred) {
652                                          if (dec->interlacing && mb->field_pred)                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
653                                                                                      fcode, bound);
654                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
655                                                                                      fcode, bound);
656                                            } else {
657                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
658                                                                                      fcode, bound);
659                                                    mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
660                                                            mb->mvs[0].x;
661                                                    mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
662                                                            mb->mvs[0].y;
663                                            }
664                                    } else if (mb->mode ==
665                                                       MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {
666                                            get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
667                                            get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
668                                            get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
669                                            get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
670                                    } else                  // MODE_INTRA, MODE_INTRA_Q
671                                    {
672                                            mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
673                                                    0;
674                                            mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
675                                                    0;
676                                            decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
677                                                                            intra_dc_threshold, bound);
678                                            continue;
679                                    }
680    
681                                    decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
682                                                                    rounding);
683                            } else                          // not coded
684                                          {                                          {
685                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);                                  //DEBUG2("P-frame MB at (X,Y)=",x,y);
686                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode);                                  mb->mode = MODE_NOT_CODED;
687                                    mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
688                                    mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
689    
690                                    // copy macroblock directly from ref to cur
691    
692                                    start_timer();
693    
694                                    transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
695                                                                     (16 * x),
696                                                                     dec->refn[0].y + (16 * y) * dec->edged_width +
697                                                                     (16 * x), dec->edged_width);
698    
699                                    transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
700                                                                     (16 * x + 8),
701                                                                     dec->refn[0].y + (16 * y) * dec->edged_width +
702                                                                     (16 * x + 8), dec->edged_width);
703    
704                                    transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
705                                                                     (16 * x),
706                                                                     dec->refn[0].y + (16 * y +
707                                                                                                       8) * dec->edged_width +
708                                                                     (16 * x), dec->edged_width);
709    
710                                    transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
711                                                                     (16 * x + 8),
712                                                                     dec->refn[0].y + (16 * y +
713                                                                                                       8) * dec->edged_width +
714                                                                     (16 * x + 8), dec->edged_width);
715    
716                                    transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 +
717                                                                     (8 * x),
718                                                                     dec->refn[0].u +
719                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
720                                                                     dec->edged_width / 2);
721    
722                                    transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +
723                                                                     (8 * x),
724                                                                     dec->refn[0].v +
725                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
726                                                                     dec->edged_width / 2);
727                                    stop_transfer_timer();
728                                    if(dec->out_frm && cp_mb > 0) {
729                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
730                                      cp_mb = 0;
731                                          }                                          }
732                                          else                                  st_mb = x+1;
733                            }
734                    }
735                    if(dec->out_frm && cp_mb > 0)
736                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
737            }
738    }
739    
740    
741    // add by MinChen <chenm001@163.com>
742    // decode B-frame motion vector
743    void
744    get_b_motion_vector(DECODER * dec,
745                                            Bitstream * bs,
746                                            int x,
747                                            int y,
748                                            VECTOR * mv,
749                                            int fcode,
750                                            const VECTOR pmv)
751                                          {                                          {
752                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);          int scale_fac = 1 << (fcode - 1);
753                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;          int high = (32 * scale_fac) - 1;
754                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;          int low = ((-32) * scale_fac);
755            int range = (64 * scale_fac);
756    
757            int mv_x, mv_y;
758            int pmv_x, pmv_y;
759    
760            pmv_x = pmv.x;
761            pmv_y = pmv.y;
762    
763            mv_x = get_mv(bs, fcode);
764            mv_y = get_mv(bs, fcode);
765    
766            mv_x += pmv_x;
767            mv_y += pmv_y;
768    
769            if (mv_x < low) {
770                    mv_x += range;
771            } else if (mv_x > high) {
772                    mv_x -= range;
773            }
774    
775            if (mv_y < low) {
776                    mv_y += range;
777            } else if (mv_y > high) {
778                    mv_y -= range;
779                                          }                                          }
780    
781            mv->x = mv_x;
782            mv->y = mv_y;
783                                  }                                  }
784                                  else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */)  
785    
786    // add by MinChen <chenm001@163.com>
787    // decode an B-frame forward & backward inter macroblock
788    void
789    decoder_bf_mbinter(DECODER * dec,
790                                       const MACROBLOCK * pMB,
791                                       const uint32_t x_pos,
792                                       const uint32_t y_pos,
793                                       const uint32_t cbp,
794                                       Bitstream * bs,
795                                       const uint32_t quant,
796                                       const uint8_t ref)
797                                  {                                  {
798                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);  
799                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
800                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
801                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);  
802            uint32_t stride = dec->edged_width;
803            uint32_t stride2 = stride / 2;
804            uint32_t next_block = stride * 8;
805            uint32_t i;
806            uint32_t iQuant = pMB->quant;
807            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
808            int uv_dx, uv_dy;
809    
810            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
811            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
812            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
813    
814    
815            if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
816                    uv_dx = pMB->mvs[0].x;
817                    uv_dy = pMB->mvs[0].y;
818    
819                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
820                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
821            } else {
822                    int sum;
823    
824                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
825                    uv_dx =
826                            (sum ==
827                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
828                                                                      (ABS(sum) / 16) * 2));
829    
830                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
831                    uv_dy =
832                            (sum ==
833                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
834                                                                      (ABS(sum) / 16) * 2));
835                                  }                                  }
836                                  else  // MODE_INTRA, MODE_INTRA_Q  
837            start_timer();
838            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,
839                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
840            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,
841                                                      16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
842            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos,
843                                                      16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,
844                                                      0);
845            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,
846                                                      16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
847                                                      0);
848            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
849                                                      uv_dx, uv_dy, stride2, 0);
850            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
851                                                      uv_dx, uv_dy, stride2, 0);
852            stop_comp_timer();
853    
854            for (i = 0; i < 6; i++) {
855                    if (cbp & (1 << (5 - i)))       // coded
856                                  {                                  {
857                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
858                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  
859                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                          start_timer();
860                                          continue;                          get_inter_block(bs, &block[i * 64]);
861                            stop_coding_timer();
862    
863                            start_timer();
864                            if (dec->quant_type == 0) {
865                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
866                            } else {
867                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
868                            }
869                            stop_iquant_timer();
870    
871                            start_timer();
872                            idct(&data[i * 64]);
873                            stop_idct_timer();
874                    }
875                                  }                                  }
876    
877                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, rounding);          if (dec->interlacing && pMB->field_dct) {
878                    next_block = stride;
879                    stride *= 2;
880                          }                          }
881                          else    // not coded  
882            start_timer();
883            if (cbp & 32)
884                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
885            if (cbp & 16)
886                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
887            if (cbp & 8)
888                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
889            if (cbp & 4)
890                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
891            if (cbp & 2)
892                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
893            if (cbp & 1)
894                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
895            stop_transfer_timer();
896    }
897    
898    
899    // add by MinChen <chenm001@163.com>
900    // decode an B-frame direct &  inter macroblock
901    void
902    decoder_bf_interpolate_mbinter(DECODER * dec,
903                                                               IMAGE forward,
904                                                               IMAGE backward,
905                                                               const MACROBLOCK * pMB,
906                                                               const uint32_t x_pos,
907                                                               const uint32_t y_pos,
908                                                               Bitstream * bs)
909                          {                          {
910    
911                                  mb->mode = MODE_NOT_CODED;          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
912                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
913                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  
914            uint32_t stride = dec->edged_width;
915            uint32_t stride2 = stride / 2;
916            uint32_t next_block = stride * 8;
917            uint32_t iQuant = pMB->quant;
918            int uv_dx, uv_dy;
919            int b_uv_dx, b_uv_dy;
920            uint32_t i;
921            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
922        const uint32_t cbp = pMB->cbp;
923    
924            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
925            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
926            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
927    
928    
929            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
930                    uv_dx = pMB->mvs[0].x;
931                    uv_dy = pMB->mvs[0].y;
932    
933                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
934                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
935    
936                    b_uv_dx = pMB->b_mvs[0].x;
937                    b_uv_dy = pMB->b_mvs[0].y;
938    
939                    b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
940                    b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
941            } else {
942                    int sum;
943    
944                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
945                    uv_dx =
946                            (sum ==
947                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
948                                                                      (ABS(sum) / 16) * 2));
949    
950                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
951                    uv_dy =
952                            (sum ==
953                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
954                                                                      (ABS(sum) / 16) * 2));
955    
956                    sum =
957                            pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +
958                            pMB->b_mvs[3].x;
959                    b_uv_dx =
960                            (sum ==
961                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
962                                                                      (ABS(sum) / 16) * 2));
963    
964                    sum =
965                            pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y +
966                            pMB->b_mvs[3].y;
967                    b_uv_dy =
968                            (sum ==
969                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
970                                                                      (ABS(sum) / 16) * 2));
971            }
972    
                                 // copy macroblock directly from ref to cur  
973    
974                                  start_timer();                                  start_timer();
975            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
976                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
977            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
978                                                      pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
979            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
980                                                      pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
981            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
982                                                      16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
983                                                      0);
984            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
985                                                      uv_dy, stride2, 0);
986            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
987                                                      uv_dy, stride2, 0);
988    
989    
990            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,
991                                                      pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
992            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
993                                                      16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
994                                                      0);
995            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,
996                                                      16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
997                                                      stride, 0);
998            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
999                                                      16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1000                                                      stride, 0);
1001            interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,
1002                                                      b_uv_dx, b_uv_dy, stride2, 0);
1003            interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,
1004                                                      b_uv_dx, b_uv_dy, stride2, 0);
1005    
1006            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos,
1007                                             stride);
1008            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos,
1009                                             stride);
1010            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,
1011                                             stride);
1012            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,
1013                                             16 * y_pos + 8, stride);
1014            interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,
1015                                             stride2);
1016            interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos,
1017                                             stride2);
1018            stop_comp_timer();
1019    
1020                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),          for (i = 0; i < 6; i++) {
1021                                                                  dec->refn.y + (16*y)*dec->edged_width + (16*x),                  if (cbp & (1 << (5 - i)))       // coded
1022                                                                  dec->edged_width);                  {
1023                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
                                 transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),  
                                                                 dec->refn.y + (16*y)*dec->edged_width + (16*x+8),  
                                                                 dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x),  
                                                                 dec->refn.y + (16*y+8)*dec->edged_width + (16*x),  
                                                                 dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8),  
                                                                 dec->refn.y + (16*y+8)*dec->edged_width + (16*x+8),  
                                                                 dec->edged_width);  
1024    
1025                                  transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),                          start_timer();
1026                                                                  dec->refn.u + (8*y)*dec->edged_width/2 + (8*x),                          get_inter_block(bs, &block[i * 64]);
1027                                                                  dec->edged_width/2);                          stop_coding_timer();
1028    
1029                                  transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),                          start_timer();
1030                                                                  dec->refn.v + (8*y)*dec->edged_width/2 + (8*x),                          if (dec->quant_type == 0) {
1031                                                                  dec->edged_width/2);                                  dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1032                            } else {
1033                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1034                            }
1035                            stop_iquant_timer();
1036    
1037                            start_timer();
1038                            idct(&data[i * 64]);
1039                            stop_idct_timer();
1040                    }
1041            }
1042    
1043            if (dec->interlacing && pMB->field_dct) {
1044                    next_block = stride;
1045                    stride *= 2;
1046            }
1047    
1048            start_timer();
1049            if (cbp & 32)
1050                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1051            if (cbp & 16)
1052                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1053            if (cbp & 8)
1054                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1055            if (cbp & 4)
1056                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1057            if (cbp & 2)
1058                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1059            if (cbp & 1)
1060                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1061                                  stop_transfer_timer();                                  stop_transfer_timer();
1062                          }                          }
1063    
1064    
1065    // add by MinChen <chenm001@163.com>
1066    // for decode B-frame dbquant
1067    int32_t __inline
1068    get_dbquant(Bitstream * bs)
1069    {
1070            if (!BitstreamGetBit(bs))       // '0'
1071                    return (0);
1072            else if (!BitstreamGetBit(bs))  // '10'
1073                    return (-2);
1074            else
1075                    return (2);                             // '11'
1076    }
1077    
1078    // add by MinChen <chenm001@163.com>
1079    // for decode B-frame mb_type
1080    // bit   ret_value
1081    // 1        0
1082    // 01       1
1083    // 001      2
1084    // 0001     3
1085    int32_t __inline
1086    get_mbtype(Bitstream * bs)
1087    {
1088            int32_t mb_type;
1089    
1090            for (mb_type = 0; mb_type <= 3; mb_type++) {
1091                    if (BitstreamGetBit(bs))
1092                            break;
1093            }
1094    
1095            if (mb_type <= 3)
1096                    return (mb_type);
1097            else
1098                    return (-1);
1099    }
1100    
1101    void
1102    decoder_bframe(DECODER * dec,
1103                               Bitstream * bs,
1104                               int quant,
1105                               int fcode_forward,
1106                               int fcode_backward)
1107    {
1108            uint32_t x, y;
1109            VECTOR mv;
1110            const VECTOR zeromv = {0,0};
1111    #ifdef BFRAMES_DEC_DEBUG
1112            FILE *fp;
1113            static char first=0;
1114    #define BFRAME_DEBUG    if (!first && fp){ \
1115                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \
1116            }
1117    #endif
1118    
1119            start_timer();
1120            image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1121                                       dec->width, dec->height, dec->interlacing);
1122            image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1123                                       dec->width, dec->height, dec->interlacing);
1124            stop_edges_timer();
1125    
1126    #ifdef BFRAMES_DEC_DEBUG
1127            if (!first){
1128                    fp=fopen("C:\\XVIDDBG.TXT","w");
1129            }
1130    #endif
1131    
1132            for (y = 0; y < dec->mb_height; y++) {
1133                    // Initialize Pred Motion Vector
1134                    dec->p_fmv = dec->p_bmv = zeromv;
1135                    for (x = 0; x < dec->mb_width; x++) {
1136                            MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1137                            MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1138    
1139                            mv =
1140                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1141                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1142    
1143                            // the last P_VOP is skip macroblock ?
1144                            if (last_mb->mode == MODE_NOT_CODED) {
1145                                    //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1146                                    mb->cbp = 0;
1147    #ifdef BFRAMES_DEC_DEBUG
1148                                    mb->mb_type = MODE_NOT_CODED;
1149            BFRAME_DEBUG
1150    #endif
1151                                    mb->mb_type = MODE_FORWARD;
1152                                    mb->quant = last_mb->quant;
1153                                    //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1154                                    //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1155    
1156                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1157                                    continue;
1158                            }
1159    
1160                            if (!BitstreamGetBit(bs)) {     // modb=='0'
1161                                    const uint8_t modb2 = BitstreamGetBit(bs);
1162    
1163                                    mb->mb_type = get_mbtype(bs);
1164    
1165                                    if (!modb2) {   // modb=='00'
1166                                            mb->cbp = BitstreamGetBits(bs, 6);
1167                                    } else {
1168                                            mb->cbp = 0;
1169                                    }
1170                                    if (mb->mb_type && mb->cbp) {
1171                                            quant += get_dbquant(bs);
1172    
1173                                            if (quant > 31) {
1174                                                    quant = 31;
1175                                            } else if (quant < 1) {
1176                                                    quant = 1;
1177                                            }
1178                                    }
1179                            } else {
1180                                    mb->mb_type = MODE_DIRECT_NONE_MV;
1181                                    mb->cbp = 0;
1182                  }                  }
1183    
1184                            mb->quant = quant;
1185                            mb->mode = MODE_INTER4V;
1186                            //DEBUG1("Switch bm_type=",mb->mb_type);
1187    
1188    #ifdef BFRAMES_DEC_DEBUG
1189            BFRAME_DEBUG
1190    #endif
1191                            switch (mb->mb_type) {
1192                            case MODE_DIRECT:
1193                                    get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1194    
1195                            case MODE_DIRECT_NONE_MV:
1196                                    {                               // Because this file is a C file not C++ so I use '{' to define var
1197                                            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1198                                            int i;
1199    
1200                                            for (i = 0; i < 4; i++) {
1201                                                    mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1202                                                                          / TRD + mv.x);
1203                                                    mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1204                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].x)
1205                                                                                      / TRD
1206                                                                                    : mb->mvs[i].x - last_mb->mvs[i].x);
1207                                                    mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1208                                                                          / TRD + mv.y);
1209                                                    mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1210                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].y)
1211                                                                                      / TRD
1212                                                                                : mb->mvs[i].y - last_mb->mvs[i].y);
1213                                            }
1214                                            //DEBUG("B-frame Direct!\n");
1215          }          }
1216                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1217                                                                                               mb, x, y, bs);
1218                                    break;
1219    
1220                            case MODE_INTERPOLATE:
1221                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1222                                                                            dec->p_fmv);
1223                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1224    
1225                                    get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1226                                                                            fcode_backward, dec->p_bmv);
1227                                    dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1228                                            mb->b_mvs[3] = mb->b_mvs[0];
1229    
1230                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1231                                                                                               mb, x, y, bs);
1232                                    //DEBUG("B-frame Bidir!\n");
1233                                    break;
1234    
1235                            case MODE_BACKWARD:
1236                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1237                                                                            dec->p_bmv);
1238                                    dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1239    
1240                                    mb->mode = MODE_INTER;
1241                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1242                                    //DEBUG("B-frame Backward!\n");
1243                                    break;
1244    
1245                            case MODE_FORWARD:
1246                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1247                                                                            dec->p_fmv);
1248                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1249    
1250                                    mb->mode = MODE_INTER;
1251                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1252                                    //DEBUG("B-frame Forward!\n");
1253                                    break;
1254    
1255                            default:
1256                                    //DEBUG1("Not support B-frame mb_type =", mb->mb_type);
1257                                    ;
1258                            }
1259    
1260                    }                                               // end of FOR
1261            }
1262    #ifdef BFRAMES_DEC_DEBUG
1263            if (!first){
1264                    first=1;
1265                    if (fp)
1266                            fclose(fp);
1267            }
1268    #endif
1269    }
1270    
1271    // swap two MACROBLOCK array
1272    void
1273    mb_swap(MACROBLOCK ** mb1,
1274                    MACROBLOCK ** mb2)
1275    {
1276            MACROBLOCK *temp = *mb1;
1277    
1278            *mb1 = *mb2;
1279            *mb2 = temp;
1280  }  }
1281    
1282  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)  int
1283    decoder_decode(DECODER * dec,
1284                               XVID_DEC_FRAME * frame)
1285  {  {
1286    
1287          Bitstream bs;          Bitstream bs;
1288          uint32_t rounding;          uint32_t rounding;
1289          uint32_t quant;          uint32_t quant;
1290          uint32_t fcode;          uint32_t fcode_forward;
1291            uint32_t fcode_backward;
1292          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1293            uint32_t vop_type;
1294    
1295          start_global_timer();          start_global_timer();
1296    
1297            dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1298    
1299          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1300    
1301          switch (BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold))          // add by chenm001 <chenm001@163.com>
1302          {          // for support B-frame to reference last 2 frame
1303            dec->frames++;
1304            vop_type =
1305                    BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,
1306                                                             &fcode_backward, &intra_dc_threshold);
1307    
1308            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0
1309    
1310            switch (vop_type) {
1311          case P_VOP :          case P_VOP :
1312                  decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold);                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,
1313                                               intra_dc_threshold);
1314    #ifdef BFRAMES_DEC
1315                    DEBUG1("P_VOP  Time=", dec->time);
1316    #endif
1317                  break;                  break;
1318    
1319          case I_VOP :          case I_VOP :
                 //DEBUG1("",intra_dc_threshold);  
1320                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1321    #ifdef BFRAMES_DEC
1322                    DEBUG1("I_VOP  Time=", dec->time);
1323    #endif
1324                  break;                  break;
1325    
1326          case B_VOP :    // ignore          case B_VOP:
1327    #ifdef BFRAMES_DEC
1328                    if (dec->time_pp > dec->time_bp) {
1329                            DEBUG1("B_VOP  Time=", dec->time);
1330                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1331                    } else {
1332                            DEBUG("broken B-frame!");
1333                    }
1334    #else
1335                    image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1336    #endif
1337                  break;                  break;
1338    
1339          case N_VOP :    // vop not coded          case N_VOP :    // vop not coded
1340                    // when low_delay==0, N_VOP's should interpolate between the past and future frames
1341                    image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1342                  break;                  break;
1343    
1344          default :          default :
1345                  return XVID_ERR_FAIL;                  return XVID_ERR_FAIL;
1346          }          }
1347    
1348    #ifdef BFRAMES_DEC_DEBUG
1349            if (frame->length != BitstreamPos(&bs) / 8){
1350                    DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8);
1351            }
1352    #endif
1353          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1354    
1355          start_timer();  
1356    #ifdef BFRAMES_DEC
1357            // test if no B_VOP
1358            if (dec->low_delay) {
1359    #endif
1360          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
1361                                  frame->image, frame->stride, frame->colorspace);                                  frame->image, frame->stride, frame->colorspace);
1362    
1363    #ifdef BFRAMES_DEC
1364            } else {
1365                    if (dec->frames >= 0) {
1366                            start_timer();
1367                            if ((vop_type == I_VOP || vop_type == P_VOP)) {
1368                                    image_output(&dec->refn[0], dec->width, dec->height,
1369                                                             dec->edged_width, frame->image, frame->stride,
1370                                                             frame->colorspace);
1371                            } else if (vop_type == B_VOP) {
1372                                    image_output(&dec->cur, dec->width, dec->height,
1373                                                             dec->edged_width, frame->image, frame->stride,
1374                                                             frame->colorspace);
1375                            }
1376          stop_conv_timer();          stop_conv_timer();
1377                    }
1378            }
1379    #endif
1380    
1381            if (vop_type == I_VOP || vop_type == P_VOP) {
1382                    image_swap(&dec->refn[0], &dec->refn[1]);
1383                    image_swap(&dec->cur, &dec->refn[0]);
1384    
1385                    // swap MACROBLOCK
1386                    // the Divx will not set the low_delay flage some times
1387                    // so follow code will wrong to not swap at that time
1388                    // this will broken bitstream! so I'm change it,
1389                    // But that is not the best way! can anyone tell me how
1390                    // to do another way?
1391                    // 18-07-2002   MinChen<chenm001@163.com>
1392                    //if (!dec->low_delay && vop_type == P_VOP)
1393                    if (vop_type == P_VOP)
1394                            mb_swap(&dec->mbs, &dec->last_mbs);
1395            }
1396    
1397          emms();          emms();
1398    

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.32

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