[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.49.2.18, Wed Oct 22 09:47:52 2003 UTC revision 1.71, Mon May 23 09:29:43 2005 UTC
# Line 4  Line 4 
4   *  - Decoder Module -   *  - Decoder Module -
5   *   *
6   *  Copyright(C) 2002      MinChen <chenm001@163.com>   *  Copyright(C) 2002      MinChen <chenm001@163.com>
7   *               2002-2003 Peter Ross <pross@xvid.org>   *               2002-2004 Peter Ross <pross@xvid.org>
8   *   *
9   *  This program is free software ; you can redistribute it and/or modify   *  This program is free software ; you can redistribute it and/or modify
10   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 41  Line 41 
41  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
42    
43  #include "quant/quant.h"  #include "quant/quant.h"
44    #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"
 #include "image/reduced.h"  
49  #include "image/font.h"  #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"
# Line 57  Line 58 
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  static int  static int
# Line 71  Line 73 
73    
74          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
75    
76          if (dec->last_mbs)    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    
83    
84                  xvid_free(dec->last_mbs);                  xvid_free(dec->last_mbs);
         if (dec->mbs)  
85                  xvid_free(dec->mbs);                  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 */          /* realloc */
92          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
# Line 83  Line 95 
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                  xvid_free(dec);              || image_create(&dec->refn[0], dec->edged_width, dec->edged_height)
100                  return XVID_ERR_MEMORY;              || 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          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {        || image_create(&dec->gmc, dec->edged_width, dec->edged_height) )
104                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);      goto memory_error;
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         /* Support B-frame to reference last 2 frame */  
         if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
         if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
105    
106          dec->mbs =          dec->mbs =
107                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
108                                          CACHE_LINE);                                          CACHE_LINE);
109          if (dec->mbs == NULL) {          if (dec->mbs == NULL)
110                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);            goto memory_error;
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
111          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
112    
113          /* For skip MB flag */          /* For skip MB flag */
114          dec->last_mbs =          dec->last_mbs =
115                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
116                                          CACHE_LINE);                                          CACHE_LINE);
117          if (dec->last_mbs == NULL) {          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);                  xvid_free(dec->mbs);
134                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
135                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
136                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
137                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
138                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);                  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    
         memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);  
   
         return 0;  
 }  
   
144    
145  int  int
146  decoder_create(xvid_dec_create_t * create)  decoder_create(xvid_dec_create_t * create)
# Line 175  Line 154 
154          if (dec == NULL) {          if (dec == NULL) {
155                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
156          }          }
157    
158          memset(dec, 0, sizeof(DECODER));          memset(dec, 0, sizeof(DECODER));
159    
160            dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
161            if (dec->mpeg_quant_matrices == NULL) {
162                    xvid_free(dec);
163                    return XVID_ERR_MEMORY;
164            }
165    
166          create->handle = dec;          create->handle = dec;
167    
168          dec->width = create->width;          dec->width = create->width;
# Line 191  Line 177 
177          /* image based GMC */          /* image based GMC */
178          image_null(&dec->gmc);          image_null(&dec->gmc);
179    
   
180          dec->mbs = NULL;          dec->mbs = NULL;
181          dec->last_mbs = NULL;          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 */          /* For B-frame support (used to save reference frame's time */
189          dec->frames = 0;          dec->frames = 0;
190          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
191          dec->low_delay = 0;          dec->low_delay = 0;
192          dec->packed_mode = 0;          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);          dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
196    
# Line 217  Line 206 
206  {  {
207          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
208          xvid_free(dec->mbs);          xvid_free(dec->mbs);
209            xvid_free(dec->qscale);
210    
211          /* image based GMC */          /* image based GMC */
212          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
# Line 226  Line 216 
216          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
217          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);          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();
# Line 247  Line 238 
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,                                  const unsigned int bound)
                                 const int reduced_resolution)  
