[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.21, Sat Dec 14 06:07:02 2002 UTC revision 1.49.2.15, Tue Oct 7 13:02:35 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Decoder main module  -   *  - Decoder Module -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002      MinChen <chenm001@163.com>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002-2003 Peter Ross <pross@xvid.org>
  *  to use this software module in hardware or software products are  
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
8   *   *
9   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
10   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 26  Line 20 
20   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
21   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   *   *
  *************************************************************************/  
   
 /**************************************************************************  
  *  
  *  History:  
  *  
  *  15.07.2002  fix a bug in B-frame decode at DIRECT mode  
  *              MinChen <chenm001@163.com>  
  *  10.07.2002  added BFRAMES_DEC_DEBUG support  
  *              Fix a little bug for low_delay flage  
  *              MinChen <chenm001@163.com>  
  *  28.06.2002  added basic resync support to iframe/pframe_decode()  
  *  22.06.2002  added primative N_VOP support  
  *                              #define BFRAMES_DEC now enables Minchen's bframe decoder  
  *  08.05.2002  add low_delay support for B_VOP decode  
  *              MinChen <chenm001@163.com>  
  *  05.05.2002  fix some B-frame decode problem  
  *  02.05.2002  add B-frame decode support(have some problem);  
  *              MinChen <chenm001@163.com>  
  *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>  
  *  29.03.2002  interlacing fix - compensated block wasn't being used when  
  *              reconstructing blocks, thus artifacts  
  *              interlacing speedup - used transfers to re-interlace  
  *              interlaced decoding should be as fast as progressive now  
  *  26.03.2002  interlacing support - moved transfers outside decode loop  
  *  26.12.2001  decoder_mbinter: dequant/idct moved within if(coded) block  
  *  22.12.2001  lock based interpolation  
  *  01.12.2001  inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>  
  *  
23   *  $Id$   *  $Id$
24   *   *
25   *************************************************************************/   ****************************************************************************/
26    
27    #include <stdio.h>
28  #include <stdlib.h>  #include <stdlib.h>
29  #include <string.h>  #include <string.h>
30    
# Line 68  Line 34 
34    
35  #include "xvid.h"  #include "xvid.h"
36  #include "portab.h"  #include "portab.h"
37    #include "global.h"
38    
39  #include "decoder.h"  #include "decoder.h"
40  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
41  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
42    
43  #include "quant/quant_h263.h"  #include "quant/quant.h"
 #include "quant/quant_mpeg4.h"  
44  #include "dct/idct.h"  #include "dct/idct.h"
45  #include "dct/fdct.h"  #include "dct/fdct.h"
46  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
# Line 87  Line 53 
53  #include "utils/timer.h"  #include "utils/timer.h"
54  #include "utils/emms.h"  #include "utils/emms.h"
55  #include "motion/motion.h"  #include "motion/motion.h"
56    #include "motion/gmc.h"
57    
58  #include "image/image.h"  #include "image/image.h"
59  #include "image/colorspace.h"  #include "image/colorspace.h"
# Line 96  Line 63 
63  decoder_resize(DECODER * dec)  decoder_resize(DECODER * dec)
64  {  {
65          /* free existing */          /* free existing */
   
66          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
67          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
68          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
69          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
70          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
71    
72            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
73    
74          if (dec->last_mbs)          if (dec->last_mbs)
75                  xvid_free(dec->last_mbs);                  xvid_free(dec->last_mbs);
76          if (dec->mbs)          if (dec->mbs)
77                  xvid_free(dec->mbs);                  xvid_free(dec->mbs);
78    
79          /* realloc */          /* realloc */
   
80          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
81          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
82    
# Line 127  Line 94 
94                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
95          }          }
96    
97          // add by chenm001 <chenm001@163.com>          /* Support B-frame to reference last 2 frame */
         // for support B-frame to reference last 2 frame  
98          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
99                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
100                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
# Line 152  Line 118 
118                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
119          }          }
120    
121            if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
122                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
123                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
124                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
125                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
126                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
127                    xvid_free(dec);
128                    return XVID_ERR_MEMORY;
129            }
130    
131          dec->mbs =          dec->mbs =
132                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
133                                          CACHE_LINE);                                          CACHE_LINE);
# Line 166  Line 142 
142          }          }
143          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
144    
145          // add by chenm001 <chenm001@163.com>          /* For skip MB flag */
         // for skip MB flag  
