[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.46, Tue Feb 11 21:56:31 2003 UTC revision 1.49.2.15, Tue Oct 7 13:02:35 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   *  Copyright(C) 2002 MinChen <chenm001@163.com>   *  Copyright(C) 2002 MinChen <chenm001@163.com>
7   *               2002 Peter Ross <pross@xvid.org>   *               2002-2003 Peter Ross <pross@xvid.org>
8   *   *
9   *  This file is part of XviD, a free MPEG-4 video encoder/decoder   *  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
  *  XviD is free software; you can redistribute it and/or modify it  
  *  under the terms of the GNU General Public License as published by  
11   *  the Free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation; either version 2 of the License, or
12   *  (at your option) any later version.   *  (at your option) any later version.
13   *   *
# Line 22  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   *   *
  *  Under section 8 of the GNU General Public License, the copyright  
  *  holders of XVID explicitly forbid distribution in the following  
  *  countries:  
  *  
  *    - Japan  
  *    - United States of America  
  *  
  *  Linking XviD statically or dynamically with other modules is making a  
  *  combined work based on XviD.  Thus, the terms and conditions of the  
  *  GNU General Public License cover the whole combination.  
  *  
  *  As a special exception, the copyright holders of XviD give you  
  *  permission to link XviD with independent modules that communicate with  
  *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the  
  *  license terms of these independent modules, and to copy and distribute  
  *  the resulting combined work under terms of your choice, provided that  
  *  every copy of the combined work is accompanied by a complete copy of  
  *  the source code of XviD (the version of XviD used to produce the  
  *  combined work), being distributed under the terms of the GNU General  
  *  Public License plus this exception.  An independent module is a module  
  *  which is not derived from or based on XviD.  
  *  
  *  Note that people who make modified versions of XviD are not obligated  
  *  to grant this special exception for their modified versions; it is  
  *  their choice whether to do so.  The GNU General Public License gives  
  *  permission to release a modified version without this exception; this  
  *  exception also makes it possible to release a modified version which  
  *  carries forward this exception.  
  *  
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    
# Line 64  Line 34 
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"
 #include "quant/quant_mpeg4.h"  
44  #include "dct/idct.h"  #include "dct/idct.h"
45  #include "dct/fdct.h"  #include "dct/fdct.h"
46  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
47  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
48    #include "image/reduced.h"
49    #include "image/font.h"
50    
51  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
52  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
53  #include "utils/timer.h"  #include "utils/timer.h"
54  #include "utils/emms.h"  #include "utils/emms.h"
55    #include "motion/motion.h"
56    #include "motion/gmc.h"
57    
58  #include "image/image.h"  #include "image/image.h"
59  #include "image/colorspace.h"  #include "image/colorspace.h"
60  #include "utils/mem_align.h"  #include "utils/mem_align.h"
61    
62  int  int
63  decoder_create(XVID_DEC_PARAM * param)  decoder_resize(DECODER * dec)
64  {  {
65          DECODER *dec;          /* free existing */
66            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
67            image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
68            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
69            image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
70            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
71    
72          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;  
73    
74          dec->width = param->width;          if (dec->last_mbs)
75          dec->height = param->height;                  xvid_free(dec->last_mbs);
76            if (dec->mbs)
77                    xvid_free(dec->mbs);
78    
79            /* realloc */
80          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
81          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
82    
83          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
84          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
         dec->low_delay = 0;  
85    
86          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
87                  xvid_free(dec);                  xvid_free(dec);
# Line 116  Line 93 
93                  xvid_free(dec);                  xvid_free(dec);
94                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
95          }          }
96          /* add by chenm001 <chenm001@163.com> */  
97          /* for support B-frame to reference last 2 frame */          /* Support B-frame to reference last 2 frame */
98          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
99                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
100                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
101                  xvid_free(dec);                  xvid_free(dec);
102                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
103          }          }
104          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
105                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
106                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
107                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
108                    xvid_free(dec);
109                    return XVID_ERR_MEMORY;
110            }
111    
112            if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
113                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
114                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
115                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
116                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
117                    xvid_free(dec);
118                    return XVID_ERR_MEMORY;
119            }
120    
121            if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
122                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
123                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
124                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
125                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
126                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
127                  xvid_free(dec);                  xvid_free(dec);
128                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
129          }          }
# Line 139  Line 135 
135                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
136                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
137                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
138                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
139                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
140                  xvid_free(dec);                  xvid_free(dec);
141                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
142          }          }
   
143          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
144    
145          /* add by chenm001 <chenm001@163.com> */          /* For skip MB flag */
         /* for skip MB flag */  
146          dec->last_mbs =          dec->last_mbs =
147                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
148                                          CACHE_LINE);                                          CACHE_LINE);
# Line 156  Line 151 
151                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
152                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
153                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
154                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
155                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
156                  xvid_free(dec);                  xvid_free(dec);
157                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
158          }          }
159    
160          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
161    
162            return 0;
163    }
164    
165    
166    int
167    decoder_create(xvid_dec_create_t * create)
168    {
169            DECODER *dec;
170    
171            if (XVID_VERSION_MAJOR(create->version) != 1)   /* v1.x.x */
172                    return XVID_ERR_VERSION;
173    
174            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
175            if (dec == NULL) {
176                    return XVID_ERR_MEMORY;
177            }
178            memset(dec, 0, sizeof(DECODER));
179    
180            create->handle = dec;
181    
182            dec->width = create->width;
183            dec->height = create->height;
184    
185            image_null(&dec->cur);
186            image_null(&dec->refn[0]);
187            image_null(&dec->refn[1]);
188            image_null(&dec->tmp);
189            image_null(&dec->qtmp);
190    
191            /* image based GMC */
192            image_null(&dec->gmc);
193    
194    
195            dec->mbs = NULL;
196            dec->last_mbs = NULL;
197    
198          init_timer();          init_timer();
199    
200          /* add by chenm001 <chenm001@163.com> */          /* For B-frame support (used to save reference frame's time */
201          /* for support B-frame to save reference frame's time */          dec->frames = 0;
         dec->frames = -1;  
202          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
203            dec->low_delay = 0;
204            dec->packed_mode = 0;
205    
206            dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
207    
208          return XVID_ERR_OK;          if (dec->fixed_dimensions)
209                    return decoder_resize(dec);
210            else
211                    return 0;
212  }  }
213    
214    
# Line 179  Line 217 
217  {  {
218          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
219          xvid_free(dec->mbs);          xvid_free(dec->mbs);
220    
221            /* image based GMC */
222            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
223    
224          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
225          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
226          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
227            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
228          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
229          xvid_free(dec);          xvid_free(dec);
230    
231          write_timer();          write_timer();
232          return XVID_ERR_OK;          return 0;
233  }  }
234    
235    
# Line 199  Line 242 
242    
243    
244  /* decode an intra macroblock */  /* decode an intra macroblock */
   
