[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.15, Sun Dec 8 05:38:56 2002 UTC revision 1.37.2.30, Sun Jan 12 13:11:50 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  
 set_block(uint8_t * dst, int stride, int width, int height, int color)  
 {  
         int i;  
         for (i = 0; i < height; i++)  
         {  
                 memset(dst, color, width);  
                 dst += stride;  
         }  
 }  
   
 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,
413                                  const uint32_t x_pos,                                  const uint32_t x_pos,
414                                  const uint32_t y_pos,                                  const uint32_t y_pos,
415                                  const uint32_t acpred_flag,                                  const uint32_t fcode,
416                                  const uint32_t cbp,                                  const uint32_t cbp,
417                                  Bitstream * bs,                                  Bitstream * bs,
418                                  const uint32_t quant,                                  const uint32_t quant,
# Line 442  Line 431 
431          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
432    
433          int uv_dx, uv_dy;          int uv_dx, uv_dy;
434          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);  
         }  
435    
436          if (reduced_resolution) {          if (reduced_resolution) {
437                  pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);                  pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
438                  pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);                  pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
439                  pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);                  pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
440                  DPRINTF(DPRINTF_MB,"[%i,%i] %i,%i  %i,%i  %i,%i  %i,%i",                  for (i = 0; i < 4; i++) {
441                          x_pos, y_pos,                          mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
442                          mv[0].x, mv[0].y,                          mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
443                          mv[1].x, mv[1].y,                  }
                         mv[2].x, mv[2].y,  
                         mv[3].x, mv[3].y);  
   
                 rrv_mv_scaleup(&mv[0]);  
                 rrv_mv_scaleup(&mv[1]);  
                 rrv_mv_scaleup(&mv[2]);  
                 rrv_mv_scaleup(&mv[3]);  
   
                 DPRINTF(DPRINTF_MB,"        %i,%i  %i,%i  %i,%i  %i,%i",  
                         mv[0].x, mv[0].y,  
                         mv[1].x, mv[1].y,  
                         mv[2].x, mv[2].y,  
                         mv[3].x, mv[3].y);  
