[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.54, Sun Apr 11 09:41:27 2004 UTC revision 1.72, Mon Aug 1 18:37:46 2005 UTC
# Line 46  Line 46 
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 73  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);
         if (dec->qscale)  
86                  xvid_free(dec->qscale);                  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 87  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                  xvid_free(dec->mbs);            goto memory_error;
                 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);  
                 image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
119          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
120    
121          /* nothing happens if that fails */          /* nothing happens if that fails */
# Line 171  Line 126 
126                  memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height);                  memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height);
127    
128          return 0;          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);
141      return XVID_ERR_MEMORY;
142  }  }
143    
144    
# Line 222  Line 190 
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 269  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 283  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 338  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();
# Line 361  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 389  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    
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                  if (cbp & (1 << (5 - i))) {     /* coded */          for (i = 0; i < 6; i++) {
386                    /* Process only coded blocks */
387                    if (cbp & (1 << (5 - i))) {
388    
389                          memset(block, 0, 64 * sizeof(int16_t)); /* clear */                          /* 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, dec->mpeg_quant_matrices);                          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    /* Up to this version, chroma rounding was wrong with qpel.
447     * So we try to be backward compatible to avoid artifacts */
448    #define BS_VERSION_BUGGY_CHROMA_ROUNDING 1
449    
450  /* decode an inter macroblock */  /* decode an inter macroblock */
451  static void  static void
# Line 468  Line 456 
456                                  const uint32_t cbp,                                  const uint32_t cbp,
457                                  Bitstream * bs,                                  Bitstream * bs,
458                                  const uint32_t rounding,                                  const uint32_t rounding,
                                 const int reduced_resolution,  
459                                  const int ref)                                  const int ref)
460  {  {
461          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
# Line 480  Line 467 
467          int uv_dx, uv_dy;          int uv_dx, uv_dy;
468          VECTOR mv[4];   /* local copy of mvs */          VECTOR mv[4];   /* local copy of mvs */
469    
         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 {  
470                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
471                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
472                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
473                  for (i = 0; i < 4; i++)                  for (i = 0; i < 4; i++)
474                          mv[i] = pMB->mvs[i];                          mv[i] = pMB->mvs[i];
         }  
475    
476          for (i = 0; i < 4; i++) {          validate_vector(mv, x_pos, y_pos, dec);
                 /* clip to valid range */  
                 int border = (int)(dec->mb_width - x_pos) << (5 + dec->quarterpel);  
                 if (mv[i].x > border) {  
                         DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", mv[i].x, border, x_pos, y_pos);  
                         mv[i].x = border;  
                 } else {  
                         border = (-(int)x_pos-1) << (5 + dec->quarterpel);  
                         if (mv[i].x < border) {  
                                 DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", mv[i].x, border, x_pos, y_pos);  
                                 mv[i].x = border;  
                         }  
                 }  
   
                 border = (int)(dec->mb_height - y_pos) << (5 + dec->quarterpel);  
                 if (mv[i].y >  border) {  
                         DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", mv[i].y, border, x_pos, y_pos);  
                         mv[i].y = border;  
                 } else {  
                         border = (-(int)y_pos-1) << (5 + dec->quarterpel);  
                         if (mv[i].y < border) {  
                                 DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", mv[i].y, border, x_pos, y_pos);  
                                 mv[i].y = border;  
                         }  
                 }  
         }  
477    
478          start_timer();          start_timer();
479    
# Line 530  Line 482 
482                  uv_dx = mv[0].x;                  uv_dx = mv[0].x;
483                  uv_dy = mv[0].y;                  uv_dy = mv[0].y;
484                  if (dec->quarterpel) {                  if (dec->quarterpel) {
485                            if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) {
486                                    uv_dx = (uv_dx>>1) | (uv_dx&1);
487                                    uv_dy = (uv_dy>>1) | (uv_dy&1);
488                            }
489                            else {
490                          uv_dx /= 2;                          uv_dx /= 2;
491                          uv_dy /= 2;                          uv_dy /= 2;
492                  }                  }
493                    }
494                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
495                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
496    
497                  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)  
498                          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,
499                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
500                                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                                          mv[0].x, mv[0].y, stride, rounding);
# Line 550  Line 505 
505          } else {        /* MODE_INTER4V */          } else {        /* MODE_INTER4V */
506    
507                  if(dec->quarterpel) {                  if(dec->quarterpel) {
508                            if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) {
509                                    int z;
510                                    uv_dx = 0; uv_dy = 0;
511                                    for (z = 0; z < 4; z++) {
512                                      uv_dx += ((mv[z].x>>1) | (mv[z].x&1));
513                                      uv_dy += ((mv[z].y>>1) | (mv[z].y&1));
514                                    }
515                            }
516                            else {
517                          uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);                          uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
518                          uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);                          uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
519                            }
520                  } else {                  } else {
521                          uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;                          uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
522                          uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;                          uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
# Line 560  Line 525 
525                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
526                  uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];                  uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
527    
528                  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) {  
529                          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,
530                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
531                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                          mv[0].x, mv[0].y, stride, rounding);
# Line 600  Line 551 
551          }          }
552    
553          /* 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 {  
554                  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,
555                                                                  uv_dx, uv_dy, stride2, rounding);                                                                  uv_dx, uv_dy, stride2, rounding);
556                  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,
557                                                                  uv_dx, uv_dy, stride2, rounding);                                                                  uv_dx, uv_dy, stride2, rounding);
         }  
558    
559          stop_comp_timer();          stop_comp_timer();
560    
561          if (cbp)          if (cbp)
562                  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);  
563  }  }
564    
565  static void  static void
# Line 663  Line 606 
606          stop_transfer_timer();          stop_transfer_timer();
607    
608          if (cbp)          if (cbp)
609                  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);
610    
611  }  }
612    
# Line 671  Line 614 
614  static void  static void
615  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
616                                  Bitstream * bs,                                  Bitstream * bs,
                                 int reduced_resolution,  
617                                  int quant,                                  int quant,
618                                  int intra_dc_threshold)                                  int intra_dc_threshold)
619  {  {
620          uint32_t bound;          uint32_t bound;
621          uint32_t x, y;          uint32_t x, y;
622          uint32_t mb_width = dec->mb_width;          const uint32_t mb_width = dec->mb_width;
623          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;  
         }  
624    
625          bound = 0;          bound = 0;
626    
# Line 739  Line 676 
676                          }                          }
677    
678                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
679                                                          intra_dc_threshold, bound, reduced_resolution);                                                          intra_dc_threshold, bound);
680    
681                  }                  }
682                  if(dec->out_frm)                  if(dec->out_frm)
# Line 797  Line 734 
734  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
735                                  Bitstream * bs,                                  Bitstream * bs,
736                                  int rounding,                                  int rounding,
                                 int reduced_resolution,  
737                                  int quant,                                  int quant,
738                                  int fcode,                                  int fcode,
739                                  int intra_dc_threshold,                                  int intra_dc_threshold,
# Line 806  Line 742 
742          uint32_t x, y;          uint32_t x, y;
743          uint32_t bound;          uint32_t bound;
744          int cp_mb, st_mb;          int cp_mb, st_mb;
745          uint32_t mb_width = dec->mb_width;          const uint32_t mb_width = dec->mb_width;
746          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;  
         }  
747    
748            if (!dec->is_edged[0]) {
749          start_timer();          start_timer();
750          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
751                                          dec->width, dec->height, dec->bs_version);                                          dec->width, dec->height, dec->bs_version);
752                    dec->is_edged[0] = 1;
753          stop_edges_timer();          stop_edges_timer();
754            }
755    
756          if (gmc_warp) {          if (gmc_warp) {
757                  /* 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 928  Line 862 
862                                          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;
863                                          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;
864                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
865                                                                          intra_dc_threshold, bound, reduced_resolution);                                                                          intra_dc_threshold, bound);
866                                          continue;                                          continue;
867                                  }                                  }
868    
869                                  decoder_mbinter(dec, mb, x, y, cbp, bs,                                  decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0);
                                                                 rounding, reduced_resolution, 0);  
870    
871                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
872                                  mb->mode = MODE_NOT_CODED_GMC;                                  mb->mode = MODE_NOT_CODED_GMC;
# Line 953  Line 886 
886                                  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;
887    
888                                  decoder_mbinter(dec, mb, x, y, 0, bs,                                  decoder_mbinter(dec, mb, x, y, 0, bs,
889                                                                  rounding, reduced_resolution, 0);                                                                  rounding, 0);
890    
891                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
892                                          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 1008  Line 941 
941  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
942                                                                  IMAGE forward,                                                                  IMAGE forward,
943                                                                  IMAGE backward,                                                                  IMAGE backward,
944                                                                  const MACROBLOCK * pMB,                                                                  MACROBLOCK * pMB,
945                                                                  const uint32_t x_pos,                                                                  const uint32_t x_pos,
946                                                                  const uint32_t y_pos,                                                                  const uint32_t y_pos,
947                                                                  Bitstream * bs,                                                                  Bitstream * bs,
# Line 1025  Line 958 
958          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
959          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
960    
961            validate_vector(pMB->mvs, x_pos, y_pos, dec);
962            validate_vector(pMB->b_mvs, x_pos, y_pos, dec);
963    
964          if (!direct) {          if (!direct) {
965                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
966                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
   
967                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
968                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
969    
970                  if (dec->quarterpel) {                  if (dec->quarterpel) {
971                            if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) {
972                                    uv_dx = (uv_dx>>1) | (uv_dx&1);
973                                    uv_dy = (uv_dy>>1) | (uv_dy&1);
974                                    b_uv_dx = (b_uv_dx>>1) | (b_uv_dx&1);
975                                    b_uv_dy = (b_uv_dy>>1) | (b_uv_dy&1);
976                            }
977                            else {
978                          uv_dx /= 2;                          uv_dx /= 2;
979                          uv_dy /= 2;                          uv_dy /= 2;
980                          b_uv_dx /= 2;                          b_uv_dx /= 2;
981                          b_uv_dy /= 2;                          b_uv_dy /= 2;
982                  }                  }
983                    }
984    
985                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
986                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
   
987                  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];
988                  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];
989    
990          } 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 {  
991                          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;
992                          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;
993                          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;
994                          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;
995    
996                    if (dec->quarterpel) {
997                            if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) {
998                                    uv_dx = (uv_dx>>1) | (uv_dx&1);
999                                    uv_dy = (uv_dy>>1) | (uv_dy&1);
1000                                    b_uv_dx = (b_uv_dx>>1) | (b_uv_dx&1);
1001                                    b_uv_dy = (b_uv_dy>>1) | (b_uv_dy&1);
1002                            }
1003                            else {
1004                                    uv_dx /= 2;
1005                                    uv_dy /= 2;
1006                                    b_uv_dx /= 2;
1007                                    b_uv_dy /= 2;
1008                            }
1009                  }                  }
1010    
1011                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
# Line 1103  Line 1053 
1053    
1054          if(dec->quarterpel) {          if(dec->quarterpel) {
1055                  if(!direct) {                  if(!direct) {
1056                          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,
1057                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1058                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1059                  } else {                  } else {
1060                          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,
1061                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1062                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1063                          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,
1064                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1065                                                                                  pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);                                                                                  pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1066                          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,
1067                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1068                                                                                  pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);                                                                                  pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1069                          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,
1070                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1071                                                                                  pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                                  pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1072                  }                  }
1073          } else {          } else {
1074                  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,
1075                                                          pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                          pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1076                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos + 8,
1077                                                          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);
1078                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,                  interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos,
1079                                                          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);
1080                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos + 8,
1081                                                          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);
1082          }          }
1083    
1084          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,
1085                                                  b_uv_dx, b_uv_dy, stride2, 0);                                                  b_uv_dx, b_uv_dy, stride2, 0);
1086          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,
1087                                                  b_uv_dx, b_uv_dy, stride2, 0);                                                  b_uv_dx, b_uv_dy, stride2, 0);
1088    
         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);  
   
1089          stop_comp_timer();          stop_comp_timer();
1090    
1091          if (cbp)          if (cbp)
1092                  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);
1093  }  }
1094    
1095  /* for decode B-frame dbquant */  /* for decode B-frame dbquant */
# Line 1214  Line 1134 
1134          uint32_t x, y;          uint32_t x, y;
1135          VECTOR mv;          VECTOR mv;
1136          const VECTOR zeromv = {0,0};          const VECTOR zeromv = {0,0};
         const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;  
