[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.12, Tue Nov 19 13:21:24 2002 UTC revision 1.42, Sat Oct 19 12:20:33 2002 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Decoder main module  -   *  -  Decoder main module  -
5   *   *
6     *  Copyright(C) 2002 MinChen <chenm001@163.com>
7     *               2002 Peter Ross <pross@xvid.org>
8     *
9   *  This program is an implementation of a part of one or more MPEG-4   *  This program is an implementation of a part of one or more MPEG-4
10   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
11   *  to use this software module in hardware or software products are   *  to use this software module in hardware or software products are
# Line 33  Line 36 
36   *  History:   *  History:
37   *   *
38   *  15.07.2002  fix a bug in B-frame decode at DIRECT mode   *  15.07.2002  fix a bug in B-frame decode at DIRECT mode
39   *              MinChen <chenm001@163.com>  
40   *  10.07.2002  added BFRAMES_DEC_DEBUG support   *  10.07.2002  added BFRAMES_DEC_DEBUG support
41   *              Fix a little bug for low_delay flage   *              Fix a little bug for low_delay flage
42   *              MinChen <chenm001@163.com>   *              MinChen <chenm001@163.com>
# Line 53  Line 56 
56   *  26.03.2002  interlacing support - moved transfers outside decode loop   *  26.03.2002  interlacing support - moved transfers outside decode loop
57   *  26.12.2001  decoder_mbinter: dequant/idct moved within if(coded) block   *  26.12.2001  decoder_mbinter: dequant/idct moved within if(coded) block
58   *  22.12.2001  lock based interpolation   *  22.12.2001  lock based interpolation
59   *  01.12.2001  inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>   *  01.12.2001  inital version; (c)2001 peter ross <pross@xvid.org>
60   *   *
61   *  $Id$   *  $Id$
62   *   *
# Line 84  Line 87 
87  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
88  #include "utils/timer.h"  #include "utils/timer.h"
89  #include "utils/emms.h"  #include "utils/emms.h"
 #include "motion/motion.h"  
90    
91  #include "image/image.h"  #include "image/image.h"
92  #include "image/colorspace.h"  #include "image/colorspace.h"
93  #include "utils/mem_align.h"  #include "utils/mem_align.h"
94    
95  int  int
96  decoder_resize(DECODER * dec)  decoder_create(XVID_DEC_PARAM * param)
97  {  {
98          /* free existing */          DECODER *dec;
   
         image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
         image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
         image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);  
         image_destroy(&dec->refh, dec->edged_width, dec->edged_height);  
         image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
99    
100          if (dec->last_mbs)          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
101                  xvid_free(dec->last_mbs);          if (dec == NULL) {
102          if (dec->mbs)                  return XVID_ERR_MEMORY;
103                  xvid_free(dec->mbs);          }
104            param->handle = dec;
105    
106          /* realloc */          dec->width = param->width;
107            dec->height = param->height;
108    
109          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
110          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
111    
112          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
113          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
114            dec->low_delay = 0;
115    
116          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
117                  xvid_free(dec);                  xvid_free(dec);
# Line 124  Line 123 
123                  xvid_free(dec);                  xvid_free(dec);
124                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
125          }          }
   
126          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
127          // for support B-frame to reference last 2 frame          // for support B-frame to reference last 2 frame
128          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
# Line 141  Line 139 
139                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
140          }          }
141    
         if (image_create(&dec->refh, dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
142          dec->mbs =          dec->mbs =
143                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
144                                          CACHE_LINE);                                          CACHE_LINE);
# Line 158  Line 147 
147                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
148                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
149                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
                 image_destroy(&dec->refh, dec->edged_width, dec->edged_height);  
150                  xvid_free(dec);                  xvid_free(dec);
151                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
152          }          }
153    
154          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
155    
156          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
# Line 175  Line 164 
164                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
165                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
166                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
                 image_destroy(&dec->refh, dec->edged_width, dec->edged_height);  