146          dec->last_mbs =          dec->last_mbs =
147                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
148                                          CACHE_LINE);                                          CACHE_LINE);
# Line 184  Line 159 
159    
160          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);
161    
162          return XVID_ERR_OK;          return 0;
163  }  }
164    
165    
166  int  int
167  decoder_create(XVID_DEC_PARAM * param)  decoder_create(xvid_dec_create_t * create)
168  {  {
169          DECODER *dec;          DECODER *dec;
170    
171            if (XVID_VERSION_MAJOR(create->version) != 1)   /* v1.x.x */
172                    return XVID_ERR_VERSION;
173    
174          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
175          if (dec == NULL) {          if (dec == NULL) {
176                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
177          }          }
178          memset(dec, 0, sizeof(DECODER));          memset(dec, 0, sizeof(DECODER));
179    
180          param->handle = dec;          create->handle = dec;
181    
182          dec->width = param->width;          dec->width = create->width;
183          dec->height = param->height;          dec->height = create->height;
184    
185          image_null(&dec->cur);          image_null(&dec->cur);
186          image_null(&dec->refn[0]);          image_null(&dec->refn[0]);
# Line 210  Line 188 
188          image_null(&dec->tmp);          image_null(&dec->tmp);
189          image_null(&dec->qtmp);          image_null(&dec->qtmp);
190    
191            /* image based GMC */
192            image_null(&dec->gmc);
193    
194    
195          dec->mbs = NULL;          dec->mbs = NULL;
196          dec->last_mbs = NULL;          dec->last_mbs = NULL;
197    
198          init_timer();          init_timer();
199    
200          // add by chenm001 <chenm001@163.com>          /* For B-frame support (used to save reference frame's time */
         // for support B-frame to save reference frame's time  
201          dec->frames = 0;          dec->frames = 0;
202          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
203          dec->low_delay = 0;          dec->low_delay = 0;
# Line 227  Line 208 
208          if (dec->fixed_dimensions)          if (dec->fixed_dimensions)
209                  return decoder_resize(dec);                  return decoder_resize(dec);
210          else          else
211                  return XVID_ERR_OK;                  return 0;
212  }  }
213    
214    
# Line 236  Line 217 
217  {  {
218          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
219          xvid_free(dec->mbs);          xvid_free(dec->mbs);
220    
221            /* image based GMC */
222            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
223    
224          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
225          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
226          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
# Line 244  Line 229 
229          xvid_free(dec);          xvid_free(dec);
230    
231          write_timer();          write_timer();
232          return XVID_ERR_OK;          return 0;
233  }  }
234    
235    
# Line 256  Line 241 
241    
242    
243    
244  // decode an intra macroblock  /* decode an intra macroblock */
   