444          }else{          }else{
445                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
446                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
447                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
448                    for (i = 0; i < 4; i++)
449                            mv[i] = pMB->mvs[i];
450          }          }
451    
452          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;  
453    
454                  if (dec->quarterpel)                  uv_dx = mv[0].x / (1 + dec->quarterpel);
455                  {                  uv_dy = mv[0].y / (1 + dec->quarterpel);
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
456    
457                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
458                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
# Line 504  Line 471 
471                  else                  else
472                  {                  {
473                          if(dec->quarterpel) {                          if(dec->quarterpel) {
474                                  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,
475                                                                                          dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
476                                                                                          mv[0].x, mv[0].y, stride,  rounding);                                                                                          mv[0].x, mv[0].y, stride,  rounding);
477                          }                          }
478                          else {                          else {
# Line 558  Line 525 
525                  else                  else
526                  {                  {
527                          if(dec->quarterpel) {                          if(dec->quarterpel) {
528                                  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,
529                                                                                    dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
530                                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                                    mv[0].x, mv[0].y, stride,  rounding);
531                                  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,
532                                                                                    dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
533                                                                                    mv[1].x, mv[1].y, stride,  rounding);                                                                                    mv[1].x, mv[1].y, stride,  rounding);
534                                  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,
535                                                                                    dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
536                                                                                    mv[2].x, mv[2].y, stride,  rounding);                                                                                    mv[2].x, mv[2].y, stride,  rounding);
537                                  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,
538                                                                                    dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
539                                                                                    mv[3].x, mv[3].y, stride,  rounding);                                                                                    mv[3].x, mv[3].y, stride,  rounding);
540                          }                          }
541                          else {                          else {
# Line 654  Line 621 
621          stop_transfer_timer();          stop_transfer_timer();
622  }  }
623    
624    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
625    {
626            int length = 1 << (fcode+4);
627    
628    //      if (quarterpel) value *= 2;
629    
630            if (value < -length)
631                    return -length;
632            else if (value >= length)
633                    return length-1;
634            else return value;
635    }
636    
637    
638    static void
639    decoder_mbgmc(DECODER * dec,
640                                    MACROBLOCK * const pMB,
641                                    const uint32_t x_pos,
642                                    const uint32_t y_pos,
643                                    const uint32_t fcode,
644                                    const uint32_t cbp,
645                                    Bitstream * bs,
646                                    const uint32_t quant,
647                                    const uint32_t rounding,
648                                    const int reduced_resolution)   /* no reduced res support */
649    {
650    
651            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
652            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
653    
654            const uint32_t stride = dec->edged_width;
655            const uint32_t stride2 = stride / 2;
656            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
657            uint32_t i;
658            const uint32_t iQuant = pMB->quant;
659            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
660            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
661            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
662    
663            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
664    
665            start_timer();
666    
667    /* this is where the calculations are done */
668    
669            {
670                    pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,
671                                            stride, stride2, dec->quarterpel, rounding, &dec->cur);
672    
673                    pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
674                    pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
675            }
676            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
677    
678    /*      transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
679            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
680            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
681    */
682    
683    
684            stop_transfer_timer();
685    
686            if (!cbp) return;
687    
688            for (i = 0; i < 6; i++) {
689                    int direction = dec->alternate_vertical_scan ? 2 : 0;
690    
691                    if (cbp & (1 << (5 - i)))       // coded
692                    {
693                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
694    
695                            start_timer();
696                            get_inter_block(bs, &block[i * 64], direction);
697                            stop_coding_timer();
698    
699                            start_timer();
700                            if (dec->quant_type == 0) {
701                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
702                            } else {
703                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
704                            }
705                            stop_iquant_timer();
706    
707                            start_timer();
708                            idct(&data[i * 64]);
709                            stop_idct_timer();
710                    }
711            }
712    
713    /* interlace + GMC is this possible ??? */
714    /*      if (dec->interlacing && pMB->field_dct) {
715                    next_block = stride;
716                    stride *= 2;
717            }
718    */
719            start_timer();
720            if (cbp & 32)
721                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
722            if (cbp & 16)
723                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
724            if (cbp & 8)
725                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
726            if (cbp & 4)
727                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
728            if (cbp & 2)
729                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
730            if (cbp & 1)
731                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
732            stop_transfer_timer();
733    }
734    
735    
736  void  void
737  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
# Line 664  Line 742 
742  {  {
743          uint32_t bound;          uint32_t bound;
744          uint32_t x, y;          uint32_t x, y;
745          int mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
746          int mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
747    
748          if (reduced_resolution)          if (reduced_resolution)
749          {          {
750                  mb_width /= 2;                  mb_width = (dec->width + 31) / 32;
751                  mb_height /= 2;                  mb_height = (dec->height + 31) / 32;
752          }          }
753    
754          bound = 0;          bound = 0;
# Line 723  Line 801 
801    
802                          if (dec->interlacing) {                          if (dec->interlacing) {
803                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
804                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
805                          }                          }
806    
807                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
# Line 761  Line 839 
839          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
840          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
841    
842          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);
843    
844          mv.x += pmv.x;          mv.x += pmv.x;
845          mv.y += pmv.y;          mv.y += pmv.y;
# Line 784  Line 862 
862    
863    
864    
 static __inline int gmc_sanitize(int value, int quarterpel, int fcode)  
 {  
         int length = 1 << (fcode+4);  
   
         if (quarterpel) value *= 2;  
   
         if (value < -length)  
                 return -length;  
         else if (value >= length)  
                 return length-1;  
         else return value;  
 }  
865    
866    
867  /* for P_VOP set gmc_mv to NULL */  /* for P_VOP set gmc_warp to NULL */
868  void  void
869  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
870                             Bitstream * bs,                             Bitstream * bs,
# Line 807  Line 873 
873                             int quant,                             int quant,
874                             int fcode,                             int fcode,
875                             int intra_dc_threshold,                             int intra_dc_threshold,
876                             VECTOR * gmc_mv)                             const WARPPOINTS *const gmc_warp)
877  {  {
878    
879          uint32_t x, y;          uint32_t x, y;
880          uint32_t bound;          uint32_t bound;
881          int cp_mb, st_mb;          int cp_mb, st_mb;
882          int mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
883          int mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
884    
885          if (reduced_resolution)          if (reduced_resolution)
886          {          {
887                  mb_width /= 2;                  mb_width = (dec->width + 31) / 32;
888                  mb_height /= 2;                  mb_height = (dec->height + 31) / 32;
889          }          }
890    
891          start_timer();          start_timer();
# Line 827  Line 893 
893                                     dec->width, dec->height);                                     dec->width, dec->height);
894          stop_edges_timer();          stop_edges_timer();
895    
896            if (gmc_warp)
897            {
898    
899                    // accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16
900                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
901                    {
902                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
903                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
904                                    dec->sprite_warping_points);
905                    }
906    
907                    generate_GMCparameters( dec->sprite_warping_points,
908                                    (2 << dec->sprite_warping_accuracy), gmc_warp,
909                                    dec->width, dec->height, &dec->gmc_data);
910    
911    /* image warping is done block-based  in decoder_mbgmc(), now */
912    /*
913            generate_GMCimage(&dec->gmc_data, &dec->refn[0],
914                                            mb_width, mb_height,
915                                            dec->edged_width, dec->edged_width/2,
916                                            fcode, dec->quarterpel, 0,
917                                            rounding, dec->mbs, &dec->gmc);
918    */
919            }
920    
921          bound = 0;          bound = 0;
922    
923          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
# Line 850  Line 941 
941                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
942    
943                          //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
944                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     // block _is_ coded
945                          {                          {
946                                  uint32_t mcbpc;                                  uint32_t mcbpc;
947                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 875  Line 966 
966                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
967                                  }                                  }
968    
969                                  if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
970                                  {                                  {
971                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
972                                  }                                  }
973    
974                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
975                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
976    
977                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
978    
# Line 901  Line 992 
992                                  if (dec->interlacing) {                                  if (dec->interlacing) {
993                                          if (cbp || intra) {                                          if (cbp || intra) {
994                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
995                                                  DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
996                                          }                                          }
997    
998                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
999                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
1000                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
1001    
1002                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
1003                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
1004                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
1005                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
1006                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
1007                                                  }                                                  }
1008                                          }                                          }
1009                                  }                                  }
1010    
1011                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
1012                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
1013                                                                    rounding, reduced_resolution);
1014                                            continue;
1015    
1016                                          if (mcsel)                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
                                         {  
                                                 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].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode);  