167                  xvid_free(dec);                  xvid_free(dec);
168                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
169          }          }
170    
171          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
172    
         return XVID_ERR_OK;  
 }  
   
   
 int  
 decoder_create(XVID_DEC_PARAM * param)  
 {  
         DECODER *dec;  
   
         dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);  
         if (dec == NULL) {  
                 return XVID_ERR_MEMORY;  
         }  
         memset(dec, 0, sizeof(DECODER));  
   
         param->handle = dec;  
   
         dec->width = param->width;  
         dec->height = param->height;  
   
         image_null(&dec->cur);  
         image_null(&dec->refn[0]);  
         image_null(&dec->refn[1]);  
         image_null(&dec->refn[2]);  
         image_null(&dec->refh);  
   
         dec->mbs = NULL;  
         dec->last_mbs = NULL;  
   
173          init_timer();          init_timer();
174    
175          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
176          // for support B-frame to save reference frame's time          // for support B-frame to save reference frame's time
177          dec->frames = -1;          dec->frames = -1;
178          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
         dec->low_delay = 0;  
   
         dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);  
179    
         if (dec->fixed_dimensions)  
                 return decoder_resize(dec);  
         else  
180                  return XVID_ERR_OK;                  return XVID_ERR_OK;
181  }  }
182    
# Line 236  Line 189 
189          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
190          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
191          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
         image_destroy(&dec->refh, dec->edged_width, dec->edged_height);  
192          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
193          xvid_free(dec);          xvid_free(dec);
194    
# Line 319  Line 271 
271                  start_timer();                  start_timer();
272                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
273                  {                  {
274                          int direction = dec->alternate_vertical_scan ?                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],
275                                  2 : pMB->acpred_directions[i];                                                          start_coeff);
   
                         get_intra_block(bs, &block[i * 64], direction, start_coeff);  
276                  }                  }
277                  stop_coding_timer();                  stop_coding_timer();
278    
# Line 364  Line 314 
314    
315  #define SIGN(X) (((X)>0)?1:-1)  #define SIGN(X) (((X)>0)?1:-1)
316  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
317    static const uint32_t roundtab[16] =
318            { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };
319    
320    
321  // decode an inter macroblock  // decode an inter macroblock
322    
# Line 400  Line 353 
353    
354                  if (dec->quarterpel)                  if (dec->quarterpel)
355                  {                  {
356                          uv_dx /= 2;                          uv_dx = (uv_dx >> 1) | (uv_dx & 1);
357                          uv_dy /= 2;                          uv_dy = (uv_dy >> 1) | (uv_dy & 1);
                 }  
   
                 uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];  
                 uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];  
   
                 start_timer();  
                 if(dec->quarterpel) {  
                         interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,  
                                                                             dec->refh.y + 128, 16*x_pos, 16*y_pos,  
                                                                             pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);  
                 }  
                 else {  
                         interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,  
                                                                   pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);  
                         interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,  
                                                               pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);  
                         interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,  
                                                                   pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);  
                         interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,  
                                                                   pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);  
358                  }                  }
359    
360                  interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
361                                                            uv_dx, uv_dy, stride2, rounding);                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
                 interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,  
                                                           uv_dx, uv_dy, stride2, rounding);  
                 stop_comp_timer();  
   
362          } else {          } else {
363                  int sum;                  int sum;
364                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
365    
366                  if(dec->quarterpel)                  if(dec->quarterpel)
367                          sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);                  {
368                  else                          sum /= 2;
369                          sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  }
370    
371                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
372    
                 if(dec->quarterpel)  
                         sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);  
                 else  
373                          sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                          sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
374    
375                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                  if (dec->quarterpel)
376                    {
377                            sum /= 2;
378                    }
379    
380                    uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
381            }
382    
383                  start_timer();                  start_timer();
384                  if(dec->quarterpel) {                  if(dec->quarterpel) {
385                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                  DPRINTF(DPRINTF_DEBUG, "QUARTERPEL\n");
386                                                                            dec->refh.y + 128, 16*x_pos, 16*y_pos,                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
387                                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);
388                          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, 16*x_pos + 8, 16*y_pos,
                                                                           dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,  
389                                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);
390                          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, 16*x_pos, 16*y_pos + 8,
                                                                           dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,  
391                                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);                                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);
392                          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, 16*x_pos + 8, 16*y_pos + 8,
                                                                           dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,  
