[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.10, Mon Apr 8 23:50:15 2002 UTC revision 1.71, Mon May 23 09:29:43 2005 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC   *      XVID MPEG-4 VIDEO CODEC
4   *      decoder main   *  - Decoder Module -
5   *   *
6   *      This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002      MinChen <chenm001@163.com>
7   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002-2004 Peter Ross <pross@xvid.org>
  *      to use this software module in hardware or software products are  
  *      advised that its use may infringe existing patents or copyrights, and  
  *      any such use would be at such party's own risk.  The original  
  *      developer of this software module and his/her company, and subsequent  
  *      editors and their companies, will have no liability for use of this  
  *      software or modifications or derivatives thereof.  
8   *   *
9   *      This program is xvid_free software; you can redistribute it and/or modify   *  This program is free software ; you can redistribute it and/or modify
10   *      it under the terms of the GNU General Public License as published by   *      it under the terms of the GNU General Public License as published by
11   *      the xvid_free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation ; either version 2 of the License, or
12   *      (at your option) any later version.   *      (at your option) any later version.
13   *   *
14   *      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 17 
17   *      GNU General Public License for more details.   *      GNU General Public License for more details.
18   *   *
19   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
20   *      along with this program; if not, write to the xvid_free Software   *  along with this program ; if not, write to the Free Software
21   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   *   *
23   *************************************************************************/   * $Id$
   
 /**************************************************************************  
  *  
  *      History:  
  *  
  *  29.03.2002  interlacing fix - compensated block wasn't being used when  
  *              reconstructing blocks, thus artifacts  
  *              interlacing speedup - used transfers to re-interlace  
  *              interlaced decoding should be as fast as progressive now  
  *  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>  
24   *   *
25   *************************************************************************/   ****************************************************************************/
26    
27    #include <stdio.h>
28  #include <stdlib.h>  #include <stdlib.h>
29  #include <string.h>  // memset  #include <string.h>
30    
31    #ifdef BFRAMES_DEC_DEBUG
32            #define BFRAMES_DEC
33    #endif
34    
35  #include "xvid.h"  #include "xvid.h"
36  #include "portab.h"  #include "portab.h"
37    #include "global.h"
38    
39  #include "decoder.h"  #include "decoder.h"
40  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
41  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
42    
43  #include "quant/quant_h263.h"  #include "quant/quant.h"
44  #include "quant/quant_mpeg4.h"  #include "quant/quant_matrix.h"
45  #include "dct/idct.h"  #include "dct/idct.h"
46  #include "dct/fdct.h"  #include "dct/fdct.h"
47  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
48  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
49    #include "image/font.h"
50    #include "image/qpel.h"
51    
52  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
53  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
54  #include "utils/timer.h"  #include "utils/timer.h"
55  #include "utils/emms.h"  #include "utils/emms.h"
56    #include "motion/motion.h"
57    #include "motion/gmc.h"
58    
59  #include "image/image.h"  #include "image/image.h"
60  #include "image/colorspace.h"  #include "image/colorspace.h"
61    #include "image/postprocessing.h"
62  #include "utils/mem_align.h"  #include "utils/mem_align.h"
63    
64  int decoder_create(XVID_DEC_PARAM * param)  static int
65    decoder_resize(DECODER * dec)
66  {  {
67          DECODER * dec;          /* free existing */
68            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
69            image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
70            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
71            image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
72            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
73    
74            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
75    
76      image_null(&dec->cur);
77      image_null(&dec->refn[0]);
78      image_null(&dec->refn[1]);
79      image_null(&dec->tmp);
80      image_null(&dec->qtmp);
81      image_null(&dec->gmc);
82    
         dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);  
         if (dec == NULL)  
         {  
                 return XVID_ERR_MEMORY;  
         }  
         param->handle = dec;  
83    
84          dec->width = param->width;    xvid_free(dec->last_mbs);
85          dec->height = param->height;    xvid_free(dec->mbs);
86      xvid_free(dec->qscale);
87      dec->last_mbs = NULL;
88      dec->mbs = NULL;
89      dec->qscale = NULL;
90    
91            /* realloc */
92          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
93          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
94    
95          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
96          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
97    
98          if (image_create(&dec->cur, dec->edged_width, dec->edged_height))          if (   image_create(&dec->cur, dec->edged_width, dec->edged_height)
99          {              || image_create(&dec->refn[0], dec->edged_width, dec->edged_height)
100                || image_create(&dec->refn[1], dec->edged_width, dec->edged_height)         /* Support B-frame to reference last 2 frame */
101                || image_create(&dec->tmp, dec->edged_width, dec->edged_height)
102                || image_create(&dec->qtmp, dec->edged_width, dec->edged_height)
103          || image_create(&dec->gmc, dec->edged_width, dec->edged_height) )
104        goto memory_error;
105    
106            dec->mbs =
107                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
108                                            CACHE_LINE);
109            if (dec->mbs == NULL)
110              goto memory_error;
111            memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
112    
113            /* For skip MB flag */
114            dec->last_mbs =
115                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
116                                            CACHE_LINE);
117            if (dec->last_mbs == NULL)
118              goto memory_error;
119            memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
120    
121            /* nothing happens if that fails */
122            dec->qscale =
123                    xvid_malloc(sizeof(int) * dec->mb_width * dec->mb_height, CACHE_LINE);
124    
125            if (dec->qscale)
126                    memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height);
127    
128            return 0;
129    
130    memory_error:
131            /* Most structures were deallocated / nullifieded, so it should be safe */
132            /* decoder_destroy(dec) minus the write_timer */
133      xvid_free(dec->mbs);
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    
140                  xvid_free(dec);                  xvid_free(dec);
141                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
142          }          }
143    
144          if (image_create(&dec->refn, dec->edged_width, dec->edged_height))  
145    int
146    decoder_create(xvid_dec_create_t * create)
147          {          {
148                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          DECODER *dec;
149                  xvid_free(dec);  
150            if (XVID_VERSION_MAJOR(create->version) != 1)   /* v1.x.x */
151                    return XVID_ERR_VERSION;
152    
153            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
154            if (dec == NULL) {
155                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
156          }          }
157    
158          dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);          memset(dec, 0, sizeof(DECODER));
159          if (dec->mbs == NULL)  
160          {          dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
161                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          if (dec->mpeg_quant_matrices == NULL) {
162                  xvid_free(dec);                  xvid_free(dec);
163                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
164          }          }
165    
166            create->handle = dec;
167    
168            dec->width = create->width;
169            dec->height = create->height;
170    
171            image_null(&dec->cur);
172            image_null(&dec->refn[0]);
173            image_null(&dec->refn[1]);
174            image_null(&dec->tmp);
175            image_null(&dec->qtmp);
176    
177            /* image based GMC */
178            image_null(&dec->gmc);
179    
180            dec->mbs = NULL;
181            dec->last_mbs = NULL;
182            dec->qscale = NULL;
183    
184          init_timer();          init_timer();
185            init_postproc(&dec->postproc);
186            init_mpeg_matrix(dec->mpeg_quant_matrices);
187    
188            /* For B-frame support (used to save reference frame's time */
189            dec->frames = 0;
190            dec->time = dec->time_base = dec->last_time_base = 0;
191            dec->low_delay = 0;
192            dec->packed_mode = 0;
193            dec->time_inc_resolution = 1; /* until VOL header says otherwise */
194    
195            dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
196    
197          return XVID_ERR_OK;          if (dec->fixed_dimensions)
198                    return decoder_resize(dec);
199            else
200                    return 0;
201  }  }
202    
203    
204  int decoder_destroy(DECODER * dec)  int
205    decoder_destroy(DECODER * dec)
206  {  {
207            xvid_free(dec->last_mbs);
208          xvid_free(dec->mbs);          xvid_free(dec->mbs);
209          image_destroy(&dec->refn, dec->edged_width, dec->edged_height);          xvid_free(dec->qscale);
210    
211            /* image based GMC */
212            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
213    
214            image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
215            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
216            image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
217            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
218          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
219            xvid_free(dec->mpeg_quant_matrices);
220          xvid_free(dec);          xvid_free(dec);
221    
222          write_timer();          write_timer();
223          return XVID_ERR_OK;          return 0;
224  }  }
225    
226    static const int32_t dquant_table[4] = {
   
 static const int32_t dquant_table[4] =  
 {  
227          -1, -2, 1, 2          -1, -2, 1, 2
228  };  };
229    
230    /* decode an intra macroblock */
231  // decode an intra macroblock  static void
232    decoder_mbintra(DECODER * dec,
 void decoder_mbintra(DECODER * dec,  
233                       MACROBLOCK * pMB,                       MACROBLOCK * pMB,
234                       const uint32_t x_pos,                       const uint32_t x_pos,
235                       const uint32_t y_pos,                       const uint32_t y_pos,
# Line 145  Line 237 
237                       const uint32_t cbp,                       const uint32_t cbp,
238                       Bitstream * bs,                       Bitstream * bs,
239                       const uint32_t quant,                       const uint32_t quant,
240                       const uint32_t intra_dc_threshold)                                  const uint32_t intra_dc_threshold,
241                                    const unsigned int bound)
242  {  {
243    
244          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 162  Line 255 
255          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
256          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
257    
258          memset(block, 0, 6*64*sizeof(int16_t));         // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
259    
260          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
261                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
262                  int16_t predictors[8];                  int16_t predictors[8];
263                  int start_coeff;                  int start_coeff;
264    
265                  start_timer();                  start_timer();
266                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i*64], iQuant, iDcScaler, predictors);                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64],
267                  if (!acpred_flag)                                           iQuant, iDcScaler, predictors, bound);
268                  {                  if (!acpred_flag) {
269                          pMB->acpred_directions[i] = 0;                          pMB->acpred_directions[i] = 0;
270                  }                  }
271                  stop_prediction_timer();                  stop_prediction_timer();
272    
273                  if (quant < intra_dc_threshold)                  if (quant < intra_dc_threshold) {
                 {  
274                          int dc_size;                          int dc_size;
275                          int dc_dif;                          int dc_dif;
276    
277                          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);
278                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;
279    
280                          if (dc_size > 8)                          if (dc_size > 8) {
281                          {                                  BitstreamSkip(bs, 1);   /* marker */
                                 BitstreamSkip(bs, 1);           // marker  
282                          }                          }
283    
284                          block[i*64 + 0] = dc_dif;                          block[i*64 + 0] = dc_dif;
285                          start_coeff = 1;                          start_coeff = 1;
286                  }  
287                  else                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);
288                  {                  } else {
289                          start_coeff = 0;                          start_coeff = 0;
290                  }                  }
291    
292                  start_timer();                  start_timer();
293                  if (cbp & (1 << (5-i)))                 // coded                  if (cbp & (1 << (5 - i)))       /* coded */
294                  {                  {
295                          get_intra_block(bs, &block[i*64], pMB->acpred_directions[i], start_coeff);                          int direction = dec->alternate_vertical_scan ?
296                                    2 : pMB->acpred_directions[i];
297    
298                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
299                  }                  }
300                  stop_coding_timer();                  stop_coding_timer();
301    
302                  start_timer();                  start_timer();
303                  add_acdc(pMB, i, &block[i*64], iDcScaler, predictors);                  add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors, dec->bs_version);
304                  stop_prediction_timer();                  stop_prediction_timer();
305    
306                  start_timer();                  start_timer();
307                  if (dec->quant_type == 0)                  if (dec->quant_type == 0) {
308                  {                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
309                          dequant_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);                  } else {
310                  }                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
                 else  
                 {  
                         dequant4_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);  