245  void  void
246  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
247                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
# Line 210  Line 252 
252                                  Bitstream * bs,                                  Bitstream * bs,
253                                  const uint32_t quant,                                  const uint32_t quant,
254                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
255                                  const unsigned int bound)                                  const unsigned int bound,
256                                    const int reduced_resolution)
257  {  {
258    
259          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 223  Line 266 
266          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
267          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
268    
269            if (reduced_resolution) {
270                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
271                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
272                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
273            }else{
274          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
275          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
276          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
277            }
278    
279          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
280    
# Line 256  Line 305 
305                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
306                          start_coeff = 1;                          start_coeff = 1;
307    
308                          DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);
309                  } else {                  } else {
310                          start_coeff = 0;                          start_coeff = 0;
311                  }                  }
# Line 264  Line 313 
313                  start_timer();                  start_timer();
314                  if (cbp & (1 << (5 - i)))       /* coded */                  if (cbp & (1 << (5 - i)))       /* coded */
315                  {                  {
316                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],                          int direction = dec->alternate_vertical_scan ?
317                                                          start_coeff);                                  2 : pMB->acpred_directions[i];
318    
319                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
320                  }                  }
321                  stop_coding_timer();                  stop_coding_timer();
322    
# Line 275  Line 326 
326    
327                  start_timer();                  start_timer();
328                  if (dec->quant_type == 0) {                  if (dec->quant_type == 0) {
329                          dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
330                  } else {                  } else {
331                          dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
332                  }                  }
333                  stop_iquant_timer();                  stop_iquant_timer();
334    
335                  start_timer();                  start_timer();
336                  idct(&data[i * 64]);                  idct(&data[i * 64]);
337                  stop_idct_timer();                  stop_idct_timer();
338    
339          }          }
340    
341          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 292  Line 344 
344          }          }
345    
346          start_timer();          start_timer();
347    
348            if (reduced_resolution)
349            {
350                    next_block*=2;
351                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
352                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
353                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
354                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
355                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
356                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
357            }else{
358          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
359          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
360          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
361          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
362          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
363          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
364            }
365          stop_transfer_timer();          stop_transfer_timer();
366  }  }
367    
368    
369    
370    
   
 #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 };  
   
   
371  /* decode an inter macroblock */  /* decode an inter macroblock */
   
