[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.30, Sun Jan 12 13:11:50 2003 UTC revision 1.49.2.17, Thu Oct 16 12:16:00 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>  #include <stdio.h>
28  #include <stdlib.h>  #include <stdlib.h>
# Line 75  Line 40 
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 89  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"
60  #include "utils/mem_align.h"  #include "utils/mem_align.h"
61    
62  int  static int
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);
# Line 113  Line 77 
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 131  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 180  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 198  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 233  Line 197 
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 245  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 255  Line 218 
218          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
219          xvid_free(dec->mbs);          xvid_free(dec->mbs);
220    
221          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          /* image based GMC */          /* 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);
# Line 265  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  static const int32_t dquant_table[4] = {  static const int32_t dquant_table[4] = {
236          -1, -2, 1, 2          -1, -2, 1, 2
237  };  };
238    
239    /* decode an intra macroblock */
240    static void
   
 // decode an intra macroblock  
   
 void  
241  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
242                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
243                                  const uint32_t x_pos,                                  const uint32_t x_pos,
# Line 313  Line 271 
271                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
272          }          }
273    
274          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
275    
276          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
277                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
# Line 336  Line 294 
294                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;
295    
296                          if (dc_size > 8) {                          if (dc_size > 8) {
297                                  BitstreamSkip(bs, 1);   // marker                                  BitstreamSkip(bs, 1);   /* marker */
298                          }                          }
299    
300                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
301                          start_coeff = 1;                          start_coeff = 1;
302    
303                          DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);
304                  } else {                  } else {
305                          start_coeff = 0;                          start_coeff = 0;
306                  }                  }
307    
308                  start_timer();                  start_timer();
309                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
310                  {                  {
311                          int direction = dec->alternate_vertical_scan ?                          int direction = dec->alternate_vertical_scan ?
312                                  2 : pMB->acpred_directions[i];                                  2 : pMB->acpred_directions[i];
# Line 363  Line 321 
321    
322                  start_timer();                  start_timer();
323                  if (dec->quant_type == 0) {                  if (dec->quant_type == 0) {
324                          dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
325                  } else {                  } else {
326                          dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
327                  }                  }
328                  stop_iquant_timer();                  stop_iquant_timer();
329    
# Line 402  Line 360 
360          stop_transfer_timer();          stop_transfer_timer();
361  }  }
362    
363    static void
364    decoder_mb_decode(DECODER * dec,
365                                    const uint32_t cbp,
366                                    Bitstream * bs,
367                                    uint8_t * pY_Cur,
368                                    uint8_t * pU_Cur,
369                                    uint8_t * pV_Cur,
370                                    const int reduced_resolution,
371                                    const MACROBLOCK * pMB)
372    {
373            DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE);
374            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
375    
376            int stride = dec->edged_width, next_block = stride * (reduced_resolution ? 16 : 8);
377            const int stride2 = stride/2;
378            int i;
379            const uint32_t iQuant = pMB->quant;
380            const int direction = dec->alternate_vertical_scan ? 2 : 0;
381            const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter;
382    
383            for (i = 0; i < 6; i++) {
384    
385                    if (cbp & (1 << (5 - i))) {     /* coded */
386    
387                            memset(block, 0, 64 * sizeof(int16_t)); /* clear */
388    
389                            start_timer();
390                            get_inter_block(bs, block, direction);
391                            stop_coding_timer();
392    
393                            start_timer();
394                            dequant(&data[i * 64], block, iQuant);
395                            stop_iquant_timer();
396    
397                            start_timer();
398                            idct(&data[i * 64]);
399                            stop_idct_timer();
400                    }
401            }
402    
403            if (dec->interlacing && pMB->field_dct) {
404                    next_block = stride;
405                    stride *= 2;
406            }
407    
408  // decode an inter macroblock          start_timer();
409            if (reduced_resolution) {
410                    if (cbp & 32)
411                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
412                    if (cbp & 16)
413                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
414                    if (cbp & 8)
415                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
416                    if (cbp & 4)
417                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
418                    if (cbp & 2)
419                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
420                    if (cbp & 1)
421                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
422            } else {
423                    if (cbp & 32)
424                            transfer_16to8add(pY_Cur, &data[0 * 64], stride);
425                    if (cbp & 16)
426                            transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
427                    if (cbp & 8)
428                            transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
429                    if (cbp & 4)
430                            transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
431                    if (cbp & 2)
432                            transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
433                    if (cbp & 1)
434                            transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
435            }
436            stop_transfer_timer();
437    }
438    
439  void  /* decode an inter macroblock */
440    static void
441  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
442                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
443                                  const uint32_t x_pos,                                  const uint32_t x_pos,
444                                  const uint32_t y_pos,                                  const uint32_t y_pos,
                                 const uint32_t fcode,  
445                                  const uint32_t cbp,                                  const uint32_t cbp,
446                                  Bitstream * bs,                                  Bitstream * bs,
                                 const uint32_t quant,  
447                                  const uint32_t rounding,                                  const uint32_t rounding,
448                                  const int reduced_resolution)                                  const int reduced_resolution,
449                                    const int ref)
450  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
451          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
452          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
453          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
454          uint32_t i;          uint32_t i;
455          uint32_t iQuant = pMB->quant;  
456          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
457    
458          int uv_dx, uv_dy;          int uv_dx, uv_dy;
# Line 449  Line 474 
474                          mv[i] = pMB->mvs[i];                          mv[i] = pMB->mvs[i];
475          }          }
476    
477          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          start_timer();
478    
479                  uv_dx = mv[0].x / (1 + dec->quarterpel);          if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */
                 uv_dy = mv[0].y / (1 + dec->quarterpel);  
480    
481                    uv_dx = mv[0].x;
482                    uv_dy = mv[0].y;
483                    if (dec->quarterpel) {
484                            uv_dx /= 2;
485                            uv_dy /= 2;
486                    }
487                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
488                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
489    
                 start_timer();  
