[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.37.2.11, Tue Nov 12 15:53:36 2002 UTC revision 1.37.2.26, Sat Jan 4 06:14:32 2003 UTC
# Line 68  Line 68 
68    
69  #include "xvid.h"  #include "xvid.h"
70  #include "portab.h"  #include "portab.h"
71    #include "global.h"
72    
73  #include "decoder.h"  #include "decoder.h"
74  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 79  Line 80 
80  #include "dct/fdct.h"  #include "dct/fdct.h"
81  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
82  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
83    #include "image/reduced.h"
84    #include "image/font.h"
85    
86  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
87  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
# Line 95  Line 98 
98  {  {
99          /* free existing */          /* free existing */
100    
101            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
102          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
103          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
104          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
105          image_destroy(&dec->refh, 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);  
106    
107          if (dec->last_mbs)          if (dec->last_mbs)
108                  xvid_free(dec->last_mbs);                  xvid_free(dec->last_mbs);
# Line 133  Line 136 
136                  xvid_free(dec);                  xvid_free(dec);
137                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
138          }          }
139          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
140                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
141                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
142                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
# Line 141  Line 144 
144                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
145          }          }
146    
147          if (image_create(&dec->refh, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
148                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
149                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
150                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
151                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
152                  xvid_free(dec);                  xvid_free(dec);
153                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
154          }          }
# Line 157  Line 160 
160                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
161                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
162                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
163                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
164                  image_destroy(&dec->refh, dec->edged_width, dec->edged_height);                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
165                  xvid_free(dec);                  xvid_free(dec);
166                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
167          }          }
# Line 174  Line 177 
177                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
178                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
179                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
180                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
181                  image_destroy(&dec->refh, dec->edged_width, dec->edged_height);                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
182                  xvid_free(dec);                  xvid_free(dec);
183                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
184          }          }
# Line 205  Line 208 
208          image_null(&dec->cur);          image_null(&dec->cur);
209          image_null(&dec->refn[0]);          image_null(&dec->refn[0]);
210          image_null(&dec->refn[1]);          image_null(&dec->refn[1]);
211          image_null(&dec->refn[2]);          image_null(&dec->tmp);
212          image_null(&dec->refh);          image_null(&dec->qtmp);
213    
214          dec->mbs = NULL;          dec->mbs = NULL;
215          dec->last_mbs = NULL;          dec->last_mbs = NULL;
# Line 215  Line 218 
218    
219          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
220          // for support B-frame to save reference frame's time          // for support B-frame to save reference frame's time
221          dec->frames = -1;          dec->frames = 0;
222          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
223          dec->low_delay = 0;          dec->low_delay = 0;
224            dec->packed_mode = 0;
225    
226          dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);          dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
227    
# Line 235  Line 239 
239          xvid_free(dec->mbs);          xvid_free(dec->mbs);
240          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
241          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
242          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
243          image_destroy(&dec->refh, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
244          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
245          xvid_free(dec);          xvid_free(dec);
246    
# Line 265  Line 269 
269                                  Bitstream * bs,                                  Bitstream * bs,
270                                  const uint32_t quant,                                  const uint32_t quant,
271                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
272                                  const unsigned int bound)                                  const unsigned int bound,
273                                    const int reduced_resolution)
274  {  {
275    
276          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 278  Line 283 
283          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
284          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
285    
286            if (reduced_resolution) {
287                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
288                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
289                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
290            }else{
291          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
292          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
293          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
294            }
295    
296          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear
297    
# Line 341  Line 352 
352                  start_timer();                  start_timer();
353                  idct(&data[i * 64]);                  idct(&data[i * 64]);
354                  stop_idct_timer();                  stop_idct_timer();
355    
356          }          }
357    
358          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 349  Line 361 
361          }          }
362    
363          start_timer();          start_timer();
364    
365            if (reduced_resolution)
366            {
367                    next_block*=2;
368                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
369                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
370                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
371                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
372                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
373                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
374            }else{
375          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
376          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
377          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
378          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
379          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
380          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
381            }
382          stop_transfer_timer();          stop_transfer_timer();
383  }  }
384    
385    
386    
387    
   
 #define SIGN(X) (((X)>0)?1:-1)  
 #define ABS(X) (((X)>0)?(X):-(X))  
   