1137          int i;          int i;
1138    
1139            if (!dec->is_edged[0]) {
1140          start_timer();          start_timer();
1141          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1142                                          dec->width, dec->height, dec->bs_version);                                          dec->width, dec->height, dec->bs_version);
1143                    dec->is_edged[0] = 1;
1144                    stop_edges_timer();
1145            }
1146    
1147            if (!dec->is_edged[1]) {
1148                    start_timer();
1149          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1150                                          dec->width, dec->height, dec->bs_version);                                          dec->width, dec->height, dec->bs_version);
1151                    dec->is_edged[1] = 1;
1152          stop_edges_timer();          stop_edges_timer();
1153            }
1154    
1155          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1156                  /* Initialize Pred Motion Vector */                  /* Initialize Pred Motion Vector */
# Line 1231  Line 1159 
1159                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1160                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1161                          const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;                          const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;
1162                          uint32_t intra_dc_threshold; /* fake variable */                          int32_t intra_dc_threshold; /* fake variable */
1163    
1164                          if (check_resync_marker(bs, fcode_max  - 1)) {                          if (check_resync_marker(bs, fcode_max  - 1)) {
1165                                  int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,                                  int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,
# Line 1256  Line 1184 
1184                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1185                                  mb->cbp = 0;                                  mb->cbp = 0;
1186                                  mb->mode = MODE_FORWARD;                                  mb->mode = MODE_FORWARD;
1187                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1);
1188                                  continue;                                  continue;
1189                          }                          }
1190    
# Line 1309  Line 1237 
1237    
1238                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
1239                                  for (i = 0; i < 4; i++) {                                  for (i = 0; i < 4; i++) {
1240                                          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;
1241                                          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;
1242                                                                          ? ((TRB - TRD) * last_mb->mvs[i].x) / TRD  
1243                                                                          : mb->mvs[i].x - last_mb->mvs[i].x);                                          mb->b_mvs[i].x = (mv.x)
1244                                          mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y);                                                  ?  mb->mvs[i].x - last_mb->mvs[i].x
1245                                          mb->b_mvs[i].y = (int32_t) ((mv.y == 0)                                                  : last_mb->mvs[i].x*(dec->time_bp - dec->time_pp)/dec->time_pp;
1246                                                                          ? ((TRB - TRD) * last_mb->mvs[i].y) / TRD                                          mb->b_mvs[i].y = (mv.y)
1247                                                                          : mb->mvs[i].y - last_mb->mvs[i].y);                                                  ? mb->mvs[i].y - last_mb->mvs[i].y
1248                                                    : last_mb->mvs[i].y*(dec->time_bp - dec->time_pp)/dec->time_pp;
1249                                  }                                  }
1250    
1251                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
# Line 1338  Line 1267 
1267                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
1268                                  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];
1269    
1270                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0);
1271                                  break;                                  break;
1272    
1273                          case MODE_FORWARD:                          case MODE_FORWARD:
1274                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
1275                                  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];
1276    
1277                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1);
1278                                  break;                                  break;
1279    
1280                          default:                          default:
# Line 1360  Line 1289 
1289                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats,                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats,
1290                                          int coding_type, int quant)                                          int coding_type, int quant)
1291  {  {
1292            const int brightness = XVID_VERSION_MINOR(frame->version) >= 1 ? frame->brightness : 0;
1293    
1294          if (dec->cartoon_mode)          if (dec->cartoon_mode)
1295                  frame->general &= ~XVID_FILMEFFECT;                  frame->general &= ~XVID_FILMEFFECT;
1296    
1297          if ((frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) || frame->brightness!=0)          if ((frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) || brightness!=0)
1298                  && mbs != NULL) /* post process */                  && mbs != NULL) /* post process */
1299          {          {
1300                  /* note: image is stored to tmp */                  /* note: image is stored to tmp */
1301                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1302                  image_postproc(&dec->postproc, &dec->tmp, dec->edged_width,                  image_postproc(&dec->postproc, &dec->tmp, dec->edged_width,
1303                                             mbs, dec->mb_width, dec->mb_height, dec->mb_width,                                             mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1304                                             frame->general, frame->brightness, dec->frames, (coding_type == B_VOP));                                             frame->general, brightness, dec->frames, (coding_type == B_VOP));
1305                  img = &dec->tmp;                  img = &dec->tmp;
1306          }          }
1307    
# Line 1393  Line 1324 
1324          }          }
1325  }  }
1326    
   