242  {  {
243    
244          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 261  Line 251 
251          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
252          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
253    
         if (reduced_resolution) {  
                 pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);  
                 pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);  
                 pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);  
         }else{  
254                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
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    
# Line 316  Line 300 
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);                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
309                  } else {                  } else {
310                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
311                  }                  }
312                  stop_iquant_timer();                  stop_iquant_timer();
313    
# Line 339  Line 323 
323          }          }
324    
325          start_timer();          start_timer();
   
         if (reduced_resolution)  
         {  
                 next_block*=2;  
                 copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);  
                 copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);  
                 copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);  
                 copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);  
                 copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);  
                 copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);  
         }else{  
326                  transfer_16to8copy(pY_Cur, &data[0 * 64], stride);                  transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
327                  transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);                  transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
328                  transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);                  transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
329                  transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);                  transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
330                  transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);                  transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
331                  transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);                  transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
         }  
332          stop_transfer_timer();          stop_transfer_timer();
333  }  }
334    
# Line 367  Line 339 
339                                  uint8_t * pY_Cur,                                  uint8_t * pY_Cur,
340                                  uint8_t * pU_Cur,                                  uint8_t * pU_Cur,
341                                  uint8_t * pV_Cur,                                  uint8_t * pV_Cur,
                                 const int reduced_resolution,  
342                                  const MACROBLOCK * pMB)                                  const MACROBLOCK * pMB)
343  {  {
344          DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(data, 1, 64, int16_t, CACHE_LINE);
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
345    
346          int stride = dec->edged_width;          int stride = dec->edged_width;
347          int next_block = stride * (reduced_resolution ? 16 : 8);          int next_block = stride * 8;
         const int stride2 = stride/2;  
348          int i;          int i;
349          const uint32_t iQuant = pMB->quant;          const uint32_t iQuant = pMB->quant;
350          const int direction = dec->alternate_vertical_scan ? 2 : 0;          const int direction = dec->alternate_vertical_scan ? 2 : 0;
351          const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter;          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          for (i = 0; i < 6; i++) {          uint8_t *dst[6];
367            int strides[6];
368    
                 if (cbp & (1 << (5 - i))) {     /* coded */  
369    
370                          memset(block, 0, 64 * sizeof(int16_t)); /* clear */          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                            /* Clear the block */
390                            memset(&data[0], 0, 64*sizeof(int16_t));
391    
392                            /* Decode coeffs and dequantize on the fly */
393                          start_timer();                          start_timer();
394                          get_inter_block(bs, block, direction);                          get_inter_block(bs, &data[0], direction, iQuant, get_inter_matrix(dec->mpeg_quant_matrices));
395                          stop_coding_timer();                          stop_coding_timer();
396    
397                            /* iDCT */
398                          start_timer();                          start_timer();
399                          dequant(&data[i * 64], block, iQuant);                          idct(&data[0]);
400                          stop_iquant_timer();                          stop_idct_timer();
401    
402                            /* Add this residual to the predicted block */
403                          start_timer();                          start_timer();
404                          idct(&data[i * 64]);                          transfer_16to8add(dst[i], &data[0], strides[i]);
405                          stop_idct_timer();                          stop_transfer_timer();
406                  }                  }
407          }          }
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
408          }          }
409    
410          start_timer();  static void __inline
411          if (reduced_resolution) {  validate_vector(VECTOR * mv, unsigned int x_pos, unsigned int y_pos, const DECODER * dec)
412                  if (cbp & 32)  {
413                          add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);          /* clip a vector to valid range
414                  if (cbp & 16)             prevents crashes if bitstream is broken
415                          add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);          */
416                  if (cbp & 8)          int shift = 5 + dec->quarterpel;
417                          add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);          int xborder_high = (int)(dec->mb_width - x_pos) << shift;
418                  if (cbp & 4)          int xborder_low = (-(int)x_pos-1) << shift;
419                          add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);          int yborder_high = (int)(dec->mb_height - y_pos) << shift;
420                  if (cbp & 2)          int yborder_low = (-(int)y_pos-1) << shift;
421                          add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);  
422                  if (cbp & 1)  #define CHECK_MV(mv) \
423                          add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);          do { \
424          } else {          if ((mv).x > xborder_high) { \
425                  if (cbp & 32)                  DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", (mv).x, xborder_high, x_pos, y_pos); \
426                          transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  (mv).x = xborder_high; \
427                  if (cbp & 16)          } else if ((mv).x < xborder_low) { \
428                          transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);                  DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", (mv).x, xborder_low, x_pos, y_pos); \
429                  if (cbp & 8)                  (mv).x = xborder_low; \
430                          transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);          } \
431                  if (cbp & 4)          if ((mv).y > yborder_high) { \
432                          transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);                  DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", (mv).y, yborder_high, x_pos, y_pos); \
433                  if (cbp & 2)                  (mv).y = yborder_high; \
434                          transfer_16to8add(pU_Cur, &data[4 * 64], stride2);          } else if ((mv).y < yborder_low) { \
435                  if (cbp & 1)                  DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", (mv).y, yborder_low, x_pos, y_pos); \
436                          transfer_16to8add(pV_Cur, &data[5 * 64], stride2);                  (mv).y = yborder_low; \
437          }          } \
438          stop_transfer_timer();          } 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 */  /* decode an inter macroblock */
# Line 446  Line 452 
452                                  const uint32_t cbp,                                  const uint32_t cbp,
453                                  Bitstream * bs,                                  Bitstream * bs,
454                                  const uint32_t rounding,                                  const uint32_t rounding,
                                 const int reduced_resolution,  