490                  if (reduced_resolution)                  if (reduced_resolution)
                 {  
491                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
492                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                    mv[0].x, mv[0].y, stride,  rounding);
493                          interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,                  else if (dec->quarterpel)
494                                                                    uv_dx, uv_dy, stride2, rounding);                          interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
                         interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
   
                 }  
                 else  
                 {  
                         if(dec->quarterpel) {  
                                 interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,  
495                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
496                                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                                          mv[0].x, mv[0].y, stride, rounding);
497                          }                  else
498                          else {                          interpolate16x16_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
                                 interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,  
499                                                                            mv[0].x, mv[0].y, stride, rounding);                                                                            mv[0].x, mv[0].y, stride, rounding);
                         }  
   
                         interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
                         interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
                 }  
                 stop_comp_timer();  
500    
501          } else {        /* MODE_INTER4V */          } else {        /* MODE_INTER4V */
                 int sum;  
   
                 if(dec->quarterpel)  
                         sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);  
                 else  
                         sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;  
   
                 uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
502    
503                  if(dec->quarterpel)                  if(dec->quarterpel) {
504                          sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);                          uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
505                  else                          uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
506                          sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;                  } else {
507                            uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
508                            uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
509                    }
510    
511                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
512                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
513    
514                  start_timer();                  if (reduced_resolution) {
                 if (reduced_resolution)  
                 {  
515                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
516                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                    mv[0].x, mv[0].y, stride,  rounding);
517                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
# Line 520  Line 525 
525                          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,
526                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
527    
528                          // set_block(pY_Cur, stride, 32, 32, 127);                  } else if (dec->quarterpel) {
                 }  
                 else  
                 {  
                         if(dec->quarterpel) {  
529                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
530                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
531                                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                                    mv[0].x, mv[0].y, stride,  rounding);
# Line 537  Line 538 
538                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
539                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
540                                                                                    mv[3].x, mv[3].y, stride,  rounding);                                                                                    mv[3].x, mv[3].y, stride,  rounding);
541                          }                  } else {
                         else {  
542                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos,
543                                                                            mv[0].x, mv[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
544                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,
# Line 548  Line 548 
548                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,
549                                                                            mv[3].x, mv[3].y, stride,  rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
550                          }                          }
551            }
552    
553                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u , 8 * x_pos, 8 * y_pos,          /* chroma */
554            if (reduced_resolution) {
555                    interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
556                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
557                          interpolate8x8_switch(dec->cur.v, dec->refn[0].v , 8 * x_pos, 8 * y_pos,                  interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
558                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
                 }  
                 stop_comp_timer();  
         }  
   
         for (i = 0; i < 6; i++) {  
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
                 if (cbp & (1 << (5 - i)))       // coded  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
559                          } else {                          } else {
560                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);                  interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
561                          }                                                                  uv_dx, uv_dy, stride2, rounding);
562                          stop_iquant_timer();                  interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
563                                                                    uv_dx, uv_dy, stride2, rounding);
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
   
         start_timer();  
         if (reduced_resolution)  
         {  
                 if (cbp & 32)  
                         add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);  
                 if (cbp & 16)  
                         add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);  
                 if (cbp & 8)  
                         add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);  
                 if (cbp & 4)  
                         add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);  
                 if (cbp & 2)  
                         add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);  
                 if (cbp & 1)  
                         add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);  
         }  
         else  
         {  
                 if (cbp & 32)  
                         transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
                 if (cbp & 16)  
                         transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
                 if (cbp & 8)  
                         transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
                 if (cbp & 4)  
                         transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
                 if (cbp & 2)  
                         transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
                 if (cbp & 1)  
                         transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         }  
         stop_transfer_timer();  
564  }  }
565    
566  static __inline int gmc_sanitize(int value, int quarterpel, int fcode)          stop_comp_timer();
 {  
         int length = 1 << (fcode+4);  
   
 //      if (quarterpel) value *= 2;  
567    
568          if (value < -length)          if (cbp)
569                  return -length;                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur,
570          else if (value >= length)                                                          reduced_resolution, pMB);
                 return length-1;  
         else return value;  
571  }  }
572    
   
573  static void  static void
574  decoder_mbgmc(DECODER * dec,  decoder_mbgmc(DECODER * dec,
575                                  MACROBLOCK * const pMB,                                  MACROBLOCK * const pMB,
# Line 643  Line 578 
578                                  const uint32_t fcode,                                  const uint32_t fcode,
579                                  const uint32_t cbp,                                  const uint32_t cbp,
580                                  Bitstream * bs,                                  Bitstream * bs,
581                                  const uint32_t quant,                                  const uint32_t rounding)
                                 const uint32_t rounding,  
                                 const int reduced_resolution)   /* no reduced res support */  
582  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
583          const uint32_t stride = dec->edged_width;          const uint32_t stride = dec->edged_width;
584          const uint32_t stride2 = stride / 2;          const uint32_t stride2 = stride / 2;
585          const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);  
         uint32_t i;  
         const uint32_t iQuant = pMB->quant;  
586          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
587          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
588          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
589    
590            NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
591    
592          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
593    
594          start_timer();          start_timer();
595    
596  /* this is where the calculations are done */  /* this is where the calculations are done */
597    
598          {          gmc_data->predict_16x16(gmc_data,
599                  pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,                          dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
600                                          stride, stride2, dec->quarterpel, rounding, &dec->cur);                          stride, stride, x_pos, y_pos, rounding);
601    
602            gmc_data->predict_8x8(gmc_data,
603                            dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,
604                            dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,
605                            stride2, stride2, x_pos, y_pos, rounding);
606    
607            gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);
608    
609                  pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);                  pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
610                  pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);                  pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
         }  
         pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;  
   
 /*      transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);  
         transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);  
         transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);  
 */  
611    
612            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
613    
614          stop_transfer_timer();          stop_transfer_timer();
615    
616          if (!cbp) return;          if (cbp)
617                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
         for (i = 0; i < 6; i++) {  
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
                 if (cbp & (1 << (5 - i)))       // coded  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
                         }  
                         stop_iquant_timer();  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