1327  int  int
1328  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1329                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
# Line 1401  Line 1331 
1331    
1332          Bitstream bs;          Bitstream bs;
1333          uint32_t rounding;          uint32_t rounding;
         uint32_t reduced_resolution;  
1334          uint32_t quant = 2;          uint32_t quant = 2;
1335          uint32_t fcode_forward;          uint32_t fcode_forward;
1336          uint32_t fcode_backward;          uint32_t fcode_backward;
# Line 1456  Line 1385 
1385    
1386  repeat:  repeat:
1387    
1388          coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,          coding_type = BitstreamReadHeaders(&bs, dec, &rounding,
1389                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1390    
1391          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=%"
1392    #if defined(_MSC_VER)
1393        "I64"
1394    #else
1395        "ll"
1396    #endif
1397        "i,  time_pp=%i,  time_bp=%i\n",
1398                                                          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);
1399    
1400          if (coding_type == -1) { /* nothing */          if (coding_type == -1) { /* nothing */
# Line 1490  Line 1425 
1425                  goto repeat;                  goto repeat;
1426          }          }
1427    
1428            if(dec->frames == 0 && coding_type != I_VOP) {
1429                    /* 1st frame is not an i-vop */
1430                    goto repeat;
1431            }
1432    
1433          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 */
1434    
1435          /* packed_mode: special-N_VOP treament */          /* packed_mode: special-N_VOP treament */
# Line 1502  Line 1442 
1442          } else if (coding_type != B_VOP) {          } else if (coding_type != B_VOP) {
1443                  switch(coding_type) {                  switch(coding_type) {
1444                  case I_VOP :                  case I_VOP :
1445                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1446                          break;                          break;
1447                  case P_VOP :                  case P_VOP :
1448                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                          decoder_pframe(dec, &bs, rounding, quant,
1449                                                  fcode_forward, intra_dc_threshold, NULL);                                                  fcode_forward, intra_dc_threshold, NULL);
1450                          break;                          break;
1451                  case S_VOP :                  case S_VOP :
1452                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                          decoder_pframe(dec, &bs, rounding, quant,
1453                                                  fcode_forward, intra_dc_threshold, &gmc_warp);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1454                          break;                          break;
1455                  case N_VOP :                  case N_VOP :
# Line 1520  Line 1460 
1460                          break;                          break;
1461                  }                  }
1462    
                 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);  
                 }  
   