455                                  const int ref)                                  const int ref)
456  {  {
457          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
# Line 458  Line 463 
463          int uv_dx, uv_dy;          int uv_dx, uv_dy;
464          VECTOR mv[4];   /* local copy of mvs */          VECTOR mv[4];   /* local copy of mvs */
465    
         if (reduced_resolution) {  
                 pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);  
                 pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);  
                 pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);  
                 for (i = 0; i < 4; i++) {  
                         mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);  
                         mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);  
                 }  
         } else {  
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++)                  for (i = 0; i < 4; i++)
470                          mv[i] = pMB->mvs[i];                          mv[i] = pMB->mvs[i];
471          }  
472            validate_vector(mv, x_pos, y_pos, dec);
473    
474          start_timer();          start_timer();
475    
# Line 487  Line 484 
484                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
485                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
486    
487                  if (reduced_resolution)                  if (dec->quarterpel)
                         interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,  
                                                                         mv[0].x, mv[0].y, stride, rounding);  
                 else if (dec->quarterpel)  
488                          interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,                          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,                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
490                                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                                          mv[0].x, mv[0].y, stride, rounding);
# Line 511  Line 505 
505                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
506                  uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];                  uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
507    
508                  if (reduced_resolution) {                  if (dec->quarterpel) {
                         interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,  
                                                                 mv[0].x, mv[0].y, stride, rounding);  
                         interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,  
                                                                 mv[1].x, mv[1].y, stride, rounding);  
                         interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos, 32*y_pos + 16,  
                                                                 mv[2].x, mv[2].y, stride, rounding);  
                         interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos + 16,  
                                                                 mv[3].x, mv[3].y, stride, rounding);  
                         interpolate16x16_switch(dec->cur.u, dec->refn[0].u , 16 * x_pos, 16 * y_pos,  
                                                                 uv_dx, uv_dy, stride2, rounding);  
                         interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,  
                                                                 uv_dx, uv_dy, stride2, rounding);  
   
                 } else if (dec->quarterpel) {  
509                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,                          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,                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
511                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                          mv[0].x, mv[0].y, stride, rounding);
# Line 551  Line 531 
531          }          }
532    
533          /* chroma */          /* chroma */
         if (reduced_resolution) {  
                 interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,  
                                                                 uv_dx, uv_dy, stride2, rounding);  
                 interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,  
                                                                 uv_dx, uv_dy, stride2, rounding);  
         } else {  
534                  interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,                  interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
535                                                                  uv_dx, uv_dy, stride2, rounding);                                                                  uv_dx, uv_dy, stride2, rounding);
536                  interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,                  interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
537                                                                  uv_dx, uv_dy, stride2, rounding);                                                                  uv_dx, uv_dy, stride2, rounding);
         }  
