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

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.49.2.8

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