[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.27, Thu Jul 11 00:15:59 2002 UTC revision 1.49.2.18, Wed Oct 22 09:47:52 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:  
  *  
  *  10.07.2002  added BFRAMES_DEC_DEBUG support  
  *              Fix a little bug for low_delay flage  
  *              MinChen <chenm001@163.com>  
  *  28.06.2002  added basic resync support to iframe/pframe_decode()  
  *  22.06.2002  added primative N_VOP support  
  *              #define BFRAMES_DEC now enables Minchen's bframe decoder  
  *  08.05.2002  add low_delay support for B_VOP decode  
  *              MinChen <chenm001@163.com>  
  *  05.05.2002  fix some B-frame decode problem  
  *  02.05.2002  add B-frame decode support(have some problem);  
  *              MinChen <chenm001@163.com>  
  *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>  
  *  29.03.2002  interlacing fix - compensated block wasn't being used when  
  *              reconstructing blocks, thus artifacts  
  *              interlacing speedup - used transfers to re-interlace  
  *              interlaced decoding should be as fast as progressive now  
  *  26.03.2002  interlacing support - moved transfers outside decode loop  
  *  26.12.2001  decoder_mbinter: dequant/idct moved within if(coded) block  
  *  22.12.2001  lock based interpolation  
  *  01.12.2001  inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>  
  *  
23   *  $Id$   *  $Id$
24   *   *
25   *************************************************************************/   ****************************************************************************/
26    
27    #include <stdio.h>
28  #include <stdlib.h>  #include <stdlib.h>
29  #include <string.h>  #include <string.h>
30    
# Line 66  Line 34 
34    
35  #include "xvid.h"  #include "xvid.h"
36  #include "portab.h"  #include "portab.h"
37    #include "global.h"
38    
39  #include "decoder.h"  #include "decoder.h"
40  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
41  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
42    
43  #include "quant/quant_h263.h"  #include "quant/quant.h"
 #include "quant/quant_mpeg4.h"  
44  #include "dct/idct.h"  #include "dct/idct.h"
45  #include "dct/fdct.h"  #include "dct/fdct.h"
46  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
47  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
48    #include "image/reduced.h"
49    #include "image/font.h"
50    
51  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
52  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
53  #include "utils/timer.h"  #include "utils/timer.h"
54  #include "utils/emms.h"  #include "utils/emms.h"
55    #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_create(XVID_DEC_PARAM * param)  decoder_resize(DECODER * dec)
64  {  {
65          DECODER *dec;          /* free existing */
66            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
67            image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
68            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
69            image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
70            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
71    
72          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
         if (dec == NULL) {  
                 return XVID_ERR_MEMORY;  
         }  
         param->handle = dec;  
73    
74          dec->width = param->width;          if (dec->last_mbs)
75          dec->height = param->height;                  xvid_free(dec->last_mbs);
76            if (dec->mbs)
77                    xvid_free(dec->mbs);
78    
79            /* 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    
83          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
84          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
         dec->low_delay = 0;  
85    
86          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
87                  xvid_free(dec);                  xvid_free(dec);
# Line 118  Line 93 
93                  xvid_free(dec);                  xvid_free(dec);
94                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
95          }          }
96          // add by chenm001 <chenm001@163.com>  
97          // for support B-frame to reference last 2 frame          /* 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);
101                  xvid_free(dec);                  xvid_free(dec);
102                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
103          }          }
104          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
105                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
106                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
107                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
108                    xvid_free(dec);
109                    return XVID_ERR_MEMORY;
110            }
111    
112            if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
113                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
114                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
115                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
116                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
117                    xvid_free(dec);
118                    return XVID_ERR_MEMORY;
119            }
120    
121            if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
122                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
123                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
124                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
125                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
126                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
127                  xvid_free(dec);                  xvid_free(dec);
128                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
129          }          }
# Line 141  Line 135 
135                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
136                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
137                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
138                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
139                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
140                  xvid_free(dec);                  xvid_free(dec);
141                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
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 158  Line 151 
151                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
152                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
153                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
154                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
155                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
156                  xvid_free(dec);                  xvid_free(dec);
157                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
158          }          }
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 0;
163    }
164    
165    
166    int
167    decoder_create(xvid_dec_create_t * create)
168    {
169            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);
175            if (dec == NULL) {
176                    return XVID_ERR_MEMORY;
177            }
178            memset(dec, 0, sizeof(DECODER));
179    
180            create->handle = dec;
181    
182            dec->width = create->width;
183            dec->height = create->height;
184    
185            image_null(&dec->cur);
186            image_null(&dec->refn[0]);
187            image_null(&dec->refn[1]);
188            image_null(&dec->tmp);
189            image_null(&dec->qtmp);
190    
191            /* image based GMC */
192            image_null(&dec->gmc);
193    
194    
195            dec->mbs = NULL;
196            dec->last_mbs = NULL;
197    
198          init_timer();          init_timer();
199    
200          // add by chenm001 <chenm001@163.com>          /* For B-frame support (used to save reference frame's time */
201          // for support B-frame to save reference frame's time          dec->frames = 0;
         dec->frames = -1;  
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;
204            dec->packed_mode = 0;
205    
206          return XVID_ERR_OK;          dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
207    
208            if (dec->fixed_dimensions)
209                    return decoder_resize(dec);
210            else
211                    return 0;
212  }  }
213    
214    
# Line 181  Line 217 
217  {  {
218          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
219          xvid_free(dec->mbs);          xvid_free(dec->mbs);
220    
221            /* image based GMC */
222            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
223    
224          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
225          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
226          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
227            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
228          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
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 212  Line 247 
247                                  Bitstream * bs,                                  Bitstream * bs,
248                                  const uint32_t quant,                                  const uint32_t quant,
249                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
250                                  const unsigned int bound)                                  const unsigned int bound,
251                                    const int reduced_resolution)
252  {  {
253    
254          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 225  Line 261 
261          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
262          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
263    
264            if (reduced_resolution) {
265                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
266                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
267                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
268            }else{
269          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
270          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
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 252  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                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],                          int direction = dec->alternate_vertical_scan ?
312                                                          start_coeff);                                  2 : pMB->acpred_directions[i];
313    
314                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
315                  }                  }
316                  stop_coding_timer();                  stop_coding_timer();
317    
# Line 277  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    
330                  start_timer();                  start_timer();
331                  idct(&data[i * 64]);                  idct(&data[i * 64]);
332                  stop_idct_timer();                  stop_idct_timer();
333    
334          }          }
335    
336          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 294  Line 339 
339          }          }
340    
341          start_timer();          start_timer();
342    
343            if (reduced_resolution)
344            {
345                    next_block*=2;
346                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
347                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
348                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
349                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
350                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
351                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
352            }else{
353          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
354          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
355          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
356          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
357          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
358          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
359            }
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;
377            int next_block = stride * (reduced_resolution ? 16 : 8);
378            const int stride2 = stride/2;
379            int i;
380            const uint32_t iQuant = pMB->quant;
381            const int direction = dec->alternate_vertical_scan ? 2 : 0;
382            const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter;
383    
384            for (i = 0; i < 6; i++) {
385    
386                    if (cbp & (1 << (5 - i))) {     /* coded */
387    
388                            memset(block, 0, 64 * sizeof(int16_t)); /* clear */
389    
390                            start_timer();
391                            get_inter_block(bs, block, direction);
392                            stop_coding_timer();
393    
394                            start_timer();
395                            dequant(&data[i * 64], block, iQuant);
396                            stop_iquant_timer();
397    
398  #define SIGN(X) (((X)>0)?1:-1)                          start_timer();
399  #define ABS(X) (((X)>0)?(X):-(X))                          idct(&data[i * 64]);
400  static const uint32_t roundtab[16] =                          stop_idct_timer();
401          { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };                  }
402            }
403    
404            if (dec->interlacing && pMB->field_dct) {
405                    next_block = stride;
406                    stride *= 2;
407            }
408    
409  // decode an inter macroblock          start_timer();
410            if (reduced_resolution) {
411                    if (cbp & 32)
412                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
413                    if (cbp & 16)
414                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
415                    if (cbp & 8)
416                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
417                    if (cbp & 4)
418                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
419                    if (cbp & 2)
420                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
421                    if (cbp & 1)
422                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
423            } else {
424                    if (cbp & 32)
425                            transfer_16to8add(pY_Cur, &data[0 * 64], stride);
426                    if (cbp & 16)
427                            transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
428                    if (cbp & 8)
429                            transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
430                    if (cbp & 4)
431                            transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
432                    if (cbp & 2)
433                            transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
434                    if (cbp & 1)
435                            transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
436            }
437            stop_transfer_timer();
438    }
439    
440  void  /* decode an inter macroblock */
441    static void
442  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
443                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
444                                  const uint32_t x_pos,                                  const uint32_t x_pos,
445                                  const uint32_t y_pos,                                  const uint32_t y_pos,
                                 const uint32_t acpred_flag,  