538    
539          stop_comp_timer();          stop_comp_timer();
540    
541          if (cbp)          if (cbp)
542                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur,                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB);
                                                         reduced_resolution, pMB);  
543  }  }
544    
545  static void  static void
# Line 614  Line 586 
586          stop_transfer_timer();          stop_transfer_timer();
587    
588          if (cbp)          if (cbp)
589                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB);
590    
591  }  }
592    
# Line 622  Line 594 
594  static void  static void
595  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
596                                  Bitstream * bs,                                  Bitstream * bs,
                                 int reduced_resolution,  
597                                  int quant,                                  int quant,
598                                  int intra_dc_threshold)                                  int intra_dc_threshold)
599  {  {
600          uint32_t bound;          uint32_t bound;
601          uint32_t x, y;          uint32_t x, y;
602          uint32_t mb_width = dec->mb_width;          const uint32_t mb_width = dec->mb_width;
603          uint32_t mb_height = dec->mb_height;          const uint32_t mb_height = dec->mb_height;
   
         if (reduced_resolution) {  
                 mb_width = (dec->width + 31) / 32;  
                 mb_height = (dec->height + 31) / 32;  
         }  
604    
605          bound = 0;          bound = 0;
606    
# Line 690  Line 656 
656                          }                          }
657    
658                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
659                                                          intra_dc_threshold, bound, reduced_resolution);                                                          intra_dc_threshold, bound);
660    
661                  }                  }
662                  if(dec->out_frm)                  if(dec->out_frm)
# Line 748  Line 714 
714  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
715                                  Bitstream * bs,                                  Bitstream * bs,
716                                  int rounding,                                  int rounding,
                                 int reduced_resolution,  
717                                  int quant,                                  int quant,
718                                  int fcode,                                  int fcode,
719                                  int intra_dc_threshold,                                  int intra_dc_threshold,
# Line 757  Line 722 
722          uint32_t x, y;          uint32_t x, y;
723          uint32_t bound;          uint32_t bound;
724          int cp_mb, st_mb;          int cp_mb, st_mb;
725          uint32_t mb_width = dec->mb_width;          const uint32_t mb_width = dec->mb_width;
726          uint32_t mb_height = dec->mb_height;          const uint32_t mb_height = dec->mb_height;
   
         if (reduced_resolution) {  
                 mb_width = (dec->width + 31) / 32;  
                 mb_height = (dec->height + 31) / 32;  
         }  