618    
 /* interlace + GMC is this possible ??? */  
 /*      if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
 */  
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         stop_transfer_timer();  
619  }  }
620    
621    
622  void  static void
623  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
624                             Bitstream * bs,                             Bitstream * bs,
625                             int reduced_resolution,                             int reduced_resolution,
# Line 745  Line 631 
631          uint32_t mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
632          uint32_t mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
633    
634          if (reduced_resolution)          if (reduced_resolution) {
         {  
635                  mb_width = (dec->width + 31) / 32;                  mb_width = (dec->width + 31) / 32;
636                  mb_height = (dec->height + 31) / 32;                  mb_height = (dec->height + 31) / 32;
637          }          }
# Line 774  Line 659 
659                          }                          }
660                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
661    
662                          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));
663    
664                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
665                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
# Line 801  Line 686 
686    
687                          if (dec->interlacing) {                          if (dec->interlacing) {
688                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
689                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
690                          }                          }
691    
692                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
# Line 815  Line 700 
700  }  }
701    
702    
703  void  static void
704  get_motion_vector(DECODER * dec,  get_motion_vector(DECODER * dec,
705                                    Bitstream * bs,                                    Bitstream * bs,
706                                    int x,                                    int x,
# Line 826  Line 711 
711                                    const int bound)                                    const int bound)
712  {  {
713    
714          int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
715          int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
716          int low = ((-32) * scale_fac);          const int low = ((-32) * scale_fac);
717          int range = (64 * scale_fac);          const int range = (64 * scale_fac);
718    
719          VECTOR pmv;          const VECTOR pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
720          VECTOR mv;          VECTOR mv;
721    
         pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);  
   
722          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
723          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
724    
725          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);          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);
726    
727          mv.x += pmv.x;          mv.x += pmv.x;
728          mv.y += pmv.y;          mv.y += pmv.y;
# Line 860  Line 743 
743          ret_mv->y = mv.y;          ret_mv->y = mv.y;
744  }  }
745    
   
   
   
   
746  /* for P_VOP set gmc_warp to NULL */  /* for P_VOP set gmc_warp to NULL */
747  void  static void
748  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
749                             Bitstream * bs,                             Bitstream * bs,
750                             int rounding,                             int rounding,
# Line 875  Line 754 
754                             int intra_dc_threshold,                             int intra_dc_threshold,
755                             const WARPPOINTS *const gmc_warp)                             const WARPPOINTS *const gmc_warp)
756  {  {
   
757          uint32_t x, y;          uint32_t x, y;
758          uint32_t bound;          uint32_t bound;
759          int cp_mb, st_mb;          int cp_mb, st_mb;
760          uint32_t mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
761          uint32_t mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
762    
763          if (reduced_resolution)          if (reduced_resolution) {
         {  
764                  mb_width = (dec->width + 31) / 32;                  mb_width = (dec->width + 31) / 32;
765                  mb_height = (dec->height + 31) / 32;                  mb_height = (dec->height + 31) / 32;
766          }          }
# Line 893  Line 770 
770                                     dec->width, dec->height);                                     dec->width, dec->height);
771          stop_edges_timer();          stop_edges_timer();
772    
773          if (gmc_warp)          if (gmc_warp) {
774          {                  /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
   
                 // accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16  
                 if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )  
                 {  
                         fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",  
                                 dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),  
                                 dec->sprite_warping_points);  
                 }  
   
775                  generate_GMCparameters( dec->sprite_warping_points,                  generate_GMCparameters( dec->sprite_warping_points,
776                                  (2 << dec->sprite_warping_accuracy), gmc_warp,                                  dec->sprite_warping_accuracy, gmc_warp,
777                                  dec->width, dec->height, &dec->gmc_data);                                  dec->width, dec->height, &dec->new_gmc_data);
778    
779  /* image warping is done block-based  in decoder_mbgmc(), now */  /* image warping is done block-based  in decoder_mbgmc(), now */
 /*  
         generate_GMCimage(&dec->gmc_data, &dec->refn[0],  
                                         mb_width, mb_height,  
                                         dec->edged_width, dec->edged_width/2,  
                                         fcode, dec->quarterpel, 0,  
                                         rounding, dec->mbs, &dec->gmc);  
 */  
780          }          }
781    
782          bound = 0;          bound = 0;
# Line 925  Line 786 
786                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
787                          MACROBLOCK *mb;                          MACROBLOCK *mb;
788    
789                          // skip stuffing                          /* skip stuffing */
790                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
791                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
792    
793                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1)) {
                         {  
794                                  bound = read_video_packet_header(bs, dec, fcode - 1,                                  bound = read_video_packet_header(bs, dec, fcode - 1,
795                                          &quant, &fcode, NULL, &intra_dc_threshold);                                          &quant, &fcode, NULL, &intra_dc_threshold);
796                                  x = bound % mb_width;                                  x = bound % mb_width;
# Line 938  Line 798 
798                          }                          }
799                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
800    
801                          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));
802    
803                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded                          if (!(BitstreamGetBit(bs)))     { /* block _is_ coded */
804                          if (!(BitstreamGetBit(bs)))     // block _is_ coded                                  uint32_t mcbpc, cbpc, cbpy, cbp;
805                          {                                  uint32_t intra, acpred_flag = 0;
806                                  uint32_t mcbpc;                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
                                 uint32_t cbpc;  
                                 uint32_t acpred_flag;  
                                 uint32_t cbpy;  
                                 uint32_t cbp;  
                                 uint32_t intra;  
                                 int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC  
807    
808                                  cp_mb++;                                  cp_mb++;
809                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
810                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
811                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
812    
813                                  DPRINTF(DPRINTF_MB, "mode %i", mb->mode);                                  DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
814                                  DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);                                  DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
                                 acpred_flag = 0;  
815    
816                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
817    
                                 if (intra) {  
                                         acpred_flag = BitstreamGetBit(bs);  
                                 }  
   
818                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
                                 {  
819                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
820                                  }                                  else if (intra)
821                                            acpred_flag = BitstreamGetBit(bs);
822    
823                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
824                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);                                  DPRINTF(XVID_DEBUG_MB, "cbpy %i mcsel %i \n", cbpy,mcsel);
825    
826                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
827    
828                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
829                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
830                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
831                                          quant += dquant;                                          quant += dquant;
832                                          if (quant > 31) {                                          if (quant > 31) {
833                                                  quant = 31;                                                  quant = 31;
834                                          } else if (quant < 1) {                                          } else if (quant < 1) {
835                                                  quant = 1;                                                  quant = 1;
836                                          }                                          }
837                                          DPRINTF(DPRINTF_MB, "quant %i", quant);                                          DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
838                                  }                                  }
839                                  mb->quant = quant;                                  mb->quant = quant;
840    
841                                  if (dec->interlacing) {                                  if (dec->interlacing) {
842                                          if (cbp || intra) {                                          if ((cbp || intra) && !mcsel) {
843                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
844                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
845                                          }                                          }
846    
847                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
848                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
849                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
850    
851                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
852                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
853                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
854                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
855                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
856                                                  }                                                  }
857                                          }                                          }
858                                  }                                  }
859    
860                                  if (mcsel) {                                  if (mcsel) {
861                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, rounding);
                                                                 rounding, reduced_resolution);  