372  void  void
373  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
374                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
375                                  const uint32_t x_pos,                                  const uint32_t x_pos,
376                                  const uint32_t y_pos,                                  const uint32_t y_pos,
377                                  const uint32_t acpred_flag,                                  const uint32_t fcode,
378                                  const uint32_t cbp,                                  const uint32_t cbp,
379                                  Bitstream * bs,                                  Bitstream * bs,
380                                  const uint32_t quant,                                  const uint32_t quant,
381                                  const uint32_t rounding)                                  const uint32_t rounding,
382                                    const int reduced_resolution)
383  {  {
384    
385          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 330  Line 387 
387    
388          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
389          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
390          uint32_t next_block = stride * 8;          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
391          uint32_t i;          uint32_t i;
392          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
393          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
394    
395          int uv_dx, uv_dy;          int uv_dx, uv_dy;
396            VECTOR mv[4];   /* local copy of mvs */
397    
398            if (reduced_resolution) {
399                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
400                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
401                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
402                    for (i = 0; i < 4; i++) {
403                            mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
404                            mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
405                    }
406            } else {
407          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
408          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
409          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
410                    for (i = 0; i < 4; i++)
411                            mv[i] = pMB->mvs[i];
412            }
413    
414          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
415    
416                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  uv_dx = mv[0].x / (1 + dec->quarterpel);
417                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  uv_dy = mv[0].y / (1 + dec->quarterpel);
         } else {  
                 int sum;  
                 sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
418    
419                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
420                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
421    
422                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  start_timer();
423                    if (reduced_resolution)
424                    {
425                            interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
426                                                                      mv[0].x, mv[0].y, stride,  rounding);
427                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
428                                                                      uv_dx, uv_dy, stride2, rounding);
429                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
430                                                                      uv_dx, uv_dy, stride2, rounding);
431    
432                    }
433                    else
434                    {
435                            if(dec->quarterpel) {
436                                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
437                                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
438                                                                                            mv[0].x, mv[0].y, stride, rounding);
439                            }
440                            else {
441                                    interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
442                                                                              mv[0].x, mv[0].y, stride, rounding);
443                            }
444    
445                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
446                                                                      uv_dx, uv_dy, stride2, rounding);
447                            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
448                                                                      uv_dx, uv_dy, stride2, rounding);
449          }          }
450                    stop_comp_timer();
451    
452            } else {        /* MODE_INTER4V */
453                    int sum;
454    
455                    if(dec->quarterpel)
456                            sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
457                    else
458                            sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
459    
460                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
461    
462                    if(dec->quarterpel)
463                            sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
464                    else
465                            sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
466    
467                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
468    
469          start_timer();          start_timer();
470                    if (reduced_resolution)
471                    {
472                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
473                                                                      mv[0].x, mv[0].y, stride,  rounding);
474                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
475                                                                      mv[1].x, mv[1].y, stride,  rounding);
476                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos, 32*y_pos + 16,
477                                                                      mv[2].x, mv[2].y, stride,  rounding);
478                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos + 16,
479                                                                      mv[3].x, mv[3].y, stride,  rounding);
480                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u , 16 * x_pos, 16 * y_pos,
481                                                                      uv_dx, uv_dy, stride2, rounding);
482                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,
483                                                                      uv_dx, uv_dy, stride2, rounding);
484    
485                            /* set_block(pY_Cur, stride, 32, 32, 127); */
486                    }
487                    else
488                    {
489                            if(dec->quarterpel) {
490                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
491                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
492                                                                                      mv[0].x, mv[0].y, stride,  rounding);
493                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
494                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
495                                                                                      mv[1].x, mv[1].y, stride,  rounding);
496                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
497                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
498                                                                                      mv[2].x, mv[2].y, stride,  rounding);
499                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
500                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
501                                                                                      mv[3].x, mv[3].y, stride,  rounding);
502                            }
503                            else {
504          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
505                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
506          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
507                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                                                                            mv[1].x, mv[1].y, stride,  rounding);
508          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
509                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);                                                                            mv[2].x, mv[2].y, stride,  rounding);
510          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,
511                                                    pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
512                            }
513    
514          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
515                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
516          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
517                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
518                    }
519          stop_comp_timer();          stop_comp_timer();
520            }
521    
522          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
523                    int direction = dec->alternate_vertical_scan ? 2 : 0;
524    
525                  if (cbp & (1 << (5 - i)))       /* coded */                  if (cbp & (1 << (5 - i)))       /* coded */
526                  {                  {
527                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
528    
529                          start_timer();                          start_timer();
530                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
531                          stop_coding_timer();                          stop_coding_timer();
532    
533                          start_timer();                          start_timer();
534                          if (dec->quant_type == 0) {                          if (dec->quant_type == 0) {
535                                  dequant_inter(&data[i * 64], &block[i * 64], iQuant);                                  dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
536                          } else {                          } else {
537                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);                                  dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
538                          }                          }
539                          stop_iquant_timer();                          stop_iquant_timer();
540    
# Line 401  Line 550 
550          }          }
551    
552          start_timer();          start_timer();
553            if (reduced_resolution)
554            {
555                    if (cbp & 32)
556                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
557                    if (cbp & 16)
558                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
559                    if (cbp & 8)
560                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
561                    if (cbp & 4)
562                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
563                    if (cbp & 2)
564                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
565                    if (cbp & 1)
566                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
567            }
568            else
569            {
570                    if (cbp & 32)
571                            transfer_16to8add(pY_Cur, &data[0 * 64], stride);
572                    if (cbp & 16)
573                            transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
574                    if (cbp & 8)
575                            transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
576                    if (cbp & 4)
577                            transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
578                    if (cbp & 2)
579                            transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
580                    if (cbp & 1)
581                            transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
582            }
583            stop_transfer_timer();
584    }
585    
586    
587    static void
588    decoder_mbgmc(DECODER * dec,
589                                    MACROBLOCK * const pMB,
590                                    const uint32_t x_pos,
591                                    const uint32_t y_pos,
592                                    const uint32_t fcode,
593                                    const uint32_t cbp,
594                                    Bitstream * bs,
595                                    const uint32_t quant,
596                                    const uint32_t rounding,
597                                    const int reduced_resolution)   /* no reduced res support */
598    {
599    
600            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
601            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
602    
603            const uint32_t stride = dec->edged_width;
604            const uint32_t stride2 = stride / 2;
605            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
606            uint32_t i;
607            const uint32_t iQuant = pMB->quant;
608            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
609            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
610            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
611    
612            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
613    
614            start_timer();
615    
616    /* this is where the calculations are done */
617    
618            {       NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
619    
620                            gmc_data->predict_16x16(gmc_data,
621                                            dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
622                                            stride, stride, x_pos, y_pos, rounding);
623    
624                            gmc_data->predict_8x8(gmc_data,
625                                            dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,
626                                            dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,
627                                            stride2, stride2, x_pos, y_pos, rounding);
628    
629                            gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);
630    
631                    pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
632                    pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
633            }
634            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
635    
636    /*
637            transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
638            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
639            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
640    */
641    
642    
643            stop_transfer_timer();
644    
645            if (!cbp) return;
646    
647            for (i = 0; i < 6; i++) {
648                    int direction = dec->alternate_vertical_scan ? 2 : 0;
649    
650                    if (cbp & (1 << (5 - i)))       /* coded */
651                    {
652                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
653    
654                            start_timer();
655                            get_inter_block(bs, &block[i * 64], direction);
656                            stop_coding_timer();
657    
658                            start_timer();
659                            if (dec->quant_type == 0) {
660                                    dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
661                            } else {
662                                    dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
663                            }
664                            stop_iquant_timer();
665    
666                            start_timer();
667                            idct(&data[i * 64]);
668                            stop_idct_timer();
669                    }
670            }
671    
672    /* interlace + GMC is this possible ??? */
673    /*
674      if (dec->interlacing && pMB->field_dct) {
675              next_block = stride;
676              stride *= 2;
677      }
678    */
679            start_timer();
680          if (cbp & 32)          if (cbp & 32)
681                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);
682          if (cbp & 16)          if (cbp & 16)
# Line 420  Line 696 
696  void  void
697  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
698                             Bitstream * bs,                             Bitstream * bs,
699                               int reduced_resolution,
700                             int quant,                             int quant,
701                             int intra_dc_threshold)                             int intra_dc_threshold)
702  {  {
703          uint32_t bound;          uint32_t bound;
704          uint32_t x, y;          uint32_t x, y;
705            uint32_t mb_width = dec->mb_width;
706            uint32_t mb_height = dec->mb_height;
707    
708            if (reduced_resolution)
709            {
710                    mb_width = (dec->width + 31) / 32;
711                    mb_height = (dec->height + 31) / 32;
712            }
713    
714          bound = 0;          bound = 0;
715    
716          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
717                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
718                          MACROBLOCK *mb;                          MACROBLOCK *mb;
719                          uint32_t mcbpc;                          uint32_t mcbpc;
720                          uint32_t cbpc;                          uint32_t cbpc;
# Line 442  Line 727 
727    
728                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
729                          {                          {
730                                  bound = read_video_packet_header(bs, 0, &quant);                                  bound = read_video_packet_header(bs, dec, 0,
731                                  x = bound % dec->mb_width;                                                          &quant, NULL, NULL, &intra_dc_threshold);
732                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
733                                    y = bound / mb_width;
734                          }                          }
735                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
736    
737                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
738    
739                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
740                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
# Line 475  Line 761 
761    
762                          if (dec->interlacing) {                          if (dec->interlacing) {
763                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
764                                  DPRINTF(DPRINTF_DEBUG, "deci: field_dct: %d", mb->field_dct);                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
765                          }                          }
766    
767                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
768                                                          intra_dc_threshold, bound);                                                          intra_dc_threshold, bound, reduced_resolution);
769    
770                  }                  }
771                  if(dec->out_frm)                  if(dec->out_frm)
772                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
   
773          }          }
774    
775  }  }
# Line 495  Line 781 
781                                    int x,                                    int x,
782                                    int y,                                    int y,
783                                    int k,                                    int k,
784                                    VECTOR * mv,                                    VECTOR * ret_mv,
785                                    int fcode,                                    int fcode,
786                                    const int bound)                                    const int bound)
787  {  {
# Line 506  Line 792 
792          int range = (64 * scale_fac);          int range = (64 * scale_fac);
793    
794          VECTOR pmv;          VECTOR pmv;
795          int mv_x, mv_y;          VECTOR mv;
796    
797          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
798    
799          mv_x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
800          mv_y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
801    
802          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);          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);
803    
804          mv_x += pmv.x;          mv.x += pmv.x;
805          mv_y += pmv.y;          mv.y += pmv.y;
806    
807          if (mv_x < low) {          if (mv.x < low) {
808                  mv_x += range;                  mv.x += range;
809          } else if (mv_x > high) {          } else if (mv.x > high) {
810                  mv_x -= range;                  mv.x -= range;
811          }          }
812    
813          if (mv_y < low) {          if (mv.y < low) {
814                  mv_y += range;                  mv.y += range;
815          } else if (mv_y > high) {          } else if (mv.y > high) {
816                  mv_y -= range;                  mv.y -= range;
817          }          }
818    
819          mv->x = mv_x;          ret_mv->x = mv.x;
820          mv->y = mv_y;          ret_mv->y = mv.y;
   