245  void  void
246  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
247                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
# Line 292  Line 276 
276                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
277          }          }
278    
279          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
280    
281          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
282                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
# Line 315  Line 299 
299                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;
300    
301                          if (dc_size > 8) {                          if (dc_size > 8) {
302                                  BitstreamSkip(bs, 1);   // marker                                  BitstreamSkip(bs, 1);   /* marker */
303                          }                          }
304    
305                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
306                          start_coeff = 1;                          start_coeff = 1;
307    
308                          DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);
309                  } else {                  } else {
310                          start_coeff = 0;                          start_coeff = 0;
311                  }                  }
312    
313                  start_timer();                  start_timer();
314                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
315                  {                  {
316                          int direction = dec->alternate_vertical_scan ?                          int direction = dec->alternate_vertical_scan ?
317                                  2 : pMB->acpred_directions[i];                                  2 : pMB->acpred_directions[i];
# Line 342  Line 326 
326    
327                  start_timer();                  start_timer();
328                  if (dec->quant_type == 0) {                  if (dec->quant_type == 0) {
329                          dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
330                  } else {                  } else {
331                          dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
332                  }                  }
333                  stop_iquant_timer();                  stop_iquant_timer();
334    
# Line 384  Line 368 
368    
369    
370    
371    /* decode an inter macroblock */
 #define SIGN(X) (((X)>0)?1:-1)  
 #define ABS(X) (((X)>0)?(X):-(X))  
   
 // decode an inter macroblock  
   
372  void  void
373  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
374                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
375                                  const uint32_t x_pos,                                  const uint32_t x_pos,
376                                  const uint32_t y_pos,                                  const uint32_t y_pos,
377                                  const uint32_t acpred_flag,                                  const uint32_t fcode,
378                                  const uint32_t cbp,                                  const uint32_t cbp,
379                                  Bitstream * bs,                                  Bitstream * bs,
380                                  const uint32_t quant,                                  const uint32_t quant,
# Line 433  Line 412 
412          }          }
413    
414          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;  
415    
416                  if (dec->quarterpel)                  uv_dx = mv[0].x / (1 + dec->quarterpel);
417                  {                  uv_dy = mv[0].y / (1 + dec->quarterpel);
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
418    
419                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
420                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
# Line 508  Line 482 
482                          interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,                          interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
483                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
484    
485                          // set_block(pY_Cur, stride, 32, 32, 127);                          /* set_block(pY_Cur, stride, 32, 32, 127); */
486                  }                  }
487                  else                  else
488                  {                  {
# Line 548  Line 522 
522          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
523                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
524    
525                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
526                  {                  {
527                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
528    
529                          start_timer();                          start_timer();
530                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 558  Line 532 
532    
533                          start_timer();                          start_timer();
534                          if (dec->quant_type == 0) {                          if (dec->quant_type == 0) {
535                                  dequant_inter(&data[i * 64], &block[i * 64], iQuant);                                  dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
536                          } else {                          } else {
537                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);                                  dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
538                          }                          }
539                          stop_iquant_timer();                          stop_iquant_timer();
540    
# Line 610  Line 584 
584  }  }
585    
586    
587    static void
588    decoder_mbgmc(DECODER * dec,
589                                    MACROBLOCK * const pMB,
590                                    const uint32_t x_pos,
591                                    const uint32_t y_pos,
592                                    const uint32_t fcode,
593                                    const uint32_t cbp,
594                                    Bitstream * bs,
595                                    const uint32_t quant,
596                                    const uint32_t rounding,
597                                    const int reduced_resolution)   /* no reduced res support */
598    {
599    
600            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
601            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
602    
603            const uint32_t stride = dec->edged_width;
604            const uint32_t stride2 = stride / 2;
605            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
606            uint32_t i;
607            const uint32_t iQuant = pMB->quant;
608            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
609            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
610            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
611    
612            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
613    
614            start_timer();
615    
616    /* this is where the calculations are done */
617    
618            {       NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
619    
620                            gmc_data->predict_16x16(gmc_data,
621                                            dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
622                                            stride, stride, x_pos, y_pos, rounding);
623    
624                            gmc_data->predict_8x8(gmc_data,
625                                            dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,
626                                            dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,
627                                            stride2, stride2, x_pos, y_pos, rounding);
628    
629                            gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);
630    
631                    pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
632                    pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
633            }
634            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
635    
636    /*
637            transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
638            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
639            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
640    */
641    
642    
643            stop_transfer_timer();
644    
645            if (!cbp) return;
646    
647            for (i = 0; i < 6; i++) {
648                    int direction = dec->alternate_vertical_scan ? 2 : 0;
649    
650                    if (cbp & (1 << (5 - i)))       /* coded */
651                    {
652                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
653    
654                            start_timer();
655                            get_inter_block(bs, &block[i * 64], direction);
656                            stop_coding_timer();
657    
658                            start_timer();
659                            if (dec->quant_type == 0) {
660                                    dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
661                            } else {
662                                    dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
663                            }
664                            stop_iquant_timer();
665    
666                            start_timer();
667                            idct(&data[i * 64]);
668                            stop_idct_timer();
669                    }
670            }
671    
672    /* interlace + GMC is this possible ??? */
673    /*
674      if (dec->interlacing && pMB->field_dct) {
675              next_block = stride;
676              stride *= 2;
677      }
678    */
679            start_timer();
680            if (cbp & 32)
681                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
682            if (cbp & 16)
683                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
684            if (cbp & 8)
685                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
686            if (cbp & 4)
687                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
688            if (cbp & 2)
689                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
690            if (cbp & 1)
691                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
692            stop_transfer_timer();
693    }
694    
695    
696  void  void
697  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
698                             Bitstream * bs,                             Bitstream * bs,
# Line 651  Line 734 
734                          }                          }
735                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
736    
737                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
738    
739                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
740                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
# Line 678  Line 761 
761    
762                          if (dec->interlacing) {                          if (dec->interlacing) {
763                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
764                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
765                          }                          }
766    
767                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
# Line 716  Line 799 
799          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
800          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
801    
802          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv.x, mv.y, pmv.x, pmv.y);          DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
803    
804          mv.x += pmv.x;          mv.x += pmv.x;
805          mv.y += pmv.y;          mv.y += pmv.y;
# Line 739  Line 822 
822    
823    
824    
 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;  
 }  
