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

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.49

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