[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.14, Fri May 3 00:41:22 2002 UTC revision 1.49.2.26, Wed Dec 17 17:07:38 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Decoder main module  -   *  - Decoder Module -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002      MinChen <chenm001@163.com>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002-2003 Peter Ross <pross@xvid.org>
  *  to use this software module in hardware or software products are  
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
8   *   *
9   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
10   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 26  Line 20 
20   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
21   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   *   *
  *************************************************************************/  
   
 /**************************************************************************  
  *  
  *  History:  
  *  
  *  02.05.2002  add B-frame decode support(have some problem);  
  *              MinChen <chenm001@163.com>  
  *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>  
  *  29.03.2002  interlacing fix - compensated block wasn't being used when  
  *              reconstructing blocks, thus artifacts  
  *              interlacing speedup - used transfers to re-interlace  
  *              interlaced decoding should be as fast as progressive now  
  *  26.03.2002  interlacing support - moved transfers outside decode loop  
  *  26.12.2001  decoder_mbinter: dequant/idct moved within if(coded) block  
  *  22.12.2001  lock based interpolation  
  *  01.12.2001  inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>  
  *  
23   *  $Id$   *  $Id$
24   *   *
25   *************************************************************************/   ****************************************************************************/
26    
27    #include <stdio.h>
28  #include <stdlib.h>  #include <stdlib.h>
29  #include <string.h>  #include <string.h>
30    
31    #ifdef BFRAMES_DEC_DEBUG
32            #define BFRAMES_DEC
33    #endif
34    
35  #include "xvid.h"  #include "xvid.h"
36  #include "portab.h"  #include "portab.h"
37    #include "global.h"
38    
39  #include "decoder.h"  #include "decoder.h"
40  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
41  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
42    
43  #include "quant/quant_h263.h"  #include "quant/quant.h"
44  #include "quant/quant_mpeg4.h"  #include "quant/quant_matrix.h"
45  #include "dct/idct.h"  #include "dct/idct.h"
46  #include "dct/fdct.h"  #include "dct/fdct.h"
47  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
48  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
49    #include "image/reduced.h"
50    #include "image/font.h"
51    
52  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
53  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
54  #include "utils/timer.h"  #include "utils/timer.h"
55  #include "utils/emms.h"  #include "utils/emms.h"
56    #include "motion/motion.h"
57    #include "motion/gmc.h"
58    
59  #include "image/image.h"  #include "image/image.h"
60  #include "image/colorspace.h"  #include "image/colorspace.h"
61    #include "image/postprocessing.h"
62  #include "utils/mem_align.h"  #include "utils/mem_align.h"
63    
64  int decoder_create(XVID_DEC_PARAM * param)  static int
65    decoder_resize(DECODER * dec)
66  {  {
67          DECODER * dec;          /* free existing */
68            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
69            image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
70            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
71            image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
72            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
73    
74          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
         if (dec == NULL)  
         {  
                 return XVID_ERR_MEMORY;  
         }  
         param->handle = dec;  
75    
76          dec->width = param->width;          if (dec->last_mbs)
77          dec->height = param->height;                  xvid_free(dec->last_mbs);
78            if (dec->mbs)
79                    xvid_free(dec->mbs);
80    
81            /* realloc */
82          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
83          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
84    
85          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
86          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
87    
88          if (image_create(&dec->cur, dec->edged_width, dec->edged_height))          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
         {  
89                  xvid_free(dec);                  xvid_free(dec);
90                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
91          }          }
92    
93          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height))          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {
         {  
94                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
95                  xvid_free(dec);                  xvid_free(dec);
96                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
97          }          }
98          // add by chenm001 <chenm001@163.com>  
99          // for support B-frame to reference last 2 frame          /* Support B-frame to reference last 2 frame */
100          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height))          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
         {  
101                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
102                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
103                  xvid_free(dec);                  xvid_free(dec);
104                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
105          }          }
106          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height))          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
         {  
107                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
108                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
109                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
# Line 124  Line 111 
111                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
112          }          }
113    
114          dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);          if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
         if (dec->mbs == NULL)  
         {  
115                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
116                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
117                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
118                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
119                  xvid_free(dec);                  xvid_free(dec);
120                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
121          }          }
122          // add by chenm001 <chenm001@163.com>  
123          // for skip MB flag          if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
124          dec->last_mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
125          if (dec->last_mbs == NULL)                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
126          {                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
127                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
128                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
129                    xvid_free(dec);
130                    return XVID_ERR_MEMORY;
131            }
132    
133            dec->mbs =
134                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
135                                            CACHE_LINE);
136            if (dec->mbs == NULL) {
137                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
138                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
139                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
140                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
141                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
142                    xvid_free(dec);
143                    return XVID_ERR_MEMORY;
144            }
145            memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
146    
147            /* For skip MB flag */
148            dec->last_mbs =
149                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
150                                            CACHE_LINE);
151            if (dec->last_mbs == NULL) {
152                  xvid_free(dec->mbs);                  xvid_free(dec->mbs);
153                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
154                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
155                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
156                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
157                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
158                    xvid_free(dec);
159                    return XVID_ERR_MEMORY;
160            }
161    
162            memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
163    
164            return 0;
165    }
166    
167    
168    int
169    decoder_create(xvid_dec_create_t * create)
170    {
171            DECODER *dec;
172    
173            if (XVID_VERSION_MAJOR(create->version) != 1)   /* v1.x.x */
174                    return XVID_ERR_VERSION;
175    
176            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
177            if (dec == NULL) {
178                    return XVID_ERR_MEMORY;
179            }
180    
181            memset(dec, 0, sizeof(DECODER));
182    
183            dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
184            if (dec->mpeg_quant_matrices == NULL) {
185                  xvid_free(dec);                  xvid_free(dec);
186                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
187          }          }
188    
189            create->handle = dec;
190    
191            dec->width = create->width;
192            dec->height = create->height;
193    
194            image_null(&dec->cur);
195            image_null(&dec->refn[0]);
196            image_null(&dec->refn[1]);
197            image_null(&dec->tmp);
198            image_null(&dec->qtmp);
199    
200            /* image based GMC */
201            image_null(&dec->gmc);
202    
203    
204            dec->mbs = NULL;
205            dec->last_mbs = NULL;
206    
207          init_timer();          init_timer();
208            init_postproc();
209            init_mpeg_matrix(dec->mpeg_quant_matrices);
210    
211          // add by chenm001 <chenm001@163.com>          /* For B-frame support (used to save reference frame's time */
212          // for support B-frame to save reference frame's time          dec->frames = 0;
         dec->frames = -1;  