825    
826    
827  /* for P_VOP set gmc_mv to NULL */  /* for P_VOP set gmc_warp to NULL */
828  void  void
829  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
830                             Bitstream * bs,                             Bitstream * bs,
# Line 762  Line 833 
833                             int quant,                             int quant,
834                             int fcode,                             int fcode,
835                             int intra_dc_threshold,                             int intra_dc_threshold,
836                             VECTOR * gmc_mv)                             const WARPPOINTS *const gmc_warp)
837  {  {
838    
839          uint32_t x, y;          uint32_t x, y;
# Line 782  Line 853 
853                                     dec->width, dec->height);                                     dec->width, dec->height);
854          stop_edges_timer();          stop_edges_timer();
855    
856            if (gmc_warp)
857            {
858    
859                    /* accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16 */
860    /*              {
861                            fprintf(stderr,"GMC parameters acc=%d(-> 1/%d), %d pts!!!\n",
862                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
863                                    dec->sprite_warping_points);
864                    }*/
865    
866                    generate_GMCparameters( dec->sprite_warping_points,
867                                    dec->sprite_warping_accuracy, gmc_warp,
868                                    dec->width, dec->height, &dec->new_gmc_data);
869    
870    /* image warping is done block-based  in decoder_mbgmc(), now */
871            }
872    
873          bound = 0;          bound = 0;
874    
875          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
# Line 789  Line 877 
877                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
878                          MACROBLOCK *mb;                          MACROBLOCK *mb;
879    
880                          // skip stuffing                          /* skip stuffing */
881                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
882                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
883    
# Line 802  Line 890 
890                          }                          }
891                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
892    
893                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
894    
895                          //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 */
896                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     /* block _is_ coded */
897                          {                          {
898                                  uint32_t mcbpc;                                  uint32_t mcbpc;
899                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 813  Line 901 
901                                  uint32_t cbpy;                                  uint32_t cbpy;
902                                  uint32_t cbp;                                  uint32_t cbp;
903                                  uint32_t intra;                                  uint32_t intra;
904                                  int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
905    
906                                  cp_mb++;                                  cp_mb++;
907                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
908                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
909                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
910    
911                                  DPRINTF(DPRINTF_MB, "mode %i", mb->mode);                                  DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
912                                  DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);                                  DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
913                                  acpred_flag = 0;                                  acpred_flag = 0;
914    
915                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
916    
917                                  if (intra) {                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
                                         acpred_flag = BitstreamGetBit(bs);  
                                 }  
   
                                 if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))  
                                 {  
918                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
919                                  }  
920                                    if (intra)
921                                            acpred_flag = BitstreamGetBit(bs);
922    
923                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
924                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(XVID_DEBUG_MB, "cbpy %i  mcsel %i \n", cbpy,mcsel);
925    
926                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
927    
928                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
929                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
930                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
931                                          quant += dquant;                                          quant += dquant;
932                                          if (quant > 31) {                                          if (quant > 31) {
933                                                  quant = 31;                                                  quant = 31;
934                                          } else if (quant < 1) {                                          } else if (quant < 1) {
935                                                  quant = 1;                                                  quant = 1;
936                                          }                                          }
937                                          DPRINTF(DPRINTF_MB, "quant %i", quant);                                          DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
938                                  }                                  }
939                                  mb->quant = quant;                                  mb->quant = quant;
940    
941                                  if (dec->interlacing) {                                  if (dec->interlacing) {
942                                          if (cbp || intra) {                                          if ((cbp || intra) && !mcsel) {
943                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
944                                                  DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
945                                          }                                          }
946    
947                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
948                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
949                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
950    
951                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
952                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
953                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
954                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
955                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
956                                                  }                                                  }
957                                          }                                          }
958                                  }                                  }
959    
960                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
961                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
962                                                                    rounding, reduced_resolution);
963                                            continue;
964    
965                                          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);  
966    
967                                          } else if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
968                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
969                                                                                    fcode, bound);                                                                                    fcode, bound);
970                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 887  Line 972 
972                                          } else {                                          } else {
973                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
974                                                                                    fcode, bound);                                                                                    fcode, bound);
975                                                  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;  
976                                          }                                          }
977                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
978    
# Line 898  Line 980 
980                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
981                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
982                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
983                                  } else                  // MODE_INTRA, MODE_INTRA_Q                                  } else                  /* MODE_INTRA, MODE_INTRA_Q */
984                                  {                                  {
985                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
986                                                  0;                                                  0;
# Line 909  Line 991 
991                                          continue;                                          continue;
992                                  }                                  }
993    
994                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
995                                                                  rounding, reduced_resolution);                                                                  rounding, reduced_resolution);
996    
997                          }                          }
998                          else if (gmc_mv)        /* not coded S_VOP macroblock */                          else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
999                          {                          {
1000                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED_GMC;
1001                                  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);  
1002                                  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();
1003                                  decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);  
1004                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1005                                                                    rounding, reduced_resolution);
1006    
1007                                    stop_transfer_timer();
1008    
1009                                    if(dec->out_frm && cp_mb > 0) {
1010                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1011                                      cp_mb = 0;
1012                                    }
1013                                    st_mb = x+1;
1014                          }                          }
1015                          else    /* not coded P_VOP macroblock */                          else    /* not coded P_VOP macroblock */
1016                          {                          {
# Line 926  Line 1018 
1018    
1019                                  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;
1020                                  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;
1021                                  // copy macroblock directly from ref to cur                                  /* copy macroblock directly from ref to cur */
1022    
1023                                  start_timer();                                  start_timer();
1024    
# Line 974  Line 1066 
1066  }  }
1067    
1068    
1069  // add by MinChen <chenm001@163.com>  /* decode B-frame motion vector */
 // decode B-frame motion vector  