727    
728            if (!dec->is_edged[0]) {
729          start_timer();          start_timer();
730          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
731                                          dec->width, dec->height);                                                  dec->width, dec->height, dec->bs_version);
732                    dec->is_edged[0] = 1;
733          stop_edges_timer();          stop_edges_timer();
734            }
735    
736          if (gmc_warp) {          if (gmc_warp) {
737                  /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */                  /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
# Line 839  Line 802 
802                                  mb->quant = quant;                                  mb->quant = quant;
803    
804                                  if (dec->interlacing) {                                  if (dec->interlacing) {
805                                          if ((cbp || intra) && !mcsel) {                                          if (cbp || intra) {
806                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
807                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", 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                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
813    
# Line 879  Line 842 
842                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
843                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =     0;                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =     0;
844                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
845                                                                          intra_dc_threshold, bound, reduced_resolution);                                                                          intra_dc_threshold, bound);
846                                          continue;                                          continue;
847                                  }                                  }
848    
849                                  decoder_mbinter(dec, mb, x, y, cbp, bs,                                  decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0);
                                                                 rounding, reduced_resolution, 0);  
850    
851                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
852                                  mb->mode = MODE_NOT_CODED_GMC;                                  mb->mode = MODE_NOT_CODED_GMC;
853                                    mb->quant = quant;
854                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
855    
856                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
# Line 897  Line 860 
860                                  st_mb = x+1;                                  st_mb = x+1;
861                          } else {        /* not coded P_VOP macroblock */                          } else {        /* not coded P_VOP macroblock */
862                                  mb->mode = MODE_NOT_CODED;                                  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;                                  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;                                  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,                                  decoder_mbinter(dec, mb, x, y, 0, bs,
869                                                                  rounding, reduced_resolution, 0);                                                                  rounding, 0);
870    
871                                  if(dec->out_frm && cp_mb > 0) {                                  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);                                          output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
# Line 923  Line 887 
887  get_b_motion_vector(Bitstream * bs,  get_b_motion_vector(Bitstream * bs,
888                                          VECTOR * mv,                                          VECTOR * mv,
889                                          int fcode,                                          int fcode,
890                                          const VECTOR pmv)                                          const VECTOR pmv,
891                                            const DECODER * const dec,
892                                            const int x, const int y)
893  {  {
894          const int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
895          const int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
# Line 955  Line 921 
921  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
922                                                                  IMAGE forward,                                                                  IMAGE forward,
923                                                                  IMAGE backward,                                                                  IMAGE backward,
924                                                                  const MACROBLOCK * pMB,                                                                  MACROBLOCK * pMB,
925                                                                  const uint32_t x_pos,                                                                  const uint32_t x_pos,
926                                                                  const uint32_t y_pos,                                                                  const uint32_t y_pos,
927                                                                  Bitstream * bs,                                                                  Bitstream * bs,
# Line 972  Line 938 
938          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          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);          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) {          if (!direct) {
945                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
946                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
   
947                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
948                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
949    
# Line 988  Line 956 
956    
957                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
958                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
   
959                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];                  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];                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
961    
962          } else {          } else {
                 if(dec->quarterpel) {  
                         uv_dx = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);  
                         uv_dy = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);  
                         b_uv_dx = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);  
                         b_uv_dy = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);  
                 } else {  
963                          uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                          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;                          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;                          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;                          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];                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
# Line 1038  Line 1005 
1005                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                          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,                  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);                                                          pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1008                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos + 8,
1009                                                          16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                          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,          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
# Line 1050  Line 1017 
1017    
1018          if(dec->quarterpel) {          if(dec->quarterpel) {
1019                  if(!direct) {                  if(!direct) {
1020                          interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          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,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1022                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1023                  } else {                  } else {
1024                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          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,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1026                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1027                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          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,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1029                                                                                  pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);                                                                                  pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1030                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          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,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1032                                                                                  pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);                                                                                  pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1033                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          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,                                                                                  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);                                                                                  pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1036                  }                  }
1037          } else {          } else {
1038                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,                  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);                                                          pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1040                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  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);                                                          16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1042                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,                  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);                                                          16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1044                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  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);                                                          16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1046          }          }
1047    
1048          interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,          interpolate8x8_add_switch(dec->cur.u, backward.u, 8 * x_pos, 8 * y_pos,
1049                                                  b_uv_dx, b_uv_dy, stride2, 0);                                                  b_uv_dx, b_uv_dy, stride2, 0);
1050          interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_add_switch(dec->cur.v, backward.v, 8 * x_pos, 8 * y_pos,
1051                                                  b_uv_dx, b_uv_dy, stride2, 0);                                                  b_uv_dx, b_uv_dy, stride2, 0);
1052    
         interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,  
                                                 dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,  
                                                 dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,  
                                                 stride, 1, 8);  
   
         interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,  
                                                 dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,  
                                                 dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,  
                                                 stride, 1, 8);  
   
         interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,  
                                                 dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,  
                                                 dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,  
                                                 stride, 1, 8);  
   
         interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,  
                                                 dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,  
                                                 dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,  
                                                 stride, 1, 8);  
   
         interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 stride2, 1, 8);  
   
         interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 stride2, 1, 8);  
   