862                                          continue;                                          continue;
863    
864                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
865    
866                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
867                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
868                                                                                    fcode, bound);                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode, bound);
                                                 get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],  
                                                                                   fcode, bound);  
869                                          } else {                                          } else {
870                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
                                                                                   fcode, bound);  
871                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
872                                          }                                          }
873                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
   
874                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
875                                          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);
876                                          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);
877                                          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);
878                                  } else                  // MODE_INTRA, MODE_INTRA_Q                                  } else {                /* MODE_INTRA, MODE_INTRA_Q */
879                                  {                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
880                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =     0;
                                                 0;  
                                         mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                                 0;  
881                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
882                                                                          intra_dc_threshold, bound, reduced_resolution);                                                                          intra_dc_threshold, bound, reduced_resolution);
883                                          continue;                                          continue;
884                                  }                                  }
885    
886                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, cbp, bs,
887                                                                  rounding, reduced_resolution);                                                                  rounding, reduced_resolution, 0);
888    
889                          }                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
                         else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */  
                         {  
890                                  mb->mode = MODE_NOT_CODED_GMC;                                  mb->mode = MODE_NOT_CODED_GMC;
891                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
                                 start_timer();  
   
                                 decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,  
                                                                 rounding, reduced_resolution);  
   
                                 stop_transfer_timer();  
892    
893                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
894                                    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);
895                                    cp_mb = 0;                                    cp_mb = 0;
896                                  }                                  }
897                                  st_mb = x+1;                                  st_mb = x+1;
898                          }                          } else {        /* not coded P_VOP macroblock */
                         else    /* not coded P_VOP macroblock */  
                         {  
899                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
900    
901                                  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;
902                                  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;
                                 // copy macroblock directly from ref to cur  
   
                                 start_timer();  
903    
904                                  if (reduced_resolution)                                  decoder_mbinter(dec, mb, x, y, 0, bs,
905                                  {                                                                  rounding, reduced_resolution, 0);
                                         transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),  
                                                                          dec->refn[0].y + (32*y)*dec->edged_width + (32*x),  
                                                                          dec->edged_width);  
   
                                         transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),  
                                                                         dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),  
                                                                         dec->edged_width/2);  
   
                                         transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),  
                                                                          dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),  
                                                                          dec->edged_width/2);  
                                 }  
                                 else  
                                 {  
                                         transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),  
                                                                          dec->refn[0].y + (16*y)*dec->edged_width + (16*x),  
                                                                          dec->edged_width);  
   
                                         transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),  
                                                                         dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),  
                                                                         dec->edged_width/2);  
   
                                         transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),  
                                                                          dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),  
                                                                          dec->edged_width/2);  
                                 }  
   
                                 stop_transfer_timer();  
906    
907                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
908                                    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);
# Line 1111  Line 911 
911                                  st_mb = x+1;                                  st_mb = x+1;
912                          }                          }
913                  }                  }
914    
915                  if(dec->out_frm && cp_mb > 0)                  if(dec->out_frm && cp_mb > 0)
916                    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);
917          }          }
918  }  }
919    
920    
921  // add by MinChen <chenm001@163.com>  /* decode B-frame motion vector */
922  // decode B-frame motion vector  static void
923  void  get_b_motion_vector(Bitstream * bs,
 get_b_motion_vector(DECODER * dec,  
                                         Bitstream * bs,  
                                         int x,  
                                         int y,  
924                                          VECTOR * mv,                                          VECTOR * mv,
925                                          int fcode,                                          int fcode,
926                                          const VECTOR pmv)                                          const VECTOR pmv)
927  {  {
928          int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
929          int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
930          int low = ((-32) * scale_fac);          const int low = ((-32) * scale_fac);
931          int range = (64 * scale_fac);          const int range = (64 * scale_fac);
   
         int mv_x, mv_y;  
         int pmv_x, pmv_y;  
932    
933          pmv_x = pmv.x;          int mv_x = get_mv(bs, fcode);
934          pmv_y = pmv.y;          int mv_y = get_mv(bs, fcode);
935    
936          mv_x = get_mv(bs, fcode);          mv_x += pmv.x;
937          mv_y = get_mv(bs, fcode);          mv_y += pmv.y;
938    
939          mv_x += pmv_x;          if (mv_x < low)
         mv_y += pmv_y;  
   
         if (mv_x < low) {  
940                  mv_x += range;                  mv_x += range;
941          } else if (mv_x > high) {          else if (mv_x > high)
942                  mv_x -= range;                  mv_x -= range;
         }  
943    
944          if (mv_y < low) {          if (mv_y < low)
945                  mv_y += range;                  mv_y += range;
946          } else if (mv_y > high) {          else if (mv_y > high)
947                  mv_y -= range;                  mv_y -= range;
         }  
948    
949          mv->x = mv_x;          mv->x = mv_x;
950          mv->y = mv_y;          mv->y = mv_y;
951  }  }
952    
953    /* decode an B-frame direct & interpolate macroblock */
954  // add by MinChen <chenm001@163.com>  static void
 // decode an B-frame forward & backward inter macroblock  
 void  
 decoder_bf_mbinter(DECODER * dec,  
                                    const MACROBLOCK * pMB,  
                                    const uint32_t x_pos,  
                                    const uint32_t y_pos,  
                                    const uint32_t cbp,  
                                    Bitstream * bs,  
                                    const uint32_t quant,  
                                    const uint8_t ref)  
 {  
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
         uint32_t stride = dec->edged_width;  
         uint32_t stride2 = stride / 2;  
         uint32_t next_block = stride * 8;  
         uint32_t i;  
         uint32_t iQuant = pMB->quant;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
         int uv_dx, uv_dy;  
   
         pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);  
         pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);  
         pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);  
   
   
         if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {  
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
   
                 if (dec->quarterpel)  
                 {  
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
   
                 uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];  
                 uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];  
         } else {  
                 int sum;  
   
                 if(dec->quarterpel)  
                         sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);  
                 else  
                         sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
   
                 uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
   
                 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  
                         sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
   
                 uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];  
         }  
   
         start_timer();  
         if(dec->quarterpel) {  
                 interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                     dec->qtmp.y + 128, 16*x_pos, 16*y_pos,  
                                                                     pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);  
         }  
         else {  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,  
                                                           pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,  
                                                       pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,  
                                                           pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,  
                                                           pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);  
         }  
   
         interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,  
                                                   uv_dx, uv_dy, stride2, 0);  
         interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,  
                                                   uv_dx, uv_dy, stride2, 0);  
         stop_comp_timer();  
   
         for (i = 0; i < 6; i++) {  
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
                 if (cbp & (1 << (5 - i)))       // coded  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
                         }  
                         stop_iquant_timer();  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
   
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         stop_transfer_timer();  
 }  
   
 // add by MinChen <chenm001@163.com>  
 // decode an B-frame direct &  inter macroblock  
 void  