393                                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);
394                  }                  }
395                  else {                  else {
# Line 478  Line 408 
408                  interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,                  interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
409                                                            uv_dx, uv_dy, stride2, rounding);                                                            uv_dx, uv_dy, stride2, rounding);
410                  stop_comp_timer();                  stop_comp_timer();
         }  
411    
412          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
413                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
414                  {                  {
415                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
416    
417                          start_timer();                          start_timer();
418                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64]);
419                          stop_coding_timer();                          stop_coding_timer();
420    
421                          start_timer();                          start_timer();
# Line 530  Line 457 
457  void  void
458  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
459                             Bitstream * bs,                             Bitstream * bs,
                            int reduced_resolution,  
460                             int quant,                             int quant,
461                             int intra_dc_threshold)                             int intra_dc_threshold)
462  {  {
463          uint32_t bound;          uint32_t bound;
464          uint32_t x, y;          uint32_t x, y;
         int mb_width = dec->mb_width;  
         int mb_height = dec->mb_height;  
   
         if (reduced_resolution)  
         {  
                 mb_width /= 2;  
                 mb_height /= 2;  
         }  
465    
466          bound = 0;          bound = 0;
467    
468          for (y = 0; y < mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
469                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
470                          MACROBLOCK *mb;                          MACROBLOCK *mb;
471                          uint32_t mcbpc;                          uint32_t mcbpc;
472                          uint32_t cbpc;                          uint32_t cbpc;
# Line 561  Line 479 
479    
480                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
481                          {                          {
482                                  bound = read_video_packet_header(bs, dec, 0,                                  bound = read_video_packet_header(bs, 0, &quant);
                                                         &quant, NULL, NULL, &intra_dc_threshold);  
483                                  x = bound % dec->mb_width;                                  x = bound % dec->mb_width;
484                                  y = bound / dec->mb_width;                                  y = bound / dec->mb_width;
485                          }                          }
# Line 595  Line 512 
512    
513                          if (dec->interlacing) {                          if (dec->interlacing) {
514                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
515                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(DPRINTF_DEBUG, "deci: field_dct: %d", mb->field_dct);
516                          }                          }
517    
518                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
519                                                          intra_dc_threshold, bound);                                                          intra_dc_threshold, bound);
   
520                  }                  }
521                  if(dec->out_frm)                  if(dec->out_frm)
522                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);
523    
524          }          }
525    
526  }  }
# Line 652  Line 569 
569    
570          mv->x = mv_x;          mv->x = mv_x;
571          mv->y = mv_y;          mv->y = mv_y;
 }  
   
   
   
 static __inline int gmc_sanitize(int value, int quarterpel, int fcode)  
 {  
         int length = 1 << (fcode+4);  
   
         if (quarterpel) value *= 2;  
572    
         if (value < -length)  
                 return -length;  
         else if (value >= length)  
                 return length-1;  
         else return value;  
573  }  }
574    
575    
 /* for P_VOP set gmc_mv to NULL */  
576  void  void
577  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
578                             Bitstream * bs,                             Bitstream * bs,
579                             int rounding,                             int rounding,
                            int reduced_resolution,  
580                             int quant,                             int quant,
581                             int fcode,                             int fcode,
582                             int intra_dc_threshold,                             int intra_dc_threshold)
                            VECTOR * gmc_mv)  
583  {  {
584    
585          uint32_t x, y;          uint32_t x, y;
# Line 704  Line 604 
604    
605                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1))
606                          {                          {
607                                  bound = read_video_packet_header(bs, dec, fcode - 1,                                  bound = read_video_packet_header(bs, fcode - 1, &quant);
                                         &quant, &fcode, NULL, &intra_dc_threshold);  
608                                  x = bound % dec->mb_width;                                  x = bound % dec->mb_width;
609                                  y = bound / dec->mb_width;                                  y = bound / dec->mb_width;
610                          }                          }
# Line 722  Line 621 
621                                  uint32_t cbpy;                                  uint32_t cbpy;
622                                  uint32_t cbp;                                  uint32_t cbp;
623                                  uint32_t intra;                                  uint32_t intra;
                                 int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC  