311                  }                  }
312                  stop_iquant_timer();                  stop_iquant_timer();
313    
314                  start_timer();                  start_timer();
315                  idct(&data[i*64]);                  idct(&data[i*64]);
316                  stop_idct_timer();                  stop_idct_timer();
317    
318          }          }
319    
320          if (dec->interlacing && pMB->field_dct)          if (dec->interlacing && pMB->field_dct) {
         {  
321                  next_block = stride;                  next_block = stride;
322                  stride *= 2;                  stride *= 2;
323          }          }
# Line 242  Line 332 
332          stop_transfer_timer();          stop_transfer_timer();
333  }  }
334    
335    static void
336    decoder_mb_decode(DECODER * dec,
337                                    const uint32_t cbp,
338                                    Bitstream * bs,
339                                    uint8_t * pY_Cur,
340                                    uint8_t * pU_Cur,
341                                    uint8_t * pV_Cur,
342                                    const MACROBLOCK * pMB)
343    {
344            DECLARE_ALIGNED_MATRIX(data, 1, 64, int16_t, CACHE_LINE);
345    
346            int stride = dec->edged_width;
347            int next_block = stride * 8;
348            int i;
349            const uint32_t iQuant = pMB->quant;
350            const int direction = dec->alternate_vertical_scan ? 2 : 0;
351            typedef void (*get_inter_block_function_t)(
352                            Bitstream * bs,
353                            int16_t * block,
354                            int direction,
355                            const int quant,
356                            const uint16_t *matrix);
357            typedef void (*add_residual_function_t)(
358                            uint8_t *predicted_block,
359                            const int16_t *residual,
360                            int stride);
361    
362            const get_inter_block_function_t get_inter_block = (dec->quant_type == 0)
363                    ? (get_inter_block_function_t)get_inter_block_h263
364                    : (get_inter_block_function_t)get_inter_block_mpeg;
365    
366            uint8_t *dst[6];
367            int strides[6];
368    
369    
370            if (dec->interlacing && pMB->field_dct) {
371                    next_block = stride;
372                    stride *= 2;
373            }
374    
375            dst[0] = pY_Cur;
376            dst[2] = pY_Cur + next_block;
377            dst[1] = dst[0] + 8;
378            dst[3] = dst[2] + 8;
379            dst[4] = pU_Cur;
380            dst[5] = pV_Cur;
381            strides[0] = strides[1] = strides[2] = strides[3] = stride;
382            strides[4] = stride/2;
383            strides[5] = stride/2;
384    
385            for (i = 0; i < 6; i++) {
386                    /* Process only coded blocks */
387                    if (cbp & (1 << (5 - i))) {
388    
389  #define SIGN(X) (((X)>0)?1:-1)                          /* Clear the block */
390  #define ABS(X) (((X)>0)?(X):-(X))                          memset(&data[0], 0, 64*sizeof(int16_t));
391  static const uint32_t roundtab[16] =  
392  { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };                          /* Decode coeffs and dequantize on the fly */
393                            start_timer();
394                            get_inter_block(bs, &data[0], direction, iQuant, get_inter_matrix(dec->mpeg_quant_matrices));
395                            stop_coding_timer();
396    
397                            /* iDCT */
398                            start_timer();
399                            idct(&data[0]);
400                            stop_idct_timer();
401    
402  // decode an inter macroblock                          /* Add this residual to the predicted block */
403                            start_timer();
404                            transfer_16to8add(dst[i], &data[0], strides[i]);
405                            stop_transfer_timer();
406                    }
407            }
408    }
409    
410  void decoder_mbinter(DECODER * dec,  static void __inline
411    validate_vector(VECTOR * mv, unsigned int x_pos, unsigned int y_pos, const DECODER * dec)
412    {
413            /* clip a vector to valid range
414               prevents crashes if bitstream is broken
415            */
416            int shift = 5 + dec->quarterpel;
417            int xborder_high = (int)(dec->mb_width - x_pos) << shift;
418            int xborder_low = (-(int)x_pos-1) << shift;
419            int yborder_high = (int)(dec->mb_height - y_pos) << shift;
420            int yborder_low = (-(int)y_pos-1) << shift;
421    
422    #define CHECK_MV(mv) \
423            do { \
424            if ((mv).x > xborder_high) { \
425                    DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", (mv).x, xborder_high, x_pos, y_pos); \
426                    (mv).x = xborder_high; \
427            } else if ((mv).x < xborder_low) { \
428                    DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", (mv).x, xborder_low, x_pos, y_pos); \
429                    (mv).x = xborder_low; \
430            } \
431            if ((mv).y > yborder_high) { \
432                    DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", (mv).y, yborder_high, x_pos, y_pos); \
433                    (mv).y = yborder_high; \
434            } else if ((mv).y < yborder_low) { \
435                    DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", (mv).y, yborder_low, x_pos, y_pos); \
436                    (mv).y = yborder_low; \
437            } \
438            } while (0)
439    
440            CHECK_MV(mv[0]);
441            CHECK_MV(mv[1]);
442            CHECK_MV(mv[2]);
443            CHECK_MV(mv[3]);
444    }
445    
446    /* decode an inter macroblock */
447    static void
448    decoder_mbinter(DECODER * dec,
449                       const MACROBLOCK * pMB,                       const MACROBLOCK * pMB,
450                       const uint32_t x_pos,                       const uint32_t x_pos,
451                       const uint32_t y_pos,                       const uint32_t y_pos,
                      const uint32_t acpred_flag,  
452                       const uint32_t cbp,                       const uint32_t cbp,
453                       Bitstream * bs,                       Bitstream * bs,
454                       const uint32_t quant,                                  const uint32_t rounding,
455                       const uint32_t rounding)                                  const int ref)
456  {  {
   
         DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
457          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
458          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * 8;  
459          uint32_t i;          uint32_t i;
460          uint32_t iQuant = pMB->quant;  
461          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
462    
463          int uv_dx, uv_dy;          int uv_dx, uv_dy;
464            VECTOR mv[4];   /* local copy of mvs */
465    
466          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
467          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
468          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
469            for (i = 0; i < 4; i++)
470                    mv[i] = pMB->mvs[i];
471    
472          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)          validate_vector(mv, x_pos, y_pos, dec);
         {  
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
473    
474                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;          start_timer();
475                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
476          }          if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */
477    
478                    uv_dx = mv[0].x;
479                    uv_dy = mv[0].y;
480                    if (dec->quarterpel) {
481                            uv_dx /= 2;
482                            uv_dy /= 2;
483                    }
484                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
485                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
486    
487                    if (dec->quarterpel)
488                            interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
489                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
490                                                                                            mv[0].x, mv[0].y, stride, rounding);
491          else          else
492          {                          interpolate16x16_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
493                  int sum;                                                                          mv[0].x, mv[0].y, stride, rounding);
                 sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
                 uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