388  // decode an inter macroblock  // decode an inter macroblock
389    
390  void  void
# Line 376  Line 396 
396                                  const uint32_t cbp,                                  const uint32_t cbp,
397                                  Bitstream * bs,                                  Bitstream * bs,
398                                  const uint32_t quant,                                  const uint32_t quant,
399                                  const uint32_t rounding)                                  const uint32_t rounding,
400                                    const int reduced_resolution)
401  {  {
402    
403          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 384  Line 405 
405    
406          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
407          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
408          uint32_t next_block = stride * 8;          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
409          uint32_t i;          uint32_t i;
410          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
411          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
412    
413          int uv_dx, uv_dy;          int uv_dx, uv_dy;
414            VECTOR mv[4];   /* local copy of mvs */
415    
416            if (reduced_resolution) {
417                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
418                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
419                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
420                    for (i = 0; i < 4; i++) {
421                            mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
422                            mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
423                    }
424            }else{
425          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
426          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
427          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
428                    for (i = 0; i < 4; i++)
429                            mv[i] = pMB->mvs[i];
430            }
431    
432          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
433                  uv_dx = pMB->mvs[0].x;                  uv_dx = mv[0].x;
434                  uv_dy = pMB->mvs[0].y;                  uv_dy = mv[0].y;
435    
436                  if (dec->quarterpel)                  if (dec->quarterpel)
437                  {                  {
# Line 408  Line 443 
443                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
444    
445                  start_timer();                  start_timer();
446                    if (reduced_resolution)
447                    {
448                            interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
449                                                                      mv[0].x, mv[0].y, stride,  rounding);
450                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
451                                                                      uv_dx, uv_dy, stride2, rounding);
452                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
453                                                                      uv_dx, uv_dy, stride2, rounding);
454    
455                    }
456                    else
457                    {
458                  if(dec->quarterpel) {                  if(dec->quarterpel) {
459                          interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
460                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
461                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                                                                                          mv[0].x, mv[0].y, stride,  rounding);
462                  }                  }
463                  else {                  else {
464                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,                                  interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
465                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
                         interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,  
                                                               pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);  
                         interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,  
                                                                   pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);  
                         interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,  
                                                                   pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);  