213          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
214            dec->low_delay = 0;
215            dec->packed_mode = 0;
216    
217          return XVID_ERR_OK;          dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
218    
219            if (dec->fixed_dimensions)
220                    return decoder_resize(dec);
221            else
222                    return 0;
223  }  }
224    
225    
226  int decoder_destroy(DECODER * dec)  int
227    decoder_destroy(DECODER * dec)
228  {  {
229          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
230          xvid_free(dec->mbs);          xvid_free(dec->mbs);
231    
232            /* image based GMC */
233            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
234    
235          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
236          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
237          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
238            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
239          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
240            xvid_free(dec->mpeg_quant_matrices);
241          xvid_free(dec);          xvid_free(dec);
242    
243          write_timer();          write_timer();
244          return XVID_ERR_OK;          return 0;
245  }  }
246    
247    static const int32_t dquant_table[4] = {
   
 static const int32_t dquant_table[4] =  
 {  
248          -1, -2, 1, 2          -1, -2, 1, 2
249  };  };
250    
251    /* decode an intra macroblock */
252    static void
253    decoder_mbintra(DECODER * dec,
 // decode an intra macroblock  
   
 void decoder_mbintra(DECODER * dec,  
254                       MACROBLOCK * pMB,                       MACROBLOCK * pMB,
255                       const uint32_t x_pos,                       const uint32_t x_pos,
256                       const uint32_t y_pos,                       const uint32_t y_pos,
# Line 193  Line 258 
258                       const uint32_t cbp,                       const uint32_t cbp,
259                       Bitstream * bs,                       Bitstream * bs,
260                       const uint32_t quant,                       const uint32_t quant,
261                       const uint32_t intra_dc_threshold)                                  const uint32_t intra_dc_threshold,
262                                    const unsigned int bound,
263                                    const int reduced_resolution)
264  {  {
265    
266          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 206  Line 273 
273          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
274          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
275    
276            if (reduced_resolution) {
277                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
278                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
279                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
280            }else{
281          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
282          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
283          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
284            }
285    
286          memset(block, 0, 6*64*sizeof(int16_t));         // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
287    
288          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
289                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
290                  int16_t predictors[8];                  int16_t predictors[8];
291                  int start_coeff;                  int start_coeff;
292    
293                  start_timer();                  start_timer();
294                  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],
295                  if (!acpred_flag)                                           iQuant, iDcScaler, predictors, bound);
296                  {                  if (!acpred_flag) {
297                          pMB->acpred_directions[i] = 0;                          pMB->acpred_directions[i] = 0;
298                  }                  }
299                  stop_prediction_timer();                  stop_prediction_timer();
300    
301                  if (quant < intra_dc_threshold)                  if (quant < intra_dc_threshold) {
                 {  
302                          int dc_size;                          int dc_size;
303                          int dc_dif;                          int dc_dif;
304    
305                          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);
306                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;
307    
308                          if (dc_size > 8)                          if (dc_size > 8) {
309                          {                                  BitstreamSkip(bs, 1);   /* marker */
                                 BitstreamSkip(bs, 1);           // marker  
310                          }                          }
311    
312                          block[i*64 + 0] = dc_dif;                          block[i*64 + 0] = dc_dif;
313                          start_coeff = 1;                          start_coeff = 1;
314                  }  
315                  else                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);
316                  {                  } else {
317                          start_coeff = 0;                          start_coeff = 0;
318                  }                  }
319    
320                  start_timer();                  start_timer();
321                  if (cbp & (1 << (5-i)))                 // coded                  if (cbp & (1 << (5 - i)))       /* coded */
322                  {                  {
323                          get_intra_block(bs, &block[i*64], pMB->acpred_directions[i], start_coeff);                          int direction = dec->alternate_vertical_scan ?
324                                    2 : pMB->acpred_directions[i];
325    
326                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
327                  }                  }
328                  stop_coding_timer();                  stop_coding_timer();
329    
# Line 259  Line 332 
332                  stop_prediction_timer();                  stop_prediction_timer();
333    
334                  start_timer();                  start_timer();
335                  if (dec->quant_type == 0)                  if (dec->quant_type == 0) {
336                  {                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
337                          dequant_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);                  } else {
338                  }                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
                 else  
                 {  
                         dequant4_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);  
339                  }                  }
340                  stop_iquant_timer();                  stop_iquant_timer();
341    
342                  start_timer();                  start_timer();
343                  idct(&data[i*64]);                  idct(&data[i*64]);
344                  stop_idct_timer();                  stop_idct_timer();
345    
346          }          }
347    
348          if (dec->interlacing && pMB->field_dct)          if (dec->interlacing && pMB->field_dct) {
         {  
349                  next_block = stride;                  next_block = stride;
350                  stride *= 2;                  stride *= 2;
351          }          }
352    
353          start_timer();          start_timer();
354    
355            if (reduced_resolution)
356            {
357                    next_block*=2;
358                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
359                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
360                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
361                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
362                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
363                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
364            }else{
365          transfer_16to8copy(pY_Cur,                  &data[0*64], stride);          transfer_16to8copy(pY_Cur,                  &data[0*64], stride);
366          transfer_16to8copy(pY_Cur + 8,              &data[1*64], stride);          transfer_16to8copy(pY_Cur + 8,              &data[1*64], stride);
367          transfer_16to8copy(pY_Cur + next_block,     &data[2*64], stride);          transfer_16to8copy(pY_Cur + next_block,     &data[2*64], stride);
368          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3*64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3*64], stride);
369          transfer_16to8copy(pU_Cur,                  &data[4*64], stride2);          transfer_16to8copy(pU_Cur,                  &data[4*64], stride2);
370          transfer_16to8copy(pV_Cur,                  &data[5*64], stride2);          transfer_16to8copy(pV_Cur,                  &data[5*64], stride2);
371            }
372          stop_transfer_timer();          stop_transfer_timer();
373  }  }
374    
375    static void
376    decoder_mb_decode(DECODER * dec,
   
   
 #define SIGN(X) (((X)>0)?1:-1)  
 #define ABS(X) (((X)>0)?(X):-(X))  
 static const uint32_t roundtab[16] =  
 { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };  
   
   
 // decode an inter macroblock  
   
 void decoder_mbinter(DECODER * dec,  
                      const MACROBLOCK * pMB,  
                      const uint32_t x_pos,  
                      const uint32_t y_pos,  
                      const uint32_t acpred_flag,  
377                       const uint32_t cbp,                       const uint32_t cbp,
378                       Bitstream * bs,                       Bitstream * bs,
379                       const uint32_t quant,                                  uint8_t * pY_Cur,
380                       const uint32_t rounding)                                  uint8_t * pU_Cur,
381                                    uint8_t * pV_Cur,
382                                    const int reduced_resolution,
383                                    const MACROBLOCK * pMB)
384  {  {
385            DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE);
         DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);  
386          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
387    
388          uint32_t stride = dec->edged_width;          int stride = dec->edged_width;
389          uint32_t stride2 = stride / 2;          int next_block = stride * (reduced_resolution ? 16 : 8);
390          uint32_t next_block = stride * 8;          const int stride2 = stride/2;
391          uint32_t i;          int i;
392          uint32_t iQuant = pMB->quant;          const uint32_t iQuant = pMB->quant;
393          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          const int direction = dec->alternate_vertical_scan ? 2 : 0;
394          int uv_dx, uv_dy;          const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter;
   
         pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);  
         pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);  
         pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);  
   
         if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)  
         {  
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
   
                 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
         }  
         else  
         {  
                 int sum;  
                 sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
                 uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
395    
396                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;          for (i = 0; i < 6; i++) {
                 uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
         }  
397    
398          start_timer();                  if (cbp & (1 << (5 - i))) {     /* coded */
         interpolate8x8_switch(dec->cur.y, dec->refn[0].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 + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);  
         interpolate8x8_switch(dec->cur.y, dec->refn[0].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, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);  
         interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);  
         interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);  
         stop_comp_timer();  