624    
625                                  cp_mb++;                                  cp_mb++;
626                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
# Line 739  Line 637 
637                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
638                                  }                                  }
639    
                                 if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))  
                                 {  
                                         mcsel = BitstreamGetBit(bs);  
                                 }  
   
640                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
641                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);
642    
# Line 765  Line 658 
658                                  if (dec->interlacing) {                                  if (dec->interlacing) {
659                                          if (cbp || intra) {                                          if (cbp || intra) {
660                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
661                                                  DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_dct: %d", mb->field_dct);
662                                          }                                          }
663    
664                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
665                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
666                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_pred: %d", mb->field_pred);
667    
668                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
669                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
670                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_top: %d", mb->field_for_top);
671                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
672                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_bot: %d", mb->field_for_bot);
673                                                  }                                                  }
674                                          }                                          }
675                                  }                                  }
676    
677                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
678                                            if (dec->interlacing && mb->field_pred) {
                                         if (mcsel)  
                                         {  
                                                 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);  
   
                                         } else if (dec->interlacing && mb->field_pred) {  
679                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
680                                                                                    fcode, bound);                                                                                    fcode, bound);
681                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 820  Line 707 
707    
708                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
709                                                                  rounding);                                                                  rounding);
710                            } else                          // not coded
                         }  
                         else if (gmc_mv)        /* not coded S_VOP macroblock */  
                         {  
                                 mb->mode = MODE_NOT_CODED;  
                                 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);  
                                 decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding);  
                         }  
                         else    /* not coded P_VOP macroblock */  
711                          {                          {
712                                    DPRINTF(DPRINTF_DEBUG, "P-frame MB at (X,Y)=(%d,%d)", x, y);
713    
714                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
715                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
716                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
717    
718                                  // copy macroblock directly from ref to cur                                  // copy macroblock directly from ref to cur
719    
720                                  start_timer();                                  start_timer();
# Line 871  Line 752 
752                                                                   dec->refn[0].v +                                                                   dec->refn[0].v +
753                                                                   (8 * y) * dec->edged_width / 2 + (8 * x),                                                                   (8 * y) * dec->edged_width / 2 + (8 * x),
754                                                                   dec->edged_width / 2);                                                                   dec->edged_width / 2);
   
755                                  stop_transfer_timer();                                  stop_transfer_timer();
   
756                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
757                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
758                                    cp_mb = 0;                                    cp_mb = 0;
# Line 1001  Line 880 
880          stop_comp_timer();          stop_comp_timer();
881    
882          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
883                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
884                  {                  {
885                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
886    
887                          start_timer();                          start_timer();
888                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64]);
889                          stop_coding_timer();                          stop_coding_timer();
890    
891                          start_timer();                          start_timer();
# Line 1046  Line 923 
923          stop_transfer_timer();          stop_transfer_timer();
924  }  }
925    
926    
927  // add by MinChen <chenm001@163.com>  // add by MinChen <chenm001@163.com>
928  // decode an B-frame direct &  inter macroblock  // decode an B-frame direct &  inter macroblock
929  void  void
# Line 1153  Line 1031 
1031          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,
1032                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1033    
1034          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos,
1035                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,                                           stride);
1036                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos,          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos,
1037                                                  stride, 0);                                           stride);
1038            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,
1039          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                           stride);
1040                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,
1041                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8,                                           16 * y_pos + 8, stride);
1042                                                  stride, 0);          interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,
1043                                             stride2);
1044          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,          interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos,
1045                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                           stride2);
                                                 dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos,  
                                                 stride, 0);  
   
         interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,  
                                                 dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,  
                                                 dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,  
                                                 stride, 0);  
   
         interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 stride2, 0);  
   
         interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 stride2, 0);  
   