466                  }                  }
467    
468                  interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,                  interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
469                                                            uv_dx, uv_dy, stride2, rounding);                                                            uv_dx, uv_dy, stride2, rounding);
470                  interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,                  interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
471                                                            uv_dx, uv_dy, stride2, rounding);                                                            uv_dx, uv_dy, stride2, rounding);
472                    }
473                  stop_comp_timer();                  stop_comp_timer();
474    
475          } else {          } else {        /* MODE_INTER4V */
476                  int sum;                  int sum;
477    
478                  if(dec->quarterpel)                  if(dec->quarterpel)
479                          sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);                          sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
480                  else                  else
481                          sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                          sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
482    
483                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
484    
485                  if(dec->quarterpel)                  if(dec->quarterpel)
486                          sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);                          sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
487                  else                  else
488                          sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                          sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
489    
490                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
491    
492                  start_timer();                  start_timer();
493                    if (reduced_resolution)
494                    {
495                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
496                                                                      mv[0].x, mv[0].y, stride,  rounding);
497                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos,
498                                                                      mv[1].x, mv[1].y, stride,  rounding);
499                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos + 16,
500                                                                      mv[2].x, mv[2].y, stride,  rounding);
501                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos + 16,
502                                                                      mv[3].x, mv[3].y, stride,  rounding);
503                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
504                                                                      uv_dx, uv_dy, stride2, rounding);
505                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
506                                                                      uv_dx, uv_dy, stride2, rounding);
507    
508                            // set_block(pY_Cur, stride, 32, 32, 127);
509                    }
510                    else
511                    {
512                  if(dec->quarterpel) {                  if(dec->quarterpel) {
513                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
514                                                                            dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
515                                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                                                                                    mv[0].x, mv[0].y, stride,  rounding);
516                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
517                                                                            dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
518                                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                                                                                    mv[1].x, mv[1].y, stride,  rounding);
519                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
520                                                                            dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
521                                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);                                                                                    mv[2].x, mv[2].y, stride,  rounding);
522                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
523                                                                            dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
524                                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                                                    mv[3].x, mv[3].y, stride,  rounding);
525                  }                  }
526                  else {                  else {
527                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
528                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
529                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
530                                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                                                                            mv[1].x, mv[1].y, stride,  rounding);
531                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
532                                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);                                                                            mv[2].x, mv[2].y, stride,  rounding);
533                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,
534                                                                    pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
535                  }                  }
536    
537                  interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,                  interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
538                                                            uv_dx, uv_dy, stride2, rounding);                                                            uv_dx, uv_dy, stride2, rounding);
539                  interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,                  interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
540                                                            uv_dx, uv_dy, stride2, rounding);                                                            uv_dx, uv_dy, stride2, rounding);
541                    }
542                  stop_comp_timer();                  stop_comp_timer();
543          }          }
544    
# Line 511  Line 573 
573          }          }
574    
575          start_timer();          start_timer();
576            if (reduced_resolution)
577            {
578                    if (cbp & 32)
579                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
580                    if (cbp & 16)
581                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
582                    if (cbp & 8)
583                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
584                    if (cbp & 4)
585                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
586                    if (cbp & 2)
587                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
588                    if (cbp & 1)
589                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
590            }
591            else
592            {
593          if (cbp & 32)          if (cbp & 32)
594                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);
595          if (cbp & 16)          if (cbp & 16)
# Line 523  Line 602 
602                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
603          if (cbp & 1)          if (cbp & 1)
604                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
605            }
606          stop_transfer_timer();          stop_transfer_timer();
607  }  }
608    
# Line 536  Line 616 
616  {  {
617          uint32_t bound;          uint32_t bound;
618          uint32_t x, y;          uint32_t x, y;
619          int mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
620          int mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
621    
622          if (reduced_resolution)          if (reduced_resolution)
623          {          {
624                  mb_width /= 2;                  mb_width = (dec->width + 31) / 32;
625                  mb_height /= 2;                  mb_height = (dec->height + 31) / 32;
626          }          }
627    
628          bound = 0;          bound = 0;
# Line 563  Line 643 
643                          {                          {
644                                  bound = read_video_packet_header(bs, dec, 0,                                  bound = read_video_packet_header(bs, dec, 0,
645                                                          &quant, NULL, NULL, &intra_dc_threshold);                                                          &quant, NULL, NULL, &intra_dc_threshold);
646                                  x = bound % dec->mb_width;                                  x = bound % mb_width;
647                                  y = bound / dec->mb_width;                                  y = bound / mb_width;
648                          }                          }
649                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
650    
# Line 595  Line 675 
675    
676                          if (dec->interlacing) {                          if (dec->interlacing) {
677                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
678                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
679                          }                          }
680    
681                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
682                                                          intra_dc_threshold, bound);                                                          intra_dc_threshold, bound, reduced_resolution);
683    
684                  }                  }
685                  if(dec->out_frm)                  if(dec->out_frm)
686                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
687          }          }
688    
689  }  }
# Line 615  Line 695 
695                                    int x,                                    int x,
696                                    int y,                                    int y,
697                                    int k,                                    int k,
698                                    VECTOR * mv,                                    VECTOR * ret_mv,
699                                    int fcode,                                    int fcode,
700                                    const int bound)                                    const int bound)
701  {  {
# Line 626  Line 706 
706          int range = (64 * scale_fac);          int range = (64 * scale_fac);
707    
708          VECTOR pmv;          VECTOR pmv;
709          int mv_x, mv_y;          VECTOR mv;
710    
711          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
712    
713          mv_x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
714          mv_y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
715    
716          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv.x, mv.y, pmv.x, pmv.y);
717    
718          mv_x += pmv.x;          mv.x += pmv.x;
719          mv_y += pmv.y;          mv.y += pmv.y;
720    
721          if (mv_x < low) {          if (mv.x < low) {
722                  mv_x += range;                  mv.x += range;
723          } else if (mv_x > high) {          } else if (mv.x > high) {
724                  mv_x -= range;                  mv.x -= range;
725          }          }
726    
727          if (mv_y < low) {          if (mv.y < low) {
728                  mv_y += range;                  mv.y += range;
729          } else if (mv_y > high) {          } else if (mv.y > high) {
730                  mv_y -= range;                  mv.y -= range;
731          }          }
732    
733          mv->x = mv_x;          ret_mv->x = mv.x;
734          mv->y = mv_y;          ret_mv->y = mv.y;
735  }  }
736    
737    
# Line 685  Line 765 
765          uint32_t x, y;          uint32_t x, y;
766          uint32_t bound;          uint32_t bound;
767          int cp_mb, st_mb;          int cp_mb, st_mb;
768            uint32_t mb_width = dec->mb_width;
769            uint32_t mb_height = dec->mb_height;
770    
771            if (reduced_resolution)
772            {
773                    mb_width = (dec->width + 31) / 32;
774                    mb_height = (dec->height + 31) / 32;
775            }
776    
777          start_timer();          start_timer();
778          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
# Line 693  Line 781 
781    
782          bound = 0;          bound = 0;
783    
784          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
785                  cp_mb = st_mb = 0;                  cp_mb = st_mb = 0;
786                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
787                          MACROBLOCK *mb;                          MACROBLOCK *mb;
788    
789                          // skip stuffing                          // skip stuffing
# Line 706  Line 794 
794                          {                          {
795                                  bound = read_video_packet_header(bs, dec, fcode - 1,                                  bound = read_video_packet_header(bs, dec, fcode - 1,
796                                          &quant, &fcode, NULL, &intra_dc_threshold);                                          &quant, &fcode, NULL, &intra_dc_threshold);
797                                  x = bound % dec->mb_width;                                  x = bound % mb_width;
798                                  y = bound / dec->mb_width;                                  y = bound / mb_width;
799                          }                          }
800                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
801    
# Line 765  Line 853 
853                                  if (dec->interlacing) {                                  if (dec->interlacing) {
854                                          if (cbp || intra) {                                          if (cbp || intra) {
855                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
856                                                  DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
857                                          }                                          }
858    
859                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
860                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
861                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
862    
863                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
864                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
865                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
866                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
867                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
868                                                  }                                                  }
869                                          }                                          }
870                                  }                                  }
# Line 814  Line 902 
902                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
903                                                  0;                                                  0;
904                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
905                                                                          intra_dc_threshold, bound);                                                                          intra_dc_threshold, bound, reduced_resolution);
906                                          continue;                                          continue;
907                                  }                                  }
908    
909                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
910                                                                  rounding);                                                                  rounding, reduced_resolution);
911    
912                          }                          }
913                          else if (gmc_mv)        /* not coded S_VOP macroblock */                          else if (gmc_mv)        /* not coded S_VOP macroblock */
914                          {                          {
915                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED_GMC;
916                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode);                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode);
917                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode);                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode);
918                                  decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding);                                  decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);
919                          }                          }
920                          else    /* not coded P_VOP macroblock */                          else    /* not coded P_VOP macroblock */
921                          {                          {
922                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
923    
924                                  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;
925                                  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;
926                                  // copy macroblock directly from ref to cur                                  // copy macroblock directly from ref to cur
927    
928                                  start_timer();                                  start_timer();
929    
930                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +                                  if (reduced_resolution)
931                                                                   (16 * x),                                  {
932                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +                                          transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
933                                                                   (16 * x), dec->edged_width);                                                                           dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
934                                                                             dec->edged_width);
935                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +  
936                                                                   (16 * x + 8),                                          transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
937                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +                                                                          dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),
938                                                                   (16 * x + 8), dec->edged_width);                                                                          dec->edged_width/2);
939    
940                                  transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +                                          transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),
941                                                                   (16 * x),                                                                           dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
942                                                                   dec->refn[0].y + (16 * y +                                                                           dec->edged_width/2);
943                                                                                                     8) * dec->edged_width +                                  }
944                                                                   (16 * x), dec->edged_width);                                  else
945                                    {
946                                  transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +                                          transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
947                                                                   (16 * x + 8),                                                                           dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
948                                                                   dec->refn[0].y + (16 * y +                                                                           dec->edged_width);
949                                                                                                     8) * dec->edged_width +  
950                                                                   (16 * x + 8), dec->edged_width);                                          transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
951                                                                            dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
                                 transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 +  
                                                                  (8 * x),  
                                                                  dec->refn[0].u +  
                                                                  (8 * y) * dec->edged_width / 2 + (8 * x),  