399    
400          for (i = 0; i < 6; i++)                          memset(block, 0, 64 * sizeof(int16_t)); /* clear */
         {  
                 if (cbp & (1 << (5-i)))                 // coded  
                 {  
                         memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear  
401    
402                          start_timer();                          start_timer();
403                          get_inter_block(bs, &block[i*64]);                          get_inter_block(bs, block, direction);
404                          stop_coding_timer();                          stop_coding_timer();
405    
406                          start_timer();                          start_timer();
407                          if (dec->quant_type == 0)                          dequant(&data[i * 64], block, iQuant, dec->mpeg_quant_matrices);
                         {  
                                 dequant_inter(&data[i*64], &block[i*64], iQuant);  
                         }  
                         else  
                         {  
                                 dequant4_inter(&data[i*64], &block[i*64], iQuant);  
                         }  
408                          stop_iquant_timer();                          stop_iquant_timer();
409    
410                          start_timer();                          start_timer();
# Line 382  Line 413 
413                  }                  }
414          }          }
415    
416          if (dec->interlacing && pMB->field_dct)          if (dec->interlacing && pMB->field_dct) {
         {  
417                  next_block = stride;                  next_block = stride;
418                  stride *= 2;                  stride *= 2;
419          }          }
420    
421          start_timer();          start_timer();
422            if (reduced_resolution) {
423                    if (cbp & 32)
424                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
425                    if (cbp & 16)
426                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
427                    if (cbp & 8)
428                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
429                    if (cbp & 4)
430                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
431                    if (cbp & 2)
432                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
433                    if (cbp & 1)
434                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
435            } else {
436          if (cbp & 32)          if (cbp & 32)
437                  transfer_16to8add(pY_Cur,                  &data[0*64], stride);                  transfer_16to8add(pY_Cur,                  &data[0*64], stride);
438          if (cbp & 16)          if (cbp & 16)
# Line 401  Line 445 
445                  transfer_16to8add(pU_Cur,                  &data[4*64], stride2);                  transfer_16to8add(pU_Cur,                  &data[4*64], stride2);
446          if (cbp & 1)          if (cbp & 1)
447                  transfer_16to8add(pV_Cur,                  &data[5*64], stride2);                  transfer_16to8add(pV_Cur,                  &data[5*64], stride2);
448            }
449          stop_transfer_timer();          stop_transfer_timer();
450  }  }
451    
452    /* decode an inter macroblock */
453  void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold)  static void
454    decoder_mbinter(DECODER * dec,
455                                    const MACROBLOCK * pMB,
456                                    const uint32_t x_pos,
457                                    const uint32_t y_pos,
458                                    const uint32_t cbp,
459                                    Bitstream * bs,
460                                    const uint32_t rounding,
461                                    const int reduced_resolution,
462                                    const int ref)
463  {  {
464            uint32_t stride = dec->edged_width;
465            uint32_t stride2 = stride / 2;
466            uint32_t i;
467    
468          uint32_t x, y;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
469    
470          for (y = 0; y < dec->mb_height; y++)          int uv_dx, uv_dy;
471          {          VECTOR mv[4];   /* local copy of mvs */
                 for (x = 0; x < dec->mb_width; x++)  
                 {  
                         MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];  
472    
473                          uint32_t mcbpc;          if (reduced_resolution) {
474                          uint32_t cbpc;                  pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
475                          uint32_t acpred_flag;                  pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
476                          uint32_t cbpy;                  pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
477                          uint32_t cbp;                  for (i = 0; i < 4; i++) {
478                            mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
479                            mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
480                    }
481            } else {
482                    pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
483                    pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
484                    pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
485                    for (i = 0; i < 4; i++)
486                            mv[i] = pMB->mvs[i];
487            }
488    
489                          mcbpc = get_mcbpc_intra(bs);          start_timer();
                         mb->mode = mcbpc & 7;  
                         cbpc = (mcbpc >> 4);  
490    
491                          acpred_flag = BitstreamGetBit(bs);          if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */
492    
493                          if (mb->mode == MODE_STUFFING)                  uv_dx = mv[0].x;
494                          {                  uv_dy = mv[0].y;
495                                  DEBUG("-- STUFFING ?");                  if (dec->quarterpel) {
496                                  continue;                          uv_dx /= 2;
497                            uv_dy /= 2;
498                          }                          }
499                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
500                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
501    
502                          cbpy = get_cbpy(bs, 1);                  if (reduced_resolution)
503                          cbp = (cbpy << 2) | cbpc;                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
504                                                                            mv[0].x, mv[0].y, stride, rounding);
505                    else if (dec->quarterpel)
506                            interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
507                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
508                                                                                            mv[0].x, mv[0].y, stride, rounding);
509                    else
510                            interpolate16x16_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
511                                                                            mv[0].x, mv[0].y, stride, rounding);
512    
513                          if (mb->mode == MODE_INTRA_Q)          } else {        /* MODE_INTER4V */
                         {  
                                 quant += dquant_table[BitstreamGetBits(bs,2)];  
                                 if (quant > 31)  
                                 {  
                                         quant = 31;  
                                 }  
                                 else if (quant < 1)  
                                 {  
                                         quant = 1;  
                                 }  
                         }  
                         mb->quant = quant;  
514    
515                          if (dec->interlacing)                  if(dec->quarterpel) {
516                          {                          uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
517                                  mb->field_dct = BitstreamGetBit(bs);                          uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
518                                  DEBUG1("deci: field_dct: ", mb->field_dct);                  } else {
519                            uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
520                            uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
521                          }                          }
522    
523                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
524                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
525    
526                    if (reduced_resolution) {
527                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
528                                                                    mv[0].x, mv[0].y, stride, rounding);
529                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
530                                                                    mv[1].x, mv[1].y, stride, rounding);
531                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos, 32*y_pos + 16,
532                                                                    mv[2].x, mv[2].y, stride, rounding);
533                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos + 16,
534                                                                    mv[3].x, mv[3].y, stride, rounding);
535                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u , 16 * x_pos, 16 * y_pos,
536                                                                    uv_dx, uv_dy, stride2, rounding);
537                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,
538                                                                    uv_dx, uv_dy, stride2, rounding);
539    
540                    } else if (dec->quarterpel) {
541                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
542                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
543                                                                            mv[0].x, mv[0].y, stride, rounding);
544                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
545                                                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
546                                                                            mv[1].x, mv[1].y, stride, rounding);
547                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
548                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
549                                                                            mv[2].x, mv[2].y, stride, rounding);
550                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
551                                                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
552                                                                            mv[3].x, mv[3].y, stride, rounding);
553                    } else {
554                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos,
555                                                                    mv[0].x, mv[0].y, stride, rounding);
556                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,
557                                                                    mv[1].x, mv[1].y, stride, rounding);
558                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos + 8,
559                                                                    mv[2].x, mv[2].y, stride, rounding);
560                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,
561                                                                    mv[3].x, mv[3].y, stride, rounding);
562                  }                  }
563          }          }
564    
565            /* chroma */
566            if (reduced_resolution) {
567                    interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
568                                                                    uv_dx, uv_dy, stride2, rounding);
569                    interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
570                                                                    uv_dx, uv_dy, stride2, rounding);
571            } else {
572                    interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
573                                                                    uv_dx, uv_dy, stride2, rounding);
574                    interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
575                                                                    uv_dx, uv_dy, stride2, rounding);
576  }  }
577    
578            stop_comp_timer();
579    
580            if (cbp)
581                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur,
582                                                            reduced_resolution, pMB);
583    }
584    
585  void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode)  static void
586    decoder_mbgmc(DECODER * dec,
587                                    MACROBLOCK * const pMB,
588                                    const uint32_t x_pos,
589                                    const uint32_t y_pos,
590                                    const uint32_t fcode,
591                                    const uint32_t cbp,
592                                    Bitstream * bs,
593                                    const uint32_t rounding)
594  {  {
595            const uint32_t stride = dec->edged_width;
596            const uint32_t stride2 = stride / 2;
597    
598          int scale_fac = 1 << (fcode - 1);          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
599          int high = (32 * scale_fac) - 1;          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
600          int low = ((-32) * scale_fac);          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
         int range = (64 * scale_fac);  
601    
602          VECTOR pmv[4];          NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
         int32_t psad[4];  
603    
604          int mv_x, mv_y;          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
         int pmv_x, pmv_y;  
605    
606            start_timer();
607    
608          get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad);  /* this is where the calculations are done */
609    
610          pmv_x = pmv[0].x;          gmc_data->predict_16x16(gmc_data,
611          pmv_y = pmv[0].y;                          dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
612                            stride, stride, x_pos, y_pos, rounding);
613    
614          mv_x = get_mv(bs, fcode);          gmc_data->predict_8x8(gmc_data,
615          mv_y = get_mv(bs, fcode);                          dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,
616                            dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,
617                            stride2, stride2, x_pos, y_pos, rounding);
618    
619          mv_x += pmv_x;          gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);
         mv_y += pmv_y;  
620    
621          if (mv_x < low)          pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
622          {          pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
                 mv_x += range;  
         }  
         else if (mv_x > high)  
         {  
                 mv_x -= range;  
         }  
623    
624          if (mv_y < low)          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
         {  
                 mv_y += range;  
         }  
         else if (mv_y > high)  
         {  
                 mv_y -= range;  
         }  