446                                  const uint32_t cbp,                                  const uint32_t cbp,
447                                  Bitstream * bs,                                  Bitstream * bs,
448                                  const uint32_t quant,                                  const uint32_t rounding,
449                                  const uint32_t rounding)                                  const int reduced_resolution,
450                                    const int ref)
451  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
452          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
453          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * 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;
459            VECTOR mv[4];   /* local copy of mvs */
460    
461            if (reduced_resolution) {
462                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
463                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
464                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
465                    for (i = 0; i < 4; i++) {
466                            mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
467                            mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
468                    }
469            } else {
470          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
471          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
472          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
473                    for (i = 0; i < 4; i++)
474                            mv[i] = pMB->mvs[i];
475            }
476    
477          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          start_timer();
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
478    
479                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;          if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
         } else {  
                 int sum;  
480    
481                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  uv_dx = mv[0].x;
482                  uv_dx =                  uv_dy = mv[0].y;
483                          (sum ==                  if (dec->quarterpel) {
484                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                          uv_dx /= 2;
485                                                                    (ABS(sum) / 16) * 2));                          uv_dy /= 2;
   
                 sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
                 uv_dy =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
486          }          }
487                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
488                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
489    
490          start_timer();                  if (reduced_resolution)
491                            interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
492                                                                            mv[0].x, mv[0].y, stride, rounding);
493                    else if (dec->quarterpel)
494                            interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
495                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
496                                                                                            mv[0].x, mv[0].y, stride, rounding);
497                    else
498                            interpolate16x16_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
499                                                                            mv[0].x, mv[0].y, stride, rounding);
500    
501            } else {        /* MODE_INTER4V */
502    
503                    if(dec->quarterpel) {
504                            uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
505                            uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
506                    } 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_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
512                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
513    
514                    if (reduced_resolution) {
515                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
516                                                                    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,
518                                                                    mv[1].x, mv[1].y, stride, rounding);
519                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos, 32*y_pos + 16,
520                                                                    mv[2].x, mv[2].y, stride, rounding);
521                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos + 16,
522                                                                    mv[3].x, mv[3].y, stride, rounding);
523                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u , 16 * x_pos, 16 * y_pos,
524                                                                    uv_dx, uv_dy, stride2, rounding);
525                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,
526                                                                    uv_dx, uv_dy, stride2, rounding);
527    
528                    } else if (dec->quarterpel) {
529                            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,
531                                                                            mv[0].x, mv[0].y, stride, rounding);
532                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
533                                                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
534                                                                            mv[1].x, mv[1].y, stride, rounding);
535                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
536                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
537                                                                            mv[2].x, mv[2].y, stride, rounding);
538                            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,
540                                                                            mv[3].x, mv[3].y, stride, rounding);
541                    } 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                                                    pMB->mvs[0].x, pMB->mvs[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,                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,
545                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride,                                                                  mv[1].x, mv[1].y, stride, rounding);
546                                                    rounding);                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos + 8,
547          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos,                                                                  mv[2].x, mv[2].y, stride, rounding);
548                                                    16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,
549                                                    rounding);                                                                  mv[3].x, mv[3].y, stride, rounding);
550          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,                  }
551                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,          }
552                                                    rounding);  
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);
557                    interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
558                                                                    uv_dx, uv_dy, stride2, rounding);
559            } else {
560                    interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
561                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
562          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,                  interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
563                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
564            }
565    
566          stop_comp_timer();          stop_comp_timer();
567    
568          for (i = 0; i < 6; i++) {          if (cbp)
569                  if (cbp & (1 << (5 - i)))       // coded                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur,
570                                                            reduced_resolution, pMB);
571    }
572    
573    static void
574    decoder_mbgmc(DECODER * dec,
575                                    MACROBLOCK * const pMB,
576                                    const uint32_t x_pos,
577                                    const uint32_t y_pos,
578                                    const uint32_t fcode,
579                                    const uint32_t cbp,
580                                    Bitstream * bs,
581                                    const uint32_t rounding)
582                  {                  {
583                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear          const uint32_t stride = dec->edged_width;
584            const uint32_t stride2 = stride / 2;
585    
586                          start_timer();          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
587                          get_inter_block(bs, &block[i * 64]);          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
588                          stop_coding_timer();          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
589    
590                          start_timer();          NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
591                          if (dec->quant_type == 0) {  
592                                  dequant_inter(&data[i * 64], &block[i * 64], iQuant);          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
                         }  
                         stop_iquant_timer();  
593    
594                          start_timer();                          start_timer();
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
595    
596          if (dec->interlacing && pMB->field_dct) {  /* this is where the calculations are done */
597                  next_block = stride;  
598                  stride *= 2;          gmc_data->predict_16x16(gmc_data,
599          }                          dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
600                            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);
610            pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
611    
612            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
613    
         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);  
614          stop_transfer_timer();          stop_transfer_timer();
615    
616            if (cbp)
617                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
618    
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,
626                             int quant,                             int quant,
627                             int intra_dc_threshold)                             int intra_dc_threshold)
628  {  {
629          uint32_t bound;          uint32_t bound;
630          uint32_t x, y;          uint32_t x, y;
631            uint32_t mb_width = dec->mb_width;
632            uint32_t mb_height = dec->mb_height;
633    
634            if (reduced_resolution) {
635                    mb_width = (dec->width + 31) / 32;
636                    mb_height = (dec->height + 31) / 32;
637            }
638    
639          bound = 0;          bound = 0;
640    
641          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
642                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
643                          MACROBLOCK *mb;                          MACROBLOCK *mb;
644                          uint32_t mcbpc;                          uint32_t mcbpc;
645                          uint32_t cbpc;                          uint32_t cbpc;
# Line 452  Line 652 
652    
653                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
654                          {                          {
655                                  bound = read_video_packet_header(bs, 0, &quant);                                  bound = read_video_packet_header(bs, dec, 0,
656                                  x = bound % dec->mb_width;                                                          &quant, NULL, NULL, &intra_dc_threshold);
657                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
658                                    y = bound / mb_width;
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 478  Line 679 
679                                  }                                  }
680                          }                          }
681                          mb->quant = quant;                          mb->quant = quant;
682                            mb->mvs[0].x = mb->mvs[0].y =
683                            mb->mvs[1].x = mb->mvs[1].y =
684                            mb->mvs[2].x = mb->mvs[2].y =
685                            mb->mvs[3].x = mb->mvs[3].y =0;
686    
687                          if (dec->interlacing) {                          if (dec->interlacing) {
688                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
689                                  DEBUG1("deci: field_dct: ", 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,
693                                                          intra_dc_threshold, bound);                                                          intra_dc_threshold, bound, reduced_resolution);
694    
695                  }                  }
696                    if(dec->out_frm)
697                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
698          }          }
699    
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,
707                                    int y,                                    int y,
708                                    int k,                                    int k,
709                                    VECTOR * mv,                                  VECTOR * ret_mv,
710                                    int fcode,                                    int fcode,
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          int mv_x, mv_y;          VECTOR mv;
721    
722          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          mv.x = get_mv(bs, fcode);
723            mv.y = get_mv(bs, fcode);
724    
725          mv_x = get_mv(bs, fcode);          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);
         mv_y = get_mv(bs, fcode);  