952                                                                   dec->edged_width / 2);                                                                   dec->edged_width / 2);
953    
954                                  transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +                                          transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
955                                                                   (8 * x),                                                                           dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
                                                                  dec->refn[0].v +  
                                                                  (8 * y) * dec->edged_width / 2 + (8 * x),  
956                                                                   dec->edged_width / 2);                                                                   dec->edged_width / 2);
957                                    }
958    
959                                  stop_transfer_timer();                                  stop_transfer_timer();
960    
# Line 965  Line 1049 
1049                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1050                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1051    
1052                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
1053                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
1054                            uv_dx /= 2;
1055                            uv_dy /= 2;
1056                    }
1057    
1058                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1059                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1060          } else {          } else {
1061                  int sum;                  int sum;
1062    
1063                    if(dec->quarterpel)
1064                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1065                    else
1066                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
                 uv_dx =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1067    
1068                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1069    
1070                    if(dec->quarterpel)
1071                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1072                    else
1073                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1074                  uv_dy =  
1075                          (sum ==                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1076          }          }
1077    
1078          start_timer();          start_timer();
1079            if(dec->quarterpel) {
1080                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1081                                                                        dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1082                                                                        pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1083            }
1084            else {
1085          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,
1086                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1087          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,
1088                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                        pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1089          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos,                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,
1090                                                    16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1091                                                    0);                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1092          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1093                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,          }
1094                                                    0);  
1095          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,
1096                                                    uv_dx, uv_dy, stride2, 0);                                                    uv_dx, uv_dy, stride2, 0);
1097          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,
# Line 1080  Line 1178 
1178                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1179                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1180    
                 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
   