494    
495                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;          } else {        /* MODE_INTER4V */
496                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
497                    if(dec->quarterpel) {
498                            uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
499                            uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
500                    } else {
501                            uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
502                            uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
503                    }
504    
505                    uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
506                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
507    
508                    if (dec->quarterpel) {
509                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
510                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
511                                                                            mv[0].x, mv[0].y, stride, rounding);
512                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
513                                                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
514                                                                            mv[1].x, mv[1].y, stride, rounding);
515                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
516                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
517                                                                            mv[2].x, mv[2].y, stride, rounding);
518                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
519                                                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
520                                                                            mv[3].x, mv[3].y, stride, rounding);
521                    } else {
522                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos,
523                                                                    mv[0].x, mv[0].y, stride, rounding);
524                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,
525                                                                    mv[1].x, mv[1].y, stride, rounding);
526                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos + 8,
527                                                                    mv[2].x, mv[2].y, stride, rounding);
528                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,
529                                                                    mv[3].x, mv[3].y, stride, rounding);
530                    }
531          }          }
532    
533          start_timer();          /* chroma */
534          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);          interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
535          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);                                                          uv_dx, uv_dy, stride2, rounding);
536          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);          interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
537          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);                                                          uv_dx, uv_dy, stride2, rounding);
538          interpolate8x8_switch(dec->cur.u, dec->refn.u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);  
         interpolate8x8_switch(dec->cur.v, dec->refn.v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);  
539          stop_comp_timer();          stop_comp_timer();
540    
541          for (i = 0; i < 6; i++)          if (cbp)
542          {                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB);
543                  if (cbp & (1 << (5-i)))                 // coded  }
544    
545    static void
546    decoder_mbgmc(DECODER * dec,
547                                    MACROBLOCK * const pMB,
548                                    const uint32_t x_pos,
549                                    const uint32_t y_pos,
550                                    const uint32_t fcode,
551                                    const uint32_t cbp,
552                                    Bitstream * bs,
553                                    const uint32_t rounding)
554                  {                  {
555                          memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear          const uint32_t stride = dec->edged_width;
556            const uint32_t stride2 = stride / 2;
557    
558                          start_timer();          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
559                          get_inter_block(bs, &block[i*64]);          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
560                          stop_coding_timer();          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
561    
562                          start_timer();          NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
563                          if (dec->quant_type == 0)  
564                          {          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
                                 dequant_inter(&data[i*64], &block[i*64], iQuant);  
                         }  
                         else  
                         {  
                                 dequant4_inter(&data[i*64], &block[i*64], iQuant);  
                         }  
                         stop_iquant_timer();  
565    
566                          start_timer();                          start_timer();
                         idct(&data[i*64]);  
                         stop_idct_timer();  
                 }  
         }  
