[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.17, Mon Dec 9 10:47:05 2002 UTC revision 1.37.2.27, Sat Jan 11 14:59:23 2003 UTC
# Line 59  Line 59 
59   *   *
60   *************************************************************************/   *************************************************************************/
61    
62    #include <stdio.h>
63  #include <stdlib.h>  #include <stdlib.h>
64  #include <string.h>  #include <string.h>
65    
# Line 68  Line 69 
69    
70  #include "xvid.h"  #include "xvid.h"
71  #include "portab.h"  #include "portab.h"
72    #include "global.h"
73    
74  #include "decoder.h"  #include "decoder.h"
75  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 80  Line 82 
82  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
83  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
84  #include "image/reduced.h"  #include "image/reduced.h"
85    #include "image/font.h"
86    
87  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
88  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
# Line 96  Line 99 
99  {  {
100          /* free existing */          /* free existing */
101    
102            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
103          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
104          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
105          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
106          image_destroy(&dec->refh, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
107          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
108            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
109    
110          if (dec->last_mbs)          if (dec->last_mbs)
111                  xvid_free(dec->last_mbs);                  xvid_free(dec->last_mbs);
# Line 134  Line 139 
139                  xvid_free(dec);                  xvid_free(dec);
140                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
141          }          }
142          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
143                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
144                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
145                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
146                    xvid_free(dec);
147                    return XVID_ERR_MEMORY;
148            }
149    
150            if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
151                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
152                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
153                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
154                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
155                  xvid_free(dec);                  xvid_free(dec);
156                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
157          }          }
158    
159          if (image_create(&dec->refh, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
160                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
161                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
162                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
163                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
164                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
165                  xvid_free(dec);                  xvid_free(dec);
166                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
167          }          }
# Line 158  Line 173 
173                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
174                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
175                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
176                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
177                  image_destroy(&dec->refh, dec->edged_width, dec->edged_height);                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
178                  xvid_free(dec);                  xvid_free(dec);
179                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
180          }          }
# Line 175  Line 190 
190                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
191                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
192                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
193                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
194                  image_destroy(&dec->refh, dec->edged_width, dec->edged_height);                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
195                  xvid_free(dec);                  xvid_free(dec);
196                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
197          }          }
# Line 206  Line 221 
221          image_null(&dec->cur);          image_null(&dec->cur);
222          image_null(&dec->refn[0]);          image_null(&dec->refn[0]);
223          image_null(&dec->refn[1]);          image_null(&dec->refn[1]);
224          image_null(&dec->refn[2]);          image_null(&dec->tmp);
225          image_null(&dec->refh);          image_null(&dec->qtmp);
226    
227    /* image based GMC */
228            image_null(&dec->gmc);
229    
230    
231          dec->mbs = NULL;          dec->mbs = NULL;
232          dec->last_mbs = NULL;          dec->last_mbs = NULL;
# Line 216  Line 235 
235    
236          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
237          // for support B-frame to save reference frame's time          // for support B-frame to save reference frame's time
238          dec->frames = -1;          dec->frames = 0;
239          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
240          dec->low_delay = 0;          dec->low_delay = 0;
241          dec->packed_mode = 0;          dec->packed_mode = 0;
# Line 235  Line 254 
254  {  {
255          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
256          xvid_free(dec->mbs);          xvid_free(dec->mbs);
257    
258            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          /* image based GMC */
259    
260          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
261          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
262          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
263          image_destroy(&dec->refh, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
264          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
265          xvid_free(dec);          xvid_free(dec);
266    
# Line 383  Line 405 
405    
406    
407    
   
 #define SIGN(X) (((X)>0)?1:-1)  
 #define ABS(X) (((X)>0)?(X):-(X))  
   
408  // decode an inter macroblock  // decode an inter macroblock
409    
 static void  
 rrv_mv_scaleup(VECTOR * mv)  
 {  
         if (mv->x > 0) {  
                 mv->x = 2*mv->x - 1;  
         } else if (mv->x < 0) {  
                 mv->x = 2*mv->x + 1;  
         }  
   
         if (mv->y > 0) {  
                 mv->y = 2*mv->y - 1;  
         } else if (mv->y < 0) {  
                 mv->y = 2*mv->y + 1;  
         }  
 }  
   
   
   
410  void  void
411  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
412                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
# Line 417  Line 417 
417                                  Bitstream * bs,                                  Bitstream * bs,
418                                  const uint32_t quant,                                  const uint32_t quant,
419                                  const uint32_t rounding,                                  const uint32_t rounding,
420                                  const int reduced_resolution)                                  const int reduced_resolution,
421                                    const int mcsel)
422  {  {
423    
424          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 429  Line 430 
430          uint32_t i;          uint32_t i;
431          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
432          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
433            uint8_t *pY_Ref, *pU_Ref, *pV_Ref;              /* ref for GMC is _not_ pRef itself */
434    
435          int uv_dx, uv_dy;          int uv_dx, uv_dy;
436          VECTOR mv[4];          VECTOR mv[4];   /* local copy of mvs */
   
         for (i = 0; i < 4; i++)  
         {  
                 mv[i] = pMB->mvs[i];  
                 //DPRINTF(DPRINTF_MB, "mv[%i]   orig=%i,%i   local=%i", i, pMB->mvs[i].x, pMB->mvs[i].y,                                                mv[i].x, mv[i].y);  
         }  
437    
438          if (reduced_resolution) {          if (reduced_resolution) {
439                  pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);                  pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
440                  pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);                  pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
441                  pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);                  pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
442                  rrv_mv_scaleup(&mv[0]);                  for (i = 0; i < 4; i++) {
443                  rrv_mv_scaleup(&mv[1]);                          mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
444                  rrv_mv_scaleup(&mv[2]);                          mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
445                  rrv_mv_scaleup(&mv[3]);                  }
446          }else{          }else{
447                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
448                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
449                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
450                    for (i = 0; i < 4; i++)
451                            mv[i] = pMB->mvs[i];
452            }
453    
454            if (mcsel) {
455                    mv[0].x = mv[0].y = mv[1].x = mv[1].y = mv[2].x = mv[2].y = mv[3].x = mv[3].y = 0;
456                            /* position in ref is same as the block, set vector to (0,0) */
457                    pY_Ref = dec->gmc.y;
458                    pU_Ref = dec->gmc.u;
459                    pV_Ref = dec->gmc.v;
460                            /* but reference itself isn't. It's warped... */
461                            /* Btw., this is too slow! For GMC it should simply be transfer_16to8add() */
462            } else {
463                    pY_Ref = dec->refn[0].y;
464                    pU_Ref = dec->refn[0].u;
465                    pV_Ref = dec->refn[0].v;
466          }          }
467    
468          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 uv_dx = mv[0].x;  
                 uv_dy = mv[0].y;  