726    
727          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);          mv.x += pmv.x;
728            mv.y += pmv.y;
         mv_x += pmv.x;  
         mv_y += pmv.y;  
729    
730          if (mv_x < low) {          if (mv.x < low) {
731                  mv_x += range;                  mv.x += range;
732          } else if (mv_x > high) {          } else if (mv.x > high) {
733                  mv_x -= range;                  mv.x -= range;
734          }          }
735    
736          if (mv_y < low) {          if (mv.y < low) {
737                  mv_y += range;                  mv.y += range;
738          } else if (mv_y > high) {          } else if (mv.y > high) {
739                  mv_y -= range;                  mv.y -= range;
740          }          }
741    
742          mv->x = mv_x;          ret_mv->x = mv.x;
743          mv->y = mv_y;          ret_mv->y = mv.y;
   
744  }  }
745    
746    /* 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,
751                                    int reduced_resolution,
752                             int quant,                             int quant,
753                             int fcode,                             int fcode,
754                             int intra_dc_threshold)                                  int intra_dc_threshold,
755                                    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;
760            uint32_t mb_width = dec->mb_width;
761            uint32_t mb_height = dec->mb_height;
762    
763            if (reduced_resolution) {
764                    mb_width = (dec->width + 31) / 32;
765                    mb_height = (dec->height + 31) / 32;
766            }
767    
768          start_timer();          start_timer();
769          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
770                                     dec->width, dec->height, dec->interlacing);                                          dec->width, dec->height);
771          stop_edges_timer();          stop_edges_timer();
772    
773            if (gmc_warp) {
774                    /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
775                    generate_GMCparameters( dec->sprite_warping_points,
776                                    dec->sprite_warping_accuracy, gmc_warp,
777                                    dec->width, dec->height, &dec->new_gmc_data);
778    
779                    /* image warping is done block-based in decoder_mbgmc(), now */
780            }
781    
782          bound = 0;          bound = 0;
783    
784          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
785                  for (x = 0; x < dec->mb_width; x++) {                  cp_mb = st_mb = 0;
786                    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,
795                                  bound = read_video_packet_header(bs, fcode - 1, &quant);                                          &quant, &fcode, NULL, &intra_dc_threshold);
796                                  x = bound % dec->mb_width;                                  x = bound % mb_width;
797                                  y = bound / dec->mb_width;                                  y = bound / mb_width;
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)))     // not_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;  
807    
808                                    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    
818                                  if (intra) {                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
819                                            mcsel = BitstreamGetBit(bs);
820                                    else if (intra)
821                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
                                 }  