567    
568          if (dec->interlacing && pMB->field_dct)  /* this is where the calculations are done */
569          {  
570                  next_block = stride;          gmc_data->predict_16x16(gmc_data,
571                  stride *= 2;                          dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
572          }                          stride, stride, x_pos, y_pos, rounding);
573    
574            gmc_data->predict_8x8(gmc_data,
575                            dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,
576                            dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,
577                            stride2, stride2, x_pos, y_pos, rounding);
578    
579            gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);
580    
581            pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
582            pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
583    
584            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
585    
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur,                  &data[0*64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8,              &data[1*64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block,     &data[2*64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur,                  &data[4*64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur,                  &data[5*64], stride2);  
586          stop_transfer_timer();          stop_transfer_timer();
587    
588            if (cbp)
589                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB);
590    
591  }  }
592    
593    
594  void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold)  static void
595    decoder_iframe(DECODER * dec,
596                                    Bitstream * bs,
597                                    int quant,
598                                    int intra_dc_threshold)
599  {  {
600            uint32_t bound;
601          uint32_t x, y;          uint32_t x, y;
602            const uint32_t mb_width = dec->mb_width;
603            const uint32_t mb_height = dec->mb_height;
604    
605          for (y = 0; y < dec->mb_height; y++)          bound = 0;
         {  
                 for (x = 0; x < dec->mb_width; x++)  
                 {  
                         MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];  
606    
607            for (y = 0; y < mb_height; y++) {
608                    for (x = 0; x < mb_width; x++) {
609                            MACROBLOCK *mb;
610                          uint32_t mcbpc;                          uint32_t mcbpc;
611                          uint32_t cbpc;                          uint32_t cbpc;
612                          uint32_t acpred_flag;                          uint32_t acpred_flag;
613                          uint32_t cbpy;                          uint32_t cbpy;
614                          uint32_t cbp;                          uint32_t cbp;
615    
616                            while (BitstreamShowBits(bs, 9) == 1)
617                                    BitstreamSkip(bs, 9);
618    
619                            if (check_resync_marker(bs, 0))
620                            {
621                                    bound = read_video_packet_header(bs, dec, 0,
622                                                            &quant, NULL, NULL, &intra_dc_threshold);
623                                    x = bound % mb_width;
624                                    y = bound / mb_width;
625                            }
626                            mb = &dec->mbs[y * dec->mb_width + x];
627    
628                            DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
629    
630                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
631                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
632                          cbpc = (mcbpc >> 4);                          cbpc = (mcbpc >> 4);
633    
634                          acpred_flag = BitstreamGetBit(bs);                          acpred_flag = BitstreamGetBit(bs);
635    
                         if (mb->mode == MODE_STUFFING)  
                         {  
                                 DEBUG("-- STUFFING ?");  
                                 continue;  
                         }  
   
636                          cbpy = get_cbpy(bs, 1);                          cbpy = get_cbpy(bs, 1);
637                          cbp = (cbpy << 2) | cbpc;                          cbp = (cbpy << 2) | cbpc;
638    
639                          if (mb->mode == MODE_INTRA_Q)                          if (mb->mode == MODE_INTRA_Q) {
                         {  
640                                  quant += dquant_table[BitstreamGetBits(bs,2)];                                  quant += dquant_table[BitstreamGetBits(bs,2)];
641                                  if (quant > 31)                                  if (quant > 31) {
                                 {  
642                                          quant = 31;                                          quant = 31;
643                                  }                                  } else if (quant < 1) {
                                 else if (quant < 1)  
                                 {  
644                                          quant = 1;                                          quant = 1;
645                                  }                                  }
646                          }                          }
647                          mb->quant = quant;                          mb->quant = quant;
648                            mb->mvs[0].x = mb->mvs[0].y =
649                            mb->mvs[1].x = mb->mvs[1].y =
650                            mb->mvs[2].x = mb->mvs[2].y =
651                            mb->mvs[3].x = mb->mvs[3].y =0;
652    
653                          if (dec->interlacing)                          if (dec->interlacing) {
                         {  
654                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
655                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
656                          }                          }
657    
658                          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,
659                                                            intra_dc_threshold, bound);
660    
661                  }                  }
662                    if(dec->out_frm)
663                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
664          }          }
665    
666  }  }
667    
668    
669  void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode)  static void
670    get_motion_vector(DECODER * dec,
671                                    Bitstream * bs,
672                                    int x,
673                                    int y,
674                                    int k,
675                                    VECTOR * ret_mv,
676                                    int fcode,
677                                    const int bound)
678  {  {
679    
680          int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
681          int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
682          int low = ((-32) * scale_fac);          const int low = ((-32) * scale_fac);
683          int range = (64 * scale_fac);          const int range = (64 * scale_fac);
   
         VECTOR pmv[4];  
         uint32_t psad[4];  
   
         int mv_x, mv_y;  
         int pmv_x, pmv_y;  
684    
685            const VECTOR pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
686            VECTOR mv;
687    
688          get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad);          mv.x = get_mv(bs, fcode);
689            mv.y = get_mv(bs, fcode);
690    
691          pmv_x = pmv[0].x;          DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
         pmv_y = pmv[0].y;  
692    
693          mv_x = get_mv(bs, fcode);          mv.x += pmv.x;
694          mv_y = get_mv(bs, fcode);          mv.y += pmv.y;
695    
696          mv_x += pmv_x;          if (mv.x < low) {
697          mv_y += pmv_y;                  mv.x += range;
698            } else if (mv.x > high) {
699          if (mv_x < low)                  mv.x -= range;
         {  
                 mv_x += range;  
700          }          }
701          else if (mv_x > high)  
702          {          if (mv.y < low) {
703                  mv_x -= range;                  mv.y += range;
704            } else if (mv.y > high) {
705                    mv.y -= range;
706          }          }
707    
708          if (mv_y < low)          ret_mv->x = mv.x;
709          {          ret_mv->y = mv.y;
                 mv_y += range;  
710          }          }
711          else if (mv_y > high)  
712    /* for P_VOP set gmc_warp to NULL */
713    static void
714    decoder_pframe(DECODER * dec,
715                                    Bitstream * bs,
716                                    int rounding,
717                                    int quant,
718                                    int fcode,
719                                    int intra_dc_threshold,
720                                    const WARPPOINTS *const gmc_warp)
721          {          {
722                  mv_y -= range;          uint32_t x, y;
723            uint32_t bound;
724            int cp_mb, st_mb;
725            const uint32_t mb_width = dec->mb_width;
726            const uint32_t mb_height = dec->mb_height;
727    
728            if (!dec->is_edged[0]) {
729                    start_timer();
730                    image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
731                                                    dec->width, dec->height, dec->bs_version);
732                    dec->is_edged[0] = 1;
733                    stop_edges_timer();
734          }          }
735    
736          mv->x = mv_x;          if (gmc_warp) {
737          mv->y = mv_y;                  /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
738                    generate_GMCparameters( dec->sprite_warping_points,
739                                    dec->sprite_warping_accuracy, gmc_warp,
740                                    dec->width, dec->height, &dec->new_gmc_data);
741    
742                    /* image warping is done block-based in decoder_mbgmc(), now */
743  }  }
744    
745            bound = 0;
746    
747  void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold)          for (y = 0; y < mb_height; y++) {
748  {                  cp_mb = st_mb = 0;
749                    for (x = 0; x < mb_width; x++) {
750                            MACROBLOCK *mb;
751    
752          uint32_t x, y;                          /* skip stuffing */
753                            while (BitstreamShowBits(bs, 10) == 1)
754                                    BitstreamSkip(bs, 10);
755    
756          image_swap(&dec->cur, &dec->refn);                          if (check_resync_marker(bs, fcode - 1)) {
757                                    bound = read_video_packet_header(bs, dec, fcode - 1,
758                                            &quant, &fcode, NULL, &intra_dc_threshold);
759                                    x = bound % mb_width;
760                                    y = bound / mb_width;
761                            }
762                            mb = &dec->mbs[y * dec->mb_width + x];
763    
764          start_timer();                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
         image_setedges(&dec->refn, dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);  
         stop_edges_timer();  
765    
766          for (y = 0; y < dec->mb_height; y++)                          if (!(BitstreamGetBit(bs)))     { /* block _is_ coded */
767          {                                  uint32_t mcbpc, cbpc, cbpy, cbp;
768                  for (x = 0; x < dec->mb_width; x++)                                  uint32_t intra, acpred_flag = 0;
769                  {                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
                         MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];  
   
                         if (!BitstreamGetBit(bs))                       // not_coded  
                         {  
                                 uint32_t mcbpc;  
                                 uint32_t cbpc;  
                                 uint32_t acpred_flag;  
                                 uint32_t cbpy;  
                                 uint32_t cbp;  
                                 uint32_t intra;  
770    
771                                    cp_mb++;
772                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
773                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
774                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
775                                  acpred_flag = 0;  
776                                    DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
777                                    DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
778    
779                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
780    
781                                  if (intra)                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
782                                  {                                          mcsel = BitstreamGetBit(bs);
783                                    else if (intra)
784                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
                                 }  
   
                                 if (mb->mode == MODE_STUFFING)  
                                 {  
                                         DEBUG("-- STUFFING ?");  
                                         continue;  
                                 }  
785    
786                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
787                                    DPRINTF(XVID_DEBUG_MB, "cbpy %i mcsel %i \n", cbpy,mcsel);
788    
789                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
790    
791                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q)                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
792                                  {                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
793                                          quant += dquant_table[BitstreamGetBits(bs,2)];                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
794                                          if (quant > 31)                                          quant += dquant;
795                                          {                                          if (quant > 31) {
796                                                  quant = 31;                                                  quant = 31;
797                                          }                                          } else if (quant < 1) {
                                         else if (mb->quant < 1)  
                                         {  
798                                                  quant = 1;                                                  quant = 1;
799                                          }                                          }
800                                            DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
801                                  }                                  }
802                                  mb->quant = quant;                                  mb->quant = quant;
803    
804                                  if (dec->interlacing)                                  if (dec->interlacing) {
805                                  {                                          if (cbp || intra) {
806                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
807                                          DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
808                                            }
809    
810                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                          if ((mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) && !mcsel) {
                                         {  
811                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
812                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
813    
814                                                  if (mb->field_pred)                                                  if (mb->field_pred) {
                                                 {  
815                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
816                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
817                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
818                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
819                                                  }                                                  }
820                                          }                                          }
821                                  }                                  }
822    
823                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                  if (mcsel) {
824                                  {                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, rounding);
825                                          if (dec->interlacing && mb->field_pred)                                          continue;
826    
827                                    } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
828    
829                                            if (dec->interlacing && mb->field_pred) {
830                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
831                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode, bound);
832                                            } else {
833                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
834                                                    mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
835                                            }
836                                    } else if (mb->mode == MODE_INTER4V ) {
837                                            get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
838                                            get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
839                                            get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
840                                            get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
841                                    } else {                /* MODE_INTRA, MODE_INTRA_Q */
842                                            mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
843                                            mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =     0;
844                                            decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
845                                                                            intra_dc_threshold, bound);
846                                            continue;
847                                    }
848    
849                                    decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0);
850    
851                            } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
852                                    mb->mode = MODE_NOT_CODED_GMC;
853                                    mb->quant = quant;
854                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
855    
856                                    if(dec->out_frm && cp_mb > 0) {
857                                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
858                                            cp_mb = 0;
859                                    }
860                                    st_mb = x+1;
861                            } else {        /* not coded P_VOP macroblock */
862                                    mb->mode = MODE_NOT_CODED;
863                                    mb->quant = quant;
864    
865                                    mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
866                                    mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
867    
868                                    decoder_mbinter(dec, mb, x, y, 0, bs,
869                                                                    rounding, 0);
870    
871                                    if(dec->out_frm && cp_mb > 0) {
872                                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
873                                            cp_mb = 0;
874                                    }
875                                    st_mb = x+1;
876                            }
877                    }
878    
879                    if(dec->out_frm && cp_mb > 0)
880                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
881            }
882    }
883    
884    
885    /* decode B-frame motion vector */
886    static void
887    get_b_motion_vector(Bitstream * bs,
888                                            VECTOR * mv,
889                                            int fcode,
890                                            const VECTOR pmv,
891                                            const DECODER * const dec,
892                                            const int x, const int y)
893                                          {                                          {
894                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);          const int scale_fac = 1 << (fcode - 1);
895                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode);          const int high = (32 * scale_fac) - 1;
896            const int low = ((-32) * scale_fac);
897            const int range = (64 * scale_fac);
898    
899            int mv_x = get_mv(bs, fcode);
900            int mv_y = get_mv(bs, fcode);
901    
902            mv_x += pmv.x;
903            mv_y += pmv.y;
904    
905            if (mv_x < low)
906                    mv_x += range;
907            else if (mv_x > high)
908                    mv_x -= range;
909    
910            if (mv_y < low)
911                    mv_y += range;
912            else if (mv_y > high)
913                    mv_y -= range;
914    
915            mv->x = mv_x;
916            mv->y = mv_y;
917                                          }                                          }
918                                          else  
919    /* decode an B-frame direct & interpolate macroblock */
920    static void
921    decoder_bf_interpolate_mbinter(DECODER * dec,
922                                                                    IMAGE forward,
923                                                                    IMAGE backward,
924                                                                    MACROBLOCK * pMB,
925                                                                    const uint32_t x_pos,
926                                                                    const uint32_t y_pos,
927                                                                    Bitstream * bs,
928                                                                    const int direct)
929                                          {                                          {
930                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);          uint32_t stride = dec->edged_width;
931                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;          uint32_t stride2 = stride / 2;
932                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;          int uv_dx, uv_dy;
933            int b_uv_dx, b_uv_dy;
934            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
935            const uint32_t cbp = pMB->cbp;
936    
937            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
938            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
939            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
940    
941            validate_vector(pMB->mvs, x_pos, y_pos, dec);
942            validate_vector(pMB->b_mvs, x_pos, y_pos, dec);
943    
944            if (!direct) {
945                    uv_dx = pMB->mvs[0].x;
946                    uv_dy = pMB->mvs[0].y;
947                    b_uv_dx = pMB->b_mvs[0].x;
948                    b_uv_dy = pMB->b_mvs[0].y;
949    
950                    if (dec->quarterpel) {
951                            uv_dx /= 2;
952                            uv_dy /= 2;
953                            b_uv_dx /= 2;
954                            b_uv_dy /= 2;
955                    }
956    
957                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
958                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
959                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
960                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
961    
962            } else {
963                    uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
964                    uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
965                    b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
966                    b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
967    
968                    if (dec->quarterpel) {
969                            uv_dx /= 2;
970                            uv_dy /= 2;
971                            b_uv_dx /= 2;
972                            b_uv_dy /= 2;
973                    }
974    
975                    uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
976                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
977                    b_uv_dx = (b_uv_dx >> 3) + roundtab_76[b_uv_dx & 0xf];
978                    b_uv_dy = (b_uv_dy >> 3) + roundtab_76[b_uv_dy & 0xf];
979                                          }                                          }
980    
981            start_timer();
982            if(dec->quarterpel) {
983                    if(!direct) {
984                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
985                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
986                                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
987                    } else {
988                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
989                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
990                                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
991                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
992                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
993                                                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
994                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
995                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
996                                                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
997                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
998                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
999                                                                                    pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1000                    }
1001            } else {
1002                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1003                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1004                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
1005                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1006                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1007                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1008                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos + 8,
1009                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1010            }
1011    
1012            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1013                                                    uv_dy, stride2, 0);
1014            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1015                                                    uv_dy, stride2, 0);
1016    
1017    
1018            if(dec->quarterpel) {
1019                    if(!direct) {
1020                            interpolate16x16_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1021                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1022                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1023                    } else {
1024                            interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1025                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1026                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1027                            interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1028                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1029                                            pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1030                            interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1031                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1032                                            pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1033                            interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1034                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1035                                            pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1036                    }
1037            } else {
1038                    interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos, 16 * y_pos,
1039                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1040                    interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos + 8,
1041                                    16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1042                    interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos,
1043                                    16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1044                    interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos + 8,
1045                                    16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1046            }
1047    
1048            interpolate8x8_add_switch(dec->cur.u, backward.u, 8 * x_pos, 8 * y_pos,
1049                            b_uv_dx, b_uv_dy, stride2, 0);
1050            interpolate8x8_add_switch(dec->cur.v, backward.v, 8 * x_pos, 8 * y_pos,
1051                            b_uv_dx, b_uv_dy, stride2, 0);
1052    
1053            stop_comp_timer();
1054    
1055            if (cbp)
1056                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB);
1057                                  }                                  }
1058                                  else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */)  
1059    /* for decode B-frame dbquant */
1060    static __inline int32_t
1061    get_dbquant(Bitstream * bs)
1062                                  {                                  {
1063                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);          if (!BitstreamGetBit(bs))               /*  '0' */
1064                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);                  return (0);
1065                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);          else if (!BitstreamGetBit(bs))  /* '10' */
1066                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);                  return (-2);
1067            else                                                    /* '11' */
1068                    return (2);
1069                                  }                                  }
1070                                  else  // MODE_INTRA, MODE_INTRA_Q  
1071    /*
1072     * decode B-frame mb_type
1073     * bit          ret_value
1074     * 1            0
1075     * 01           1
1076     * 001          2
1077     * 0001         3
1078     */
1079    static int32_t __inline
1080    get_mbtype(Bitstream * bs)
1081                                  {                                  {
1082                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;          int32_t mb_type;
1083                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  
1084                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);          for (mb_type = 0; mb_type <= 3; mb_type++)
1085                    if (BitstreamGetBit(bs))
1086                            return (mb_type);
1087    
1088            return -1;
1089    }
1090    
1091    static void
1092    decoder_bframe(DECODER * dec,
1093                                    Bitstream * bs,
1094                                    int quant,
1095                                    int fcode_forward,
1096                                    int fcode_backward)
1097    {
1098            uint32_t x, y;
1099            VECTOR mv;
1100            const VECTOR zeromv = {0,0};
1101            int i;
1102    
1103            if (!dec->is_edged[0]) {
1104                    start_timer();
1105                    image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1106                                                    dec->width, dec->height, dec->bs_version);
1107                    dec->is_edged[0] = 1;
1108                    stop_edges_timer();
1109            }
1110    
1111            if (!dec->is_edged[1]) {
1112                    start_timer();
1113                    image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1114                                                    dec->width, dec->height, dec->bs_version);
1115                    dec->is_edged[1] = 1;
1116                    stop_edges_timer();
1117            }
1118    
1119            for (y = 0; y < dec->mb_height; y++) {
1120                    /* Initialize Pred Motion Vector */
1121                    dec->p_fmv = dec->p_bmv = zeromv;
1122                    for (x = 0; x < dec->mb_width; x++) {
1123                            MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1124                            MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1125                            const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;
1126                            int32_t intra_dc_threshold; /* fake variable */
1127    
1128                            if (check_resync_marker(bs, fcode_max  - 1)) {
1129                                    int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,
1130                                                                                                             &fcode_forward, &fcode_backward, &intra_dc_threshold);
1131                                    x = bound % dec->mb_width;
1132                                    y = bound / dec->mb_width;
1133                                    /* reset predicted macroblocks */
1134                                    dec->p_fmv = dec->p_bmv = zeromv;
1135                            }
1136    
1137                            mv =
1138                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1139                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1140                            mb->quant = quant;
1141    
1142                            /*
1143                             * skip if the co-located P_VOP macroblock is not coded
1144                             * if not codec in co-located S_VOP macroblock is _not_
1145                             * automatically skipped
1146                             */
1147    
1148                            if (last_mb->mode == MODE_NOT_CODED) {
1149                                    mb->cbp = 0;
1150                                    mb->mode = MODE_FORWARD;
1151                                    decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1);
1152                                          continue;                                          continue;
1153                                  }                                  }
1154    
1155                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, rounding);                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1156                                    const uint8_t modb2 = BitstreamGetBit(bs);
1157    
1158                                    mb->mode = get_mbtype(bs);
1159    
1160                                    if (!modb2)             /* modb=='00' */
1161                                            mb->cbp = BitstreamGetBits(bs, 6);
1162                                    else
1163                                            mb->cbp = 0;
1164    
1165                                    if (mb->mode && mb->cbp) {
1166                                            quant += get_dbquant(bs);
1167                                            if (quant > 31)
1168                                                    quant = 31;
1169                                            else if (quant < 1)
1170                                                    quant = 1;
1171                          }                          }
1172                          else    // not coded                                  mb->quant = quant;
                         {  
1173    
1174                                  mb->mode = MODE_NOT_CODED;                                  if (dec->interlacing) {
1175                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                          if (mb->cbp) {
1176                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                                  mb->field_dct = BitstreamGetBit(bs);
1177                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1178                                            }
1179    
1180                                  // copy macroblock directly from ref to cur                                          if (mb->mode) {
1181                                                    mb->field_pred = BitstreamGetBit(bs);
1182                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1183    
1184                                  start_timer();                                                  if (mb->field_pred) {
1185                                                            mb->field_for_top = BitstreamGetBit(bs);
1186                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1187                                                            mb->field_for_bot = BitstreamGetBit(bs);
1188                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1189                                                    }
1190                                            }
1191                                    }
1192    
1193                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),                          } else {
1194                                                   dec->refn.y + (16*y)*dec->edged_width + (16*x),                                  mb->mode = MODE_DIRECT_NONE_MV;
1195                                                   dec->edged_width);                                  mb->cbp = 0;
1196                            }
1197    
1198                            switch (mb->mode) {
1199                            case MODE_DIRECT:
1200                                    get_b_motion_vector(bs, &mv, 1, zeromv, dec, x, y);
1201    
1202                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),                          case MODE_DIRECT_NONE_MV:
1203                                                   dec->refn.y + (16*y)*dec->edged_width + (16*x+8),                                  for (i = 0; i < 4; i++) {
1204                                                   dec->edged_width);                                          mb->mvs[i].x = last_mb->mvs[i].x*dec->time_bp/dec->time_pp + mv.x;
1205                                            mb->mvs[i].y = last_mb->mvs[i].y*dec->time_bp/dec->time_pp + mv.y;
1206    
1207                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x),                                          mb->b_mvs[i].x = (mv.x)
1208                                                   dec->refn.y + (16*y+8)*dec->edged_width + (16*x),                                                  ?  mb->mvs[i].x - last_mb->mvs[i].x
1209                                                   dec->edged_width);                                                  : last_mb->mvs[i].x*(dec->time_bp - dec->time_pp)/dec->time_pp;
1210                                            mb->b_mvs[i].y = (mv.y)
1211                                                    ? mb->mvs[i].y - last_mb->mvs[i].y
1212                                                    : last_mb->mvs[i].y*(dec->time_bp - dec->time_pp)/dec->time_pp;
1213                                    }
1214    
1215                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8),                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1216                                                   dec->refn.y + (16*y+8)*dec->edged_width + (16*x+8),                                                                                                  mb, x, y, bs, 1);
1217                                                   dec->edged_width);                                  break;
1218    
1219                                  transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),                          case MODE_INTERPOLATE:
1220                                                   dec->refn.u + (8*y)*dec->edged_width/2 + (8*x),                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
1221                                                   dec->edged_width/2);                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1222    
1223                                  transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
1224                                                   dec->refn.v + (8*y)*dec->edged_width/2 + (8*x),                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];
                                                  dec->edged_width/2);  