1046          stop_comp_timer();          stop_comp_timer();
1047    
1048          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
1049                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
1050                  {                  {
1051                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1052    
1053                          start_timer();                          start_timer();
1054                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64]);
1055                          stop_coding_timer();                          stop_coding_timer();
1056    
1057                          start_timer();                          start_timer();
# Line 1424  Line 1282 
1282                                  break;                                  break;
1283    
1284                          default:                          default:
1285                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(DPRINTF_ERROR, "Not support B-frame mb_type = %d", mb->mb_type);
1286                          }                          }
1287    
1288                  }                                               // end of FOR                  }                                               // end of FOR
# Line 1451  Line 1309 
1309    
1310  int  int
1311  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1312                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                             XVID_DEC_FRAME * frame)
1313  {  {
1314    
1315          Bitstream bs;          Bitstream bs;
1316          uint32_t rounding;          uint32_t rounding;
         uint32_t reduced_resolution;  
1317          uint32_t quant;          uint32_t quant;
1318          uint32_t fcode_forward;          uint32_t fcode_forward;
1319          uint32_t fcode_backward;          uint32_t fcode_backward;
1320          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
         VECTOR gmc_mv[5];  
1321          uint32_t vop_type;          uint32_t vop_type;
         int success = 0;  
1322    
1323          start_global_timer();          start_global_timer();
1324    
# Line 1471  Line 1326 
1326    
1327          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1328    
         // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's  
         if(frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)  
         {  
                 if (stats)  
                         stats->notify = XVID_DEC_VOP;  
                 frame->length = 1;  
                 image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,  
                                          frame->image, frame->stride, frame->colorspace, dec->interlacing);  
                 emms();  
                 return XVID_ERR_OK;  
         }  
   
 start:  
1329          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
1330          // for support B-frame to reference last 2 frame          // for support B-frame to reference last 2 frame
1331          dec->frames++;          dec->frames++;
   
 xxx:  
1332          vop_type =          vop_type =
1333                  BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,
1334                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);                                                           &fcode_backward, &intra_dc_threshold);
   
         DPRINTF(DPRINTF_HEADER, "vop_type=%i", vop_type);  
   
         if (vop_type == -1 && success)  
                 goto done;  
   
         if (vop_type == -2 || vop_type == -3)  
         {  
                 if (vop_type == -3)  
                         decoder_resize(dec);  
   
                 if (stats)  
                 {  
                         stats->notify = XVID_DEC_VOL;  
                         stats->data.vol.general = 0;  
                         if (dec->interlacing)  
                                 stats->data.vol.general |= XVID_INTERLACING;  
                         stats->data.vol.width = dec->width;  
                         stats->data.vol.height = dec->height;  
                         stats->data.vol.aspect_ratio = dec->aspect_ratio;  
                         stats->data.vol.par_width = dec->par_width;  
                         stats->data.vol.par_height = dec->par_height;  
                         frame->length = BitstreamPos(&bs) / 8;  
                         return XVID_ERR_OK;  
                 }  
                 goto xxx;  
         }  
1335    
1336          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
1337    
1338          switch (vop_type) {          switch (vop_type) {
1339          case P_VOP:          case P_VOP:
1340                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,
1341                                                  fcode_forward, intra_dc_threshold, NULL);                                             intra_dc_threshold);
1342  #ifdef BFRAMES_DEC  #ifdef BFRAMES_DEC
1343                  DEBUG1("P_VOP  Time=", dec->time);                  DPRINTF(DPRINTF_DEBUG, "P_VOP  Time=%d", dec->time);
1344  #endif  #endif
1345                  break;                  break;
1346    
1347          case I_VOP:          case I_VOP:
1348                  decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1349  #ifdef BFRAMES_DEC  #ifdef BFRAMES_DEC
1350                  DEBUG1("I_VOP  Time=", dec->time);                  DPRINTF(DPRINTF_DEBUG, "I_VOP  Time=%d", dec->time);
1351  #endif  #endif
1352                  break;                  break;
1353    
1354          case B_VOP:          case B_VOP:
1355  #ifdef BFRAMES_DEC  #ifdef BFRAMES_DEC
1356                  if (dec->time_pp > dec->time_bp) {                  if (dec->time_pp > dec->time_bp) {
1357                          DEBUG1("B_VOP  Time=", dec->time);                          DPRINTF(DPRINTF_DEBUG, "B_VOP  Time=%d", dec->time);
1358                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1359                  } else {                  } else {
1360                          DEBUG("broken B-frame!");                          DPRINTF(DPRINTF_DEBUG, "Broken B_VOP");
1361                  }                  }
1362  #else  #else
1363                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1364  #endif  #endif
1365                  break;                  break;
1366    
         case S_VOP :  
                 decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,  
                                                 fcode_forward, intra_dc_threshold, gmc_mv);  
                 break;  
   