469    
470                  if (dec->quarterpel)                  uv_dx = mv[0].x / (1 + dec->quarterpel);
471                  {                  uv_dy = mv[0].y / (1 + dec->quarterpel);
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
472    
473                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
474                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
# Line 469  Line 476 
476                  start_timer();                  start_timer();
477                  if (reduced_resolution)                  if (reduced_resolution)
478                  {                  {
479                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,                          interpolate32x32_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos,
480                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                    mv[0].x, mv[0].y, stride,  rounding);
481                          interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,                          interpolate16x16_switch(dec->cur.u, pU_Ref , 16 * x_pos, 16 * y_pos,
482                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
483                          interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,                          interpolate16x16_switch(dec->cur.v, pV_Ref , 16 * x_pos, 16 * y_pos,
484                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
485    
486                  }                  }
487                  else                  else
488                  {                  {
489                          if(dec->quarterpel) {                          if(dec->quarterpel) {
490                                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate16x16_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
491                                                                                          dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
492                                                                                          mv[0].x, mv[0].y, stride,  rounding);                                                                                          mv[0].x, mv[0].y, stride,  rounding);
493                          }                          }
494                          else {                          else {
495                                  interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,                                  interpolate16x16_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos,
496                                                                            mv[0].x, mv[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
497                          }                          }
498    
499                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,                          interpolate8x8_switch(dec->cur.u, pU_Ref , 8 * x_pos, 8 * y_pos,
500                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
501                          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,                          interpolate8x8_switch(dec->cur.v, pV_Ref , 8 * x_pos, 8 * y_pos,
502                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
503                  }                  }
504                  stop_comp_timer();                  stop_comp_timer();
# Line 516  Line 523 
523                  start_timer();                  start_timer();
524                  if (reduced_resolution)                  if (reduced_resolution)
525                  {                  {
526                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,                          interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos,
527                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                    mv[0].x, mv[0].y, stride,  rounding);
528                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos,                          interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos + 16, 32*y_pos,
529                                                                    mv[1].x, mv[1].y, stride,  rounding);                                                                    mv[1].x, mv[1].y, stride,  rounding);
530                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos + 16,                          interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos + 16,
531                                                                    mv[2].x, mv[2].y, stride,  rounding);                                                                    mv[2].x, mv[2].y, stride,  rounding);
532                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos + 16,                          interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos + 16, 32*y_pos + 16,
533                                                                    mv[3].x, mv[3].y, stride,  rounding);                                                                    mv[3].x, mv[3].y, stride,  rounding);
534                          interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,                          interpolate16x16_switch(dec->cur.u, pU_Ref , 16 * x_pos, 16 * y_pos,
535                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
536                          interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,                          interpolate16x16_switch(dec->cur.v, pV_Ref , 16 * x_pos, 16 * y_pos,
537                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
538    
539                          // set_block(pY_Cur, stride, 32, 32, 127);                          // set_block(pY_Cur, stride, 32, 32, 127);
# Line 534  Line 541 
541                  else                  else
542                  {                  {
543                          if(dec->quarterpel) {                          if(dec->quarterpel) {
544                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
545                                                                                    dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
546                                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                                    mv[0].x, mv[0].y, stride,  rounding);
547                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
548                                                                                    dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
549                                                                                    mv[1].x, mv[1].y, stride,  rounding);                                                                                    mv[1].x, mv[1].y, stride,  rounding);
550                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
551                                                                                    dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
552                                                                                    mv[2].x, mv[2].y, stride,  rounding);                                                                                    mv[2].x, mv[2].y, stride,  rounding);
553                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
554                                                                                    dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
555                                                                                    mv[3].x, mv[3].y, stride,  rounding);                                                                                    mv[3].x, mv[3].y, stride,  rounding);
556                          }                          }
557                          else {                          else {
558                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos,
559                                                                            mv[0].x, mv[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
560                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos + 8, 16*y_pos,
561                                                                            mv[1].x, mv[1].y, stride,  rounding);                                                                            mv[1].x, mv[1].y, stride,  rounding);
562                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos + 8,
563                                                                            mv[2].x, mv[2].y, stride,  rounding);                                                                            mv[2].x, mv[2].y, stride,  rounding);
564                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos + 8, 16*y_pos + 8,
565                                                                            mv[3].x, mv[3].y, stride,  rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
566                          }                          }
567    
568                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,                          interpolate8x8_switch(dec->cur.u, pU_Ref , 8 * x_pos, 8 * y_pos,
569                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
570                          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,                          interpolate8x8_switch(dec->cur.v, pV_Ref , 8 * x_pos, 8 * y_pos,
571                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
572                  }                  }
573                  stop_comp_timer();                  stop_comp_timer();
# Line 640  Line 647 
647  {  {
648          uint32_t bound;          uint32_t bound;
649          uint32_t x, y;          uint32_t x, y;
650          int mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
651          int mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
652    
653          if (reduced_resolution)          if (reduced_resolution)
654          {          {
# Line 699  Line 706 
706    
707                          if (dec->interlacing) {                          if (dec->interlacing) {
708                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
709                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
710                          }                          }
711    
712                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
# Line 737  Line 744 
744          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
745          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
746    
747          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) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
748    
749          mv.x += pmv.x;          mv.x += pmv.x;
750          mv.y += pmv.y;          mv.y += pmv.y;
# Line 774  Line 781 
781  }  }
782    
783    
784  /* for P_VOP set gmc_mv to NULL */  /* for P_VOP set gmc_warp to NULL */
785  void  void
786  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
787                             Bitstream * bs,                             Bitstream * bs,
# Line 783  Line 790 
790                             int quant,                             int quant,
791                             int fcode,                             int fcode,
792                             int intra_dc_threshold,                             int intra_dc_threshold,
793                             VECTOR * gmc_mv)                             const WARPPOINTS *const gmc_warp)
794  {  {
795    
796          uint32_t x, y;          uint32_t x, y;
797          uint32_t bound;          uint32_t bound;
798          int cp_mb, st_mb;          int cp_mb, st_mb;
799          int mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
800          int mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
801    
802            static int framecount=0;
803          if (reduced_resolution)          if (reduced_resolution)
804          {          {
805                  mb_width = (dec->width + 31) / 32;                  mb_width = (dec->width + 31) / 32;
# Line 803  Line 811 
811                                     dec->width, dec->height);                                     dec->width, dec->height);
812          stop_edges_timer();          stop_edges_timer();
813    
814            if (gmc_warp)
815            {
816                    char filename[80];
817                    sprintf(filename,"dGMC%05d.pgm",framecount);
818                    // accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16
819                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
820                    {
821                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
822                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
823                                    dec->sprite_warping_points);
824                    }
825    
826                    generate_GMCparameters( dec->sprite_warping_points,
827                                    (2 << dec->sprite_warping_accuracy), gmc_warp,
828                                    dec->width, dec->height, &dec->gmc_data);
829    
830                    generate_GMCimage(&dec->gmc_data, &dec->refn[0],
831                                            mb_width, mb_height,
832                                            dec->edged_width, dec->edged_width/2,
833                                            fcode, 0, 0,
834                                            rounding, dec->mbs, &dec->gmc);
835    
836    /*
837                    sprintf(filename,"dGMC%05d.pgm",framecount);
838                    image_dump_yuvpgm(&dec->gmc,
839                                            dec->edged_width, dec->width, dec->height, filename);
840    
841                    sprintf(filename,"dREF%05d.pgm",framecount);
842                    image_dump_yuvpgm(&dec->refn[0],
843                                            dec->edged_width, dec->width, dec->height, filename);
844                    sprintf(filename,"dCUR%05d.pgm",framecount);
845                    image_dump_yuvpgm(&dec->cur,
846                                            dec->edged_width, dec->width, dec->height, filename);
847                    framecount++;
848    */
849            }
850    
851          bound = 0;          bound = 0;
852    
853          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
# Line 826  Line 871 
871                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
872    
873                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded
874                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     // block _is_ coded
875                          {                          {
876                                  uint32_t mcbpc;                                  uint32_t mcbpc;
877                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 851  Line 896 
896                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
897                                  }                                  }
898    
899                                  if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
900                                  {                                  {
901                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
902                                  }                                  }
903    
904                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
905                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
906    
907                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
908    
# Line 877  Line 922 
922                                  if (dec->interlacing) {                                  if (dec->interlacing) {
923                                          if (cbp || intra) {                                          if (cbp || intra) {
924                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
925                                                  DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
926                                          }                                          }
927    
928                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
929                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
930                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
931    
932                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
933                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
934                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
935                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
936                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
937                                                  }                                                  }
938                                          }                                          }
939                                  }                                  }
# Line 897  Line 942 
942    
943                                          if (mcsel)                                          if (mcsel)
944                                          {                                          {
945                                                  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] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->amv;
946                                                  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);                                             /* already clipped to fcode */
947    
948                                          } else if (dec->interlacing && mb->field_pred) {                                          } else if (dec->interlacing && mb->field_pred) {
949                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
# Line 908  Line 953 
953                                          } else {                                          } else {
954                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
955                                                                                    fcode, bound);                                                                                    fcode, bound);
956                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
                                                         mb->mvs[0].x;  
                                                 mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                                         mb->mvs[0].y;  