822    
823                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
824                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  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) && !mcsel) {
843                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
844                                          DEBUG1("decp: field_dct: ", 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                                                  DEBUG1("decp: field_pred: ", 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                                                          DEBUG1("decp: field_for_top: ", 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                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
856                                                  }                                                  }
857                                          }                                          }
858                                  }                                  }
859    
860                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
861                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, rounding);
862                                            continue;
863    
864                                    } 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);
871                                                                                    fcode, bound);                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
                                                 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;  
872                                          }                                          }
873                                  } else if (mb->mode ==                                  } else if (mb->mode == MODE_INTER4V ) {
                                                    MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {  
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);                                                                          intra_dc_threshold, bound, reduced_resolution);
883                                          continue;                                          continue;
884                                  }                                  }
885    
886                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, cbp, bs,
887                                                                  rounding);                                                                  rounding, reduced_resolution, 0);
888                          } else                          // not coded  
889                          {                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
890                                  //DEBUG2("P-frame MB at (X,Y)=",x,y);                                  mb->mode = MODE_NOT_CODED_GMC;
891                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
892    
893                                    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);
895                                            cp_mb = 0;
896                                    }
897                                    st_mb = x+1;
898                            } 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;
903    
904                                  // copy macroblock directly from ref to cur                                  decoder_mbinter(dec, mb, x, y, 0, bs,
905                                                                    rounding, reduced_resolution, 0);
                                 start_timer();  
   
                                 transfer8x8_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.y + (16 * y) * dec->edged_width +  
                                                                  (16 * x + 8),  
                                                                  dec->refn[0].y + (16 * y) * dec->edged_width +  
                                                                  (16 * x + 8), dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +  
                                                                  (16 * x),  
                                                                  dec->refn[0].y + (16 * y +  
                                                                                                    8) * dec->edged_width +  
                                                                  (16 * x), dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +  
                                                                  (16 * x + 8),  
                                                                  dec->refn[0].y + (16 * y +  
                                                                                                    8) * dec->edged_width +  
                                                                  (16 * x + 8), 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);  
906    
907                                  stop_transfer_timer();                                  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);
909                                            cp_mb = 0;
910                                    }
911                                    st_mb = x+1;
912                          }                          }
913                  }                  }
914    
915                    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);
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);
932    
933          int mv_x, mv_y;          int mv_x = get_mv(bs, fcode);
934          int pmv_x, pmv_y;          int mv_y = get_mv(bs, fcode);
935    
936          pmv_x = pmv.x;          mv_x += pmv.x;
937          pmv_y = pmv.y;          mv_y += pmv.y;
   
         mv_x = get_mv(bs, fcode);  
         mv_y = get_mv(bs, fcode);  
   
         mv_x += pmv_x;  
         mv_y += pmv_y;  
938    
939          if (mv_x < low) {          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
955  // decode an B-frame forward & backward inter macroblock  decoder_bf_interpolate_mbinter(DECODER * dec,
956  void                                                                  IMAGE forward,
957  decoder_bf_mbinter(DECODER * dec,                                                                  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,
                                    const uint32_t cbp,  
961                                     Bitstream * bs,                                     Bitstream * bs,
962                                     const uint32_t quant,                                                                  const int direct)
                                    const uint8_t ref)  
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;
         uint32_t next_block = stride * 8;  
         uint32_t i;  
         uint32_t iQuant = pMB->quant;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
966          int uv_dx, uv_dy;          int uv_dx, uv_dy;
967            int b_uv_dx, b_uv_dy;
968            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
969            const uint32_t cbp = pMB->cbp;
970    
971          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
972          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
973          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
974    
975          if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {          if (!direct) {
976                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
977                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
978    
979                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  b_uv_dx = pMB->b_mvs[0].x;
980                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  b_uv_dy = pMB->b_mvs[0].y;
         } else {  
                 int sum;  
981    
982                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  if (dec->quarterpel) {
983                  uv_dx =                          uv_dx /= 2;
984                          (sum ==                          uv_dy /= 2;
985                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                          b_uv_dx /= 2;
986                                                                    (ABS(sum) / 16) * 2));                          b_uv_dy /= 2;
   
                 sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
                 uv_dy =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
987          }          }
988    
989          start_timer();                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
990          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
                                                   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();  
