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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.49.4.1

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