625    
626          mv->x = mv_x;          stop_transfer_timer();
627          mv->y = mv_y;  
628            if (cbp)
629                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
630    
631  }  }
632    
633    
634  void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold)  static void
635    decoder_iframe(DECODER * dec,
636                                    Bitstream * bs,
637                                    int reduced_resolution,
638                                    int quant,
639                                    int intra_dc_threshold)
640  {  {
641            uint32_t bound;
642          uint32_t x, y;          uint32_t x, y;
643            uint32_t mb_width = dec->mb_width;
644            uint32_t mb_height = dec->mb_height;
645    
646          start_timer();          if (reduced_resolution) {
647          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);                  mb_width = (dec->width + 31) / 32;
648          stop_edges_timer();                  mb_height = (dec->height + 31) / 32;
649            }
650    
651          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];  
652    
653                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))                 // not_coded          for (y = 0; y < mb_height; y++) {
654                          if (!(BitstreamGetBit(bs)))                     // not_coded                  for (x = 0; x < mb_width; x++) {
655                          {                          MACROBLOCK *mb;
656                                  uint32_t mcbpc;                                  uint32_t mcbpc;
657                                  uint32_t cbpc;                                  uint32_t cbpc;
658                                  uint32_t acpred_flag;                                  uint32_t acpred_flag;
659                                  uint32_t cbpy;                                  uint32_t cbpy;
660                                  uint32_t cbp;                                  uint32_t cbp;
                                 uint32_t intra;  
   
                                 mcbpc = get_mcbpc_inter(bs);  
                                 mb->mode = mcbpc & 7;  
                                 cbpc = (mcbpc >> 4);  
                                 acpred_flag = 0;  
661    
662                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                          while (BitstreamShowBits(bs, 9) == 1)
663                                    BitstreamSkip(bs, 9);
664    
665                                  if (intra)                          if (check_resync_marker(bs, 0))
666                                  {                                  {
667                                          acpred_flag = BitstreamGetBit(bs);                                  bound = read_video_packet_header(bs, dec, 0,
668                                                            &quant, NULL, NULL, &intra_dc_threshold);
669                                    x = bound % mb_width;
670                                    y = bound / mb_width;
671                                  }                                  }
672                            mb = &dec->mbs[y * dec->mb_width + x];
673    
674                                  if (mb->mode == MODE_STUFFING)                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
                                 {  
                                         DEBUG("-- STUFFING ?");  
                                         continue;  
                                 }  
675    
676                                  cbpy = get_cbpy(bs, intra);                          mcbpc = get_mcbpc_intra(bs);
677                                  cbp = (cbpy << 2) | cbpc;                          mb->mode = mcbpc & 7;
678                            cbpc = (mcbpc >> 4);
679    
680                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q)                          acpred_flag = BitstreamGetBit(bs);
681                                  {  
682                            cbpy = get_cbpy(bs, 1);
683                            cbp = (cbpy << 2) | cbpc;
684    
685                            if (mb->mode == MODE_INTRA_Q) {
686                                          quant += dquant_table[BitstreamGetBits(bs,2)];                                          quant += dquant_table[BitstreamGetBits(bs,2)];
687                                          if (quant > 31)                                  if (quant > 31) {
                                         {  
688                                                  quant = 31;                                                  quant = 31;
689                                          }                                  } else if (quant < 1) {
                                         else if (mb->quant < 1)  
                                         {  
690                                                  quant = 1;                                                  quant = 1;
691                                          }                                          }
692                                  }                                  }
693                                  mb->quant = quant;                                  mb->quant = quant;
694                            mb->mvs[0].x = mb->mvs[0].y =
695                            mb->mvs[1].x = mb->mvs[1].y =
696                            mb->mvs[2].x = mb->mvs[2].y =
697                            mb->mvs[3].x = mb->mvs[3].y =0;
698    
699                                  if (dec->interlacing)                          if (dec->interlacing) {
                                 {  
700                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
701                                          DEBUG1("decp: field_dct: ", mb->field_dct);                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
702                            }
703    
704                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
705                                          {                                                          intra_dc_threshold, bound, reduced_resolution);
                                                 mb->field_pred = BitstreamGetBit(bs);  
                                                 DEBUG1("decp: field_pred: ", mb->field_pred);  
706    
                                                 if (mb->field_pred)  
                                                 {  
                                                         mb->field_for_top = BitstreamGetBit(bs);  
                                                         DEBUG1("decp: field_for_top: ", mb->field_for_top);  
                                                         mb->field_for_bot = BitstreamGetBit(bs);  
                                                         DEBUG1("decp: field_for_bot: ", mb->field_for_bot);  
                                                 }  
707                                          }                                          }
708                    if(dec->out_frm)
709                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
710                                  }                                  }
711    
                                 if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)  
                                 {  
                                         if (dec->interlacing && mb->field_pred)  
                                         {  
                                                 get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);  
                                                 get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode);  
712                                          }                                          }
713                                          else  
714                                          {  
715                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);  static void
716                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;  get_motion_vector(DECODER * dec,
717                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                  Bitstream * bs,
718                                          }                                  int x,
719                                  }                                  int y,
720                                  else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */)                                  int k,
721                                    VECTOR * ret_mv,
722                                    int fcode,
723                                    const int bound)
724                                  {                                  {
725                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);  
726                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);          const int scale_fac = 1 << (fcode - 1);
727                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);          const int high = (32 * scale_fac) - 1;
728                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);          const int low = ((-32) * scale_fac);
729            const int range = (64 * scale_fac);
730    
731            const VECTOR pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
732            VECTOR mv;
733    
734            mv.x = get_mv(bs, fcode);
735            mv.y = get_mv(bs, fcode);
736    
737            DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
738    
739            mv.x += pmv.x;
740            mv.y += pmv.y;
741    
742            if (mv.x < low) {
743                    mv.x += range;
744            } else if (mv.x > high) {
745                    mv.x -= range;
746                                  }                                  }
747                                  else  // MODE_INTRA, MODE_INTRA_Q  
748                                  {          if (mv.y < low) {
749                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                  mv.y += range;
750                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;          } else if (mv.y > high) {
751                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                  mv.y -= range;
                                         continue;  
752                                  }                                  }
753    
754                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, rounding);          ret_mv->x = mv.x;
755            ret_mv->y = mv.y;
756                          }                          }
                         else    // not coded  
                         {  
757    
758                                  mb->mode = MODE_NOT_CODED;  /* for P_VOP set gmc_warp to NULL */
759                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;  static void
760                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  decoder_pframe(DECODER * dec,
761                                    Bitstream * bs,
762                                    int rounding,
763                                    int reduced_resolution,
764                                    int quant,
765                                    int fcode,
766                                    int intra_dc_threshold,
767                                    const WARPPOINTS *const gmc_warp)
768    {
769            uint32_t x, y;
770            uint32_t bound;
771            int cp_mb, st_mb;
772            uint32_t mb_width = dec->mb_width;
773            uint32_t mb_height = dec->mb_height;
774    
775                                  // copy macroblock directly from ref to cur          if (reduced_resolution) {
776                    mb_width = (dec->width + 31) / 32;
777                    mb_height = (dec->height + 31) / 32;
778            }
779    
780                                  start_timer();                                  start_timer();
781            image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
782                                            dec->width, dec->height);
783            stop_edges_timer();
784    
785                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),          if (gmc_warp) {
786                                                   dec->refn[0].y + (16*y)*dec->edged_width + (16*x),                  /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
787                                                   dec->edged_width);                  generate_GMCparameters( dec->sprite_warping_points,
788                                    dec->sprite_warping_accuracy, gmc_warp,
789                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),                                  dec->width, dec->height, &dec->new_gmc_data);
                                                  dec->refn[0].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[0].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[0].y + (16*y+8)*dec->edged_width + (16*x+8),  
                                                  dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),  
                                                  dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),  
                                                  dec->edged_width/2);  
   
                                 transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),  
                                                  dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),  
                                                  dec->edged_width/2);  
790    
791                                  stop_transfer_timer();                  /* image warping is done block-based in decoder_mbgmc(), now */
                         }  
                 }  