821  }  }
822    
823    
824    
825    
826    
827    /* for P_VOP set gmc_warp to NULL */
828  void  void
829  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
830                             Bitstream * bs,                             Bitstream * bs,
831                             int rounding,                             int rounding,
832                               int reduced_resolution,
833                             int quant,                             int quant,
834                             int fcode,                             int fcode,
835                             int intra_dc_threshold)                             int intra_dc_threshold,
836                               const WARPPOINTS *const gmc_warp)
837  {  {
838    
839          uint32_t x, y;          uint32_t x, y;
840          uint32_t bound;          uint32_t bound;
841          int cp_mb, st_mb;          int cp_mb, st_mb;
842            uint32_t mb_width = dec->mb_width;
843            uint32_t mb_height = dec->mb_height;
844    
845            if (reduced_resolution)
846            {
847                    mb_width = (dec->width + 31) / 32;
848                    mb_height = (dec->height + 31) / 32;
849            }
850    
851          start_timer();          start_timer();
852          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
853                                     dec->width, dec->height);                                     dec->width, dec->height);
854          stop_edges_timer();          stop_edges_timer();
855    
856            if (gmc_warp)
857            {
858    
859                    /* accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16 */
860    /*              {
861                            fprintf(stderr,"GMC parameters acc=%d(-> 1/%d), %d pts!!!\n",
862                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
863                                    dec->sprite_warping_points);
864                    }*/
865    
866                    generate_GMCparameters( dec->sprite_warping_points,
867                                    dec->sprite_warping_accuracy, gmc_warp,
868                                    dec->width, dec->height, &dec->new_gmc_data);
869    
870    /* image warping is done block-based  in decoder_mbgmc(), now */
871            }
872    
873          bound = 0;          bound = 0;
874    
875          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
876                  cp_mb = st_mb = 0;                  cp_mb = st_mb = 0;
877                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
878                          MACROBLOCK *mb;                          MACROBLOCK *mb;
879    
880                          /* skip stuffing */                          /* skip stuffing */
# Line 567  Line 883 
883    
884                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1))
885                          {                          {
886                                  bound = read_video_packet_header(bs, fcode - 1, &quant);                                  bound = read_video_packet_header(bs, dec, fcode - 1,
887                                  x = bound % dec->mb_width;                                          &quant, &fcode, NULL, &intra_dc_threshold);
888                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
889                                    y = bound / mb_width;
890                          }                          }
891                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
892    
893                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
894    
895                          /*if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))          not_coded */                          /* if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) */ /* not_coded */
896                          if (!(BitstreamGetBit(bs)))     /* not_coded */                          if (!(BitstreamGetBit(bs)))     /* block _is_ coded */
897                          {                          {
898                                  uint32_t mcbpc;                                  uint32_t mcbpc;
899                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 584  Line 901 
901                                  uint32_t cbpy;                                  uint32_t cbpy;
902                                  uint32_t cbp;                                  uint32_t cbp;
903                                  uint32_t intra;                                  uint32_t intra;
904                                    int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
905    
906                                  cp_mb++;                                  cp_mb++;
907                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
908                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
909                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
910    
911                                  DPRINTF(DPRINTF_MB, "mode %i", mb->mode);                                  DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
912                                  DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);                                  DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
913                                  acpred_flag = 0;                                  acpred_flag = 0;
914    
915                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
916    
917                                  if (intra) {                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
918                                            mcsel = BitstreamGetBit(bs);
919    
920                                    if (intra)
921                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
                                 }  
922    
923                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
924                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(XVID_DEBUG_MB, "cbpy %i  mcsel %i \n", cbpy,mcsel);
925    
926                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
927    
928                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
929                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
930                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
931                                          quant += dquant;                                          quant += dquant;
932                                          if (quant > 31) {                                          if (quant > 31) {
933                                                  quant = 31;                                                  quant = 31;
934                                          } else if (quant < 1) {                                          } else if (quant < 1) {
935                                                  quant = 1;                                                  quant = 1;
936                                          }                                          }
937                                          DPRINTF(DPRINTF_MB, "quant %i", quant);                                          DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
938                                  }                                  }
939                                  mb->quant = quant;                                  mb->quant = quant;
940    
941                                  if (dec->interlacing) {                                  if (dec->interlacing) {
942                                          if (cbp || intra) {                                          if ((cbp || intra) && !mcsel) {
943                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
944                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_dct: %d", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
945                                          }                                          }
946    
947                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
948                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
949                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_pred: %d", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
950    
951                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
952                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
953                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_top: %d", mb->field_for_top);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
954                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
955                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_bot: %d", mb->field_for_bot);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
956                                                  }                                                  }
957                                          }                                          }
958                                  }                                  }
959    
960                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
961                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
962                                                                    rounding, reduced_resolution);
963                                            continue;
964    
965                                    } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
966    
967                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
968                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
969                                                                                    fcode, bound);                                                                                    fcode, bound);
# Line 646  Line 972 
972                                          } else {                                          } else {
973                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
974                                                                                    fcode, bound);                                                                                    fcode, bound);
975                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
                                                         mb->mvs[0].x;  
                                                 mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                                         mb->mvs[0].y;  
976                                          }                                          }
977                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
978    
# Line 664  Line 987 
987                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
988                                                  0;                                                  0;
989                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
990                                                                          intra_dc_threshold, bound);                                                                          intra_dc_threshold, bound, reduced_resolution);
991                                          continue;                                          continue;
992                                  }                                  }
993    
994                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
995                                                                  rounding);                                                                  rounding, reduced_resolution);
                         } else                          /* not coded */  
                         {  
                                 DPRINTF(DPRINTF_DEBUG, "P-frame MB at (X,Y)=(%d,%d)", x, y);  
   
                                 mb->mode = MODE_NOT_CODED;  
                                 mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;  
                                 mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  
996    
997                                  /* copy macroblock directly from ref to cur */                          }
998                            else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
999                            {
1000                                    mb->mode = MODE_NOT_CODED_GMC;
1001    
1002                                  start_timer();                                  start_timer();
1003    
1004                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1005                                                                   (16 * x),                                                                  rounding, reduced_resolution);
                                                                  dec->refn[0].y + (16 * y) * dec->edged_width +  
                                                                  (16 * x), 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);  
