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

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

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