957                                          }                                          }
958                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
959    
# Line 931  Line 973 
973                                  }                                  }
974    
975                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
976                                                                  rounding, reduced_resolution);                                                                  rounding, reduced_resolution, mcsel);
977    
978                          }                          }
979                          else if (gmc_mv)        /* not coded S_VOP macroblock */                          else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
980                          {                          {
981                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED_GMC;
982                                  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] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->amv;
983                                  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);  
984                                  decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);                                  start_timer();
985    
986                                    transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
987                                                                     dec->gmc.y + (16*y)*dec->edged_width + (16*x),
988                                                                     dec->edged_width);
989    
990                                    transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
991                                                                    dec->gmc.u + (8*y)*dec->edged_width/2 + (8*x),
992                                                                    dec->edged_width/2);
993    
994                                    transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
995                                                                     dec->gmc.v + (8*y)*dec->edged_width/2 + (8*x),
996                                                                     dec->edged_width/2);
997    
998                                    stop_transfer_timer();
999    
1000                                    if(dec->out_frm && cp_mb > 0) {
1001                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1002                                      cp_mb = 0;
1003                                    }
1004                                    st_mb = x+1;
1005                          }                          }
1006                          else    /* not coded P_VOP macroblock */                          else    /* not coded P_VOP macroblock */
1007                          {                          {
# Line 1101  Line 1163 
1163    
1164          start_timer();          start_timer();
1165          if(dec->quarterpel) {          if(dec->quarterpel) {
1166                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->refh.y, dec->refh.y + 64,                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1167                                                                      dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1168                                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1169          }          }
1170          else {          else {
# Line 1256  Line 1318 
1318          start_timer();          start_timer();
1319          if(dec->quarterpel) {          if(dec->quarterpel) {
1320                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1321                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1322                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1323                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1324                  else {                  else {
1325                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1326                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1327                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1328                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1329                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1330                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1331                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1332                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1333                                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1334                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1335                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1336                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1337                  }                  }
1338          }          }
# Line 1294  Line 1356 
1356    
1357          if(dec->quarterpel) {          if(dec->quarterpel) {
1358                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1359                          interpolate16x16_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1360                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1361                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1362                  else {                  else {
1363                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1364                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1365                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1366                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1367                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1368                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1369                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1370                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1371                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1372                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1373                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1374                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1375                  }                  }
1376          }          }
1377          else {          else {
1378                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1379                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1380                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1381                                                            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,
1382                                                            0);                                                            0);
1383                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1384                                                            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,
1385                                                            stride, 0);                                                            stride, 0);
1386                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1387                                                            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,
1388                                                            stride, 0);                                                            stride, 0);
1389          }          }
1390    
1391          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1392                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1393          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,
1394                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1395    
1396          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1397                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1398                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1399                                                  stride, 1, 8);                                                  stride, 1, 8);
1400    
1401          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,
1402                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1403                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1404                                                  stride, 1, 8);                                                  stride, 1, 8);
1405    
1406          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,
1407                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1408                                                  dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1409                                                  stride, 1, 8);                                                  stride, 1, 8);
1410    
1411          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,
1412                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1413                                                  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,
1414                                                  stride, 1, 8);                                                  stride, 1, 8);
1415    
1416          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1417                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1418                                                  dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1419                                                  stride2, 1, 8);                                                  stride2, 1, 8);
1420    
1421          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1422                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1423                                                  dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1424                                                  stride2, 1, 8);                                                  stride2, 1, 8);
1425    
1426          stop_comp_timer();          stop_comp_timer();
# Line 1488  Line 1550 
1550                          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] =
1551                          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;
1552    
1553                          // the last P_VOP is skip macroblock ?                          // skip if the co-located P_VOP macroblock is not coded
1554                            // note: gmc+not_coded isn't skipped
1555    
1556                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1557                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1558                                  mb->cbp = 0;                                  mb->cbp = 0;
# Line 1602  Line 1666 
1666                                  break;                                  break;
1667    
1668                          default:                          default:
1669                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
1670                          }                          }
1671    
1672                  }                                               // end of FOR                  }                                               // end of FOR
# Line 1627  Line 1691 
1691          *mb2 = temp;          *mb2 = temp;
1692  }  }
1693    
1694    
1695    /* perform post processing if necessary, and output the image */
1696    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1697                                            const XVID_DEC_FRAME * frame, int pp_disable)
1698    {
1699    
1700            if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1701            {
1702                    /* note: image is stored to tmp */
1703                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1704                    image_deblock_rrv(&dec->tmp, dec->edged_width,
1705                                                    mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1706                                                    8, frame->general);
1707                    img = &dec->tmp;
1708            }
1709    
1710            image_output(img, dec->width, dec->height,
1711                                     dec->edged_width, frame->image, frame->stride,
1712                                     frame->colorspace, dec->interlacing);
1713    }
1714    
1715    
1716  int  int
1717  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1718                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
# Line 1639  Line 1725 
1725          uint32_t fcode_forward;          uint32_t fcode_forward;
1726          uint32_t fcode_backward;          uint32_t fcode_backward;
1727          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1728          VECTOR gmc_mv[5];          WARPPOINTS gmc_warp;
1729          uint32_t vop_type;          uint32_t vop_type;
1730          int success = 0;          int success = 0;
1731            int output = 0;
1732            int seen_something = 0;
1733    
1734          start_global_timer();          start_global_timer();
1735    
1736            dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1737          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1738    
1739            if ((frame->general & XVID_DEC_DISCONTINUITY))
1740                    dec->frames = 0;
1741    
1742            if (frame->length < 0)  /* decoder flush */
1743            {
1744                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1745                        we have a reference frame, then outout the reference frame */
1746                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)
1747                    {
1748                            decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1749                            output = 1;
1750                    }
1751    
1752                    frame->length = 0;
1753                    if (stats)
1754                    {
1755                            stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1756                            stats->data.vop.time_base = (int)dec->time_base;
1757                            stats->data.vop.time_increment = 0;     //XXX: todo
1758                    }
1759    
1760                    emms();
1761    
1762                    stop_global_timer();
1763                    return XVID_ERR_OK;
1764            }
1765    
1766          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1767    
1768          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's
1769          if(frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1770          {          {
1771                  if (stats)                  if (stats)
1772                          stats->notify = XVID_DEC_VOP;                          stats->notify = XVID_DEC_VOP;
# Line 1661  Line 1777 
1777                  return XVID_ERR_OK;                  return XVID_ERR_OK;
1778          }          }
1779    
1780  start:  repeat:
         // add by chenm001 <chenm001@163.com>  
         // for support B-frame to reference last 2 frame  
         dec->frames++;  
1781    
1782  xxx:          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1783          vop_type =                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
                 BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,  
                         &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);  