955  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
956                                                             IMAGE forward,                                                             IMAGE forward,
957                                                             IMAGE backward,                                                             IMAGE backward,
958                                                             const MACROBLOCK * pMB,                                                             const MACROBLOCK * pMB,
959                                                             const uint32_t x_pos,                                                             const uint32_t x_pos,
960                                                             const uint32_t y_pos,                                                             const uint32_t y_pos,
961                                                             Bitstream * bs)                                                                  Bitstream * bs,
962                                                                    const int direct)
963  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
964          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
965          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
966          uint32_t next_block = stride * 8;          uint32_t next_block = stride * 8;
         uint32_t iQuant = pMB->quant;  
967          int uv_dx, uv_dy;          int uv_dx, uv_dy;
968          int b_uv_dx, b_uv_dy;          int b_uv_dx, b_uv_dy;
         uint32_t i;  
969          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
970      const uint32_t cbp = pMB->cbp;      const uint32_t cbp = pMB->cbp;
971    
# Line 1319  Line 973 
973          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
974          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
975    
976            if (!direct) {
         if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {  
977                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
978                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
979    
980                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
981                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
982    
983                  if (dec->quarterpel)                  if (dec->quarterpel) {
                 {  
984                          uv_dx /= 2;                          uv_dx /= 2;
985                          uv_dy /= 2;                          uv_dy /= 2;
   
986                          b_uv_dx /= 2;                          b_uv_dx /= 2;
987                          b_uv_dy /= 2;                          b_uv_dy /= 2;
988                  }                  }
# Line 1341  Line 992 
992    
993                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
994                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
         } else {  
                 int sum;  
   
                 if(dec->quarterpel)  
                         sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);  
                 else  
                         sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
   
                 uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
   
                 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  
                         sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
   
                 uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];  
   
995    
996                  if(dec->quarterpel)          } else {
997                          sum = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);                  if(dec->quarterpel) {
998                  else                          uv_dx = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
999                          sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;                          uv_dy = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1000                            b_uv_dx = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);
1001                  b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];                          b_uv_dy = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);
1002                    } else {
1003                  if(dec->quarterpel)                          uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1004                          sum = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);                          uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1005                  else                          b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1006                          sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;                          b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
   
                 b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];  
1007          }          }
1008    
1009                    uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
1010                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
1011                    b_uv_dx = (b_uv_dx >> 3) + roundtab_76[b_uv_dx & 0xf];
1012                    b_uv_dy = (b_uv_dy >> 3) + roundtab_76[b_uv_dy & 0xf];
1013            }
1014    
1015          start_timer();          start_timer();
1016          if(dec->quarterpel) {          if(dec->quarterpel) {
1017                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if(!direct) {
1018                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1019                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1020                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1021                  else {                  } else {
1022                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1023                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1024                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
# Line 1395  Line 1032 
1032                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1033                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1034                  }                  }
1035          }          } else {
         else {  
1036                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1037                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1038                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
# Line 1404  Line 1040 
1040                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1041                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1042                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1043                                                            16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,                                                          16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
                                                           0);  
1044          }          }
1045    
1046          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
# Line 1415  Line 1050 
1050    
1051    
1052          if(dec->quarterpel) {          if(dec->quarterpel) {
1053                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if(!direct) {
1054                          interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1055                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1056                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1057                  else {                  } else {
1058                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1059                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1060                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
# Line 1433  Line 1068 
1068                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1069                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1070                  }                  }
1071          }          } else {
         else {  
1072                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1073                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1074                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1075                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,                                                          16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
                                                           0);  
1076                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1077                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,                                                          16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
                                                           stride, 0);  
1078                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1079                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,                                                          16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
                                                           stride, 0);  
1080          }          }
1081    
1082          interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
# Line 1485  Line 1116 
1116    
1117          stop_comp_timer();          stop_comp_timer();
1118    
1119          for (i = 0; i < 6; i++) {          if (cbp)
1120                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
   
                 if (cbp & (1 << (5 - i)))       // coded  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
1121                          }                          }
                         stop_iquant_timer();  
1122    
1123                          start_timer();  /* for decode B-frame dbquant */
1124                          idct(&data[i * 64]);  static __inline int32_t
                         stop_idct_timer();  
                 }  
         }  
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
   
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         stop_transfer_timer();  
 }  
   
   
 // add by MinChen <chenm001@163.com>  
 // for decode B-frame dbquant  
 int32_t __inline  