1053          stop_comp_timer();          stop_comp_timer();
1054    
1055          if (cbp)          if (cbp)
1056                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB);
1057  }  }
1058    
1059  /* for decode B-frame dbquant */  /* for decode B-frame dbquant */
# Line 1161  Line 1098 
1098          uint32_t x, y;          uint32_t x, y;
1099          VECTOR mv;          VECTOR mv;
1100          const VECTOR zeromv = {0,0};          const VECTOR zeromv = {0,0};
         const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;  
1101          int i;          int i;
1102    
1103  #ifdef BFRAMES_DEC_DEBUG          if (!dec->is_edged[0]) {
1104          FILE *fp;                  start_timer();
1105          static char first=0;                  image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1106  #define BFRAME_DEBUG                                                  dec->width, dec->height, dec->bs_version);
1107          if (!first && fp) { \                  dec->is_edged[0] = 1;
1108                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \                  stop_edges_timer();
1109          }          }
 #endif  
1110    
1111            if (!dec->is_edged[1]) {
1112          start_timer();          start_timer();
         image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,  
                                         dec->width, dec->height);  
1113          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1114                                          dec->width, dec->height);                                                  dec->width, dec->height, dec->bs_version);
1115                    dec->is_edged[1] = 1;
1116          stop_edges_timer();          stop_edges_timer();
   
 #ifdef BFRAMES_DEC_DEBUG  
         if (!first){  
                 fp=fopen("C:\\XVIDDBG.TXT","w");  
1117          }          }
 #endif  
1118    
1119          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1120                  /* Initialize Pred Motion Vector */                  /* Initialize Pred Motion Vector */
# Line 1192  Line 1122 
1122                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1123                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1124                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          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 =                          mv =
1138                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
# Line 1207  Line 1148 
1148                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1149                                  mb->cbp = 0;                                  mb->cbp = 0;
1150                                  mb->mode = MODE_FORWARD;                                  mb->mode = MODE_FORWARD;
1151                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1);
1152                                  continue;                                  continue;
1153                          }                          }
1154    
# Line 1256  Line 1197 
1197    
1198                          switch (mb->mode) {                          switch (mb->mode) {
1199                          case MODE_DIRECT:                          case MODE_DIRECT:
1200                                  get_b_motion_vector(bs, &mv, 1, zeromv);                                  get_b_motion_vector(bs, &mv, 1, zeromv, dec, x, y);
1201    
1202                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
1203                                  for (i = 0; i < 4; i++) {                                  for (i = 0; i < 4; i++) {
1204                                          mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + mv.x);                                          mb->mvs[i].x = last_mb->mvs[i].x*dec->time_bp/dec->time_pp + mv.x;
1205                                          mb->b_mvs[i].x = (int32_t) ((mv.x == 0)                                          mb->mvs[i].y = last_mb->mvs[i].y*dec->time_bp/dec->time_pp + mv.y;
1206                                                                          ? ((TRB - TRD) * last_mb->mvs[i].x) / TRD  
1207                                                                          : mb->mvs[i].x - last_mb->mvs[i].x);                                          mb->b_mvs[i].x = (mv.x)
1208                                          mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y);                                                  ?  mb->mvs[i].x - last_mb->mvs[i].x
1209                                          mb->b_mvs[i].y = (int32_t) ((mv.y == 0)                                                  : last_mb->mvs[i].x*(dec->time_bp - dec->time_pp)/dec->time_pp;
1210                                                                          ? ((TRB - TRD) * last_mb->mvs[i].y) / TRD                                          mb->b_mvs[i].y = (mv.y)
1211                                                                          : mb->mvs[i].y - last_mb->mvs[i].y);                                                  ? 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                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
# Line 1275  Line 1217 
1217                                  break;                                  break;
1218    
1219                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1220                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
1221                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1222    
1223                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv);                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
1224                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];
1225    
1226                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
# Line 1286  Line 1228 
1228                                  break;                                  break;
1229    
1230                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1231                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv);                                  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];                                  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, 0);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0);
1235                                  break;                                  break;
1236    
1237                          case MODE_FORWARD:                          case MODE_FORWARD:
1238                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  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];                                  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, 0, 1);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1);
1242                                  break;                                  break;
1243    
1244                          default:                          default:
# Line 1304  Line 1246 
1246                          }                          }
1247                  } /* End of for */                  } /* End of for */
1248          }          }
   
 #ifdef BFRAMES_DEC_DEBUG  
         if (!first){  
                 first=1;  
                 if (fp)  
                         fclose(fp);  
         }  
 #endif  