1181                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
1182                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
1183    
1184                  b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
1185                  b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
1186                            uv_dx /= 2;
1187                            uv_dy /= 2;
1188    
1189                            b_uv_dx /= 2;
1190                            b_uv_dy /= 2;
1191                    }
1192    
1193                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1194                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1195    
1196                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1197                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1198          } else {          } else {
1199                  int sum;                  int sum;
1200    
1201                    if(dec->quarterpel)
1202                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1203                    else
1204                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
                 uv_dx =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1205    
1206                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1207    
1208                    if(dec->quarterpel)
1209                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1210                    else
1211                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1212                  uv_dy =  
1213                          (sum ==                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1214                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
1215                                                                    (ABS(sum) / 16) * 2));  
1216                    if(dec->quarterpel)
1217                  sum =                          sum = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);
1218                          pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +                  else
1219                          pMB->b_mvs[3].x;                          sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1220                  b_uv_dx =  
1221                          (sum ==                  b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1222                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
1223                                                                    (ABS(sum) / 16) * 2));                  if(dec->quarterpel)
1224                            sum = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);
1225                  sum =                  else
1226                          pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y +                          sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1227                          pMB->b_mvs[3].y;  
1228                  b_uv_dy =                  b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1229          }          }
1230    
1231    
1232          start_timer();          start_timer();
1233            if(dec->quarterpel) {
1234                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1235                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1236                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1237                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1238                    else {
1239                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1240                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1241                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1242                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1243                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1244                                                                                pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1245                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1246                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1247                                                                                pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1248                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1249                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1250                                                                                pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1251                    }
1252            }
1253            else {
1254          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1255                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1256          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
# Line 1131  Line 1260 
1260          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1261                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1262                                                    0);                                                    0);
1263            }
1264    
1265          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,
1266                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1267          interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1268                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1269    
1270    
1271          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,          if(dec->quarterpel) {
1272                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1273                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1274                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1275                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1276                    else {
1277                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1278                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1279                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1280                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1281                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1282                                                                                pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1283                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1284                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1285                                                                                pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1286                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1287                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1288                                                                                pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1289                    }
1290            }
1291            else {
1292                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1293                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1294          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1295                                                    16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,                                                    16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1296                                                    0);                                                    0);
1297          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1298                                                    16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,                                                    16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1299                                                    stride, 0);                                                    stride, 0);
1300          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1301                                                    16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,                                                    16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1302                                                    stride, 0);                                                    stride, 0);
1303          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,          }
1304    
1305            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1306                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1307          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1308                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1309    
1310          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1311                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1312                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1313                                                  stride, 0);                                                  stride, 1, 8);
1314    
1315          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1316                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1317                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1318                                                  stride, 0);                                                  stride, 1, 8);
1319    
1320          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1321                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1322                                                  dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1323                                                  stride, 0);                                                  stride, 1, 8);
1324    
1325          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1326                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1327                                                  dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,                                                  dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1328                                                  stride, 0);                                                  stride, 1, 8);
1329    
1330          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1331                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1332                                                  dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1333                                                  stride2, 0);                                                  stride2, 1, 8);
1334    
1335          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1336                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1337                                                  dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1338                                                  stride2, 0);                                                  stride2, 1, 8);
1339    
1340          stop_comp_timer();          stop_comp_timer();
1341    
# Line 1310  Line 1464 
1464                          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] =
1465                          mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;                          mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1466    
1467                          // the last P_VOP is skip macroblock ?                          // skip if the co-located P_VOP macroblock is not coded
1468                            // note: gmc+not_coded isn't skipped
1469    
1470                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1471                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1472                                  mb->cbp = 0;                                  mb->cbp = 0;
# Line 1424  Line 1580 
1580                                  break;                                  break;
1581    
1582                          default:                          default:
1583                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
1584                          }                          }
1585    
1586                  }                                               // end of FOR                  }                                               // end of FOR
# Line 1449  Line 1605 
1605          *mb2 = temp;          *mb2 = temp;
1606  }  }
1607    
1608    
1609    /* perform post processing if necessary, and output the image */
1610    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1611                                            const XVID_DEC_FRAME * frame, int pp_disable)
1612    {
1613    
1614            if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1615            {
1616                    /* note: image is stored to tmp */
1617                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1618                    image_deblock_rrv(&dec->tmp, dec->edged_width,
1619                                                    mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1620                                                    8, frame->general);
1621                    img = &dec->tmp;
1622            }
1623    
1624            image_output(img, dec->width, dec->height,
1625                                     dec->edged_width, frame->image, frame->stride,
1626                                     frame->colorspace, dec->interlacing);
1627    }
1628    
1629    
1630  int  int
1631  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1632                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
# Line 1463  Line 1641 
1641          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1642          VECTOR gmc_mv[5];          VECTOR gmc_mv[5];
1643          uint32_t vop_type;          uint32_t vop_type;
1644            int success = 0;
1645            int output = 0;
1646            int seen_something = 0;
1647    
1648          start_global_timer();          start_global_timer();
1649    
1650            dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1651          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1652    
1653            if ((frame->general & XVID_DEC_DISCONTINUITY))
1654                    dec->frames = 0;
1655    
1656            if (frame->length < 0)  /* decoder flush */
1657            {
1658                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1659                        we have a reference frame, then outout the reference frame */
1660                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)
1661                    {
1662                            decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1663                            output = 1;
1664                    }
1665    
1666                    frame->length = 0;
1667                    if (stats)
1668                    {
1669                            stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1670                            stats->data.vop.time_base = (int)dec->time_base;
1671                            stats->data.vop.time_increment = 0;     //XXX: todo
1672                    }
1673    
1674                    emms();
1675    
1676                    stop_global_timer();
1677                    return XVID_ERR_OK;
1678            }
1679    
1680          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1681    
1682          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's
1683          if(frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1684          {          {
1685                  if (stats)                  if (stats)
1686                          stats->notify = XVID_DEC_VOP;                          stats->notify = XVID_DEC_VOP;
# Line 1482  Line 1691 
1691                  return XVID_ERR_OK;                  return XVID_ERR_OK;
1692          }          }
1693    
1694  start:  repeat:
         // add by chenm001 <chenm001@163.com>  
         // for support B-frame to reference last 2 frame  
         dec->frames++;  
1695    
1696  xxx:          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
         vop_type =  
                 BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,  
1697                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);
1698    
1699          DPRINTF(DPRINTF_HEADER, "vop_type=%i", vop_type);          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%i,  time_pp=%i,  time_bp=%i",
1700                                                            vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1701    
1702            if (vop_type == - 1)
1703            {
1704                    if (success) goto done;
1705                    emms();
1706                    return XVID_ERR_FAIL;
1707            }
1708    
1709          if (vop_type == -2 || vop_type == -3)          if (vop_type == -2 || vop_type == -3)
1710          {          {
# Line 1511  Line 1723 
1723                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1724                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
1725                          frame->length = BitstreamPos(&bs) / 8;                          frame->length = BitstreamPos(&bs) / 8;
1726                            emms();
1727                          return XVID_ERR_OK;                          return XVID_ERR_OK;
1728                  }                  }
1729                  goto xxx;                  goto repeat;
1730          }          }
1731    
1732          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
1733    
         switch (vop_type) {  
         case P_VOP:  
                 decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,  
                                                 fcode_forward, intra_dc_threshold, NULL);  
 #ifdef BFRAMES_DEC  
                 DEBUG1("P_VOP  Time=", dec->time);  
 #endif  
                 break;  