1784    
1785          //DPRINTF(DPRINTF_HEADER, "vop_type=%i", vop_type);          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",
1786                                                            vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1787    
1788          if (vop_type == -1 && success)          if (vop_type == - 1)
1789                  goto done;          {
1790                    if (success) goto done;
1791                    emms();
1792                    return XVID_ERR_FAIL;
1793            }
1794    
1795          if (vop_type == -2 || vop_type == -3)          if (vop_type == -2 || vop_type == -3)
1796          {          {
# Line 1693  Line 1809 
1809                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1810                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
1811                          frame->length = BitstreamPos(&bs) / 8;                          frame->length = BitstreamPos(&bs) / 8;
1812                            emms();
1813                          return XVID_ERR_OK;                          return XVID_ERR_OK;
1814                  }                  }
1815                  goto xxx;                  goto repeat;
1816          }          }
1817    
1818          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
1819    
         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;  
1820    
1821            /* packed_mode: special-N_VOP treament */
1822            if (dec->packed_mode && vop_type == N_VOP)
1823            {
1824                    if (dec->low_delay_default && dec->frames > 0)
1825                    {
1826                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1827                            output = 1;
1828                    }
1829                    /* ignore otherwise */
1830            }
1831            else if (vop_type != B_VOP)
1832            {
1833                    switch(vop_type)
1834                    {
1835          case I_VOP:          case I_VOP:
1836                  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  
1837                  break;                  break;
1838                    case P_VOP :
1839          case B_VOP:                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1840  #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  
1841                  break;                  break;
   
1842          case S_VOP :          case S_VOP :
1843                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1844                                                  fcode_forward, intra_dc_threshold, gmc_mv);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1845                  break;                  break;
1846                    case N_VOP :
         case N_VOP:                             // vop not coded  
                 // when low_delay==0, N_VOP's should interpolate between the past and future frames  
1847                  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  
1848                  break;                  break;
   
         default:  
                 if (stats)  
                         stats->notify = 0;  
   
                 emms();  
                 return XVID_ERR_FAIL;  
1849          }          }
1850    
   
         /* reduced resolution deblocking filter */  
   