1070  void  void
1071  get_b_motion_vector(DECODER * dec,  get_b_motion_vector(DECODER * dec,
1072                                          Bitstream * bs,                                          Bitstream * bs,
# Line 1019  Line 1110 
1110  }  }
1111    
1112    
1113  // add by MinChen <chenm001@163.com>  /* decode an B-frame forward & backward inter macroblock */
 // decode an B-frame forward & backward inter macroblock  
1114  void  void
1115  decoder_bf_mbinter(DECODER * dec,  decoder_bf_mbinter(DECODER * dec,
1116                                     const MACROBLOCK * pMB,                                     const MACROBLOCK * pMB,
# Line 1104  Line 1194 
1194          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1195                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1196    
1197                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
1198                  {                  {
1199                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1200    
1201                          start_timer();                          start_timer();
1202                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 1114  Line 1204 
1204    
1205                          start_timer();                          start_timer();
1206                          if (dec->quant_type == 0) {                          if (dec->quant_type == 0) {
1207                                  dequant_inter(&data[i * 64], &block[i * 64], iQuant);                                  dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
1208                          } else {                          } else {
1209                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);                                  dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
1210                          }                          }
1211                          stop_iquant_timer();                          stop_iquant_timer();
1212    
# Line 1147  Line 1237 
1237          stop_transfer_timer();          stop_transfer_timer();
1238  }  }
1239    
1240  // add by MinChen <chenm001@163.com>  /* decode an B-frame direct &  inter macroblock */
 // decode an B-frame direct &  inter macroblock  
1241  void  void
1242  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
1243                                                             IMAGE forward,                                                             IMAGE forward,
# Line 1345  Line 1434 
1434          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1435                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1436    
1437                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
1438                  {                  {
1439                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1440    
1441                          start_timer();                          start_timer();
1442                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 1355  Line 1444 
1444    
1445                          start_timer();                          start_timer();
1446                          if (dec->quant_type == 0) {                          if (dec->quant_type == 0) {
1447                                  dequant_inter(&data[i * 64], &block[i * 64], iQuant);                                  dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
1448                          } else {                          } else {
1449                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);                                  dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
1450                          }                          }
1451                          stop_iquant_timer();                          stop_iquant_timer();
1452    
# Line 1389  Line 1478 
1478  }  }
1479    
1480    
1481  // add by MinChen <chenm001@163.com>  /* for decode B-frame dbquant */
 // for decode B-frame dbquant  