1734    
1735            /* packed_mode: special-N_VOP treament */
1736            if (dec->packed_mode && vop_type == N_VOP)
1737            {
1738                    if (dec->low_delay_default && dec->frames > 0)
1739                    {
1740                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1741                            output = 1;
1742                    }
1743                    /* ignore otherwise */
1744            }
1745            else if (vop_type != B_VOP)
1746            {
1747                    switch(vop_type)
1748                    {
1749          case I_VOP:          case I_VOP:
1750                  decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
 #ifdef BFRAMES_DEC  
                 DEBUG1("I_VOP  Time=", dec->time);  
 #endif  
1751                  break;                  break;
1752                    case P_VOP :
1753          case B_VOP:                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1754  #ifdef BFRAMES_DEC                                                  fcode_forward, intra_dc_threshold, NULL);
                 if (dec->time_pp > dec->time_bp) {  
                         DEBUG1("B_VOP  Time=", dec->time);  
                         decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);  
                 } else {  
                         DEBUG("broken B-frame!");  
                 }  
 #else  
                 image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);  
 #endif  
1755                  break;                  break;
   
1756          case S_VOP :          case S_VOP :
1757                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1758                                                  fcode_forward, intra_dc_threshold, gmc_mv);                                                  fcode_forward, intra_dc_threshold, gmc_mv);
1759                  break;                  break;
1760                    case N_VOP :
         case N_VOP:                             // vop not coded  
                 // when low_delay==0, N_VOP's should interpolate between the past and future frames  