792          }          }
793    
794            bound = 0;
795    
796            for (y = 0; y < mb_height; y++) {
797                    cp_mb = st_mb = 0;
798                    for (x = 0; x < mb_width; x++) {
799                            MACROBLOCK *mb;
800    
801                            /* skip stuffing */
802                            while (BitstreamShowBits(bs, 10) == 1)
803                                    BitstreamSkip(bs, 10);
804    
805                            if (check_resync_marker(bs, fcode - 1)) {
806                                    bound = read_video_packet_header(bs, dec, fcode - 1,
807                                            &quant, &fcode, NULL, &intra_dc_threshold);
808                                    x = bound % mb_width;
809                                    y = bound / mb_width;
810  }  }
811                            mb = &dec->mbs[y * dec->mb_width + x];
812    
813                            DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
814    
815  // add by MinChen <chenm001@163.com>                          if (!(BitstreamGetBit(bs)))     { /* block _is_ coded */
816  // decode B-frame motion vector                                  uint32_t mcbpc, cbpc, cbpy, cbp;
817  void get_b_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, VECTOR * mv, int fcode, const VECTOR pmv)                                  uint32_t intra, acpred_flag = 0;
818  {                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
         int scale_fac = 1 << (fcode - 1);  
         int high = (32 * scale_fac) - 1;  
         int low = ((-32) * scale_fac);  
         int range = (64 * scale_fac);  
   
         int mv_x, mv_y;  
         int pmv_x, pmv_y;  
819    
820          pmv_x = pmv.x;                                  cp_mb++;
821          pmv_y = pmv.y;                                  mcbpc = get_mcbpc_inter(bs);
822                                    mb->mode = mcbpc & 7;
823                                    cbpc = (mcbpc >> 4);
824    
825          mv_x = get_mv(bs, fcode);                                  DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
826          mv_y = get_mv(bs, fcode);                                  DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
827    
828          mv_x += pmv_x;                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
         mv_y += pmv_y;  
829    
830          if (mv_x < low)                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
831          {                                          mcsel = BitstreamGetBit(bs);
832                  mv_x += range;                                  else if (intra)
833                                            acpred_flag = BitstreamGetBit(bs);
834    
835                                    cbpy = get_cbpy(bs, intra);
836                                    DPRINTF(XVID_DEBUG_MB, "cbpy %i mcsel %i \n", cbpy,mcsel);
837    
838                                    cbp = (cbpy << 2) | cbpc;
839    
840                                    if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
841                                            int dquant = dquant_table[BitstreamGetBits(bs, 2)];
842                                            DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
843                                            quant += dquant;
844                                            if (quant > 31) {
845                                                    quant = 31;
846                                            } else if (quant < 1) {
847                                                    quant = 1;
848          }          }
849          else if (mv_x > high)                                          DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
         {  
                 mv_x -= range;  
850          }          }
851                                    mb->quant = quant;
852    
853          if (mv_y < low)                                  if (dec->interlacing) {
854          {                                          if ((cbp || intra) && !mcsel) {
855                  mv_y += range;                                                  mb->field_dct = BitstreamGetBit(bs);
856          }                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
         else if (mv_y > high)  
         {  
                 mv_y -= range;  
857          }          }
858    
859          mv->x = mv_x;                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
860          mv->y = mv_y;                                                  mb->field_pred = BitstreamGetBit(bs);
861                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
862    
863                                                    if (mb->field_pred) {
864                                                            mb->field_for_top = BitstreamGetBit(bs);
865                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
866                                                            mb->field_for_bot = BitstreamGetBit(bs);
867                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
868                                                    }
869                                            }
870  }  }
871    
872                                    if (mcsel) {
873                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, rounding);
874                                            continue;
875    
876  // add by MinChen <chenm001@163.com>                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
 // decode an B-frame forward & backward inter macroblock  
 void decoder_bf_mbinter(DECODER * dec,  
                      const MACROBLOCK * pMB,  
                      const uint32_t x_pos,  
                      const uint32_t y_pos,  
                      const uint32_t cbp,  
                      Bitstream * bs,  
                      const uint32_t quant,  
                          const uint8_t ref)  
 {  
877    
878          DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);                                          if (dec->interlacing && mb->field_pred) {
879          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
880                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode, bound);
881                                            } else {
882                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
883                                                    mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
884                                            }
885                                    } else if (mb->mode == MODE_INTER4V ) {
886                                            get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
887                                            get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
888                                            get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
889                                            get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
890                                    } else {                /* MODE_INTRA, MODE_INTRA_Q */
891                                            mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
892                                            mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =     0;
893                                            decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
894                                                                            intra_dc_threshold, bound, reduced_resolution);
895                                            continue;
896                                    }
897    
898          uint32_t stride = dec->edged_width;                                  decoder_mbinter(dec, mb, x, y, cbp, bs,
899          uint32_t stride2 = stride / 2;                                                                  rounding, reduced_resolution, 0);
         uint32_t next_block = stride * 8;  
         uint32_t i;  
         uint32_t iQuant = pMB->quant;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
         int uv_dx, uv_dy;  
900    
901          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
902          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                                  mb->mode = MODE_NOT_CODED_GMC;
903          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
904    
905                                    if(dec->out_frm && cp_mb > 0) {
906                                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
907                                            cp_mb = 0;
908                                    }
909                                    st_mb = x+1;
910                            } else {        /* not coded P_VOP macroblock */
911                                    mb->mode = MODE_NOT_CODED;
912    
913          if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
914          {                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
915                  uv_dx = pMB->mvs[0].x;  
916                  uv_dy = pMB->mvs[0].y;                                  decoder_mbinter(dec, mb, x, y, 0, bs,
917                                                                    rounding, reduced_resolution, 0);
918    
919                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                                  if(dec->out_frm && cp_mb > 0) {
920                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                                          output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
921                                            cp_mb = 0;
922                                    }
923                                    st_mb = x+1;
924                            }
925          }          }
         else  
         {  
                 int sum;  
                 sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
                 uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
926    
927                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  if(dec->out_frm && cp_mb > 0)
928                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                          output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
929            }
930          }          }
931    
         start_timer();  
         interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  0);  
         interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  0);  
         interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  0);  
         interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  0);  
         interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);  
         interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);  
         stop_comp_timer();  
932    
933          for (i = 0; i < 6; i++)  /* decode B-frame motion vector */
934          {  static void
935                  if (cbp & (1 << (5-i)))                 // coded  get_b_motion_vector(Bitstream * bs,
936                                            VECTOR * mv,
937                                            int fcode,
938                                            const VECTOR pmv)
939                  {                  {
940                          memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear          const int scale_fac = 1 << (fcode - 1);
941            const int high = (32 * scale_fac) - 1;
942            const int low = ((-32) * scale_fac);
943            const int range = (64 * scale_fac);
944    
945                          start_timer();          int mv_x = get_mv(bs, fcode);
946                          get_inter_block(bs, &block[i*64]);          int mv_y = get_mv(bs, fcode);
                         stop_coding_timer();  
947    
948                          start_timer();          mv_x += pmv.x;
949                          if (dec->quant_type == 0)          mv_y += pmv.y;
                         {  
                                 dequant_inter(&data[i*64], &block[i*64], iQuant);  
                         }  
                         else  
                         {  
                                 dequant4_inter(&data[i*64], &block[i*64], iQuant);  
                         }  
                         stop_iquant_timer();  
950    
951                          start_timer();          if (mv_x < low)
952                          idct(&data[i*64]);                  mv_x += range;
953                          stop_idct_timer();          else if (mv_x > high)
954                  }                  mv_x -= range;
         }  
955    
956          if (dec->interlacing && pMB->field_dct)          if (mv_y < low)
957          {                  mv_y += range;
958                  next_block = stride;          else if (mv_y > high)
959                  stride *= 2;                  mv_y -= range;
         }  