1125  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1126  {  {
1127          if (!BitstreamGetBit(bs))       // '0'          if (!BitstreamGetBit(bs))               /*  '0' */
1128                  return (0);                  return (0);
1129          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs))  /* '10' */
1130                  return (-2);                  return (-2);
1131          else          else                                                    /* '11' */
1132                  return (2);                             // '11'                  return (2);
1133  }  }
1134    
1135  // add by MinChen <chenm001@163.com>  /*
1136  // for decode B-frame mb_type   * decode B-frame mb_type
1137  // bit   ret_value   * bit          ret_value
1138  // 1        0   * 1            0
1139  // 01       1   * 01           1
1140  // 001      2   * 001          2
1141  // 0001     3   * 0001         3
1142  int32_t __inline   */
1143    static int32_t __inline
1144  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1145  {  {
1146          int32_t mb_type;          int32_t mb_type;
1147    
1148          for (mb_type = 0; mb_type <= 3; mb_type++) {          for (mb_type = 0; mb_type <= 3; mb_type++)
1149                  if (BitstreamGetBit(bs))                  if (BitstreamGetBit(bs))
                         break;  
         }  
   
         if (mb_type <= 3)  
1150                  return (mb_type);                  return (mb_type);
1151          else  
1152                  return (-1);          return -1;
1153  }  }
1154    
1155  void  static void
1156  decoder_bframe(DECODER * dec,  decoder_bframe(DECODER * dec,
1157                             Bitstream * bs,                             Bitstream * bs,
1158                             int quant,                             int quant,
# Line 1578  Line 1162 
1162          uint32_t x, y;          uint32_t x, y;
1163          VECTOR mv;          VECTOR mv;
1164          const VECTOR zeromv = {0,0};          const VECTOR zeromv = {0,0};
1165            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1166            int i;
1167    
1168  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1169          FILE *fp;          FILE *fp;
1170          static char first=0;          static char first=0;
1171  #define BFRAME_DEBUG    if (!first && fp){ \  #define BFRAME_DEBUG
1172                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \          if (!first && fp) { \
1173                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1174          }          }
1175  #endif  #endif
1176    
# Line 1600  Line 1188 
1188  #endif  #endif
1189    
1190          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1191                  // Initialize Pred Motion Vector                  /* Initialize Pred Motion Vector */
1192                  dec->p_fmv = dec->p_bmv = zeromv;                  dec->p_fmv = dec->p_bmv = zeromv;
1193                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1194                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
# Line 1609  Line 1197 
1197                          mv =                          mv =
1198                          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] =
1199                          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;
1200                            mb->quant = quant;
1201    
1202                          // skip if the co-located P_VOP macroblock is not coded                          /*
1203                          // if not codec in co-located S_VOP macroblock is _not_ automatically skipped                           * skip if the co-located P_VOP macroblock is not coded
1204                             * if not codec in co-located S_VOP macroblock is _not_
1205                             * automatically skipped
1206                             */
1207    
1208                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
                                 //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);  
1209                                  mb->cbp = 0;                                  mb->cbp = 0;
1210  #ifdef BFRAMES_DEC_DEBUG                                  mb->mode = MODE_FORWARD;
1211                                  mb->mb_type = MODE_NOT_CODED;                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
         BFRAME_DEBUG  
 #endif  
                                 mb->mb_type = MODE_FORWARD;  
                                 mb->quant = last_mb->quant;  
                                 //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;  
                                 //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;  
   
                                 decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);  
1212                                  continue;                                  continue;
1213                          }                          }
1214    
1215                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1216                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1217    
1218                                  mb->mb_type = get_mbtype(bs);                                  mb->mode = get_mbtype(bs);
1219    
1220                                  if (!modb2) {   // modb=='00'                                  if (!modb2)             /* modb=='00' */
1221                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1222                                  } else {                                  else
1223                                          mb->cbp = 0;                                          mb->cbp = 0;
                                 }  
                                 if (mb->mb_type && mb->cbp) {  
                                         quant += get_dbquant(bs);  
1224    
1225                                          if (quant > 31) {                                  if (mb->mode && mb->cbp) {
1226                                            quant += get_dbquant(bs);
1227                                            if (quant > 31)
1228                                                  quant = 31;                                                  quant = 31;
1229                                          } else if (quant < 1) {                                          else if (quant < 1)
1230                                                  quant = 1;                                                  quant = 1;
1231                                          }                                          }
1232                                    mb->quant = quant;
1233    
1234                                    if (dec->interlacing) {
1235                                            if (mb->cbp) {
1236                                                    mb->field_dct = BitstreamGetBit(bs);
1237                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1238                                            }
1239    
1240                                            if (mb->mode) {
1241                                                    mb->field_pred = BitstreamGetBit(bs);
1242                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1243    
1244                                                    if (mb->field_pred) {
1245                                                            mb->field_for_top = BitstreamGetBit(bs);
1246                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1247                                                            mb->field_for_bot = BitstreamGetBit(bs);
1248                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1249                                                    }
1250                                            }
1251                                  }                                  }
1252    
1253                          } else {                          } else {
1254                                  mb->mb_type = MODE_DIRECT_NONE_MV;                                  mb->mode = MODE_DIRECT_NONE_MV;
1255                                  mb->cbp = 0;                                  mb->cbp = 0;
1256                          }                          }
1257    
1258                          mb->quant = quant;                          switch (mb->mode) {
                         mb->mode = MODE_INTER4V;  
                         //DEBUG1("Switch bm_type=",mb->mb_type);  
   
 #ifdef BFRAMES_DEC_DEBUG  
         BFRAME_DEBUG  
 #endif  
   
                         switch (mb->mb_type) {  
1259                          case MODE_DIRECT:                          case MODE_DIRECT:
1260                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);                                  get_b_motion_vector(bs, &mv, 1, zeromv);
1261    
1262                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
                                 {  
                                         const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;  
                                         int i;  
   
1263                                          for (i = 0; i < 4; i++) {                                          for (i = 0; i < 4; i++) {
1264                                                  mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)                                          mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + mv.x);
                                                                       / TRD + mv.x);  
1265                                                  mb->b_mvs[i].x = (int32_t) ((mv.x == 0)                                                  mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1266                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].x)                                                                          ? ((TRB - TRD) * last_mb->mvs[i].x) / TRD
                                                                                   / TRD  
1267                                                                                  : mb->mvs[i].x - last_mb->mvs[i].x);                                                                                  : mb->mvs[i].x - last_mb->mvs[i].x);
1268                                                  mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)                                          mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y);
                                                                       / TRD + mv.y);  