1761                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
 #ifdef BFRAMES_DEC  
                 DEBUG1("N_VOP  Time=", dec->time);  
 #endif  
1762                  break;                  break;
1763                    }
1764    
1765          default:                  if (reduced_resolution)
1766                  if (stats)                  {
1767                          stats->notify = 0;                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1768                  return XVID_ERR_FAIL;                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1769                                    16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1770          }          }
1771    
1772          BitstreamByteAlign(&bs);                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1773                    if (!(dec->low_delay_default && dec->packed_mode))
1774                    {
1775                            if (dec->low_delay)
1776                            {
1777                                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1778                                    output = 1;
1779                            }
1780                            else if (dec->frames > 0)       /* is the reference frame valid? */
1781                            {
1782                                    /* output the reference frame */
1783                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1784                                    output = 1;
1785                            }
1786                    }
1787    
1788  #ifdef BFRAMES_DEC                  image_swap(&dec->refn[0], &dec->refn[1]);
1789          // test if no B_VOP                  image_swap(&dec->cur, &dec->refn[0]);
1790          if (dec->low_delay || dec->frames == 0 || ((dec->packed_mode) && !(frame->length > BitstreamPos(&bs) / 8))) {                  mb_swap(&dec->mbs, &dec->last_mbs);
1791  #endif                  dec->last_reduced_resolution = reduced_resolution;
1792                  image_output(&dec->cur, dec->width, dec->height, dec->edged_width,  
1793                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);                  dec->frames++;
1794                    seen_something = 1;
1795    
1796            }else{  /* B_VOP */
1797    
1798  #ifdef BFRAMES_DEC                  if (dec->low_delay)
1799                    {
1800                            DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1801                            dec->low_delay = 1;
1802                    }
1803    
1804                    if (dec->frames < 2)
1805                    {
1806                            /* attemping to decode a bvop without atleast 2 reference frames */
1807                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1808                                                    "broken b-frame, mising ref frames");
1809                    }else if (dec->time_pp <= dec->time_bp) {
1810                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1811                            decoded in vfw. */
1812                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1813                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1814          } else {          } else {
1815                  if (dec->frames >= 1 && !(dec->packed_mode)) {                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
                         start_timer();  
                         if ((vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP)) {  
                                 image_output(&dec->refn[0], dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
                         } else if (vop_type == B_VOP) {  
                                 image_output(&dec->cur, dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
1816                          }                          }
1817                          stop_conv_timer();  
1818                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1819                    output = 1;
1820                    dec->frames++;
1821                  }                  }
1822    
1823            BitstreamByteAlign(&bs);
1824    
1825            /* low_delay_default mode: repeat in packed_mode */
1826            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1827            {
1828                    success = 1;
1829                    goto repeat;
1830          }          }
 #endif  