1225    
1226                                  stop_transfer_timer();                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1227                                                                                            mb, x, y, bs, 0);
1228                                    break;
1229    
1230                            case MODE_BACKWARD:
1231                                    get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
1232                                    dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1233    
1234                                    decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0);
1235                                    break;
1236    
1237                            case MODE_FORWARD:
1238                                    get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
1239                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1240    
1241                                    decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1);
1242                                    break;
1243    
1244                            default:
1245                                    DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);
1246                            }
1247                    } /* End of for */
1248                          }                          }
1249                  }                  }
1250    
1251    /* perform post processing if necessary, and output the image */
1252    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1253                                            xvid_dec_frame_t * frame, xvid_dec_stats_t * stats,
1254                                            int coding_type, int quant)
1255    {
1256            const int brightness = XVID_VERSION_MINOR(frame->version) >= 1 ? frame->brightness : 0;
1257    
1258            if (dec->cartoon_mode)
1259                    frame->general &= ~XVID_FILMEFFECT;
1260    
1261            if ((frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) || brightness!=0)
1262                    && mbs != NULL) /* post process */
1263            {
1264                    /* note: image is stored to tmp */
1265                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1266                    image_postproc(&dec->postproc, &dec->tmp, dec->edged_width,
1267                                               mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1268                                               frame->general, brightness, dec->frames, (coding_type == B_VOP));
1269                    img = &dec->tmp;
1270            }
1271    
1272            image_output(img, dec->width, dec->height,
1273                                     dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1274                                     frame->output.csp, dec->interlacing);
1275    
1276            if (stats) {
1277                    stats->type = coding2type(coding_type);
1278                    stats->data.vop.time_base = (int)dec->time_base;
1279                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1280                    stats->data.vop.qscale_stride = dec->mb_width;
1281                    stats->data.vop.qscale = dec->qscale;
1282                    if (stats->data.vop.qscale != NULL && mbs != NULL) {
1283                            int i;
1284                            for (i = 0; i < dec->mb_width*dec->mb_height; i++)
1285                                    stats->data.vop.qscale[i] = mbs[i].quant;
1286                    } else
1287                            stats->data.vop.qscale = NULL;
1288          }          }
1289  }  }
1290    
1291  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)  int
1292    decoder_decode(DECODER * dec,
1293                                    xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1294  {  {
1295    
1296          Bitstream bs;          Bitstream bs;
1297          uint32_t rounding;          uint32_t rounding;
1298          uint32_t quant;          uint32_t quant = 2;
1299          uint32_t fcode;          uint32_t fcode_forward;
1300            uint32_t fcode_backward;
1301          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1302            WARPPOINTS gmc_warp;
1303            int coding_type;
1304            int success, output, seen_something;
1305    
1306            if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1307                    return XVID_ERR_VERSION;
1308    
1309          start_global_timer();          start_global_timer();
1310    
1311            dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1312            if ((frame->general & XVID_DISCONTINUITY))
1313                    dec->frames = 0;
1314            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1315    
1316            if (frame->length < 0) {        /* decoder flush */
1317                    int ret;
1318                    /* if not decoding "low_delay/packed", and this isn't low_delay and
1319                            we have a reference frame, then outout the reference frame */
1320                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1321                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1322                            dec->frames = 0;
1323                            ret = 0;
1324                    } else {
1325                            if (stats) stats->type = XVID_TYPE_NOTHING;
1326                            ret = XVID_ERR_END;
1327                    }
1328    
1329                    emms();
1330                    stop_global_timer();
1331                    return ret;
1332            }
1333    
1334          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1335    
1336          switch (BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold))          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1337            if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1338          {          {
1339          case P_VOP :                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1340                  decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold);                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1341                  break;                  if (stats) stats->type = XVID_TYPE_NOTHING;
1342                    emms();
1343                    return 1;       /* one byte consumed */
1344            }
1345    
1346            success = 0;
1347            output = 0;
1348            seen_something = 0;
1349    
1350    repeat:
1351    
1352            coding_type = BitstreamReadHeaders(&bs, dec, &rounding,
1353                            &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1354    
1355            DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%"
1356    #if defined(_MSC_VER)
1357        "I64"
1358    #else
1359        "ll"
1360    #endif
1361        "i,  time_pp=%i,  time_bp=%i\n",
1362                                                            coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1363    
1364            if (coding_type == -1) { /* nothing */
1365                    if (success) goto done;
1366                    if (stats) stats->type = XVID_TYPE_NOTHING;
1367                    emms();
1368                    return BitstreamPos(&bs)/8;
1369            }
1370    
1371            if (coding_type == -2 || coding_type == -3) {   /* vol and/or resize */
1372    
1373                    if (coding_type == -3)
1374                            decoder_resize(dec);
1375    
1376                    if (stats) {
1377                            stats->type = XVID_TYPE_VOL;
1378                            stats->data.vol.general = 0;
1379                            /*XXX: if (dec->interlacing)
1380                                    stats->data.vol.general |= ++INTERLACING; */
1381                            stats->data.vol.width = dec->width;
1382                            stats->data.vol.height = dec->height;
1383                            stats->data.vol.par = dec->aspect_ratio;
1384                            stats->data.vol.par_width = dec->par_width;
1385                            stats->data.vol.par_height = dec->par_height;
1386                            emms();
1387                            return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1388                    }
1389                    goto repeat;
1390            }
1391    
1392            if(dec->frames == 0 && coding_type != I_VOP) {
1393                    /* 1st frame is not an i-vop */
1394                    goto repeat;
1395            }
1396    
1397            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1398    
1399            /* packed_mode: special-N_VOP treament */
1400            if (dec->packed_mode && coding_type == N_VOP) {
1401                    if (dec->low_delay_default && dec->frames > 0) {
1402                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1403                            output = 1;
1404                    }
1405                    /* ignore otherwise */
1406            } else if (coding_type != B_VOP) {
1407                    switch(coding_type) {
1408          case I_VOP :          case I_VOP :
                 //DEBUG1("",intra_dc_threshold);  
1409                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1410                  break;                  break;
1411                    case P_VOP :
1412          case B_VOP :    // ignore                          decoder_pframe(dec, &bs, rounding, quant,
1413                                                    fcode_forward, intra_dc_threshold, NULL);
1414                  break;                  break;
1415                    case S_VOP :
1416          case N_VOP :    // vop not coded                          decoder_pframe(dec, &bs, rounding, quant,
1417                                                    fcode_forward, intra_dc_threshold, &gmc_warp);
1418                            break;
1419                    case N_VOP :
1420                            /* XXX: not_coded vops are not used for forward prediction */
1421                            /* we should not swap(last_mbs,mbs) */
1422                            image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1423                            SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */
1424                  break;                  break;
   
         default :  
                 return XVID_ERR_FAIL;  
1425          }          }
1426    
1427          frame->length = BitstreamPos(&bs) / 8;                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1428                    if (!(dec->low_delay_default && dec->packed_mode)) {
1429          start_timer();                          if (dec->low_delay) {
1430          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1431                       frame->image, frame->stride, frame->colorspace);                                  output = 1;
1432          stop_conv_timer();                          } else if (dec->frames > 0)     { /* is the reference frame valid? */
1433                                    /* output the reference frame */
1434                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1435                                    output = 1;
1436                            }
1437                    }
1438    
1439                    image_swap(&dec->refn[0], &dec->refn[1]);
1440                    dec->is_edged[1] = dec->is_edged[0];
1441                    image_swap(&dec->cur, &dec->refn[0]);
1442                    dec->is_edged[0] = 0;
1443                    SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1444                    dec->last_coding_type = coding_type;
1445    
1446                    dec->frames++;
1447                    seen_something = 1;
1448    
1449            } else {        /* B_VOP */
1450    
1451                    if (dec->low_delay) {
1452                            DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1453                            dec->low_delay = 0;
1454                    }
1455    
1456                    if (dec->frames < 2) {
1457                            /* attemping to decode a bvop without atleast 2 reference frames */
1458                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1459                                                    "broken b-frame, mising ref frames");
1460                            if (stats) stats->type = XVID_TYPE_NOTHING;
1461                    } else if (dec->time_pp <= dec->time_bp) {
1462                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1463                            decoded in vfw. */
1464                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1465                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1466                            if (stats) stats->type = XVID_TYPE_NOTHING;
1467                    } else {
1468                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1469                            decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1470                    }
1471    
1472                    output = 1;
1473                    dec->frames++;
1474            }
1475    
1476    #if 0 /* Avoids to read to much data because of 32bit reads in our BS functions */
1477             BitstreamByteAlign(&bs);
1478    #endif
1479    
1480            /* low_delay_default mode: repeat in packed_mode */
1481            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
1482                    success = 1;
1483                    goto repeat;
1484            }
1485    
1486    done :
1487    
1488      /* if we reach here without outputing anything _and_
1489         the calling application has specified low_delay_default,
1490         we *must* output something.
1491         this always occurs on the first call to decode() call
1492         when bframes are present in the bitstream. it may also
1493         occur if no vops  were seen in the bitstream
1494    
1495         if packed_mode is enabled, then we output the recently
1496         decoded frame (the very first ivop). otherwise we have
1497         nothing to display, and therefore output a black screen.
1498      */
1499      if (dec->low_delay_default && output == 0) {
1500        if (dec->packed_mode && seen_something) {
1501                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1502        } else {
1503                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1504                            decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant);
1505                            if (stats) stats->type = XVID_TYPE_NOTHING;
1506                    }
1507            }
1508    
1509          emms();          emms();
   
1510          stop_global_timer();          stop_global_timer();
1511    
1512          return XVID_ERR_OK;          return (BitstreamPos(&bs) + 7) / 8;     /* number of bytes consumed */
   
1513  }  }

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.71

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