1006    
                                 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);  
1007                                  stop_transfer_timer();                                  stop_transfer_timer();
1008    
1009                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
1010                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1011                                    cp_mb = 0;                                    cp_mb = 0;
1012                                  }                                  }
1013                                  st_mb = x+1;                                  st_mb = x+1;
1014                          }                          }
1015                  }                          else    /* not coded P_VOP macroblock */
                 if(dec->out_frm && cp_mb > 0)  
                   output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);  
         }  
 }  
   
 /* swap two MACROBLOCK array */  
 void  
 mb_swap(MACROBLOCK ** mb1,  
                 MACROBLOCK ** mb2)  
1016  {  {
1017          MACROBLOCK *temp = *mb1;                                  mb->mode = MODE_NOT_CODED;
1018    
1019          *mb1 = *mb2;                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
1020          *mb2 = temp;                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
1021  }                                  /* copy macroblock directly from ref to cur */
1022    
1023  int                                  start_timer();
1024  decoder_decode(DECODER * dec,  
1025                             XVID_DEC_FRAME * frame)                                  if (reduced_resolution)
1026                                    {
1027                                            transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
1028                                                                             dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
1029                                                                             dec->edged_width);
1030    
1031                                            transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
1032                                                                            dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),
1033                                                                            dec->edged_width/2);
1034    
1035                                            transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),
1036                                                                             dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
1037                                                                             dec->edged_width/2);
1038                                    }
1039                                    else
1040                                    {
1041                                            transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
1042                                                                             dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
1043                                                                             dec->edged_width);
1044    
1045                                            transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
1046                                                                            dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
1047                                                                            dec->edged_width/2);
1048    
1049                                            transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
1050                                                                             dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
1051                                                                             dec->edged_width/2);
1052                                    }
1053    
1054                                    stop_transfer_timer();
1055    
1056                                    if(dec->out_frm && cp_mb > 0) {
1057                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1058                                      cp_mb = 0;
1059                                    }
1060                                    st_mb = x+1;
1061                            }
1062                    }
1063                    if(dec->out_frm && cp_mb > 0)
1064                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1065            }
1066    }
1067    
1068    
1069    /* decode B-frame motion vector */
1070    void
1071    get_b_motion_vector(DECODER * dec,
1072                                            Bitstream * bs,
1073                                            int x,
1074                                            int y,
1075                                            VECTOR * mv,
1076                                            int fcode,
1077                                            const VECTOR pmv)
1078    {
1079            int scale_fac = 1 << (fcode - 1);
1080            int high = (32 * scale_fac) - 1;
1081            int low = ((-32) * scale_fac);
1082            int range = (64 * scale_fac);
1083    
1084            int mv_x, mv_y;
1085            int pmv_x, pmv_y;
1086    
1087            pmv_x = pmv.x;
1088            pmv_y = pmv.y;
1089    
1090            mv_x = get_mv(bs, fcode);
1091            mv_y = get_mv(bs, fcode);
1092    
1093            mv_x += pmv_x;
1094            mv_y += pmv_y;
1095    
1096            if (mv_x < low) {
1097                    mv_x += range;
1098            } else if (mv_x > high) {
1099                    mv_x -= range;
1100            }
1101    
1102            if (mv_y < low) {
1103                    mv_y += range;
1104            } else if (mv_y > high) {
1105                    mv_y -= range;
1106            }
1107    
1108            mv->x = mv_x;
1109            mv->y = mv_y;
1110    }
1111    
1112    
1113    /* decode an B-frame forward & backward inter macroblock */
1114    void
1115    decoder_bf_mbinter(DECODER * dec,
1116                                       const MACROBLOCK * pMB,
1117                                       const uint32_t x_pos,
1118                                       const uint32_t y_pos,
1119                                       const uint32_t cbp,
1120                                       Bitstream * bs,
1121                                       const uint32_t quant,
1122                                       const uint8_t ref)
1123    {
1124    
1125            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1126            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1127    
1128            uint32_t stride = dec->edged_width;
1129            uint32_t stride2 = stride / 2;
1130            uint32_t next_block = stride * 8;
1131            uint32_t i;
1132            uint32_t iQuant = pMB->quant;
1133            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1134            int uv_dx, uv_dy;
1135    
1136            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1137            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1138            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1139    
1140    
1141            if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1142                    uv_dx = pMB->mvs[0].x;
1143                    uv_dy = pMB->mvs[0].y;
1144    
1145                    if (dec->quarterpel)
1146                    {
1147                            uv_dx /= 2;
1148                            uv_dy /= 2;
1149                    }
1150    
1151                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1152                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1153            } else {
1154                    int sum;
1155    
1156                    if(dec->quarterpel)
1157                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1158                    else
1159                            sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1160    
1161                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1162    
1163                    if(dec->quarterpel)
1164                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1165                    else
1166                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1167    
1168                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1169            }
1170    
1171            start_timer();
1172            if(dec->quarterpel) {
1173                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1174                                                                        dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1175                                                                        pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1176            }
1177            else {
1178                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
1179                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1180                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,
1181                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1182                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,
1183                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1184                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1185                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1186            }
1187    
1188            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
1189                                                      uv_dx, uv_dy, stride2, 0);
1190            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
1191                                                      uv_dx, uv_dy, stride2, 0);
1192            stop_comp_timer();
1193    
1194            for (i = 0; i < 6; i++) {
1195                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1196    
1197                    if (cbp & (1 << (5 - i)))       /* coded */
1198                    {
1199                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1200    
1201                            start_timer();
1202                            get_inter_block(bs, &block[i * 64], direction);
1203                            stop_coding_timer();
1204    
1205                            start_timer();
1206                            if (dec->quant_type == 0) {
1207                                    dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
1208                            } else {
1209                                    dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
1210                            }
1211                            stop_iquant_timer();
1212    
1213                            start_timer();
1214                            idct(&data[i * 64]);
1215                            stop_idct_timer();
1216                    }
1217            }
1218    
1219            if (dec->interlacing && pMB->field_dct) {
1220                    next_block = stride;
1221                    stride *= 2;
1222            }
1223    
1224            start_timer();
1225            if (cbp & 32)
1226                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1227            if (cbp & 16)
1228                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1229            if (cbp & 8)
1230                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1231            if (cbp & 4)
1232                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1233            if (cbp & 2)
1234                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1235            if (cbp & 1)
1236                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1237            stop_transfer_timer();
1238    }
1239    
1240    /* decode an B-frame direct &  inter macroblock */
1241    void
1242    decoder_bf_interpolate_mbinter(DECODER * dec,
1243                                                               IMAGE forward,
1244                                                               IMAGE backward,
1245                                                               const MACROBLOCK * pMB,
1246                                                               const uint32_t x_pos,
1247                                                               const uint32_t y_pos,
1248                                                               Bitstream * bs)
1249    {
1250    
1251            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1252            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1253    
1254            uint32_t stride = dec->edged_width;
1255            uint32_t stride2 = stride / 2;
1256            uint32_t next_block = stride * 8;
1257            uint32_t iQuant = pMB->quant;
1258            int uv_dx, uv_dy;
1259            int b_uv_dx, b_uv_dy;
1260            uint32_t i;
1261            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1262        const uint32_t cbp = pMB->cbp;
1263    
1264            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1265            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1266            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1267    
1268    
1269            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1270                    uv_dx = pMB->mvs[0].x;
1271                    uv_dy = pMB->mvs[0].y;
1272    
1273                    b_uv_dx = pMB->b_mvs[0].x;
1274                    b_uv_dy = pMB->b_mvs[0].y;
1275    
1276                    if (dec->quarterpel)
1277                    {
1278                            uv_dx /= 2;
1279                            uv_dy /= 2;
1280    
1281                            b_uv_dx /= 2;
1282                            b_uv_dy /= 2;
1283                    }
1284    
1285                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1286                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1287    
1288                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1289                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1290            } else {
1291                    int sum;
1292    
1293                    if(dec->quarterpel)
1294                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1295                    else
1296                            sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1297    
1298                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1299    
1300                    if(dec->quarterpel)
1301                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1302                    else
1303                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1304    
1305                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1306    
1307    
1308                    if(dec->quarterpel)
1309                            sum = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);
1310                    else
1311                            sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1312    
1313                    b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1314    
1315                    if(dec->quarterpel)
1316                            sum = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);
1317                    else
1318                            sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1319    
1320                    b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1321            }
1322    
1323    
1324            start_timer();
1325            if(dec->quarterpel) {
1326                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1327                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1328                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1329                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1330                    else {
1331                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1332                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1333                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1334                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1335                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1336                                                                                pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1337                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1338                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1339                                                                                pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1340                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1341                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1342                                                                                pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1343                    }
1344            }
1345            else {
1346                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1347                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1348                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
1349                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1350                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1351                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1352                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1353                                                              16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1354                                                              0);
1355            }
1356    
1357            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1358                                                      uv_dy, stride2, 0);
1359            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1360                                                      uv_dy, stride2, 0);
1361    
1362    
1363            if(dec->quarterpel) {
1364                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1365                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1366                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1367                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1368                    else {
1369                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1370                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1371                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1372                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1373                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1374                                                                                pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1375                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1376                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1377                                                                                pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1378                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1379                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1380                                                                                pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1381                    }
1382            }
1383            else {
1384                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1385                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1386                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1387                                                              16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1388                                                              0);
1389                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1390                                                              16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1391                                                              stride, 0);
1392                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1393                                                              16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1394                                                              stride, 0);
1395            }
1396    
1397            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1398                                                      b_uv_dx, b_uv_dy, stride2, 0);
1399            interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1400                                                      b_uv_dx, b_uv_dy, stride2, 0);
1401    
1402            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1403                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1404                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1405                                                    stride, 1, 8);
1406    
1407            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1408                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1409                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1410                                                    stride, 1, 8);
1411    
1412            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1413                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1414                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1415                                                    stride, 1, 8);
1416    
1417            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1418                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1419                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1420                                                    stride, 1, 8);
1421    
1422            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1423                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1424                                                    dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1425                                                    stride2, 1, 8);
1426    
1427            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1428                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1429                                                    dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1430                                                    stride2, 1, 8);
1431    
1432            stop_comp_timer();
1433    
1434            for (i = 0; i < 6; i++) {
1435                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1436    
1437                    if (cbp & (1 << (5 - i)))       /* coded */
1438                    {
1439                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1440    
1441                            start_timer();
1442                            get_inter_block(bs, &block[i * 64], direction);
1443                            stop_coding_timer();
1444    
1445                            start_timer();
1446                            if (dec->quant_type == 0) {
1447                                    dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
1448                            } else {
1449                                    dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
1450                            }
1451                            stop_iquant_timer();
1452    
1453                            start_timer();
1454                            idct(&data[i * 64]);
1455                            stop_idct_timer();
1456                    }
1457            }
1458    
1459            if (dec->interlacing && pMB->field_dct) {
1460                    next_block = stride;
1461                    stride *= 2;
1462            }
1463    
1464            start_timer();
1465            if (cbp & 32)
1466                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1467            if (cbp & 16)
1468                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1469            if (cbp & 8)
1470                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1471            if (cbp & 4)
1472                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1473            if (cbp & 2)
1474                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1475            if (cbp & 1)
1476                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1477            stop_transfer_timer();
1478    }
1479    
1480    
1481    /* for decode B-frame dbquant */
1482    int32_t __inline
1483    get_dbquant(Bitstream * bs)
1484    {
1485            if (!BitstreamGetBit(bs))      /*  '0' */
1486                    return (0);
1487            else if (!BitstreamGetBit(bs)) /* '10' */
1488                    return (-2);
1489            else                           /* '11' */
1490                    return (2);
1491    }
1492    
1493    /*
1494     * For decode B-frame mb_type
1495     * bit   ret_value
1496     * 1        0
1497     * 01       1
1498     * 001      2
1499     * 0001     3
1500     */
1501    int32_t __inline
1502    get_mbtype(Bitstream * bs)
1503    {
1504            int32_t mb_type;
1505    
1506            for (mb_type = 0; mb_type <= 3; mb_type++) {
1507                    if (BitstreamGetBit(bs))
1508                            break;
1509            }
1510    
1511            if (mb_type <= 3)
1512                    return (mb_type);
1513            else
1514                    return (-1);
1515    }
1516    
1517    void
1518    decoder_bframe(DECODER * dec,
1519                               Bitstream * bs,
1520                               int quant,
1521                               int fcode_forward,
1522                               int fcode_backward)
1523    {
1524            uint32_t x, y;
1525            VECTOR mv;
1526            const VECTOR zeromv = {0,0};
1527    #ifdef BFRAMES_DEC_DEBUG
1528            FILE *fp;
1529            static char first=0;
1530    #define BFRAME_DEBUG    if (!first && fp){ \
1531                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \
1532            }
1533    #endif
1534    
1535            start_timer();
1536            image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1537                                       dec->width, dec->height);
1538            image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1539                                       dec->width, dec->height);
1540            stop_edges_timer();
1541    
1542    #ifdef BFRAMES_DEC_DEBUG
1543            if (!first){
1544                    fp=fopen("C:\\XVIDDBG.TXT","w");
1545            }
1546    #endif
1547    
1548            for (y = 0; y < dec->mb_height; y++) {
1549                    /* Initialize Pred Motion Vector */
1550                    dec->p_fmv = dec->p_bmv = zeromv;
1551                    for (x = 0; x < dec->mb_width; x++) {
1552                            MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1553                            MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1554    
1555                            mv =
1556                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1557                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1558    
1559                            /*
1560                             * skip if the co-located P_VOP macroblock is not coded
1561                             * if not codec in co-located S_VOP macroblock is _not_
1562                             * automatically skipped
1563                             */
1564    
1565                            if (last_mb->mode == MODE_NOT_CODED) {
1566                                    /* DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */
1567                                    mb->cbp = 0;
1568    #ifdef BFRAMES_DEC_DEBUG
1569                                    mb->mb_type = MODE_NOT_CODED;
1570            BFRAME_DEBUG
1571    #endif
1572                                    mb->mb_type = MODE_FORWARD;
1573                                    mb->quant = last_mb->quant;
1574                                    /*
1575                                      mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1576                                      mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1577                                    */
1578    
1579                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1580                                    continue;
1581                            }
1582    
1583                            if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1584                                    const uint8_t modb2 = BitstreamGetBit(bs);
1585    
1586                                    mb->mb_type = get_mbtype(bs);
1587    
1588                                    if (!modb2) {   /* modb=='00' */
1589                                            mb->cbp = BitstreamGetBits(bs, 6);
1590                                    } else {
1591                                            mb->cbp = 0;
1592                                    }
1593                                    if (mb->mb_type && mb->cbp) {
1594                                            quant += get_dbquant(bs);
1595    
1596                                            if (quant > 31) {
1597                                                    quant = 31;
1598                                            } else if (quant < 1) {
1599                                                    quant = 1;
1600                                            }
1601                                    }
1602    
1603                                    if (dec->interlacing) {
1604                                            if (mb->cbp) {
1605                                                    mb->field_dct = BitstreamGetBit(bs);
1606                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1607                                            }
1608    
1609                                            if (mb->mb_type) {
1610                                                    mb->field_pred = BitstreamGetBit(bs);
1611                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1612    
1613                                                    if (mb->field_pred) {
1614                                                            mb->field_for_top = BitstreamGetBit(bs);
1615                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1616                                                            mb->field_for_bot = BitstreamGetBit(bs);
1617                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1618                                                    }
1619                                            }
1620                                    }
1621    
1622                            } else {
1623                                    mb->mb_type = MODE_DIRECT_NONE_MV;
1624                                    mb->cbp = 0;
1625                            }
1626    
1627                            mb->quant = quant;
1628                            mb->mode = MODE_INTER4V;
1629                            /* DEBUG1("Switch bm_type=",mb->mb_type); */
1630    
1631    #ifdef BFRAMES_DEC_DEBUG
1632            BFRAME_DEBUG
1633    #endif
1634    
1635                            switch (mb->mb_type) {
1636                            case MODE_DIRECT:
1637                                    get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1638    
1639                            case MODE_DIRECT_NONE_MV:
1640                                    {
1641                                            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1642                                            int i;
1643    
1644                                            for (i = 0; i < 4; i++) {
1645                                                    mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1646                                                                          / TRD + mv.x);
1647                                                    mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1648                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].x)
1649                                                                                      / TRD
1650                                                                                    : mb->mvs[i].x - last_mb->mvs[i].x);
1651                                                    mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1652                                                                          / TRD + mv.y);
1653                                                    mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1654                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].y)
1655                                                                                      / TRD
1656                                                                                : mb->mvs[i].y - last_mb->mvs[i].y);
1657                                            }
1658                                            /* DEBUG("B-frame Direct!\n"); */
1659                                    }
1660                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1661                                                                                               mb, x, y, bs);
1662                                    break;
1663    
1664                            case MODE_INTERPOLATE:
1665                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1666                                                                            dec->p_fmv);
1667                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1668    
1669                                    get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1670                                                                            fcode_backward, dec->p_bmv);
1671                                    dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1672                                            mb->b_mvs[3] = mb->b_mvs[0];
1673    
1674                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1675                                                                                               mb, x, y, bs);
1676                                    /* DEBUG("B-frame Bidir!\n"); */
1677                                    break;
1678    
1679                            case MODE_BACKWARD:
1680                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1681                                                                            dec->p_bmv);
1682                                    dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1683    
1684                                    mb->mode = MODE_INTER;
1685                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1686                                    /* DEBUG("B-frame Backward!\n"); */
1687                                    break;
1688    
1689                            case MODE_FORWARD:
1690                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1691                                                                            dec->p_fmv);
1692                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1693    
1694                                    mb->mode = MODE_INTER;
1695                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1696                                    /* DEBUG("B-frame Forward!\n"); */
1697                                    break;
1698    
1699                            default:
1700                                    DPRINTF(XVID_DEBUG_ERROR,"Not support B-frame mb_type = %i\n", mb->mb_type);
1701                            }
1702                    } /* End of for */
1703            }
1704    
1705    #ifdef BFRAMES_DEC_DEBUG
1706            if (!first){
1707                    first=1;
1708                    if (fp)
1709                            fclose(fp);
1710            }
1711    #endif
1712    }
1713    
1714    
1715    
1716    /* perform post processing if necessary, and output the image */
1717    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1718                                            xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)
1719    {
1720    
1721    
1722            image_output(img, dec->width, dec->height,
1723                                     dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1724                                     frame->output.csp, dec->interlacing);
1725    
1726            if (stats)
1727            {
1728                    stats->type = coding2type(coding_type);
1729                    stats->data.vop.time_base = (int)dec->time_base;
1730                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1731            }
1732    }
1733    
1734    
1735    int
1736    decoder_decode(DECODER * dec,
1737                               xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1738  {  {
1739    
1740          Bitstream bs;          Bitstream bs;
1741          uint32_t rounding;          uint32_t rounding;
1742            uint32_t reduced_resolution;
1743          uint32_t quant;          uint32_t quant;
1744          uint32_t fcode_forward;          uint32_t fcode_forward;
1745          uint32_t fcode_backward;          uint32_t fcode_backward;
1746          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1747          uint32_t vop_type;          WARPPOINTS gmc_warp;
1748            int coding_type;
1749            int success, output, seen_something;
1750    
1751            if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1752                    return XVID_ERR_VERSION;
1753    
1754          start_global_timer();          start_global_timer();
1755    
1756          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1757            if ((frame->general & XVID_DISCONTINUITY))
1758                    dec->frames = 0;
1759            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1760    
1761            if (frame->length < 0)  /* decoder flush */
1762            {
1763            int ret;
1764                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1765                        we have a reference frame, then outout the reference frame */
1766                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1767                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1768                dec->frames = 0;
1769                ret = 0;
1770            }else{
1771                if (stats) stats->type = XVID_TYPE_NOTHING;
1772                ret = XVID_ERR_END;
1773            }
1774    
1775                    emms();
1776                    stop_global_timer();
1777                    return ret;
1778            }
1779    
1780          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1781    
1782          /* add by chenm001 <chenm001@163.com> */          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1783          /* for support B-frame to reference last 2 frame */          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1784          dec->frames++;          {
1785          vop_type =                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1786                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1787                                                           &fcode_backward, &intra_dc_threshold);                  if (stats) stats->type = XVID_TYPE_NOTHING;
1788                    emms();
1789                    return 1;   /* one byte consumed */
1790            }
1791    
1792          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */          success = 0;
1793            output = 0;
1794            seen_something = 0;
1795    
1796          switch (vop_type) {  repeat:
1797          case P_VOP:  
1798                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,          coding_type =   BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1799                                             intra_dc_threshold);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1800                  break;  
1801            DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",
1802                                                            coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1803    
1804            if (coding_type == -1) /* nothing */
1805            {
1806                    if (success) goto done;
1807            if (stats) stats->type = XVID_TYPE_NOTHING;
1808                    emms();
1809            return BitstreamPos(&bs)/8;
1810            }
1811    
1812            if (coding_type == -2 || coding_type == -3)   /* vol and/or resize */
1813            {
1814                    if (coding_type == -3)
1815                            decoder_resize(dec);
1816    
1817                    if (stats)
1818                    {
1819                            stats->type = XVID_TYPE_VOL;
1820                            stats->data.vol.general = 0;
1821                            /*XXX: if (dec->interlacing)
1822                                    stats->data.vol.general |= ++INTERLACING; */
1823                            stats->data.vol.width = dec->width;
1824                            stats->data.vol.height = dec->height;
1825                            stats->data.vol.par = dec->aspect_ratio;
1826                            stats->data.vol.par_width = dec->par_width;
1827                            stats->data.vol.par_height = dec->par_height;
1828                            emms();
1829                            return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1830                    }
1831                    goto repeat;
1832            }
1833    
1834            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1835    
1836            /* packed_mode: special-N_VOP treament */
1837            if (dec->packed_mode && coding_type == N_VOP)
1838            {
1839                    if (dec->low_delay_default && dec->frames > 0)
1840                    {
1841                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1842                            output = 1;
1843                    }
1844                    /* ignore otherwise */
1845            }
1846            else if (coding_type != B_VOP)
1847            {
1848                    switch(coding_type)
1849                    {
1850          case I_VOP:          case I_VOP:
1851                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1852                  break;                  break;
1853          case B_VOP:                  case P_VOP :
1854                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1855                                                    fcode_forward, intra_dc_threshold, NULL);
1856                            break;
1857                    case S_VOP :
1858                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1859                                                    fcode_forward, intra_dc_threshold, &gmc_warp);
1860                  break;                  break;
1861          case N_VOP:          case N_VOP:
1862                  /* when low_delay==0, N_VOP's should interpolate between the past and future frames */                          /* XXX: not_coded vops are not used for forward prediction */
1863                            /* we should not swap(last_mbs,mbs) */
1864                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1865                  break;                  break;
   
         default:  
                 return XVID_ERR_FAIL;  
1866          }          }
1867    
1868          frame->length = BitstreamPos(&bs) / 8;                  if (reduced_resolution)
1869                    {
1870                            image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1871                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1872                                    16, 0);
1873                    }
1874    
1875          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1876                                           frame->image, frame->stride, frame->colorspace);                  if (!(dec->low_delay_default && dec->packed_mode))
1877                    {
1878                            if (dec->low_delay)
1879                            {
1880                                    decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1881                                    output = 1;
1882                            }
1883                            else if (dec->frames > 0)       /* is the reference frame valid? */
1884                            {
1885                                    /* output the reference frame */
1886                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1887                                    output = 1;
1888                            }
1889                    }
1890    
         if (vop_type == I_VOP || vop_type == P_VOP) {  
1891                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1892                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1893            SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1894                    dec->last_reduced_resolution = reduced_resolution;
1895            dec->last_coding_type = coding_type;
1896    
1897                  /* swap MACROBLOCK */                  dec->frames++;
1898                  /* the Divx will not set the low_delay flage some times */                  seen_something = 1;
1899                  /* so follow code will wrong to not swap at that time */  
1900                  /* this will broken bitstream! so I'm change it, */          }else{  /* B_VOP */
1901                  /* But that is not the best way! can anyone tell me how */  
1902                  /* to do another way? */                  if (dec->low_delay)
1903                  /* 18-07-2002   MinChen<chenm001@163.com> */                  {
1904                  /*if (!dec->low_delay && vop_type == P_VOP) */                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1905                  if (vop_type == P_VOP)                          dec->low_delay = 1;
                         mb_swap(&dec->mbs, &dec->last_mbs);  
1906          }          }
1907    
1908          emms();                  if (dec->frames < 2)
1909                    {
1910                            /* attemping to decode a bvop without atleast 2 reference frames */
1911                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1912                                                    "broken b-frame, mising ref frames");
1913                    }else if (dec->time_pp <= dec->time_bp) {
1914                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1915                            decoded in vfw. */
1916                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1917                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1918                    }else{
1919                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1920                    }
1921    
1922                    decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1923                    output = 1;
1924                    dec->frames++;
1925            }
1926    
1927            BitstreamByteAlign(&bs);
1928    
1929            /* low_delay_default mode: repeat in packed_mode */
1930            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1931            {
1932                    success = 1;
1933                    goto repeat;
1934            }
1935    
1936    done :
1937    
1938            /* low_delay_default mode: if we've gotten here without outputting anything,
1939               then output the recently decoded frame, or print an error message  */
1940            if (dec->low_delay_default && output == 0)
1941            {
1942                    if (dec->packed_mode && seen_something)
1943                    {
1944                            /* output the recently decoded frame */
1945                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1946                    }
1947                    else
1948                    {
1949                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1950                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1951                                    "warning: nothing to output");
1952                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1953                                    "bframe decoder lag");
1954    
1955                            decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);
1956                            if (stats) stats->type = XVID_TYPE_NOTHING;
1957    
1958                    }
1959            }
1960    
1961            emms();
1962          stop_global_timer();          stop_global_timer();
1963    
1964          return XVID_ERR_OK;          return BitstreamPos(&bs) / 8;   /* number of bytes consumed */
1965  }  }

Legend:
Removed from v.1.46  
changed lines
  Added in v.1.49.2.15

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