1017    
1018                                          } else if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
1019                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
1020                                                                                    fcode, bound);                                                                                    fcode, bound);
1021                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 932  Line 1023 
1023                                          } else {                                          } else {
1024                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
1025                                                                                    fcode, bound);                                                                                    fcode, bound);
1026                                                  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;  
1027                                          }                                          }
1028                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
1029    
# Line 954  Line 1042 
1042                                          continue;                                          continue;
1043                                  }                                  }
1044    
1045                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
1046                                                                  rounding, reduced_resolution);                                                                  rounding, reduced_resolution);
1047    
1048                          }                          }
1049                          else if (gmc_mv)        /* not coded S_VOP macroblock */                          else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
1050                          {                          {
1051                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED_GMC;
1052                                  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);  
1053                                  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);                                  start_timer();
1054                                  decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);  
1055                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1056                                                                    rounding, reduced_resolution);
1057    
1058                                    stop_transfer_timer();
1059    
1060                                    if(dec->out_frm && cp_mb > 0) {
1061                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1062                                      cp_mb = 0;
1063                                    }
1064                                    st_mb = x+1;
1065                          }                          }
1066                          else    /* not coded P_VOP macroblock */                          else    /* not coded P_VOP macroblock */
1067                          {                          {
# Line 1125  Line 1223 
1223    
1224          start_timer();          start_timer();
1225          if(dec->quarterpel) {          if(dec->quarterpel) {
1226                  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,
1227                                                                      dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1228                                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1229          }          }
1230          else {          else {
# Line 1280  Line 1378 
1378          start_timer();          start_timer();
1379          if(dec->quarterpel) {          if(dec->quarterpel) {
1380                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1381                          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,
1382                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1383                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1384                  else {                  else {
1385                          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,
1386                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1387                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1388                          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,
1389                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1390                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1391                          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,
1392                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1393                                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1394                          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,
1395                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1396                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1397                  }                  }
1398          }          }
# Line 1318  Line 1416 
1416    
1417          if(dec->quarterpel) {          if(dec->quarterpel) {
1418                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1419                          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,
1420                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1421                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1422                  else {                  else {
1423                          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,
1424                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1425                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1426                          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,
1427                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1428                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1429                          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,
1430                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1431                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1432                          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,
1433                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1434                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1435                  }                  }
1436          }          }
1437          else {          else {
1438                  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,
1439                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1440                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1441                                                            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,
1442                                                            0);                                                            0);
1443                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1444                                                            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,
1445                                                            stride, 0);                                                            stride, 0);
1446                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1447                                                            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,
1448                                                            stride, 0);                                                            stride, 0);
1449          }          }
1450    
1451          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,
1452                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1453          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,
1454                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1455    
1456          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1457                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1458                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1459                                                  stride, 1, 8);                                                  stride, 1, 8);
1460    
1461          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,
1462                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1463                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1464                                                  stride, 1, 8);                                                  stride, 1, 8);
1465    
1466          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,
1467                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1468                                                  dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1469                                                  stride, 1, 8);                                                  stride, 1, 8);
1470    
1471          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,
1472                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1473                                                  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,
1474                                                  stride, 1, 8);                                                  stride, 1, 8);
1475    
1476          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1477                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1478                                                  dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1479                                                  stride2, 1, 8);                                                  stride2, 1, 8);
1480    
1481          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1482                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1483                                                  dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1484                                                  stride2, 1, 8);                                                  stride2, 1, 8);
1485    
1486          stop_comp_timer();          stop_comp_timer();
# Line 1512  Line 1610 
1610                          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] =
1611                          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;
1612    
1613                          // the last P_VOP is skip macroblock ?                          // skip if the co-located P_VOP macroblock is not coded
1614                            // if not codec in co-located S_VOP macroblock is _not_ automatically skipped
1615    
1616                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1617                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1618                                  mb->cbp = 0;                                  mb->cbp = 0;
# Line 1626  Line 1726 
1726                                  break;                                  break;
1727    
1728                          default:                          default:
1729                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
1730                          }                          }
1731    
1732                  }                                               // end of FOR                  }                                               // end of FOR
# Line 1651  Line 1751 
1751          *mb2 = temp;          *mb2 = temp;
1752  }  }
1753    
1754    
1755    /* perform post processing if necessary, and output the image */
1756    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1757                                            const XVID_DEC_FRAME * frame, int pp_disable)
1758    {
1759    
1760            if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1761            {
1762                    /* note: image is stored to tmp */
1763                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1764                    image_deblock_rrv(&dec->tmp, dec->edged_width,
1765                                                    mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1766                                                    8, frame->general);
1767                    img = &dec->tmp;
1768            }
1769    
1770            image_output(img, dec->width, dec->height,
1771                                     dec->edged_width, frame->image, frame->stride,
1772                                     frame->colorspace, dec->interlacing);
1773    }
1774    
1775    
1776  int  int
1777  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1778                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
# Line 1663  Line 1785 
1785          uint32_t fcode_forward;          uint32_t fcode_forward;
1786          uint32_t fcode_backward;          uint32_t fcode_backward;
1787          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1788          VECTOR gmc_mv[5];          WARPPOINTS gmc_warp;
1789          uint32_t vop_type;          uint32_t vop_type;
1790          int success = 0;          int success = 0;
1791            int output = 0;
1792            int seen_something = 0;
1793    
1794          start_global_timer();          start_global_timer();
1795    
1796            dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1797          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1798    
1799            if ((frame->general & XVID_DEC_DISCONTINUITY))
1800                    dec->frames = 0;
1801    
1802            if (frame->length < 0)  /* decoder flush */
1803            {
1804                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1805                        we have a reference frame, then outout the reference frame */
1806                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)
1807                    {
1808                            decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1809                            output = 1;
1810                    }
1811    
1812                    frame->length = 0;
1813                    if (stats)
1814                    {
1815                            stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1816                            stats->data.vop.time_base = (int)dec->time_base;
1817                            stats->data.vop.time_increment = 0;     //XXX: todo
1818                    }
1819    
1820                    emms();
1821    
1822                    stop_global_timer();
1823                    return XVID_ERR_OK;
1824            }
1825    
1826          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1827    
1828          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's
1829          if(frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1830          {          {
1831                  if (stats)                  if (stats)
1832                          stats->notify = XVID_DEC_VOP;                          stats->notify = XVID_DEC_VOP;
# Line 1685  Line 1837 
1837                  return XVID_ERR_OK;                  return XVID_ERR_OK;
1838          }          }
1839    
1840  start:  repeat:
         // add by chenm001 <chenm001@163.com>  
         // for support B-frame to reference last 2 frame  
         dec->frames++;  
1841    
1842  xxx:          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1843          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);  
1844    
1845          //DPRINTF(DPRINTF_HEADER, "vop_type=%i", vop_type);          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",
1846                                                            vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1847    
1848          if (vop_type == -1 && success)          if (vop_type == - 1)
1849                  goto done;          {
1850                    if (success) goto done;
1851                    emms();
1852                    return XVID_ERR_FAIL;
1853            }
1854    
1855          if (vop_type == -2 || vop_type == -3)          if (vop_type == -2 || vop_type == -3)
1856          {          {
# Line 1717  Line 1869 
1869                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1870                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
1871                          frame->length = BitstreamPos(&bs) / 8;                          frame->length = BitstreamPos(&bs) / 8;
1872                            emms();
1873                          return XVID_ERR_OK;                          return XVID_ERR_OK;
1874                  }                  }
1875                  goto xxx;                  goto repeat;
1876          }          }
1877    
1878          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
1879    
         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;  