1851          if (reduced_resolution)          if (reduced_resolution)
1852          {          {
1853                  const int rmb_width = (dec->width + 31) / 32;                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1854                  const int rmb_height = (dec->height + 31) / 32;                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1855                  const int edged_width2 = dec->edged_width /2;                                  16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1856                  int i,j;                  }
   
                 /* horizontal deblocking */  
1857    
1858                  for (j = 1; j < rmb_height*2; j++)      // luma: j,i in block units                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1859                  for (i = 0; i < rmb_width*2; i++)                  if (!(dec->low_delay_default && dec->packed_mode))
1860                  {                  {
1861                          if (dec->mbs[(j-1)/2*dec->mb_width + (i/2)].mode != MODE_NOT_CODED ||                          if (dec->low_delay)
                                 dec->mbs[(j+0)/2*dec->mb_width + (i/2)].mode != MODE_NOT_CODED)  
1862                          {                          {
1863                                  xvid_HFilter_31_C(dec->cur.y + (j*16 - 1)*dec->edged_width + i*16,                                  decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1864                                                                dec->cur.y + (j*16 + 0)*dec->edged_width + i*16, 2);                                  output = 1;
                         }  
1865                  }                  }
1866                            else if (dec->frames > 0)       /* is the reference frame valid? */
                 for (j = 1; j < rmb_height; j++)        // chroma  
                 for (i = 0; i < rmb_width; i++)  
1867                  {                  {
1868                          if (dec->mbs[(j-1)*dec->mb_width + i].mode != MODE_NOT_CODED ||                                  /* output the reference frame */
1869                                  dec->mbs[(j+0)*dec->mb_width + i].mode != MODE_NOT_CODED)                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1870                          {                                  output = 1;
                                 hfilter_31(dec->cur.u + (j*16 - 1)*edged_width2 + i*16,  
                                                                   dec->cur.u + (j*16 + 0)*edged_width2 + i*16, 2);  
                                 hfilter_31(dec->cur.v + (j*16 - 1)*edged_width2 + i*16,  
                                                                   dec->cur.v + (j*16 + 0)*edged_width2 + i*16, 2);  
1871                          }                          }
1872                  }                  }
1873    
1874                  /* vertical deblocking */                  image_swap(&dec->refn[0], &dec->refn[1]);
1875                    image_swap(&dec->cur, &dec->refn[0]);
1876                    mb_swap(&dec->mbs, &dec->last_mbs);
1877                    dec->last_reduced_resolution = reduced_resolution;
1878    
1879                  for (j = 0; j < rmb_height*2; j++)              // luma: i,j in block units                  dec->frames++;
1880                  for (i = 1; i < rmb_width*2; i++)                  seen_something = 1;
                 {  
                         if (dec->mbs[(j/2)*dec->mb_width + (i-1)/2].mode != MODE_NOT_CODED ||  
                                 dec->mbs[(j/2)*dec->mb_width + (i+0)/2].mode != MODE_NOT_CODED)  
                         {  
                                 vfilter_31(dec->cur.y + (j*16)*dec->edged_width + i*16 - 1,  
                                                               dec->cur.y + (j*16)*dec->edged_width + i*16 + 0,  
                                                                   dec->edged_width, 2);  
                         }  
                 }  
1881    
1882                  for (j = 0; j < rmb_height; j++)        // chroma          }else{  /* B_VOP */
1883                  for (i = 1; i < rmb_width; i++)  
1884                  {                  if (dec->low_delay)
                         if (dec->mbs[j*dec->mb_width + i - 1].mode != MODE_NOT_CODED ||  
                                 dec->mbs[j*dec->mb_width + i + 0].mode != MODE_NOT_CODED)  
1885                          {                          {
1886                                  vfilter_31(dec->cur.u + (j*16)*edged_width2 + i*16 - 1,                          DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1887                                                                    dec->cur.u + (j*16)*edged_width2 + i*16 + 0,                          dec->low_delay = 1;
                                                                   edged_width2, 2);  
                                 vfilter_31(dec->cur.v + (j*16)*edged_width2 + i*16 - 1,  
                                                                   dec->cur.v + (j*16)*edged_width2 + i*16 + 0,  
                                                                   edged_width2, 2);  
                         }  
1888                  }                  }
         }  
   
         BitstreamByteAlign(&bs);  
