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

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

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