991    
992          for (i = 0; i < 6; i++) {                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
993                  if (cbp & (1 << (5 - i)))       // coded                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
994    
                         start_timer();  
                         get_inter_block(bs, &block[i * 64]);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
995                          } else {                          } else {
996                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);                  if(dec->quarterpel) {
997                          }                          uv_dx = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
998                          stop_iquant_timer();                          uv_dy = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
999                            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);
1000                          start_timer();                          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);
1001                          idct(&data[i * 64]);                  } else {
1002                          stop_idct_timer();                          uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1003                  }                          uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1004                            b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1005                            b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1006          }          }
1007    
1008          if (dec->interlacing && pMB->field_dct) {                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
1009                  next_block = stride;                  uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
1010                  stride *= 2;                  b_uv_dx = (b_uv_dx >> 3) + roundtab_76[b_uv_dx & 0xf];
1011                    b_uv_dy = (b_uv_dy >> 3) + roundtab_76[b_uv_dy & 0xf];
1012          }          }
1013    
1014          start_timer();          start_timer();
1015          if (cbp & 32)          if(dec->quarterpel) {
1016                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  if(!direct) {
1017          if (cbp & 16)                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1018                  transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1019          if (cbp & 8)                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
                 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  
 decoder_bf_interpolate_mbinter(DECODER * dec,  
                                                            IMAGE forward,  
                                                            IMAGE backward,  
                                                            const MACROBLOCK * pMB,  
                                                            const uint32_t x_pos,  
                                                            const uint32_t y_pos,  
                                                            const uint32_t cbp,  
                                                            Bitstream * bs)  
 {  
   
         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 iQuant = pMB->quant;  
         int uv_dx, uv_dy;  
         int b_uv_dx, b_uv_dy;  
         uint32_t i;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
   
         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;  
   
                 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
   
                 b_uv_dx = pMB->b_mvs[0].x;  
                 b_uv_dy = pMB->b_mvs[0].y;  
   
                 b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
1020          } else {          } else {
1021                  int sum;                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1022                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1023                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1024                  uv_dx =                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1025                          (sum ==                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1026                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                                                                                  pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1027                                                                    (ABS(sum) / 16) * 2));                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1028                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1029                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                                                                                  pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1030                  uv_dy =                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1031                          (sum ==                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1032                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                                                                                  pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
                                                                   (ABS(sum) / 16) * 2));  
   
                 sum =  
                         pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +  
                         pMB->b_mvs[3].x;  
                 b_uv_dx =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
   
                 sum =  
                         pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y +  
                         pMB->b_mvs[3].y;  
                 b_uv_dy =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1033          }          }
1034            } else {
   
         start_timer();  
1035          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,
1036                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1037          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 959  Line 1039 
1039          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,
1040                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1041          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1042                                                    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);
1043                                                    0);          }
1044    
1045          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,
1046                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1047          interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1048                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1049    
1050    
1051          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,          if(dec->quarterpel) {
1052                    if(!direct) {
1053                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1054                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1055                                                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1056                    } else {
1057                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1058                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1059                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1060          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1061                                                    16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1062                                                    0);                                                                                  pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1063          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1064                                                    16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1065                                                    stride, 0);                                                                                  pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1066          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1067                                                    16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1068                                                    stride, 0);                                                                                  pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1069          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,                  }
1070            } else {
1071                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1072                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1073                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1074                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1075                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1076                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1077                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1078                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1079            }
1080    
1081            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1082                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1083          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1084                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1085    
1086          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1087                                           stride);                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1088          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1089                                           stride);                                                  stride, 1, 8);
1090          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,  
1091                                           stride);          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1092          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1093                                           16 * y_pos + 8, stride);                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1094          interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,                                                  stride, 1, 8);
1095                                           stride2);  
1096          interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1097                                           stride2);                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1098                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1099                                                    stride, 1, 8);
1100    
1101            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1102                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1103                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1104                                                    stride, 1, 8);
1105    
1106            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1107                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1108                                                    dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1109                                                    stride2, 1, 8);
1110    
1111            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1112                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1113                                                    dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1114                                                    stride2, 1, 8);
1115    
1116          stop_comp_timer();          stop_comp_timer();
1117    
1118          for (i = 0; i < 6; i++) {          if (cbp)
1119                  if (cbp & (1 << (5 - i)))       // coded                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64]);  
                         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();  
                 }  
1120          }          }
1121    
1122          if (dec->interlacing && pMB->field_dct) {  /* for decode B-frame dbquant */
1123                  next_block = stride;  static __inline int32_t
                 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  
1124  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1125  {  {
1126          if (!BitstreamGetBit(bs))       // '0'          if (!BitstreamGetBit(bs))               /*  '0' */
1127                  return (0);                  return (0);
1128          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs))  /* '10' */
1129                  return (-2);                  return (-2);
1130          else          else                                                    /* '11' */
1131                  return (2);                             // '11'                  return (2);
1132  }  }
1133    
1134  // add by MinChen <chenm001@163.com>  /*
1135  // for decode B-frame mb_type   * decode B-frame mb_type
1136  // bit   ret_value   * bit          ret_value
1137  // 1        0   * 1            0
1138  // 01       1   * 01           1
1139  // 001      2   * 001          2
1140  // 0001     3   * 0001         3
1141  int32_t __inline   */
1142    static int32_t __inline
1143  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1144  {  {
1145          int32_t mb_type;          int32_t mb_type;
1146    
1147          for (mb_type = 0; mb_type <= 3; mb_type++) {          for (mb_type = 0; mb_type <= 3; mb_type++)
1148                  if (BitstreamGetBit(bs))                  if (BitstreamGetBit(bs))
                         break;  
         }  
   
         if (mb_type <= 3)  
1149                  return (mb_type);                  return (mb_type);
1150          else  
1151                  return (-1);          return -1;
1152  }  }
1153    
1154  void  static void
1155  decoder_bframe(DECODER * dec,  decoder_bframe(DECODER * dec,
1156                             Bitstream * bs,                             Bitstream * bs,
1157                             int quant,                             int quant,
1158                             int fcode_forward,                             int fcode_forward,
1159                             int fcode_backward)                             int fcode_backward)
1160  {  {
   
1161          uint32_t x, y;          uint32_t x, y;
1162          VECTOR mv, zeromv;          VECTOR mv;
1163            const VECTOR zeromv = {0,0};
1164            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1165            int i;
1166    
1167  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1168          FILE *fp;          FILE *fp;
1169          static char first=0;          static char first=0;
1170  #define BFRAME_DEBUG    if (!first && fp){ \  #define BFRAME_DEBUG
1171                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \          if (!first && fp) { \
1172                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1173          }          }
1174  #endif  #endif
1175    
1176          start_timer();          start_timer();
1177          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1178                                     dec->width, dec->height, dec->interlacing);                                          dec->width, dec->height);
1179          //image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1180                                            dec->width, dec->height);
1181          stop_edges_timer();          stop_edges_timer();
1182    
1183  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
# Line 1110  Line 1187 
1187  #endif  #endif
1188    
1189          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1190                  // Initialize Pred Motion Vector                  /* Initialize Pred Motion Vector */
1191                  dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0;                  dec->p_fmv = dec->p_bmv = zeromv;
1192                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1193                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1194                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1195    
1196                          mb->mvs[0].x = mb->mvs[0].y = zeromv.x = zeromv.y = mv.x = mv.y = 0;                          mv =
1197                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1198                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1199                            mb->quant = quant;
1200    
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_
1204                             * automatically skipped
1205                             */
1206    
                         // the last P_VOP is skip macroblock ?  