1482  int32_t __inline  int32_t __inline
1483  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1484  {  {
1485          if (!BitstreamGetBit(bs))       // '0'          if (!BitstreamGetBit(bs))      /*  '0' */
1486                  return (0);                  return (0);
1487          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs)) /* '10' */
1488                  return (-2);                  return (-2);
1489          else          else                           /* '11' */
1490                  return (2);                             // '11'                  return (2);
1491  }  }
1492    
1493  // add by MinChen <chenm001@163.com>  /*
1494  // for decode B-frame mb_type   * For decode B-frame mb_type
1495  // bit   ret_value   * bit   ret_value
1496  // 1        0   * 1        0
1497  // 01       1   * 01       1
1498  // 001      2   * 001      2
1499  // 0001     3   * 0001     3
1500     */
1501  int32_t __inline  int32_t __inline
1502  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1503  {  {
# Line 1457  Line 1546 
1546  #endif  #endif
1547    
1548          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1549                  // Initialize Pred Motion Vector                  /* Initialize Pred Motion Vector */
1550                  dec->p_fmv = dec->p_bmv = zeromv;                  dec->p_fmv = dec->p_bmv = zeromv;
1551                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1552                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
# Line 1467  Line 1556 
1556                          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] =
1557                          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;
1558    
1559                          // the last P_VOP is skip macroblock ?                          /*
1560                             * skip if the co-located P_VOP macroblock is not coded
1561                             * if not codec in co-located S_VOP macroblock is _not_
1562                             * automatically skipped
1563                             */
1564    
1565                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1566                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  /* DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */
1567                                  mb->cbp = 0;                                  mb->cbp = 0;
1568  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1569                                  mb->mb_type = MODE_NOT_CODED;                                  mb->mb_type = MODE_NOT_CODED;
# Line 1477  Line 1571 
1571  #endif  #endif
1572                                  mb->mb_type = MODE_FORWARD;                                  mb->mb_type = MODE_FORWARD;
1573                                  mb->quant = last_mb->quant;                                  mb->quant = last_mb->quant;
1574                                  //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                  /*
1575                                  //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                    mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1576                                      mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1577                                    */
1578    
1579                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1580                                  continue;                                  continue;
1581                          }                          }
1582    
1583                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1584                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1585    
1586                                  mb->mb_type = get_mbtype(bs);                                  mb->mb_type = get_mbtype(bs);
1587    
1588                                  if (!modb2) {   // modb=='00'                                  if (!modb2) {   /* modb=='00' */
1589                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1590                                  } else {                                  } else {
1591                                          mb->cbp = 0;                                          mb->cbp = 0;
# Line 1503  Line 1599 
1599                                                  quant = 1;                                                  quant = 1;
1600                                          }                                          }
1601                                  }                                  }
1602    
1603                                    if (dec->interlacing) {
1604                                            if (mb->cbp) {
1605                                                    mb->field_dct = BitstreamGetBit(bs);
1606                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1607                                            }
1608    
1609                                            if (mb->mb_type) {
1610                                                    mb->field_pred = BitstreamGetBit(bs);
1611                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1612    
1613                                                    if (mb->field_pred) {
1614                                                            mb->field_for_top = BitstreamGetBit(bs);
1615                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1616                                                            mb->field_for_bot = BitstreamGetBit(bs);
1617                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1618                                                    }
1619                                            }
1620                                    }
1621    
1622                          } else {                          } else {
1623                                  mb->mb_type = MODE_DIRECT_NONE_MV;                                  mb->mb_type = MODE_DIRECT_NONE_MV;
1624                                  mb->cbp = 0;                                  mb->cbp = 0;
# Line 1510  Line 1626 
1626    
1627                          mb->quant = quant;                          mb->quant = quant;
1628                          mb->mode = MODE_INTER4V;                          mb->mode = MODE_INTER4V;
1629                          //DEBUG1("Switch bm_type=",mb->mb_type);                          /* DEBUG1("Switch bm_type=",mb->mb_type); */
1630    
1631  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1632          BFRAME_DEBUG          BFRAME_DEBUG
# Line 1539  Line 1655 
1655                                                                                    / TRD                                                                                    / TRD
1656                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);
1657                                          }                                          }
1658                                          //DEBUG("B-frame Direct!\n");                                          /* DEBUG("B-frame Direct!\n"); */
1659                                  }                                  }
1660                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1661                                                                                             mb, x, y, bs);                                                                                             mb, x, y, bs);
# Line 1557  Line 1673 
1673    
1674                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1675                                                                                             mb, x, y, bs);                                                                                             mb, x, y, bs);
1676                                  //DEBUG("B-frame Bidir!\n");                                  /* DEBUG("B-frame Bidir!\n"); */
1677                                  break;                                  break;
1678    
1679                          case MODE_BACKWARD:                          case MODE_BACKWARD:
# Line 1567  Line 1683 
1683    
1684                                  mb->mode = MODE_INTER;                                  mb->mode = MODE_INTER;
1685                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1686                                  //DEBUG("B-frame Backward!\n");                                  /* DEBUG("B-frame Backward!\n"); */
1687                                  break;                                  break;
1688    
1689                          case MODE_FORWARD:                          case MODE_FORWARD:
# Line 1577  Line 1693 
1693    
1694                                  mb->mode = MODE_INTER;                                  mb->mode = MODE_INTER;
1695                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1696                                  //DEBUG("B-frame Forward!\n");                                  /* DEBUG("B-frame Forward!\n"); */
1697                                  break;                                  break;
1698    
1699                          default:                          default:
1700                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(XVID_DEBUG_ERROR,"Not support B-frame mb_type = %i\n", mb->mb_type);
1701                          }                          }
1702                    } /* End of for */
                 }                                               // end of FOR  
1703          }          }
1704    
1705  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1706          if (!first){          if (!first){
1707                  first=1;                  first=1;
# Line 1595  Line 1711 
1711  #endif  #endif
1712  }  }
1713    
 // swap two MACROBLOCK array  
 void  
 mb_swap(MACROBLOCK ** mb1,  
                 MACROBLOCK ** mb2)  
 {  
         MACROBLOCK *temp = *mb1;  
   
         *mb1 = *mb2;  
         *mb2 = temp;  
 }  