960    
961          start_timer();          mv->x = mv_x;
962          if (cbp & 32)          mv->y = mv_y;
                 transfer_16to8add(pY_Cur,                  &data[0*64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8,              &data[1*64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block,     &data[2*64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur,                  &data[4*64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur,                  &data[5*64], stride2);  
         stop_transfer_timer();  
963  }  }
964    
965    /* decode an B-frame direct & interpolate macroblock */
966  // add by MinChen <chenm001@163.com>  static void
967  // decode an B-frame direct &  inter macroblock  decoder_bf_interpolate_mbinter(DECODER * dec,
968  void decoder_bf_interpolate_mbinter(DECODER * dec,                                                                  IMAGE forward,
969                           IMAGE forward, IMAGE backward,                                                                  IMAGE backward,
970                       const MACROBLOCK * pMB,                       const MACROBLOCK * pMB,
971                       const uint32_t x_pos,                       const uint32_t x_pos,
972                       const uint32_t y_pos,                       const uint32_t y_pos,
973                       Bitstream * bs)                                                                  Bitstream * bs,
974                                                                    const int direct)
975  {  {
   
         DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
976          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
977          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * 8;  
         uint32_t iQuant = pMB->quant;  
978          int uv_dx, uv_dy;          int uv_dx, uv_dy;
979          int b_uv_dx, b_uv_dy;          int b_uv_dx, b_uv_dy;
980            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
981            const uint32_t cbp = pMB->cbp;
982    
983          if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
984          {          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
985            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
986    
987            if (!direct) {
988                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
989                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
990    
                 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
   
991                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
992                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
993    
994                  b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel) {
995                  b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                          uv_dx /= 2;
996                            uv_dy /= 2;
997                            b_uv_dx /= 2;
998                            b_uv_dy /= 2;
999          }          }
         else  
         {  
                 int sum;  
                 sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
                 uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
1000    
1001                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1002                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1003    
1004                  sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1005                  b_uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1006    
1007                  sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;          } else {
1008                  b_uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                  if(dec->quarterpel) {
1009                            uv_dx = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1010                            uv_dy = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1011                            b_uv_dx = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);
1012                            b_uv_dy = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);
1013                    } else {
1014                            uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1015                            uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1016                            b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1017                            b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1018          }          }
1019    
1020                    uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
1021                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
1022                    b_uv_dx = (b_uv_dx >> 3) + roundtab_76[b_uv_dx & 0xf];
1023                    b_uv_dy = (b_uv_dy >> 3) + roundtab_76[b_uv_dy & 0xf];
1024            }
1025    
1026          start_timer();          start_timer();
1027            if(dec->quarterpel) {
1028                    if(!direct) {
1029                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1030                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1031                                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1032                    } else {
1033                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1034                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1035                                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1036                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1037                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1038                                                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1039                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1040                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1041                                                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1042                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1043                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1044                                                                                    pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1045                    }
1046            } else {
1047                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1048                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1049                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
1050                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1051                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1052                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1053                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1054                                                            16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1055            }
1056    
1057            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1058                                                    uv_dy, stride2, 0);
1059            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1060                                                    uv_dy, stride2, 0);
1061    
1062    
1063            if(dec->quarterpel) {
1064                    if(!direct) {
1065                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1066                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1067                                                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1068                    } else {
1069                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1070                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1071                                                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1072                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1073                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1074                                                                                    pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1075                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1076                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1077                                                                                    pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1078                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1079                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1080                                                                                    pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1081                    }
1082            } else {
1083                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1084                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1085                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1086                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1087                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1088                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1089                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1090                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1091            }
1092    
1093            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1094                                                    b_uv_dx, b_uv_dy, stride2, 0);
1095            interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1096                                                    b_uv_dx, b_uv_dy, stride2, 0);
1097    
1098            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1099                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1100                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1101                                                    stride, 1, 8);
1102    
1103            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1104                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1105                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1106                                                    stride, 1, 8);
1107    
1108            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1109                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1110                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1111                                                    stride, 1, 8);
1112    
1113            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1114                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1115                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1116                                                    stride, 1, 8);
1117    
1118            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1119                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1120                                                    dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1121                                                    stride2, 1, 8);
1122    
1123            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1124                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1125                                                    dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1126                                                    stride2, 1, 8);
1127    
         interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  0);  
         interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  0);  
         interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  0);  
         interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  0);  
         interpolate8x8_switch(dec->cur.u, forward.u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);  
         interpolate8x8_switch(dec->cur.v, forward.v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);  
   
         interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos,     16*y_pos    , pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride,  0);  
         interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos + 8, 16*y_pos    , pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,  0);  
         interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos,     16*y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride,  0);  
         interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos + 8, 16*y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride,  0);  
         interpolate8x8_switch(dec->refn[2].u, backward.u, 8*x_pos,      8*y_pos,      b_uv_dx,         b_uv_dy,         stride2, 0);  
         interpolate8x8_switch(dec->refn[2].v, backward.v, 8*x_pos,      8*y_pos,      b_uv_dx,         b_uv_dy,         stride2, 0);  
   
         interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos,     16*y_pos    , stride);  
         interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos    , stride);  
         interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos,     16*y_pos + 8, stride);  
         interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos + 8, stride);  
         interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8*x_pos,      8*y_pos,      stride2);  
         interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8*x_pos,      8*y_pos,      stride2);  
   
 /*  
         interpolate8x8_c(dec->cur.y, backward.y, 16*x_pos,     16*y_pos    , pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride);  
         interpolate8x8_c(dec->cur.y, backward.y, 16*x_pos + 8, 16*y_pos    , pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride);  
         interpolate8x8_c(dec->cur.y, backward.y, 16*x_pos,     16*y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride);  
         interpolate8x8_c(dec->cur.y, backward.y, 16*x_pos + 8, 16*y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride);  
         interpolate8x8_c(dec->cur.u, backward.u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2);  
         interpolate8x8_c(dec->cur.v, backward.v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2);  
 */  
1128          stop_comp_timer();          stop_comp_timer();
1129    
1130            if (cbp)
1131                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
1132  }  }
1133    
1134    /* for decode B-frame dbquant */
1135  // add by MinChen <chenm001@163.com>  static __inline int32_t
1136  // for decode B-frame dbquant  get_dbquant(Bitstream * bs)
 int32_t __inline get_dbquant(Bitstream * bs)  
1137  {  {
1138          if (!BitstreamGetBit(bs))               // '0'          if (!BitstreamGetBit(bs))               /*  '0' */
1139                  return(0);                  return(0);
1140          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs))  /* '10' */
1141                  return(-2);                  return(-2);
1142          else          else                                                    /* '11' */
1143                  return(2);                                      // '11'                  return (2);
1144  }  }
1145    
1146  // add by MinChen <chenm001@163.com>  /*
1147  // for decode B-frame mb_type   * decode B-frame mb_type
1148  // bit   ret_value   * bit          ret_value
1149  // 1        0   * 1            0
1150  // 01       1   * 01           1
1151  // 001      2   * 001          2
1152  // 0001     3   * 0001         3
1153  int32_t __inline get_mbtype(Bitstream * bs)   */
1154  {  static int32_t __inline
1155          int32_t mb_type=0;  get_mbtype(Bitstream * bs)
1156          for(;mb_type<=3 && (!BitstreamGetBit(bs));mb_type++);  {
1157          if (mb_type<=3)          int32_t mb_type;
1158    
1159            for (mb_type = 0; mb_type <= 3; mb_type++)
1160                    if (BitstreamGetBit(bs))
1161                  return(mb_type);                  return(mb_type);
1162          else  
1163                  return(-1);          return -1;
1164  }  }
1165    
1166  void decoder_bframe(DECODER * dec, Bitstream * bs, int quant, int fcode_forward, int fcode_backward)  static void
1167    decoder_bframe(DECODER * dec,
1168                                    Bitstream * bs,
1169                                    int quant,
1170                                    int fcode_forward,
1171                                    int fcode_backward)
1172  {  {
   
1173          uint32_t        x, y;          uint32_t        x, y;
1174          VECTOR          mv, zeromv;          VECTOR mv;
1175            const VECTOR zeromv = {0,0};
1176            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1177            int i;
1178    
1179          start_timer();          start_timer();
1180          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1181          //image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);                                          dec->width, dec->height);
1182            image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1183                                            dec->width, dec->height);
1184          stop_edges_timer();          stop_edges_timer();
1185    
1186          for (y = 0; y < dec->mb_height; y++)          for (y = 0; y < dec->mb_height; y++) {
1187          {                  /* Initialize Pred Motion Vector */
1188                  for (x = 0; x < dec->mb_width; x++)                  dec->p_fmv = dec->p_bmv = zeromv;
1189                  {                  for (x = 0; x < dec->mb_width; x++) {
1190                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];
1191                          MACROBLOCK * last_mb = &dec->last_mbs[y*dec->mb_width + x];                          MACROBLOCK * last_mb = &dec->last_mbs[y*dec->mb_width + x];
1192                            const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;
1193                            uint32_t intra_dc_threshold; /* fake variable */
1194    
1195                          // the last P_VOP is skip macroblock ?                          if (check_resync_marker(bs, fcode_max  - 1)) {
1196                                    int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,
1197                                                                                                             &fcode_forward, &fcode_backward, &intra_dc_threshold);
1198                                    x = bound % dec->mb_width;
1199                                    y = bound / dec->mb_width;
1200                                    /* reset predicted macroblocks */
1201                                    dec->p_fmv = dec->p_bmv = zeromv;
1202                            }
1203    
1204                          if (last_mb->mode == MODE_NOT_CODED){                          mv =
1205                                  DEBUG("Skip MB in B-frame!");                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1206                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1207                            mb->quant = quant;
1208    
1209                                  // copy macroblock directly from ref to cur                          /*
1210                                  start_timer();                           * skip if the co-located P_VOP macroblock is not coded
1211                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),                           * if not codec in co-located S_VOP macroblock is _not_
1212                                                   dec->refn[0].y + (16*y)*dec->edged_width + (16*x),                           * automatically skipped
1213                                                   dec->edged_width);                           */
                                 transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),  
                                                  dec->refn[0].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[0].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[0].y + (16*y+8)*dec->edged_width + (16*x+8),  
                                                  dec->edged_width);  
                                 transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),  
                                                  dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),  
                                                  dec->edged_width/2);  
                                 transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),  
                                                  dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),  
                                                  dec->edged_width/2);  
                                 stop_transfer_timer();  