1249  }  }
1250    
1251  /* perform post processing if necessary, and output the image */  /* perform post processing if necessary, and output the image */
1252  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1253                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)                                          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,          image_output(img, dec->width, dec->height,
1273                                   dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,                                   dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1274                                   frame->output.csp, dec->interlacing);                                   frame->output.csp, dec->interlacing);
# Line 1326  Line 1277 
1277                  stats->type = coding2type(coding_type);                  stats->type = coding2type(coding_type);
1278                  stats->data.vop.time_base = (int)dec->time_base;                  stats->data.vop.time_base = (int)dec->time_base;
1279                  stats->data.vop.time_increment = 0;     /* XXX: todo */                  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  int
1292  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1293                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
# Line 1337  Line 1295 
1295    
1296          Bitstream bs;          Bitstream bs;
1297          uint32_t rounding;          uint32_t rounding;
1298          uint32_t reduced_resolution;          uint32_t quant = 2;
         uint32_t quant;  
1299          uint32_t fcode_forward;          uint32_t fcode_forward;
1300          uint32_t fcode_backward;          uint32_t fcode_backward;
1301          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
# Line 1361  Line 1318 
1318                  /* if not decoding "low_delay/packed", and this isn't low_delay and                  /* if not decoding "low_delay/packed", and this isn't low_delay and
1319                          we have a reference frame, then outout the reference frame */                          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) {                  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);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1322                          dec->frames = 0;                          dec->frames = 0;
1323                          ret = 0;                          ret = 0;
1324                  } else {                  } else {
# Line 1392  Line 1349 
1349    
1350  repeat:  repeat:
1351    
1352          coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,          coding_type = BitstreamReadHeaders(&bs, dec, &rounding,
1353                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1354    
1355          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",          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);                                                          coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1363    
1364          if (coding_type == -1) { /* nothing */          if (coding_type == -1) { /* nothing */
# Line 1426  Line 1389 
1389                  goto repeat;                  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 */          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 */          /* packed_mode: special-N_VOP treament */
1400          if (dec->packed_mode && coding_type == N_VOP) {          if (dec->packed_mode && coding_type == N_VOP) {
1401                  if (dec->low_delay_default && dec->frames > 0) {                  if (dec->low_delay_default && dec->frames > 0) {
1402                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1403                          output = 1;                          output = 1;
1404                  }                  }
1405                  /* ignore otherwise */                  /* ignore otherwise */
1406          } else if (coding_type != B_VOP) {          } else if (coding_type != B_VOP) {
1407                  switch(coding_type) {                  switch(coding_type) {
1408                  case I_VOP :                  case I_VOP :
1409                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1410                          break;                          break;
1411                  case P_VOP :                  case P_VOP :
1412                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                          decoder_pframe(dec, &bs, rounding, quant,
1413                                                  fcode_forward, intra_dc_threshold, NULL);                                                  fcode_forward, intra_dc_threshold, NULL);
1414                          break;                          break;
1415                  case S_VOP :                  case S_VOP :
1416                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                          decoder_pframe(dec, &bs, rounding, quant,
1417                                                  fcode_forward, intra_dc_threshold, &gmc_warp);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1418                          break;                          break;
1419                  case N_VOP :                  case N_VOP :
1420                          /* XXX: not_coded vops are not used for forward prediction */                          /* XXX: not_coded vops are not used for forward prediction */
1421                          /* we should not swap(last_mbs,mbs) */                          /* we should not swap(last_mbs,mbs) */
1422                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          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;
1425                  }                  }
1426    
                 if (reduced_resolution) {  
                         image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,  
                                 (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,  
                                 16, 0);  
                 }  
   
1427                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1428                  if (!(dec->low_delay_default && dec->packed_mode)) {                  if (!(dec->low_delay_default && dec->packed_mode)) {
1429                          if (dec->low_delay) {                          if (dec->low_delay) {
1430                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1431                                  output = 1;                                  output = 1;
1432                          } else if (dec->frames > 0)     { /* is the reference frame valid? */                          } else if (dec->frames > 0)     { /* is the reference frame valid? */
1433                                  /* output the reference frame */                                  /* output the reference frame */
1434                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1435                                  output = 1;                                  output = 1;
1436                          }                          }
1437                  }                  }
1438    
1439                  image_swap(&dec->refn[0], &dec->refn[1]);                  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]);                  image_swap(&dec->cur, &dec->refn[0]);
1442                    dec->is_edged[0] = 0;
1443                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
                 dec->last_reduced_resolution = reduced_resolution;  
1444                  dec->last_coding_type = coding_type;                  dec->last_coding_type = coding_type;
1445    
1446                  dec->frames++;                  dec->frames++;
# Line 1486  Line 1450 
1450    
1451                  if (dec->low_delay) {                  if (dec->low_delay) {
1452                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1453                          dec->low_delay = 1;                          dec->low_delay = 0;
1454                  }                  }
1455    
1456                  if (dec->frames < 2) {                  if (dec->frames < 2) {
1457                          /* attemping to decode a bvop without atleast 2 reference frames */                          /* attemping to decode a bvop without atleast 2 reference frames */
1458                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1459                                                  "broken b-frame, mising ref frames");                                                  "broken b-frame, mising ref frames");
1460                            if (stats) stats->type = XVID_TYPE_NOTHING;
1461                  } else if (dec->time_pp <= dec->time_bp) {                  } else if (dec->time_pp <= dec->time_bp) {
1462                          /* this occurs when dx50_bvop_compatibility==0 sequences are                          /* this occurs when dx50_bvop_compatibility==0 sequences are
1463                          decoded in vfw. */                          decoded in vfw. */
1464                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          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);                                                  "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1466                            if (stats) stats->type = XVID_TYPE_NOTHING;
1467                  } else {                  } else {
1468                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1469                            decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1470                  }                  }
1471    
                 decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);  