1714    
1715    
1716  /* perform post processing if necessary, and output the image */  /* perform post processing if necessary, and output the image */
1717  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1718                                          const XVID_DEC_FRAME * frame, int pp_disable)                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)
1719  {  {
1720    
         if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */  
         {  
                 /* note: image is stored to tmp */  
                 image_copy(&dec->tmp, img, dec->edged_width, dec->height);  
                 image_deblock_rrv(&dec->tmp, dec->edged_width,  
                                                 mbs, dec->mb_width, dec->mb_height, dec->mb_width,  
                                                 8, frame->general);  
                 img = &dec->tmp;  
         }  
1721    
1722          image_output(img, dec->width, dec->height,          image_output(img, dec->width, dec->height,
1723                                   dec->edged_width, frame->image, frame->stride,                                   dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1724                                   frame->colorspace, dec->interlacing);                                   frame->output.csp, dec->interlacing);
1725    
1726            if (stats)
1727            {
1728                    stats->type = coding2type(coding_type);
1729                    stats->data.vop.time_base = (int)dec->time_base;
1730                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1731            }
1732  }  }
1733    
1734    
1735  int  int
1736  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1737                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                             xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1738  {  {
1739    
1740          Bitstream bs;          Bitstream bs;
# Line 1640  Line 1744 
1744          uint32_t fcode_forward;          uint32_t fcode_forward;
1745          uint32_t fcode_backward;          uint32_t fcode_backward;
1746          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1747          VECTOR gmc_mv[5];          WARPPOINTS gmc_warp;
1748          uint32_t vop_type;          int coding_type;
1749          int success = 0;          int success, output, seen_something;
1750          int output = 0;  
1751          int seen_something = 0;          if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1752                    return XVID_ERR_VERSION;
1753    
1754          start_global_timer();          start_global_timer();
1755    
1756          dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);          dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1757          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          if ((frame->general & XVID_DISCONTINUITY))
1758                    dec->frames = 0;
1759            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1760    
1761          if (frame->length < 0)  /* decoder flush */          if (frame->length < 0)  /* decoder flush */
1762          {          {
1763            int ret;
1764                  /* if  not decoding "low_delay/packed", and this isn't low_delay and                  /* if  not decoding "low_delay/packed", and this isn't low_delay and
1765                      we have a reference frame, then outout the reference frame */                      we have a reference frame, then outout the reference frame */
1766                  if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)                  if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1767                  {                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1768                          decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);              dec->frames = 0;
1769                          output = 1;              ret = 0;
1770                  }          }else{
1771                if (stats) stats->type = XVID_TYPE_NOTHING;
1772                  frame->length = 0;              ret = XVID_ERR_END;
                 if (stats)  
                 {  
                         stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;  
                         stats->data.vop.time_base = (int)dec->time_base;  
                         stats->data.vop.time_increment = 0;     //XXX: todo  
1773                  }                  }
1774    
1775                  emms();                  emms();
   
1776                  stop_global_timer();                  stop_global_timer();
1777                  return XVID_ERR_OK;                  return ret;
1778          }          }
1779    
1780          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1781    
1782          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1783          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1784          {          {
                 if (stats)  
                         stats->notify = XVID_DEC_VOP;  
                 frame->length = 1;  
1785                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1786                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1787                    if (stats) stats->type = XVID_TYPE_NOTHING;
1788                  emms();                  emms();
1789                  return XVID_ERR_OK;                  return 1;   /* one byte consumed */
1790          }          }
1791    
1792            success = 0;
1793            output = 0;
1794            seen_something = 0;
1795    
1796  repeat:  repeat:
1797    
1798          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,          coding_type =   BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1799                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1800    
1801          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%i,  time_pp=%i,  time_bp=%i",          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",
1802                                                          vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);                                                          coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1803    
1804          if (vop_type == - 1)          if (coding_type == -1) /* nothing */
1805          {          {
1806                  if (success) goto done;                  if (success) goto done;
1807                  return XVID_ERR_FAIL;          if (stats) stats->type = XVID_TYPE_NOTHING;
1808                    emms();
1809            return BitstreamPos(&bs)/8;
1810          }          }
1811    
1812          if (vop_type == -2 || vop_type == -3)          if (coding_type == -2 || coding_type == -3)   /* vol and/or resize */
1813          {          {
1814                  if (vop_type == -3)                  if (coding_type == -3)
1815                          decoder_resize(dec);                          decoder_resize(dec);
1816    
1817                  if (stats)                  if (stats)
1818                  {                  {
1819                          stats->notify = XVID_DEC_VOL;                          stats->type = XVID_TYPE_VOL;
1820                          stats->data.vol.general = 0;                          stats->data.vol.general = 0;
1821                          if (dec->interlacing)                          /*XXX: if (dec->interlacing)
1822                                  stats->data.vol.general |= XVID_INTERLACING;                                  stats->data.vol.general |= ++INTERLACING; */
1823                          stats->data.vol.width = dec->width;                          stats->data.vol.width = dec->width;
1824                          stats->data.vol.height = dec->height;                          stats->data.vol.height = dec->height;
1825                          stats->data.vol.aspect_ratio = dec->aspect_ratio;                          stats->data.vol.par = dec->aspect_ratio;
1826                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1827                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
1828                          frame->length = BitstreamPos(&bs) / 8;                          emms();
1829                          return XVID_ERR_OK;                          return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1830                  }                  }
1831                  goto repeat;                  goto repeat;
1832          }          }
1833    
1834          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 */
   