1269                                                  mb->b_mvs[i].y = (int32_t) ((mv.y == 0)                                                  mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1270                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].y)                                                                          ? ((TRB - TRD) * last_mb->mvs[i].y) / TRD
                                                                                   / TRD  
1271                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);
1272                                          }                                          }
1273                                          //DEBUG("B-frame Direct!\n");  
                                 }  
1274                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1275                                                                                             mb, x, y, bs);                                                                                                  mb, x, y, bs, 1);
1276                                  break;                                  break;
1277    
1278                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1279                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);
                                                                         dec->p_fmv);  
1280                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1281    
1282                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv);
1283                                                                          fcode_backward, dec->p_bmv);                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];
                                 dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =  
                                         mb->b_mvs[3] = mb->b_mvs[0];  
1284    
1285                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1286                                                                                             mb, x, y, bs);                                                                                          mb, x, y, bs, 0);
                                 //DEBUG("B-frame Bidir!\n");  
1287                                  break;                                  break;
1288    
1289                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1290                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv);
                                                                         dec->p_bmv);  
1291                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1292    
1293                                  mb->mode = MODE_INTER;                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);
                                 decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);  
                                 //DEBUG("B-frame Backward!\n");  
1294                                  break;                                  break;
1295    
1296                          case MODE_FORWARD:                          case MODE_FORWARD:
1297                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);
                                                                         dec->p_fmv);  
1298                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1299    
1300                                  mb->mode = MODE_INTER;                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
                                 decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);  
                                 //DEBUG("B-frame Forward!\n");  
1301                                  break;                                  break;
1302    
1303                          default:                          default:
1304                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);                                  DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);
1305                          }                          }
1306                    } /* End of for */
                 }                                               // end of FOR  
1307          }          }
1308    
1309  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1310          if (!first){          if (!first){
1311                  first=1;                  first=1;
# Line 1740  Line 1315 
1315  #endif  #endif
1316  }  }
1317    
 // swap two MACROBLOCK array  
 void  
 mb_swap(MACROBLOCK ** mb1,  
                 MACROBLOCK ** mb2)  
 {  
         MACROBLOCK *temp = *mb1;  
   
         *mb1 = *mb2;  
         *mb2 = temp;  
 }  
   
   
1318  /* perform post processing if necessary, and output the image */  /* perform post processing if necessary, and output the image */
1319  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1320                                          const XVID_DEC_FRAME * frame, int pp_disable)                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)
1321  {  {
1322            image_output(img, dec->width, dec->height,
1323                                     dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1324                                     frame->output.csp, dec->interlacing);
1325    
1326          if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */          if (stats) {
1327          {                  stats->type = coding2type(coding_type);
1328                  /* note: image is stored to tmp */                  stats->data.vop.time_base = (int)dec->time_base;
1329                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);                  stats->data.vop.time_increment = 0;     /* XXX: todo */
                 image_deblock_rrv(&dec->tmp, dec->edged_width,  
                                                 mbs, dec->mb_width, dec->mb_height, dec->mb_width,  
                                                 8, frame->general);  
                 img = &dec->tmp;  
1330          }          }
   
         image_output(img, dec->width, dec->height,  
                                  dec->edged_width, frame->image, frame->stride,  
                                  frame->colorspace, dec->interlacing);  
1331  }  }
1332    
1333    
1334  int  int
1335  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1336                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1337  {  {
1338    
1339          Bitstream bs;          Bitstream bs;
# Line 1786  Line 1344 
1344          uint32_t fcode_backward;          uint32_t fcode_backward;
1345          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1346          WARPPOINTS gmc_warp;          WARPPOINTS gmc_warp;
1347          uint32_t vop_type;          int coding_type;
1348          int success = 0;          int success, output, seen_something;
         int output = 0;  
         int seen_something = 0;  
1349    
1350          start_global_timer();          if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1351                    return XVID_ERR_VERSION;
1352    
1353          dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);          start_global_timer();
         dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;  
1354    
1355          if ((frame->general & XVID_DEC_DISCONTINUITY))          dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1356            if ((frame->general & XVID_DISCONTINUITY))
1357                  dec->frames = 0;                  dec->frames = 0;
1358            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1359    
1360          if (frame->length < 0)  /* decoder flush */          if (frame->length < 0) {        /* decoder flush */
1361          {                  int ret;
1362                  /* 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
1363                      we have a reference frame, then outout the reference frame */                      we have a reference frame, then outout the reference frame */
1364                  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) {
1365                  {                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1366                          decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);                          dec->frames = 0;
1367                          output = 1;                          ret = 0;
1368                  }                  } else {
1369                            if (stats) stats->type = XVID_TYPE_NOTHING;
1370                  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  
1371                  }                  }
1372    
1373                  emms();                  emms();
   
1374                  stop_global_timer();                  stop_global_timer();
1375                  return XVID_ERR_OK;                  return ret;
1376          }          }
1377    
1378          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1379    
1380          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1381          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1382          {          {
                 if (stats)  
                         stats->notify = XVID_DEC_VOP;  
                 frame->length = 1;  
1383                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1384                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1385                    if (stats) stats->type = XVID_TYPE_NOTHING;
1386                  emms();                  emms();
1387                  return XVID_ERR_OK;                  return 1;       /* one byte consumed */
1388          }          }
1389    
1390            success = 0;
1391            output = 0;
1392            seen_something = 0;
1393    
1394  repeat:  repeat:
1395    
1396          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,          coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1397                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1398    
1399          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",
1400                                                          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);
1401    
1402          if (vop_type == - 1)          if (coding_type == -1) { /* nothing */
         {  
1403                  if (success) goto done;                  if (success) goto done;
1404                    if (stats) stats->type = XVID_TYPE_NOTHING;
1405                  emms();                  emms();
1406                  return XVID_ERR_FAIL;                  return BitstreamPos(&bs)/8;
1407          }          }
1408    
1409          if (vop_type == -2 || vop_type == -3)          if (coding_type == -2 || coding_type == -3) {   /* vol and/or resize */
1410          {  
1411                  if (vop_type == -3)                  if (coding_type == -3)
1412                          decoder_resize(dec);                          decoder_resize(dec);
1413    
1414                  if (stats)                  if (stats) {
1415                  {                          stats->type = XVID_TYPE_VOL;
                         stats->notify = XVID_DEC_VOL;  
1416                          stats->data.vol.general = 0;                          stats->data.vol.general = 0;
1417                          if (dec->interlacing)                          /*XXX: if (dec->interlacing)
1418                                  stats->data.vol.general |= XVID_INTERLACING;                                  stats->data.vol.general |= ++INTERLACING; */
1419                          stats->data.vol.width = dec->width;                          stats->data.vol.width = dec->width;
1420                          stats->data.vol.height = dec->height;                          stats->data.vol.height = dec->height;
1421                          stats->data.vol.aspect_ratio = dec->aspect_ratio;                          stats->data.vol.par = dec->aspect_ratio;
1422                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1423                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
                         frame->length = BitstreamPos(&bs) / 8;  
1424                          emms();                          emms();
1425                          return XVID_ERR_OK;                          return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1426                  }                  }
1427                  goto repeat;                  goto repeat;
1428          }          }
1429    
1430          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 */
   