1880    
1881            /* packed_mode: special-N_VOP treament */
1882            if (dec->packed_mode && vop_type == N_VOP)
1883            {
1884                    if (dec->low_delay_default && dec->frames > 0)
1885                    {
1886                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1887                            output = 1;
1888                    }
1889                    /* ignore otherwise */
1890            }
1891            else if (vop_type != B_VOP)
1892            {
1893                    switch(vop_type)
1894                    {
1895          case I_VOP:          case I_VOP:
1896                  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  
1897                  break;                  break;
1898                    case P_VOP :
1899          case B_VOP:                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1900  #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  
1901                  break;                  break;
   
1902          case S_VOP :          case S_VOP :
1903                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1904                                                  fcode_forward, intra_dc_threshold, gmc_mv);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1905                  break;                  break;
1906                    case N_VOP :
         case N_VOP:                             // vop not coded  
                 // when low_delay==0, N_VOP's should interpolate between the past and future frames  
1907                  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  
1908                  break;                  break;
   
         default:  
                 if (stats)  
                         stats->notify = 0;  
   
                 emms();  
                 return XVID_ERR_FAIL;  
1909          }          }
1910    
   
         /* reduced resolution deblocking filter */  
   
1911          if (reduced_resolution)          if (reduced_resolution)
1912          {          {
1913                  const int rmb_height = dec->mb_height / 2;                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1914                  const int rmb_width = dec->mb_width / 2;                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1915                  const int edged_width2 = dec->edged_width /2;                                  16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1916                  int i,j;                  }
   
                 /* horizontal deblocking */  
1917    
1918                  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 */
1919                  for (i = 0; i < rmb_width*2; i++)                  if (!(dec->low_delay_default && dec->packed_mode))
1920                  {                  {
1921                          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)  
1922                          {                          {
1923                                  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);
1924                                                                dec->cur.y + (j*16 + 0)*dec->edged_width + i*16, 2);                                  output = 1;
                         }  
1925                  }                  }
1926                            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++)  
                 {  
                         if (dec->mbs[(j-1)*dec->mb_width + i].mode != MODE_NOT_CODED ||  
                                 dec->mbs[(j+0)*dec->mb_width + i].mode != MODE_NOT_CODED)  
1927                          {                          {
1928                                  hfilter_31(dec->cur.u + (j*16 - 1)*edged_width2 + i*16,                                  /* output the reference frame */
1929                                                                    dec->cur.u + (j*16 + 0)*edged_width2 + i*16, 2);                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1930                                  hfilter_31(dec->cur.v + (j*16 - 1)*edged_width2 + i*16,                                  output = 1;
                                                                   dec->cur.v + (j*16 + 0)*edged_width2 + i*16, 2);  
1931                          }                          }
1932                  }                  }
1933    
1934                  /* vertical deblocking */                  image_swap(&dec->refn[0], &dec->refn[1]);
1935                    image_swap(&dec->cur, &dec->refn[0]);
1936                    mb_swap(&dec->mbs, &dec->last_mbs);
1937                    dec->last_reduced_resolution = reduced_resolution;
1938    
1939                  for (j = 0; j < rmb_height*2; j++)              // luma: i,j in block units                  dec->frames++;
1940                  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);  
                         }  
                 }  