1889    
1890  #ifdef BFRAMES_DEC                  if (dec->frames < 2)
1891          // test if no B_VOP                  {
1892          if (dec->low_delay || dec->frames == 0 || ((dec->packed_mode) && !(frame->length > BitstreamPos(&bs) / 8))) {                          /* attemping to decode a bvop without atleast 2 reference frames */
1893  #endif                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1894                  image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                                                  "broken b-frame, mising ref frames");
1895                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);                  }else if (dec->time_pp <= dec->time_bp) {
1896                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1897  #ifdef BFRAMES_DEC                          decoded in vfw. */
1898                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1899                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1900          } else {          } else {
1901                  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);  
                         }  
                         stop_conv_timer();  
                 }  
1902          }          }
 #endif  
   
         if (vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP) {  
                 image_swap(&dec->refn[0], &dec->refn[1]);  
                 image_swap(&dec->cur, &dec->refn[0]);  
1903    
1904                  // swap MACROBLOCK                  decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1905                  // the Divx will not set the low_delay flage some times                  output = 1;
1906                  // so follow code will wrong to not swap at that time                  dec->frames++;
                 // this will broken bitstream! so I'm change it,  
                 // But that is not the best way! can anyone tell me how  
                 // to do another way?  
                 // 18-07-2002   MinChen<chenm001@163.com>  
                 //if (!dec->low_delay && vop_type == P_VOP)  
                 if (vop_type == P_VOP)  
                         mb_swap(&dec->mbs, &dec->last_mbs);  
1907          }          }
1908    
1909            BitstreamByteAlign(&bs);
1910    
1911          if (success == 0 && dec->packed_mode)          /* low_delay_default mode: repeat in packed_mode */
1912            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1913          {          {
1914                  success = 1;                  success = 1;
1915          //      if (frame->length > BitstreamPos(&bs) / 8)      // multiple vops packed together                  goto repeat;
                 goto start;  
1916          }          }
1917    
1918  done :  done :
1919    
1920            /* low_delay_default mode: if we've gotten here without outputting anything,
1921               then output the recently decoded frame, or print an error message  */
1922            if (dec->low_delay_default && output == 0)
1923            {
1924                    if (dec->packed_mode && seen_something)
1925                    {
1926                            /* output the recently decoded frame */
1927                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1928                            output = 1;
1929                    }
1930                    else
1931                    {
1932                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1933                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1934                                    "warning: nothing to output");
1935                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1936                                    "bframe decoder lag");
1937    
1938                            decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
1939                    }
1940            }
1941    
1942          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1943    
1944          if (stats)          if (stats)
1945          {          {
1946                  stats->notify = XVID_DEC_VOP;                  stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1947                  stats->data.vop.time_base = (int)dec->time_base;                  stats->data.vop.time_base = (int)dec->time_base;
1948                  stats->data.vop.time_increment = 0;     //XXX: todo                  stats->data.vop.time_increment = 0;     //XXX: todo
1949          }          }

Legend:
Removed from v.1.37.2.17  
changed lines
  Added in v.1.37.2.27

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