1207                          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);  
                                 mb->mb_type = MODE_NOT_CODED;  
1208                                  mb->cbp = 0;                                  mb->cbp = 0;
1209  #ifdef BFRAMES_DEC_DEBUG                                  mb->mode = MODE_FORWARD;
1210          BFRAME_DEBUG                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
 #endif  
                                 mb->mb_type = MODE_FORWARD;  
                                 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;  
                                 mb->quant = 8;  
   
                                 decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);  
1211                                  continue;                                  continue;
1212                          }                          }
                         //t=BitstreamShowBits(bs,32);  
1213    
1214                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1215                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1216    
1217                                  mb->mb_type = get_mbtype(bs);                                  mb->mode = get_mbtype(bs);
1218    
1219                                  if (!modb2) {   // modb=='00'                                  if (!modb2)             /* modb=='00' */
1220                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1221                                  } else {                                  else
1222                                          mb->cbp = 0;                                          mb->cbp = 0;
                                 }  
                                 if (mb->mb_type && mb->cbp) {  
                                         quant += get_dbquant(bs);  
1223    
1224                                          if (quant > 31) {                                  if (mb->mode && mb->cbp) {
1225                                            quant += get_dbquant(bs);
1226                                            if (quant > 31)
1227                                                  quant = 31;                                                  quant = 31;
1228                                          } else if (quant < 1) {                                          else if (quant < 1)
1229                                                  quant = 1;                                                  quant = 1;
1230                                          }                                          }
                                 } else {  
                                         quant = 8;  
                                 }  
1231                                  mb->quant = quant;                                  mb->quant = quant;
1232    
1233                                    if (dec->interlacing) {
1234                                            if (mb->cbp) {
1235                                                    mb->field_dct = BitstreamGetBit(bs);
1236                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1237                                            }
1238    
1239                                            if (mb->mode) {
1240                                                    mb->field_pred = BitstreamGetBit(bs);
1241                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1242    
1243                                                    if (mb->field_pred) {
1244                                                            mb->field_for_top = BitstreamGetBit(bs);
1245                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1246                                                            mb->field_for_bot = BitstreamGetBit(bs);
1247                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1248                                                    }
1249                                            }
1250                                    }
1251    
1252                          } else {                          } else {
1253                                  mb->mb_type = MODE_DIRECT_NONE_MV;                                  mb->mode = MODE_DIRECT_NONE_MV;
1254                                  mb->cbp = 0;                                  mb->cbp = 0;
1255                          }                          }
1256    
1257                          mb->mode = MODE_INTER;                          switch (mb->mode) {
                         //DEBUG1("Switch bm_type=",mb->mb_type);  
   
 #ifdef BFRAMES_DEC_DEBUG  
         BFRAME_DEBUG  
 #endif  
                         switch (mb->mb_type) {  
1258                          case MODE_DIRECT:                          case MODE_DIRECT:
1259                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv);                                  get_b_motion_vector(bs, &mv, 1, zeromv);
1260    
1261                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
                                 {                               // Because this file is a C file not C++ so I use '{' to define var  
                                         const int64_t TRB = dec->time_pp - dec->time_bp, TRD =  
                                                 dec->time_pp;  
                                         int i;  
   
1262                                          for (i = 0; i < 4; i++) {                                          for (i = 0; i < 4; i++) {
1263                                                  mb->mvs[i].x =                                          mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + mv.x);
1264                                                          (int32_t) ((TRB * last_mb->mvs[i].x) / TRD +                                          mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1265                                                                             mb->mvs[0].x);                                                                          ? ((TRB - TRD) * last_mb->mvs[i].x) / TRD
1266                                                  mb->b_mvs[i].x =                                                                          : mb->mvs[i].x - last_mb->mvs[i].x);
1267                                                          (int32_t) ((mb->mvs[0].x ==                                          mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y);
1268                                                                                  0) ? ((TRB -                                          mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1269                                                                                             TRD) * last_mb->mvs[i].x) /                                                                          ? ((TRB - TRD) * last_mb->mvs[i].y) / TRD
1270                                                                             TRD : mb->mvs[i].x - last_mb->mvs[i].x);                                                                          : mb->mvs[i].y - last_mb->mvs[i].y);
                                                 mb->mvs[i].y =  
                                                         (int32_t) ((TRB * last_mb->mvs[i].y) / TRD +  
                                                                            mb->mvs[0].y);  
                                                 mb->b_mvs[i].y =  
                                                         (int32_t) ((mb->mvs[0].y ==  
                                                                                 0) ? ((TRB -  
                                                                                            TRD) * last_mb->mvs[i].y) /  
                                                                            TRD : mb->mvs[i].y - last_mb->mvs[i].y);  
                                         }  
                                         //DEBUG("B-frame Direct!\n");  