1831    
1832          if (vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP) {  done :
                 image_swap(&dec->refn[0], &dec->refn[1]);  
                 image_swap(&dec->cur, &dec->refn[0]);  
1833    
1834                  // swap MACROBLOCK          /* low_delay_default mode: if we've gotten here without outputting anything,
1835                  // the Divx will not set the low_delay flage some times             then output the recently decoded frame, or print an error message  */
1836                  // so follow code will wrong to not swap at that time          if (dec->low_delay_default && output == 0)
1837                  // this will broken bitstream! so I'm change it,          {
1838                  // But that is not the best way! can anyone tell me how                  if (dec->packed_mode && seen_something)
1839                  // to do another way?                  {
1840                  // 18-07-2002   MinChen<chenm001@163.com>                          /* output the recently decoded frame */
1841                  //if (!dec->low_delay && vop_type == P_VOP)                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1842                  if (vop_type == P_VOP)                          output = 1;
                         mb_swap(&dec->mbs, &dec->last_mbs);  
1843          }          }
1844                    else
1845                    {
1846                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1847                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1848                                    "warning: nothing to output");
1849                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1850                                    "bframe decoder lag");
1851    
1852          if (frame->length > BitstreamPos(&bs) / 8)      // multiple vops packed together                          decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
1853                  goto start;                  }
1854            }
1855    
1856          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1857    
1858          if (stats)          if (stats)
1859          {          {
1860                  stats->notify = XVID_DEC_VOP;                  stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1861                  stats->data.vop.time_base = (int)dec->time_base;                  stats->data.vop.time_base = (int)dec->time_base;
1862                  stats->data.vop.time_increment = 0;     //XXX: todo                  stats->data.vop.time_increment = 0;     //XXX: todo
1863          }          }

Legend:
Removed from v.1.37.2.11  
changed lines
  Added in v.1.37.2.26

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