1941    
1942                  for (j = 0; j < rmb_height; j++)        // chroma          }else{  /* B_VOP */
1943                  for (i = 1; i < rmb_width; i++)  
1944                  {                  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)  
1945                          {                          {
1946                                  vfilter_31(dec->cur.u + (j*16)*edged_width2 + i*16 - 1,                          DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1947                                                                    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);  
                         }  
1948                  }                  }
         }  
   
         BitstreamByteAlign(&bs);  
   
 #ifdef BFRAMES_DEC  
         // test if no B_VOP  
         if (dec->low_delay || dec->frames == 0 || ((dec->packed_mode) && !(frame->length > BitstreamPos(&bs) / 8))) {  
 #endif  
                 image_output(&dec->cur, dec->width, dec->height, dec->edged_width,  
                                          frame->image, frame->stride, frame->colorspace, dec->interlacing);  
1949    
1950  #ifdef BFRAMES_DEC                  if (dec->frames < 2)
1951                    {
1952                            /* attemping to decode a bvop without atleast 2 reference frames */
1953                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1954                                                    "broken b-frame, mising ref frames");
1955                    }else if (dec->time_pp <= dec->time_bp) {
1956                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1957                            decoded in vfw. */
1958                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1959                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1960          } else {          } else {
1961                  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();  
                 }  