1271                                  }                                  }
1272                                  mb->mode = MODE_INTER4V;  
1273                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1274                                                                                             mb, x, y, mb->cbp, bs);                                                                                                  mb, x, y, bs, 1);
1275                                  break;                                  break;
1276    
1277                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1278                                  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);
1279                                                                          dec->p_fmv);                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1280                                  dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =  
1281                                          mb->mvs[0].x;                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv);
1282                                  dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];
                                         mb->mvs[0].y;  
   
                                 get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],  
                                                                         fcode_backward, dec->p_bmv);  
                                 dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x =  
                                         mb->b_mvs[3].x = mb->b_mvs[0].x;  
                                 dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y =  
                                         mb->b_mvs[3].y = mb->b_mvs[0].y;  
1283    
1284                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1285                                                                                             mb, x, y, mb->cbp, bs);                                                                                          mb, x, y, bs, 0);
                                 //DEBUG("B-frame Bidir!\n");  
1286                                  break;                                  break;
1287    
1288                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1289                                  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);
1290                                                                          dec->p_bmv);                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
                                 dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =  
                                         mb->mvs[0].x;  
                                 dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                         mb->mvs[0].y;  
1291    
1292                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);
                                 //DEBUG("B-frame Backward!\n");  
1293                                  break;                                  break;
1294    
1295                          case MODE_FORWARD:                          case MODE_FORWARD:
1296                                  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);
1297                                                                          dec->p_fmv);                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
                                 dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =  
                                         mb->mvs[0].x;  
                                 dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                         mb->mvs[0].y;  
1298    
1299                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
                                 //DEBUG("B-frame Forward!\n");  
1300                                  break;                                  break;
1301    
1302                          default:                          default:
1303                                  //DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);
                                 ;  
1304                          }                          }
1305                    } /* End of for */
                 }                                               // end of FOR  
1306          }          }
1307    
1308  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1309          if (!first){          if (!first){
1310                  first=1;                  first=1;
# Line 1264  Line 1314 
1314  #endif  #endif
1315  }  }
1316    
1317  // swap two MACROBLOCK array  /* perform post processing if necessary, and output the image */
1318  void  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1319  mb_swap(MACROBLOCK ** mb1,                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)
1320                  MACROBLOCK ** mb2)  {
1321  {          image_output(img, dec->width, dec->height,
1322          MACROBLOCK *temp = *mb1;                                   dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1323                                     frame->output.csp, dec->interlacing);
1324          *mb1 = *mb2;  
1325          *mb2 = temp;          if (stats) {
1326                    stats->type = coding2type(coding_type);
1327                    stats->data.vop.time_base = (int)dec->time_base;
1328                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1329            }
1330  }  }
1331    
1332    
1333  int  int
1334  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1335                             XVID_DEC_FRAME * frame)                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1336  {  {
1337    
1338          Bitstream bs;          Bitstream bs;
1339          uint32_t rounding;          uint32_t rounding;
1340            uint32_t reduced_resolution;
1341          uint32_t quant;          uint32_t quant;
1342          uint32_t fcode_forward;          uint32_t fcode_forward;
1343          uint32_t fcode_backward;          uint32_t fcode_backward;
1344          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1345          uint32_t vop_type;          WARPPOINTS gmc_warp;
1346            int coding_type;
1347            int success, output, seen_something;
1348    
1349            if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1350                    return XVID_ERR_VERSION;
1351    
1352          start_global_timer();          start_global_timer();
1353    
1354            dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1355            if ((frame->general & XVID_DISCONTINUITY))
1356                    dec->frames = 0;
1357            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1358    
1359            if (frame->length < 0) {        /* decoder flush */
1360                    int ret;
1361                    /* if not decoding "low_delay/packed", and this isn't low_delay and
1362                            we have a reference frame, then outout the reference frame */
1363                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1364                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1365                            dec->frames = 0;
1366                            ret = 0;
1367                    } else {
1368                            if (stats) stats->type = XVID_TYPE_NOTHING;
1369                            ret = XVID_ERR_END;
1370                    }
1371    
1372                    emms();
1373                    stop_global_timer();
1374                    return ret;
1375            }
1376    
1377          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1378    
1379          // add by chenm001 <chenm001@163.com>          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1380          // for support B-frame to reference last 2 frame          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1381          dec->frames++;          {
1382          vop_type =                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1383                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1384                                                           &fcode_backward, &intra_dc_threshold);                  if (stats) stats->type = XVID_TYPE_NOTHING;
1385                    emms();
1386                    return 1;       /* one byte consumed */
1387            }
1388    
1389          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0          success = 0;
1390            output = 0;
1391            seen_something = 0;
1392    
1393          switch (vop_type) {  repeat:
         case P_VOP:  
                 decoder_pframe(dec, &bs, rounding, quant, fcode_forward,  
                                            intra_dc_threshold);  
 #ifdef BFRAMES_DEC  
                 DEBUG1("P_VOP  Time=", dec->time);  
 #endif  
                 break;  
1394    
1395          case I_VOP:          coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1396                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
 #ifdef BFRAMES_DEC  
                 DEBUG1("I_VOP  Time=", dec->time);  
 #endif  
                 break;  
1397    
1398          case B_VOP:          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",
1399  #ifdef BFRAMES_DEC                                                          coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1400                  if (dec->time_pp > dec->time_bp) {  
1401                          DEBUG1("B_VOP  Time=", dec->time);          if (coding_type == -1) { /* nothing */
1402                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                  if (success) goto done;
1403                  } else {                  if (stats) stats->type = XVID_TYPE_NOTHING;
1404                          DEBUG("broken B-frame!");                  emms();
1405                    return BitstreamPos(&bs)/8;
1406                  }                  }
 #else  
                 image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);  
 #endif  
                 break;  