1367          case N_VOP:                             // vop not coded          case N_VOP:                             // vop not coded
1368                  // when low_delay==0, N_VOP's should interpolate between the past and future frames                  // when low_delay==0, N_VOP's should interpolate between the past and future frames
1369                  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  
1370                  break;                  break;
1371    
1372          default:          default:
                 if (stats)  
                         stats->notify = 0;  
   
                 emms();  
1373                  return XVID_ERR_FAIL;                  return XVID_ERR_FAIL;
1374          }          }
1375    
1376          BitstreamByteAlign(&bs);  #ifdef BFRAMES_DEC_DEBUG
1377            if (frame->length != BitstreamPos(&bs) / 8){
1378                    DPRINTF(DPRINTF_DEBUG, "InLen: %d / UseLen: %d", frame->length, BitstreamPos(&bs) / 8);
1379            }
1380    #endif
1381            frame->length = BitstreamPos(&bs) / 8;
1382    
1383    
1384  #ifdef BFRAMES_DEC  #ifdef BFRAMES_DEC
1385          // test if no B_VOP          // test if no B_VOP
1386          if (dec->low_delay || dec->frames == 0 || ((dec->packed_mode) && !(frame->length > BitstreamPos(&bs) / 8))) {          if (dec->low_delay || dec->frames == 0) {
1387  #endif  #endif
1388                  image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                  image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
1389                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);                                           frame->image, frame->stride, frame->colorspace);
1390    
1391  #ifdef BFRAMES_DEC  #ifdef BFRAMES_DEC
1392          } else {          } else {
1393                  if (dec->frames >= 1 && !(dec->packed_mode)) {                  if (dec->frames >= 1) {
1394                          start_timer();                          start_timer();
1395                          if ((vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP)) {                          if ((vop_type == I_VOP || vop_type == P_VOP)) {
1396                                  image_output(&dec->refn[0], dec->width, dec->height,                                  image_output(&dec->refn[0], dec->width, dec->height,
1397                                                           dec->edged_width, frame->image, frame->stride,                                                           dec->edged_width, frame->image, frame->stride,
1398                                                           frame->colorspace, dec->interlacing);                                                           frame->colorspace);
1399                          } else if (vop_type == B_VOP) {                          } else if (vop_type == B_VOP) {
1400                                  image_output(&dec->cur, dec->width, dec->height,                                  image_output(&dec->cur, dec->width, dec->height,
1401                                                           dec->edged_width, frame->image, frame->stride,                                                           dec->edged_width, frame->image, frame->stride,
1402                                                           frame->colorspace, dec->interlacing);                                                           frame->colorspace);
1403                          }                          }
1404                          stop_conv_timer();                          stop_conv_timer();
1405                  }                  }
1406          }          }
1407  #endif  #endif
1408    
1409          if (vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP) {          if (vop_type == I_VOP || vop_type == P_VOP) {
1410                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1411                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1412    
# Line 1615  Line 1422 
1422                          mb_swap(&dec->mbs, &dec->last_mbs);                          mb_swap(&dec->mbs, &dec->last_mbs);
1423          }          }
1424    
         success = 1;  
   
         if (frame->length > BitstreamPos(&bs) / 8)      // multiple vops packed together  
                 goto start;  
   
 done :  
   
         frame->length = BitstreamPos(&bs) / 8;  
   
         if (stats)  
         {  
                 stats->notify = XVID_DEC_VOP;  
                 stats->data.vop.time_base = (int)dec->time_base;  
                 stats->data.vop.time_increment = 0;     //XXX: todo  
         }  
   
1425          emms();          emms();
1426    
1427          stop_global_timer();          stop_global_timer();

Legend:
Removed from v.1.37.2.12  
changed lines
  Added in v.1.42

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