1835    
1836          /* packed_mode: special-N_VOP treament */          /* packed_mode: special-N_VOP treament */
1837          if (dec->packed_mode && vop_type == N_VOP)          if (dec->packed_mode && coding_type == N_VOP)
1838          {          {
1839                  if (dec->low_delay_default && dec->frames > 0)                  if (dec->low_delay_default && dec->frames > 0)
1840                  {                  {
1841                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1842                          output = 1;                          output = 1;
1843                  }                  }
1844                  /* ignore otherwise */                  /* ignore otherwise */
1845          }          }
1846          else if (vop_type != B_VOP)          else if (coding_type != B_VOP)
1847          {          {
1848                  switch(vop_type)                  switch(coding_type)
1849                  {                  {
1850                  case I_VOP :                  case I_VOP :
1851                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
# Line 1751  Line 1856 
1856                          break;                          break;
1857                  case S_VOP :                  case S_VOP :
1858                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1859                                                  fcode_forward, intra_dc_threshold, gmc_mv);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1860                          break;                          break;
1861                  case N_VOP :                  case N_VOP :
1862                            /* XXX: not_coded vops are not used for forward prediction */
1863                            /* we should not swap(last_mbs,mbs) */
1864                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1865                          break;                          break;
1866                  }                  }
# Line 1762  Line 1869 
1869                  {                  {
1870                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1871                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1872                                  16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);                                  16, 0);
1873                  }                  }
1874    
1875                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
# Line 1770  Line 1877 
1877                  {                  {
1878                          if (dec->low_delay)                          if (dec->low_delay)
1879                          {                          {
1880                                  decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1881                                  output = 1;                                  output = 1;
1882                          }                          }
1883                          else if (dec->frames > 0)       /* is the reference frame valid? */                          else if (dec->frames > 0)       /* is the reference frame valid? */
1884                          {                          {
1885                                  /* output the reference frame */                                  /* output the reference frame */
1886                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1887                                  output = 1;                                  output = 1;
1888                          }                          }
1889                  }                  }
1890    
1891                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1892                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1893                  mb_swap(&dec->mbs, &dec->last_mbs);          SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1894                  dec->last_reduced_resolution = reduced_resolution;                  dec->last_reduced_resolution = reduced_resolution;
1895            dec->last_coding_type = coding_type;
1896    
1897                  dec->frames++;                  dec->frames++;
1898                  seen_something = 1;                  seen_something = 1;
# Line 1793  Line 1901 
1901    
1902                  if (dec->low_delay)                  if (dec->low_delay)
1903                  {                  {
1904                          DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1905                          dec->low_delay = 1;                          dec->low_delay = 1;
1906                  }                  }
1907    
# Line 1811  Line 1919 
1919                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1920                  }                  }
1921    
1922                  decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1923                  output = 1;                  output = 1;
1924                  dec->frames++;                  dec->frames++;
1925          }          }
# Line 1834  Line 1942 
1942                  if (dec->packed_mode && seen_something)                  if (dec->packed_mode && seen_something)
1943                  {                  {
1944                          /* output the recently decoded frame */                          /* output the recently decoded frame */
1945                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1946                  }                  }
1947                  else                  else
1948                  {                  {
# Line 1844  Line 1952 
1952                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1953                                  "bframe decoder lag");                                  "bframe decoder lag");
1954    
1955                          decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);
1956                  }                          if (stats) stats->type = XVID_TYPE_NOTHING;
                 output = 1;  
         }  
   
         frame->length = BitstreamPos(&bs) / 8;  
1957    
1958          if (stats)                  }
         {  
                 stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;  
                 stats->data.vop.time_base = (int)dec->time_base;  
                 stats->data.vop.time_increment = 0;     //XXX: todo  
1959          }          }
1960    
1961          emms();          emms();
   
1962          stop_global_timer();          stop_global_timer();
1963    
1964          return XVID_ERR_OK;          return BitstreamPos(&bs) / 8;   /* number of bytes consumed */
1965  }  }

Legend:
Removed from v.1.37.2.21  
changed lines
  Added in v.1.49.2.15

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