1407    
1408          case N_VOP:                             // vop not coded          if (coding_type == -2 || coding_type == -3) {   /* vol and/or resize */
1409                  // when low_delay==0, N_VOP's should interpolate between the past and future frames  
1410                    if (coding_type == -3)
1411                            decoder_resize(dec);
1412    
1413                    if (stats) {
1414                            stats->type = XVID_TYPE_VOL;
1415                            stats->data.vol.general = 0;
1416                            /*XXX: if (dec->interlacing)
1417                                    stats->data.vol.general |= ++INTERLACING; */
1418                            stats->data.vol.width = dec->width;
1419                            stats->data.vol.height = dec->height;
1420                            stats->data.vol.par = dec->aspect_ratio;
1421                            stats->data.vol.par_width = dec->par_width;
1422                            stats->data.vol.par_height = dec->par_height;
1423                            emms();
1424                            return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1425                    }
1426                    goto repeat;
1427            }
1428    
1429            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1430    
1431            /* packed_mode: special-N_VOP treament */
1432            if (dec->packed_mode && coding_type == N_VOP) {
1433                    if (dec->low_delay_default && dec->frames > 0) {
1434                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1435                            output = 1;
1436                    }
1437                    /* ignore otherwise */
1438            } else if (coding_type != B_VOP) {
1439                    switch(coding_type) {
1440                    case I_VOP :
1441                            decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1442                            break;
1443                    case P_VOP :
1444                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1445                                                    fcode_forward, intra_dc_threshold, NULL);
1446                            break;
1447                    case S_VOP :
1448                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1449                                                    fcode_forward, intra_dc_threshold, &gmc_warp);
1450                            break;
1451                    case N_VOP :
1452                            /* XXX: not_coded vops are not used for forward prediction */
1453                            /* we should not swap(last_mbs,mbs) */
1454                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1455                  break;                  break;
1456                    }
1457    
1458          default:                  if (reduced_resolution) {
1459                  return XVID_ERR_FAIL;                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1460                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1461                                    16, 0);
1462          }          }
1463    
1464          if (frame->length != BitstreamPos(&bs) / 8){                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1465                  DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8);                  if (!(dec->low_delay_default && dec->packed_mode)) {
1466                            if (dec->low_delay) {
1467                                    decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1468                                    output = 1;
1469                            } else if (dec->frames > 0)     { /* is the reference frame valid? */
1470                                    /* output the reference frame */
1471                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1472                                    output = 1;
1473                            }
1474          }          }
         frame->length = BitstreamPos(&bs) / 8;  
1475    
1476                    image_swap(&dec->refn[0], &dec->refn[1]);
1477                    image_swap(&dec->cur, &dec->refn[0]);
1478                    SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1479                    dec->last_reduced_resolution = reduced_resolution;
1480                    dec->last_coding_type = coding_type;
1481    
1482                    dec->frames++;
1483                    seen_something = 1;
1484    
1485            } else {        /* B_VOP */
1486    
 #ifdef BFRAMES_DEC  
         // test if no B_VOP  
1487          if (dec->low_delay) {          if (dec->low_delay) {
1488  #endif                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1489          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                          dec->low_delay = 1;
1490                                           frame->image, frame->stride, frame->colorspace);                  }
1491    
1492  #ifdef BFRAMES_DEC                  if (dec->frames < 2) {
1493                            /* attemping to decode a bvop without atleast 2 reference frames */
1494                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1495                                                    "broken b-frame, mising ref frames");
1496                    } else if (dec->time_pp <= dec->time_bp) {
1497                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1498                            decoded in vfw. */
1499                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1500                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1501          } else {          } else {
1502                  if (dec->frames >= 0) {                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
                         start_timer();  
                         if ((vop_type == I_VOP || vop_type == P_VOP)) {  
                                 image_output(&dec->refn[0], dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace);  
                         } else if (vop_type == B_VOP) {  
                                 image_output(&dec->cur, dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace);  
                         }  
                         stop_conv_timer();  
1503                  }                  }
1504    
1505                    decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1506                    output = 1;
1507                    dec->frames++;
1508          }          }
 #endif  
1509    
1510          if (vop_type == I_VOP || vop_type == P_VOP) {          BitstreamByteAlign(&bs);
1511                  image_swap(&dec->refn[0], &dec->refn[1]);  
1512                  image_swap(&dec->cur, &dec->refn[0]);          /* low_delay_default mode: repeat in packed_mode */
1513                  // swap MACROBLOCK          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
1514                  if (!dec->low_delay && vop_type == P_VOP)                  success = 1;
1515                          mb_swap(&dec->mbs, &dec->last_mbs);                  goto repeat;
1516            }
1517    
1518    done :
1519    
1520            /* low_delay_default mode: if we've gotten here without outputting anything,
1521               then output the recently decoded frame, or print an error message  */
1522            if (dec->low_delay_default && output == 0) {
1523                    if (dec->packed_mode && seen_something) {
1524                            /* output the recently decoded frame */
1525                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1526                    } else {
1527                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1528                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1529                                    "warning: nothing to output");
1530                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1531                                    "bframe decoder lag");
1532    
1533                            decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);
1534                            if (stats) stats->type = XVID_TYPE_NOTHING;
1535                    }
1536          }          }
1537    
1538          emms();          emms();
   
1539          stop_global_timer();          stop_global_timer();
1540    
1541          return XVID_ERR_OK;          return BitstreamPos(&bs) / 8;   /* number of bytes consumed */
1542  }  }

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.49.2.18

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