1431    
1432          /* packed_mode: special-N_VOP treament */          /* packed_mode: special-N_VOP treament */
1433          if (dec->packed_mode && vop_type == N_VOP)          if (dec->packed_mode && coding_type == N_VOP) {
1434          {                  if (dec->low_delay_default && dec->frames > 0) {
1435                  if (dec->low_delay_default && dec->frames > 0)                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
                 {  
                         decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);  
1436                          output = 1;                          output = 1;
1437                  }                  }
1438                  /* ignore otherwise */                  /* ignore otherwise */
1439          }          } else if (coding_type != B_VOP) {
1440          else if (vop_type != B_VOP)                  switch(coding_type) {
         {  
                 switch(vop_type)  
                 {  
1441                  case I_VOP :                  case I_VOP :
1442                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1443                          break;                          break;
# Line 1904  Line 1450 
1450                                                  fcode_forward, intra_dc_threshold, &gmc_warp);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1451                          break;                          break;
1452                  case N_VOP :                  case N_VOP :
1453                            /* XXX: not_coded vops are not used for forward prediction */
1454                            /* we should not swap(last_mbs,mbs) */
1455                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1456                          break;                          break;
1457                  }                  }
1458    
1459                  if (reduced_resolution)                  if (reduced_resolution) {
                 {  
1460                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1461                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1462                                  16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);                                  16, 0);
1463                  }                  }
1464    
1465                  /* 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 */
1466                  if (!(dec->low_delay_default && dec->packed_mode))                  if (!(dec->low_delay_default && dec->packed_mode)) {
1467                  {                          if (dec->low_delay) {
1468                          if (dec->low_delay)                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
                         {  
                                 decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);  
1469                                  output = 1;                                  output = 1;
1470                          }                          } else if (dec->frames > 0)     { /* is the reference frame valid? */
                         else if (dec->frames > 0)       /* is the reference frame valid? */  
                         {  
1471                                  /* output the reference frame */                                  /* output the reference frame */
1472                                  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);
1473                                  output = 1;                                  output = 1;
1474                          }                          }
1475                  }                  }
1476    
1477                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1478                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1479                  mb_swap(&dec->mbs, &dec->last_mbs);                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1480                  dec->last_reduced_resolution = reduced_resolution;                  dec->last_reduced_resolution = reduced_resolution;
1481                    dec->last_coding_type = coding_type;
1482    
1483                  dec->frames++;                  dec->frames++;
1484                  seen_something = 1;                  seen_something = 1;
1485    
1486          }else{  /* B_VOP */          }else{  /* B_VOP */
1487    
1488                  if (dec->low_delay)                  if (dec->low_delay) {
1489                  {                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
                         DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");  
1490                          dec->low_delay = 1;                          dec->low_delay = 1;
1491                  }                  }
1492    
1493                  if (dec->frames < 2)                  if (dec->frames < 2) {
                 {  
1494                          /* attemping to decode a bvop without atleast 2 reference frames */                          /* attemping to decode a bvop without atleast 2 reference frames */
1495                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1496                                                  "broken b-frame, mising ref frames");                                                  "broken b-frame, mising ref frames");
# Line 1961  Line 1503 
1503                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1504                  }                  }
1505    
1506                  decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1507                  output = 1;                  output = 1;
1508                  dec->frames++;                  dec->frames++;
1509          }          }
# Line 1969  Line 1511 
1511          BitstreamByteAlign(&bs);          BitstreamByteAlign(&bs);
1512    
1513          /* low_delay_default mode: repeat in packed_mode */          /* low_delay_default mode: repeat in packed_mode */
1514          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
         {  
1515                  success = 1;                  success = 1;
1516                  goto repeat;                  goto repeat;
1517          }          }
# Line 1979  Line 1520 
1520    
1521          /* low_delay_default mode: if we've gotten here without outputting anything,          /* low_delay_default mode: if we've gotten here without outputting anything,
1522             then output the recently decoded frame, or print an error message  */             then output the recently decoded frame, or print an error message  */
1523          if (dec->low_delay_default && output == 0)          if (dec->low_delay_default && output == 0) {
1524          {                  if (dec->packed_mode && seen_something) {
                 if (dec->packed_mode && seen_something)  
                 {  
1525                          /* output the recently decoded frame */                          /* output the recently decoded frame */
1526                          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);
1527                          output = 1;                  } else {
                 }  
                 else  
                 {  
1528                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1529                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1530                                  "warning: nothing to output");                                  "warning: nothing to output");
1531                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1532                                  "bframe decoder lag");                                  "bframe decoder lag");
1533    
1534                          decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);
1535                  }                          if (stats) stats->type = XVID_TYPE_NOTHING;
1536          }          }
   
         frame->length = BitstreamPos(&bs) / 8;  
   
         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  
1537          }          }
1538    
1539          emms();          emms();
   
1540          stop_global_timer();          stop_global_timer();
1541    
1542          return XVID_ERR_OK;          return BitstreamPos(&bs) / 8;   /* number of bytes consumed */
1543  }  }

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

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