1472                  output = 1;                  output = 1;
1473                  dec->frames++;                  dec->frames++;
1474          }          }
1475    
1476    #if 0 /* Avoids to read to much data because of 32bit reads in our BS functions */
1477          BitstreamByteAlign(&bs);          BitstreamByteAlign(&bs);
1478    #endif
1479    
1480          /* low_delay_default mode: repeat in packed_mode */          /* low_delay_default mode: repeat in packed_mode */
1481          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
# Line 1517  Line 1485 
1485    
1486  done :  done :
1487    
1488          /* low_delay_default mode: if we've gotten here without outputting anything,    /* if we reach here without outputing anything _and_
1489             then output the recently decoded frame, or print an error message  */       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) {          if (dec->low_delay_default && output == 0) {
1500                  if (dec->packed_mode && seen_something) {                  if (dec->packed_mode && seen_something) {
1501                          /* output the recently decoded frame */                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
                         decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);  
1502                  } else {                  } else {
1503                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1504                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant);
                                 "warning: nothing to output");  
                         image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,  
                                 "bframe decoder lag");  
   
                         decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);  
1505                          if (stats) stats->type = XVID_TYPE_NOTHING;                          if (stats) stats->type = XVID_TYPE_NOTHING;
1506                  }                  }
1507          }          }
# Line 1538  Line 1509 
1509          emms();          emms();
1510          stop_global_timer();          stop_global_timer();
1511    
1512          return BitstreamPos(&bs) / 8;   /* number of bytes consumed */          return (BitstreamPos(&bs) + 7) / 8;     /* number of bytes consumed */
1513  }  }

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

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