1214    
1215                                  DEBUG("skip MB in B-frame!");                          if (last_mb->mode == MODE_NOT_CODED) {
1216                                    mb->cbp = 0;
1217                                    mb->mode = MODE_FORWARD;
1218                                    decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
1219                                  continue;                                  continue;
1220                          }                          }
1221    
1222                          if (!BitstreamGetBit(bs)){      // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1223                                  uint8_t modb2=BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1224    
1225                                  mb->mb_type = get_mbtype(bs);                                  mb->mode = get_mbtype(bs);
1226    
1227                                  if (!modb2){    // modb=='00'                                  if (!modb2)             /* modb=='00' */
1228                                          mb->cbp = BitstreamGetBits(bs,6);                                          mb->cbp = BitstreamGetBits(bs,6);
1229                                  } else {                                  else
1230                                          mb->cbp = 0;                                          mb->cbp = 0;
                                 }  
                                 if (mb->mb_type && mb->cbp){  
                                         quant += get_dbquant(bs);  
1231    
1232                                    if (mb->mode && mb->cbp) {
1233                                            quant += get_dbquant(bs);
1234                                          if (quant > 31)                                          if (quant > 31)
                                         {  
1235                                                  quant = 31;                                                  quant = 31;
1236                                          }                                          else if (quant < 1)
                                         else if (mb->quant < 1)  
                                         {  
1237                                                  quant = 1;                                                  quant = 1;
1238                                          }                                          }
                                 }  
1239                                  mb->quant = quant;                                  mb->quant = quant;
1240    
1241                                    if (dec->interlacing) {
1242                                            if (mb->cbp) {
1243                                                    mb->field_dct = BitstreamGetBit(bs);
1244                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1245                                            }
1246    
1247                                            if (mb->mode) {
1248                                                    mb->field_pred = BitstreamGetBit(bs);
1249                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1250    
1251                                                    if (mb->field_pred) {
1252                                                            mb->field_for_top = BitstreamGetBit(bs);
1253                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1254                                                            mb->field_for_bot = BitstreamGetBit(bs);
1255                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1256                                                    }
1257                                            }
1258                                    }
1259    
1260                          } else {                          } else {
1261                                  mb->mb_type = MODE_DIRECT_NONE_MV;                                  mb->mode = MODE_DIRECT_NONE_MV;
1262                                  mb->cbp = 0;                                  mb->cbp = 0;
1263                          }                          }
1264    
1265                          mb->mb_type = MODE_INTER;                          switch (mb->mode) {
                         mb->mvs[0].x=mb->mvs[0].y=zeromv.x = zeromv.y = mv.x = mv.y = 0;  
                         //DEBUG1("Switch bm_type=",mb->mb_type);  
   
                         switch(mb->mb_type)  
                         {  
1266                          case MODE_DIRECT:                          case MODE_DIRECT:
1267                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv);                                  get_b_motion_vector(bs, &mv, 1, zeromv);
1268    
1269                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
                                 {       // Because this file is a C file not C++ so I use '{' to define var  
                                         const int32_t   TRB=dec->time_pp-dec->time_bp,  
                                                                         TRD=dec->time_pp;  
                                         int i;  
1270                                          for(i=0;i<4;i++){                                          for(i=0;i<4;i++){
1271                                                  mb->mvs[i].x    = (TRB * last_mb->mvs[i].x)/TRD+mb->mvs[0].x;                                          mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + mv.x);
1272                                                  mb->b_mvs[i].x  = (mb->mvs[0].x==0)?((TRB-TRD)*last_mb->mvs[i].x)/TRD:mb->mvs[i].x-last_mb->mvs[i].x;                                          mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1273                                                  mb->mvs[i].y    = (TRB * last_mb->mvs[i].y)/TRD+mb->mvs[0].y;                                                                          ? ((TRB - TRD) * last_mb->mvs[i].x) / TRD
1274                                                  mb->b_mvs[i].y  = (mb->mvs[0].y==0)?((TRB-TRD)*last_mb->mvs[i].y)/TRD:mb->mvs[i].y-last_mb->mvs[i].y;                                                                          : mb->mvs[i].x - last_mb->mvs[i].x);
1275                                            mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y);
1276                                                  //DEBUG2("B-direct MVF=",mb->mvs[i].x,mb->mvs[i].y);                                          mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1277                                                  //DEBUG2("B-direct MVB=",mb->b_mvs[i].x,mb->b_mvs[i].y);                                                                          ? ((TRB - TRD) * last_mb->mvs[i].y) / TRD
1278                                                                            : mb->mvs[i].y - last_mb->mvs[i].y);
1279                                          }                                          }
1280                                          //DEBUG("B-frame Direct!\n");  
1281                                  }                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1282                                  mb->mode = MODE_INTER4V;                                                                                                  mb, x, y, bs, 1);
                                 decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, bs);  
1283                                  break;                                  break;
1284    
1285                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1286                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);
1287                                  dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1288                                  dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;  
1289                                    get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv);
1290                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], fcode_backward, dec->p_bmv);                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];
1291                                  dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = mb->b_mvs[3].x = mb->b_mvs[0].x;  
1292                                  dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = mb->b_mvs[3].y = mb->b_mvs[0].y;                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1293                                                                                            mb, x, y, bs, 0);
                                 mb->mode = MODE_INTER4V;  
                                 decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, bs);  
                                 //DEBUG("B-frame Bidir!\n");  
1294                                  break;                                  break;
1295    
1296                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1297                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, dec->p_bmv);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv);
1298                                  dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1299                                  dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;  
1300                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);
                                 //DEBUG("B-frame Backward!\n");  
1301                                  break;                                  break;
1302    
1303                          case MODE_FORWARD:                          case MODE_FORWARD:
1304                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);
1305                                  dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1306                                  dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;  
1307                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
                                 DEBUG("B-frame Forward!\n");  
1308                                  break;                                  break;
1309    
1310                          default:                          default:
1311                                  DEBUG1("Not support B-frame mb_type =",mb->mb_type);                                  DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);
1312                          }                          }
1313                    } /* End of for */
                 }       // end of FOR  