1463                  /* 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 */
1464                  if (!(dec->low_delay_default && dec->packed_mode)) {                  if (!(dec->low_delay_default && dec->packed_mode)) {
1465                          if (dec->low_delay) {                          if (dec->low_delay) {
# Line 1539  Line 1473 
1473                  }                  }
1474    
1475                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1476                    dec->is_edged[1] = dec->is_edged[0];
1477                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1478                    dec->is_edged[0] = 0;
1479                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
                 dec->last_reduced_resolution = reduced_resolution;  
1480                  dec->last_coding_type = coding_type;                  dec->last_coding_type = coding_type;
1481    
1482                  dec->frames++;                  dec->frames++;
# Line 1551  Line 1486 
1486    
1487                  if (dec->low_delay) {                  if (dec->low_delay) {
1488                          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");
1489                          dec->low_delay = 1;                          dec->low_delay = 0;
1490                  }                  }
1491    
1492                  if (dec->frames < 2) {                  if (dec->frames < 2) {
# Line 1586  Line 1521 
1521    
1522  done :  done :
1523    
1524          /* low_delay_default mode: if we've gotten here without outputting anything,    /* if we reach here without outputing anything _and_
1525             then output the recently decoded frame, or print an error message  */       the calling application has specified low_delay_default,
1526         we *must* output something.
1527         this always occurs on the first call to decode() call
1528         when bframes are present in the bitstream. it may also
1529         occur if no vops  were seen in the bitstream
1530    
1531         if packed_mode is enabled, then we output the recently
1532         decoded frame (the very first ivop). otherwise we have
1533         nothing to display, and therefore output a black screen.
1534      */
1535          if (dec->low_delay_default && output == 0) {          if (dec->low_delay_default && output == 0) {
1536                  if (dec->packed_mode && seen_something) {                  if (dec->packed_mode && seen_something) {
                         /* output the recently decoded frame */  
1537                          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, quant);
1538                  } else {                  } else {
1539                          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);
                         image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,  
                                 "warning: nothing to output");  
                         image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,  
                                 "bframe decoder lag");  
   
1540                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant);                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant);
1541                          if (stats) stats->type = XVID_TYPE_NOTHING;                          if (stats) stats->type = XVID_TYPE_NOTHING;
1542                  }                  }

Legend:
Removed from v.1.54  
changed lines
  Added in v.1.72

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