1962          }          }
 #endif  
1963    
1964          if (vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP) {                  decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1965                  image_swap(&dec->refn[0], &dec->refn[1]);                  output = 1;
1966                  image_swap(&dec->cur, &dec->refn[0]);                  dec->frames++;
   
                 // swap MACROBLOCK  
                 // the Divx will not set the low_delay flage some times  
                 // so follow code will wrong to not swap at that time  
                 // 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);  
1967          }          }
1968    
1969            BitstreamByteAlign(&bs);
1970    
1971          if (success == 0 && dec->packed_mode)          /* low_delay_default mode: repeat in packed_mode */
1972            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1973          {          {
1974                  success = 1;                  success = 1;
1975          //      if (frame->length > BitstreamPos(&bs) / 8)      // multiple vops packed together                  goto repeat;
                 goto start;  
1976          }          }
1977    
1978  done :  done :
1979    
1980            /* low_delay_default mode: if we've gotten here without outputting anything,
1981               then output the recently decoded frame, or print an error message  */
1982            if (dec->low_delay_default && output == 0)
1983            {
1984                    if (dec->packed_mode && seen_something)
1985                    {
1986                            /* output the recently decoded frame */
1987                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1988                            output = 1;
1989                    }
1990                    else
1991                    {
1992                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1993                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1994                                    "warning: nothing to output");
1995                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1996                                    "bframe decoder lag");
1997    
1998                            decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
1999                    }
2000            }
2001    
2002          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
2003    
2004          if (stats)          if (stats)
2005          {          {
2006                  stats->notify = XVID_DEC_VOP;                  stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
2007                  stats->data.vop.time_base = (int)dec->time_base;                  stats->data.vop.time_base = (int)dec->time_base;
2008                  stats->data.vop.time_increment = 0;     //XXX: todo                  stats->data.vop.time_increment = 0;     //XXX: todo
2009          }          }

Legend:
Removed from v.1.37.2.15  
changed lines
  Added in v.1.37.2.30

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