1314          }          }
1315  }  }
1316    
1317  // swap two MACROBLOCK array  /* perform post processing if necessary, and output the image */
1318  void mb_swap(MACROBLOCK *mb1, MACROBLOCK *mb2)  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1319                                            xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)
1320  {  {
1321          MACROBLOCK *temp=mb1;          if (frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV) && mbs != NULL)     /* post process */
1322          mb1=mb2;          {
1323          mb2=temp;                  /* note: image is stored to tmp */
1324                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1325                    image_postproc(&dec->tmp, dec->edged_width,
1326                                               mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1327                                               frame->general, dec->frames);
1328                    img = &dec->tmp;
1329            }
1330    
1331            image_output(img, dec->width, dec->height,
1332                                     dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1333                                     frame->output.csp, dec->interlacing);
1334    
1335            if (stats) {
1336                    stats->type = coding2type(coding_type);
1337                    stats->data.vop.time_base = (int)dec->time_base;
1338                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1339            }
1340  }  }
1341    
1342  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)  
1343    int
1344    decoder_decode(DECODER * dec,
1345                                    xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1346  {  {
1347    
1348          Bitstream bs;          Bitstream bs;
1349          uint32_t rounding;          uint32_t rounding;
1350            uint32_t reduced_resolution;
1351          uint32_t quant;          uint32_t quant;
1352          uint32_t fcode_forward;          uint32_t fcode_forward;
1353          uint32_t fcode_backward;          uint32_t fcode_backward;
1354          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1355          uint32_t vop_type;          WARPPOINTS gmc_warp;
1356            int coding_type;
1357            int success, output, seen_something;
1358    
1359            if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1360                    return XVID_ERR_VERSION;
1361    
1362          start_global_timer();          start_global_timer();
1363    
1364          BitstreamInit(&bs, frame->bitstream, frame->length);          dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1365            if ((frame->general & XVID_DISCONTINUITY))
1366                    dec->frames = 0;
1367            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1368    
1369            if (frame->length < 0) {        /* decoder flush */
1370                    int ret;
1371                    /* if not decoding "low_delay/packed", and this isn't low_delay and
1372                            we have a reference frame, then outout the reference frame */
1373                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1374                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1375                            dec->frames = 0;
1376                            ret = 0;
1377                    } else {
1378                            if (stats) stats->type = XVID_TYPE_NOTHING;
1379                            ret = XVID_ERR_END;
1380                    }
1381    
1382          // add by chenm001 <chenm001@163.com>                  emms();
1383          // for support B-frame to reference last 2 frame                  stop_global_timer();
1384          dec->frames ++;                  return ret;
1385          vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold);          }
1386    
1387            BitstreamInit(&bs, frame->bitstream, frame->length);
1388    
1389          dec->p_bmv.x=dec->p_bmv.y=dec->p_fmv.y=dec->p_fmv.y=0;          // init pred vector to 0          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1390          switch (vop_type)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1391          {          {
1392          case P_VOP :                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1393                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold);                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1394                  DEBUG1("P_VOP  Time=",dec->time);                  if (stats) stats->type = XVID_TYPE_NOTHING;
1395                  break;                  emms();
1396                    return 1;       /* one byte consumed */
1397            }
1398    
1399          case I_VOP :          success = 0;
1400                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);          output = 0;
1401                  DEBUG1("I_VOP  Time=",dec->time);          seen_something = 0;
                 break;  
1402    
1403          case B_VOP :  repeat:
1404                  if (dec->time_pp > dec->time_bp){  
1405                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);          coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1406                          DEBUG1("B_VOP  Time=",dec->time);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1407                  } else {  
1408                          DEBUG("broken B-frame!");          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",
1409                                                            coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1410    
1411            if (coding_type == -1) { /* nothing */
1412                    if (success) goto done;
1413                    if (stats) stats->type = XVID_TYPE_NOTHING;
1414                    emms();
1415                    return BitstreamPos(&bs)/8;
1416                  }                  }
                 break;  
1417    
1418          case N_VOP :    // vop not coded          if (coding_type == -2 || coding_type == -3) {   /* vol and/or resize */
                 break;  
1419    
1420          default :                  if (coding_type == -3)
1421                  return XVID_ERR_FAIL;                          decoder_resize(dec);
1422    
1423                    if (stats) {
1424                            stats->type = XVID_TYPE_VOL;
1425                            stats->data.vol.general = 0;
1426                            /*XXX: if (dec->interlacing)
1427                                    stats->data.vol.general |= ++INTERLACING; */
1428                            stats->data.vol.width = dec->width;
1429                            stats->data.vol.height = dec->height;
1430                            stats->data.vol.par = dec->aspect_ratio;
1431                            stats->data.vol.par_width = dec->par_width;
1432                            stats->data.vol.par_height = dec->par_height;
1433                            emms();
1434                            return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1435                    }
1436                    goto repeat;
1437          }          }
1438    
1439          frame->length = BitstreamPos(&bs) / 8;          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1440    
1441          if (dec->frames >= 1){          /* packed_mode: special-N_VOP treament */
1442                  start_timer();          if (dec->packed_mode && coding_type == N_VOP) {
1443                  if ((vop_type == I_VOP || vop_type == P_VOP))                  if (dec->low_delay_default && dec->frames > 0) {
1444                  {                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1445                          image_output(&dec->refn[1], dec->width, dec->height, dec->edged_width,                          output = 1;
1446                                       frame->image, frame->stride, frame->colorspace);                  }
1447                  } else if (vop_type == B_VOP) {                  /* ignore otherwise */
1448                          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,          } else if (coding_type != B_VOP) {
1449                                       frame->image, frame->stride, frame->colorspace);                  switch(coding_type) {
1450                    case I_VOP :
1451                            decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1452                            break;
1453                    case P_VOP :
1454                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1455                                                    fcode_forward, intra_dc_threshold, NULL);
1456                            break;
1457                    case S_VOP :
1458                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1459                                                    fcode_forward, intra_dc_threshold, &gmc_warp);
1460                            break;
1461                    case N_VOP :
1462                            /* XXX: not_coded vops are not used for forward prediction */
1463                            /* we should not swap(last_mbs,mbs) */
1464                            image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1465                            SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */
1466                            break;
1467                    }
1468    
1469                    if (reduced_resolution) {
1470                            image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1471                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1472                                    16, 0);
1473                    }
1474    
1475                    /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1476                    if (!(dec->low_delay_default && dec->packed_mode)) {
1477                            if (dec->low_delay) {
1478                                    decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1479                                    output = 1;
1480                            } else if (dec->frames > 0)     { /* is the reference frame valid? */
1481                                    /* output the reference frame */
1482                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1483                                    output = 1;
1484                  }                  }
                 stop_conv_timer();  
1485          }          }
1486          if (vop_type==I_VOP || vop_type==P_VOP){  
1487                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1488                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1489                  // swap MACROBLOCK                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1490                  mb_swap(dec->mbs, dec->last_mbs);                  dec->last_reduced_resolution = reduced_resolution;
1491                    dec->last_coding_type = coding_type;
1492    
1493                    dec->frames++;
1494                    seen_something = 1;
1495    
1496            } else {        /* B_VOP */
1497    
1498                    if (dec->low_delay) {
1499                            DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1500                            dec->low_delay = 1;
1501                    }
1502    
1503                    if (dec->frames < 2) {
1504                            /* attemping to decode a bvop without atleast 2 reference frames */
1505                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1506                                                    "broken b-frame, mising ref frames");
1507                            stats->type = XVID_TYPE_NOTHING;
1508                    } else if (dec->time_pp <= dec->time_bp) {
1509                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1510                            decoded in vfw. */
1511                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1512                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1513                            stats->type = XVID_TYPE_NOTHING;
1514                    } else {
1515                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1516                            decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1517          }          }
1518    
1519          emms();                  output = 1;
1520                    dec->frames++;
1521            }
1522    
1523          stop_global_timer();          BitstreamByteAlign(&bs);
1524    
1525            /* low_delay_default mode: repeat in packed_mode */
1526            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
1527                    success = 1;
1528                    goto repeat;
1529            }
1530    
1531    done :
1532    
1533            /* low_delay_default mode: if we've gotten here without outputting anything,
1534               then output the recently decoded frame, or print an error message  */
1535            if (dec->low_delay_default && output == 0) {
1536                    if (dec->packed_mode && seen_something) {
1537                            /* output the recently decoded frame */
1538                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1539                    } else {
1540                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1541                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1542                                    "warning: nothing to output");
1543                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1544                                    "bframe decoder lag");
1545    
1546          return XVID_ERR_OK;                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);
1547                            if (stats) stats->type = XVID_TYPE_NOTHING;
1548                    }
1549            }
1550    
1551            emms();
1552            stop_global_timer();
1553    
1554            return BitstreamPos(&bs) / 8;   /* number of bytes consumed */
1555  }  }

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.49.2.26

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