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

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.42

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