[svn] / trunk / xvidcore / src / decoder.c Repository:
ViewVC logotype

Diff of /trunk/xvidcore/src/decoder.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 830, Tue Feb 11 21:56:31 2003 UTC revision 851, Sat Feb 15 15:22:19 2003 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Decoder main module  -   *  -  Decoder main module  -
5   *   *
6   *  Copyright(C) 2002 MinChen <chenm001@163.com>   *  This program is an implementation of a part of one or more MPEG-4
7   *               2002 Peter Ross <pross@xvid.org>   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
8     *  to use this software module in hardware or software products are
9     *  advised that its use may infringe existing patents or copyrights, and
10     *  any such use would be at such party's own risk.  The original
11     *  developer of this software module and his/her company, and subsequent
12     *  editors and their companies, will have no liability for use of this
13     *  software or modifications or derivatives thereof.
14   *   *
15   *  This file is part of XviD, a free MPEG-4 video encoder/decoder   *  This program is free software; you can redistribute it and/or modify
16   *   *  it under the terms of the GNU General Public License as published by
  *  XviD is free software; you can redistribute it and/or modify it  
  *  under the terms of the GNU General Public License as published by  
17   *  the Free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation; either version 2 of the License, or
18   *  (at your option) any later version.   *  (at your option) any later version.
19   *   *
# Line 22  Line 26 
26   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28   *   *
29   *  Under section 8 of the GNU General Public License, the copyright   *************************************************************************/
30   *  holders of XVID explicitly forbid distribution in the following  
31   *  countries:  /**************************************************************************
  *  
  *    - Japan  
  *    - United States of America  
  *  
  *  Linking XviD statically or dynamically with other modules is making a  
  *  combined work based on XviD.  Thus, the terms and conditions of the  
  *  GNU General Public License cover the whole combination.  
32   *   *
33   *  As a special exception, the copyright holders of XviD give you   *  History:
  *  permission to link XviD with independent modules that communicate with  
  *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the  
  *  license terms of these independent modules, and to copy and distribute  
  *  the resulting combined work under terms of your choice, provided that  
  *  every copy of the combined work is accompanied by a complete copy of  
  *  the source code of XviD (the version of XviD used to produce the  
  *  combined work), being distributed under the terms of the GNU General  
  *  Public License plus this exception.  An independent module is a module  
  *  which is not derived from or based on XviD.  
34   *   *
35   *  Note that people who make modified versions of XviD are not obligated   *  15.07.2002  fix a bug in B-frame decode at DIRECT mode
36   *  to grant this special exception for their modified versions; it is   *              MinChen <chenm001@163.com>
37   *  their choice whether to do so.  The GNU General Public License gives   *  10.07.2002  added BFRAMES_DEC_DEBUG support
38   *  permission to release a modified version without this exception; this   *              Fix a little bug for low_delay flage
39   *  exception also makes it possible to release a modified version which   *              MinChen <chenm001@163.com>
40   *  carries forward this exception.   *  28.06.2002  added basic resync support to iframe/pframe_decode()
41     *  22.06.2002  added primative N_VOP support
42     *                              #define BFRAMES_DEC now enables Minchen's bframe decoder
43     *  08.05.2002  add low_delay support for B_VOP decode
44     *              MinChen <chenm001@163.com>
45     *  05.05.2002  fix some B-frame decode problem
46     *  02.05.2002  add B-frame decode support(have some problem);
47     *              MinChen <chenm001@163.com>
48     *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>
49     *  29.03.2002  interlacing fix - compensated block wasn't being used when
50     *              reconstructing blocks, thus artifacts
51     *              interlacing speedup - used transfers to re-interlace
52     *              interlaced decoding should be as fast as progressive now
53     *  26.03.2002  interlacing support - moved transfers outside decode loop
54     *  26.12.2001  decoder_mbinter: dequant/idct moved within if(coded) block
55     *  22.12.2001  lock based interpolation
56     *  01.12.2001  inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>
57   *   *
58   * $Id: decoder.c,v 1.46 2003-02-11 21:56:31 edgomez Exp $   *  $Id: decoder.c,v 1.47 2003-02-15 15:22:17 edgomez Exp $
59   *   *
60   *************************************************************************/   *************************************************************************/
61    
62    #include <stdio.h>
63  #include <stdlib.h>  #include <stdlib.h>
64  #include <string.h>  #include <string.h>
65    
# Line 64  Line 69 
69    
70  #include "xvid.h"  #include "xvid.h"
71  #include "portab.h"  #include "portab.h"
72    #include "global.h"
73    
74  #include "decoder.h"  #include "decoder.h"
75  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 75  Line 81 
81  #include "dct/fdct.h"  #include "dct/fdct.h"
82  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
83  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
84    #include "image/reduced.h"
85    #include "image/font.h"
86    
87  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
88  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
89  #include "utils/timer.h"  #include "utils/timer.h"
90  #include "utils/emms.h"  #include "utils/emms.h"
91    #include "motion/motion.h"
92    
93  #include "image/image.h"  #include "image/image.h"
94  #include "image/colorspace.h"  #include "image/colorspace.h"
95  #include "utils/mem_align.h"  #include "utils/mem_align.h"
96    
97  int  int
98  decoder_create(XVID_DEC_PARAM * param)  decoder_resize(DECODER * dec)
99  {  {
100          DECODER *dec;          /* free existing */
101    
102          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
103          if (dec == NULL) {          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
104                  return XVID_ERR_MEMORY;          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
105          }          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
106          param->handle = dec;          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
107    
108          dec->width = param->width;          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
109          dec->height = param->height;  
110            if (dec->last_mbs)
111                    xvid_free(dec->last_mbs);
112            if (dec->mbs)
113                    xvid_free(dec->mbs);
114    
115            /* realloc */
116    
117          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
118          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
119    
120          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
121          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
         dec->low_delay = 0;  
122    
123          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
124                  xvid_free(dec);                  xvid_free(dec);
# Line 116  Line 130 
130                  xvid_free(dec);                  xvid_free(dec);
131                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
132          }          }
133          /* add by chenm001 <chenm001@163.com> */  
134          /* for support B-frame to reference last 2 frame */          // add by chenm001 <chenm001@163.com>
135            // for support B-frame to reference last 2 frame
136          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
137                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
138                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
139                  xvid_free(dec);                  xvid_free(dec);
140                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
141          }          }
142          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
143                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
144                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
145                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
146                    xvid_free(dec);
147                    return XVID_ERR_MEMORY;
148            }
149    
150            if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
151                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
152                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
153                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
154                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
155                    xvid_free(dec);
156                    return XVID_ERR_MEMORY;
157            }
158    
159            if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
160                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
161                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
162                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
163                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
164                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
165                  xvid_free(dec);                  xvid_free(dec);
166                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
167          }          }
# Line 139  Line 173 
173                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
174                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
175                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
176                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
177                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
178                  xvid_free(dec);                  xvid_free(dec);
179                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
180          }          }
   
181          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
182    
183          /* add by chenm001 <chenm001@163.com> */          // add by chenm001 <chenm001@163.com>
184          /* for skip MB flag */          // for skip MB flag
185          dec->last_mbs =          dec->last_mbs =
186                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
187                                          CACHE_LINE);                                          CACHE_LINE);
# Line 156  Line 190 
190                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
191                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
192                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
193                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
194                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
195                  xvid_free(dec);                  xvid_free(dec);
196                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
197          }          }
198    
199          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);
200    
201            return XVID_ERR_OK;
202    }
203    
204    
205    int
206    decoder_create(XVID_DEC_PARAM * param)
207    {
208            DECODER *dec;
209    
210            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
211            if (dec == NULL) {
212                    return XVID_ERR_MEMORY;
213            }
214            memset(dec, 0, sizeof(DECODER));
215    
216            param->handle = dec;
217    
218            dec->width = param->width;
219            dec->height = param->height;
220    
221            image_null(&dec->cur);
222            image_null(&dec->refn[0]);
223            image_null(&dec->refn[1]);
224            image_null(&dec->tmp);
225            image_null(&dec->qtmp);
226    
227    /* image based GMC */
228            image_null(&dec->gmc);
229    
230    
231            dec->mbs = NULL;
232            dec->last_mbs = NULL;
233    
234          init_timer();          init_timer();
235    
236          /* add by chenm001 <chenm001@163.com> */          // add by chenm001 <chenm001@163.com>
237          /* for support B-frame to save reference frame's time */          // for support B-frame to save reference frame's time
238          dec->frames = -1;          dec->frames = 0;
239          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
240            dec->low_delay = 0;
241            dec->packed_mode = 0;
242    
243            dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
244    
245            if (dec->fixed_dimensions)
246                    return decoder_resize(dec);
247            else
248          return XVID_ERR_OK;          return XVID_ERR_OK;
249  }  }
250    
# Line 179  Line 254 
254  {  {
255          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
256          xvid_free(dec->mbs);          xvid_free(dec->mbs);
257    
258            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          /* image based GMC */
259    
260          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
261          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
262          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
263            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
264          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
265          xvid_free(dec);          xvid_free(dec);
266    
# Line 198  Line 277 
277    
278    
279    
280  /* decode an intra macroblock */  // decode an intra macroblock
281    
282  void  void
283  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
# Line 210  Line 289 
289                                  Bitstream * bs,                                  Bitstream * bs,
290                                  const uint32_t quant,                                  const uint32_t quant,
291                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
292                                  const unsigned int bound)                                  const unsigned int bound,
293                                    const int reduced_resolution)
294  {  {
295    
296          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 223  Line 303 
303          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
304          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
305    
306            if (reduced_resolution) {
307                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
308                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
309                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
310            }else{
311          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
312          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
313          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
314            }
315    
316          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear
317    
318          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
319                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
# Line 250  Line 336 
336                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;
337    
338                          if (dc_size > 8) {                          if (dc_size > 8) {
339                                  BitstreamSkip(bs, 1);   /* marker */                                  BitstreamSkip(bs, 1);   // marker
340                          }                          }
341    
342                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
# Line 262  Line 348 
348                  }                  }
349    
350                  start_timer();                  start_timer();
351                  if (cbp & (1 << (5 - i)))       /* coded */                  if (cbp & (1 << (5 - i)))       // coded
352                  {                  {
353                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],                          int direction = dec->alternate_vertical_scan ?
354                                                          start_coeff);                                  2 : pMB->acpred_directions[i];
355    
356                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
357                  }                  }
358                  stop_coding_timer();                  stop_coding_timer();
359    
# Line 284  Line 372 
372                  start_timer();                  start_timer();
373                  idct(&data[i * 64]);                  idct(&data[i * 64]);
374                  stop_idct_timer();                  stop_idct_timer();
375    
376          }          }
377    
378          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 292  Line 381 
381          }          }
382    
383          start_timer();          start_timer();
384    
385            if (reduced_resolution)
386            {
387                    next_block*=2;
388                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
389                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
390                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
391                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
392                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
393                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
394            }else{
395          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
396          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
397          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
398          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
399          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
400          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
401            }
402          stop_transfer_timer();          stop_transfer_timer();
403  }  }
404    
405    
406    
407    
408    // decode an inter macroblock
 #define SIGN(X) (((X)>0)?1:-1)  
 #define ABS(X) (((X)>0)?(X):-(X))  
 static const uint32_t roundtab[16] =  
         { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };  
   
   
 /* decode an inter macroblock */  
409    
410  void  void
411  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
412                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
413                                  const uint32_t x_pos,                                  const uint32_t x_pos,
414                                  const uint32_t y_pos,                                  const uint32_t y_pos,
415                                  const uint32_t acpred_flag,                                  const uint32_t fcode,
416                                  const uint32_t cbp,                                  const uint32_t cbp,
417                                  Bitstream * bs,                                  Bitstream * bs,
418                                  const uint32_t quant,                                  const uint32_t quant,
419                                  const uint32_t rounding)                                  const uint32_t rounding,
420                                    const int reduced_resolution)
421  {  {
422    
423          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 330  Line 425 
425    
426          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
427          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
428          uint32_t next_block = stride * 8;          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
429          uint32_t i;          uint32_t i;
430          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
431          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
432    
433          int uv_dx, uv_dy;          int uv_dx, uv_dy;
434            VECTOR mv[4];   /* local copy of mvs */
435    
436            if (reduced_resolution) {
437                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
438                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
439                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
440                    for (i = 0; i < 4; i++) {
441                            mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
442                            mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
443                    }
444            } else {
445          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
446          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
447          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
448                    for (i = 0; i < 4; i++)
449                            mv[i] = pMB->mvs[i];
450            }
451    
452          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
453    
454                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  uv_dx = mv[0].x / (1 + dec->quarterpel);
455                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  uv_dy = mv[0].y / (1 + dec->quarterpel);
         } else {  
                 int sum;  
                 sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
456    
457                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
458                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
459    
460                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  start_timer();
461                    if (reduced_resolution)
462                    {
463                            interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
464                                                                      mv[0].x, mv[0].y, stride,  rounding);
465                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
466                                                                      uv_dx, uv_dy, stride2, rounding);
467                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
468                                                                      uv_dx, uv_dy, stride2, rounding);
469    
470                    }
471                    else
472                    {
473                            if(dec->quarterpel) {
474                                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
475                                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
476                                                                                            mv[0].x, mv[0].y, stride, rounding);
477                            }
478                            else {
479                                    interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
480                                                                              mv[0].x, mv[0].y, stride, rounding);
481                            }
482    
483                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
484                                                                      uv_dx, uv_dy, stride2, rounding);
485                            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
486                                                                      uv_dx, uv_dy, stride2, rounding);
487          }          }
488                    stop_comp_timer();
489    
490            } else {        /* MODE_INTER4V */
491                    int sum;
492    
493                    if(dec->quarterpel)
494                            sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
495                    else
496                            sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
497    
498                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
499    
500                    if(dec->quarterpel)
501                            sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
502                    else
503                            sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
504    
505                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
506    
507          start_timer();          start_timer();
508                    if (reduced_resolution)
509                    {
510                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
511                                                                      mv[0].x, mv[0].y, stride,  rounding);
512                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
513                                                                      mv[1].x, mv[1].y, stride,  rounding);
514                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos, 32*y_pos + 16,
515                                                                      mv[2].x, mv[2].y, stride,  rounding);
516                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos + 16,
517                                                                      mv[3].x, mv[3].y, stride,  rounding);
518                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u , 16 * x_pos, 16 * y_pos,
519                                                                      uv_dx, uv_dy, stride2, rounding);
520                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,
521                                                                      uv_dx, uv_dy, stride2, rounding);
522    
523                            // set_block(pY_Cur, stride, 32, 32, 127);
524                    }
525                    else
526                    {
527                            if(dec->quarterpel) {
528                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
529                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
530                                                                                      mv[0].x, mv[0].y, stride,  rounding);
531                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
532                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
533                                                                                      mv[1].x, mv[1].y, stride,  rounding);
534                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
535                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
536                                                                                      mv[2].x, mv[2].y, stride,  rounding);
537                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
538                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
539                                                                                      mv[3].x, mv[3].y, stride,  rounding);
540                            }
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, 16*y_pos,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
545                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                                                                            mv[1].x, mv[1].y, stride,  rounding);
546          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
547                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);                                                                            mv[2].x, mv[2].y, stride,  rounding);
548          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,
549                                                    pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
550                            }
551    
552          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
553                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
554          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
555                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
556                    }
557          stop_comp_timer();          stop_comp_timer();
558            }
559    
560          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
561                  if (cbp & (1 << (5 - i)))       /* coded */                  int direction = dec->alternate_vertical_scan ? 2 : 0;
562    
563                    if (cbp & (1 << (5 - i)))       // coded
564                  {                  {
565                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
566    
567                          start_timer();                          start_timer();
568                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
569                          stop_coding_timer();                          stop_coding_timer();
570    
571                          start_timer();                          start_timer();
# Line 401  Line 588 
588          }          }
589    
590          start_timer();          start_timer();
591            if (reduced_resolution)
592            {
593                    if (cbp & 32)
594                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
595                    if (cbp & 16)
596                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
597                    if (cbp & 8)
598                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
599                    if (cbp & 4)
600                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
601                    if (cbp & 2)
602                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
603                    if (cbp & 1)
604                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
605            }
606            else
607            {
608                    if (cbp & 32)
609                            transfer_16to8add(pY_Cur, &data[0 * 64], stride);
610                    if (cbp & 16)
611                            transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
612                    if (cbp & 8)
613                            transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
614                    if (cbp & 4)
615                            transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
616                    if (cbp & 2)
617                            transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
618                    if (cbp & 1)
619                            transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
620            }
621            stop_transfer_timer();
622    }
623    
624    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
625    {
626            int length = 1 << (fcode+4);
627    
628    //      if (quarterpel) value *= 2;
629    
630            if (value < -length)
631                    return -length;
632            else if (value >= length)
633                    return length-1;
634            else return value;
635    }
636    
637    
638    static void
639    decoder_mbgmc(DECODER * dec,
640                                    MACROBLOCK * const pMB,
641                                    const uint32_t x_pos,
642                                    const uint32_t y_pos,
643                                    const uint32_t fcode,
644                                    const uint32_t cbp,
645                                    Bitstream * bs,
646                                    const uint32_t quant,
647                                    const uint32_t rounding,
648                                    const int reduced_resolution)   /* no reduced res support */
649    {
650    
651            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
652            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
653    
654            const uint32_t stride = dec->edged_width;
655            const uint32_t stride2 = stride / 2;
656            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
657            uint32_t i;
658            const uint32_t iQuant = pMB->quant;
659            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
660            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
661            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
662    
663            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
664    
665            start_timer();
666    
667    /* this is where the calculations are done */
668    
669            {
670                    pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,
671                                            stride, stride2, dec->quarterpel, rounding, &dec->cur);
672    
673                    pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
674                    pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
675            }
676            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
677    
678    /*      transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
679            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
680            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
681    */
682    
683    
684            stop_transfer_timer();
685    
686            if (!cbp) return;
687    
688            for (i = 0; i < 6; i++) {
689                    int direction = dec->alternate_vertical_scan ? 2 : 0;
690    
691                    if (cbp & (1 << (5 - i)))       // coded
692                    {
693                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
694    
695                            start_timer();
696                            get_inter_block(bs, &block[i * 64], direction);
697                            stop_coding_timer();
698    
699                            start_timer();
700                            if (dec->quant_type == 0) {
701                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
702                            } else {
703                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
704                            }
705                            stop_iquant_timer();
706    
707                            start_timer();
708                            idct(&data[i * 64]);
709                            stop_idct_timer();
710                    }
711            }
712    
713    /* interlace + GMC is this possible ??? */
714    /*      if (dec->interlacing && pMB->field_dct) {
715                    next_block = stride;
716                    stride *= 2;
717            }
718    */
719            start_timer();
720          if (cbp & 32)          if (cbp & 32)
721                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);
722          if (cbp & 16)          if (cbp & 16)
# Line 420  Line 736 
736  void  void
737  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
738                             Bitstream * bs,                             Bitstream * bs,
739                               int reduced_resolution,
740                             int quant,                             int quant,
741                             int intra_dc_threshold)                             int intra_dc_threshold)
742  {  {
743          uint32_t bound;          uint32_t bound;
744          uint32_t x, y;          uint32_t x, y;
745            uint32_t mb_width = dec->mb_width;
746            uint32_t mb_height = dec->mb_height;
747    
748            if (reduced_resolution)
749            {
750                    mb_width = (dec->width + 31) / 32;
751                    mb_height = (dec->height + 31) / 32;
752            }
753    
754          bound = 0;          bound = 0;
755    
756          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
757                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
758                          MACROBLOCK *mb;                          MACROBLOCK *mb;
759                          uint32_t mcbpc;                          uint32_t mcbpc;
760                          uint32_t cbpc;                          uint32_t cbpc;
# Line 442  Line 767 
767    
768                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
769                          {                          {
770                                  bound = read_video_packet_header(bs, 0, &quant);                                  bound = read_video_packet_header(bs, dec, 0,
771                                  x = bound % dec->mb_width;                                                          &quant, NULL, NULL, &intra_dc_threshold);
772                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
773                                    y = bound / mb_width;
774                          }                          }
775                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
776    
# Line 475  Line 801 
801    
802                          if (dec->interlacing) {                          if (dec->interlacing) {
803                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
804                                  DPRINTF(DPRINTF_DEBUG, "deci: field_dct: %d", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
805                          }                          }
806    
807                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
808                                                          intra_dc_threshold, bound);                                                          intra_dc_threshold, bound, reduced_resolution);
809    
810                  }                  }
811                  if(dec->out_frm)                  if(dec->out_frm)
812                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
   
813          }          }
814    
815  }  }
# Line 495  Line 821 
821                                    int x,                                    int x,
822                                    int y,                                    int y,
823                                    int k,                                    int k,
824                                    VECTOR * mv,                                    VECTOR * ret_mv,
825                                    int fcode,                                    int fcode,
826                                    const int bound)                                    const int bound)
827  {  {
# Line 506  Line 832 
832          int range = (64 * scale_fac);          int range = (64 * scale_fac);
833    
834          VECTOR pmv;          VECTOR pmv;
835          int mv_x, mv_y;          VECTOR mv;
836    
837          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
838    
839          mv_x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
840          mv_y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
841    
842          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
843    
844          mv_x += pmv.x;          mv.x += pmv.x;
845          mv_y += pmv.y;          mv.y += pmv.y;
846    
847          if (mv_x < low) {          if (mv.x < low) {
848                  mv_x += range;                  mv.x += range;
849          } else if (mv_x > high) {          } else if (mv.x > high) {
850                  mv_x -= range;                  mv.x -= range;
851          }          }
852    
853          if (mv_y < low) {          if (mv.y < low) {
854                  mv_y += range;                  mv.y += range;
855          } else if (mv_y > high) {          } else if (mv.y > high) {
856                  mv_y -= range;                  mv.y -= range;
857          }          }
858    
859          mv->x = mv_x;          ret_mv->x = mv.x;
860          mv->y = mv_y;          ret_mv->y = mv.y;
   
861  }  }
862    
863    
864    
865    
866    
867    /* for P_VOP set gmc_warp to NULL */
868  void  void
869  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
870                             Bitstream * bs,                             Bitstream * bs,
871                             int rounding,                             int rounding,
872                               int reduced_resolution,
873                             int quant,                             int quant,
874                             int fcode,                             int fcode,
875                             int intra_dc_threshold)                             int intra_dc_threshold,
876                               const WARPPOINTS *const gmc_warp)
877  {  {
878    
879          uint32_t x, y;          uint32_t x, y;
880          uint32_t bound;          uint32_t bound;
881          int cp_mb, st_mb;          int cp_mb, st_mb;
882            uint32_t mb_width = dec->mb_width;
883            uint32_t mb_height = dec->mb_height;
884    
885            if (reduced_resolution)
886            {
887                    mb_width = (dec->width + 31) / 32;
888                    mb_height = (dec->height + 31) / 32;
889            }
890    
891          start_timer();          start_timer();
892          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
893                                     dec->width, dec->height);                                     dec->width, dec->height);
894          stop_edges_timer();          stop_edges_timer();
895    
896            if (gmc_warp)
897            {
898    
899                    // accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16
900                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
901                    {
902                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
903                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
904                                    dec->sprite_warping_points);
905                    }
906    
907                    generate_GMCparameters( dec->sprite_warping_points,
908                                    (2 << dec->sprite_warping_accuracy), gmc_warp,
909                                    dec->width, dec->height, &dec->gmc_data);
910    
911    /* image warping is done block-based  in decoder_mbgmc(), now */
912    /*
913            generate_GMCimage(&dec->gmc_data, &dec->refn[0],
914                                            mb_width, mb_height,
915                                            dec->edged_width, dec->edged_width/2,
916                                            fcode, dec->quarterpel, 0,
917                                            rounding, dec->mbs, &dec->gmc);
918    */
919            }
920    
921          bound = 0;          bound = 0;
922    
923          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
924                  cp_mb = st_mb = 0;                  cp_mb = st_mb = 0;
925                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
926                          MACROBLOCK *mb;                          MACROBLOCK *mb;
927    
928                          /* skip stuffing */                          // skip stuffing
929                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
930                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
931    
932                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1))
933                          {                          {
934                                  bound = read_video_packet_header(bs, fcode - 1, &quant);                                  bound = read_video_packet_header(bs, dec, fcode - 1,
935                                  x = bound % dec->mb_width;                                          &quant, &fcode, NULL, &intra_dc_threshold);
936                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
937                                    y = bound / mb_width;
938                          }                          }
939                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
940    
941                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
942    
943                          /*if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))          not_coded */                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded
944                          if (!(BitstreamGetBit(bs)))     /* not_coded */                          if (!(BitstreamGetBit(bs)))     // block _is_ coded
945                          {                          {
946                                  uint32_t mcbpc;                                  uint32_t mcbpc;
947                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 584  Line 949 
949                                  uint32_t cbpy;                                  uint32_t cbpy;
950                                  uint32_t cbp;                                  uint32_t cbp;
951                                  uint32_t intra;                                  uint32_t intra;
952                                    int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC
953    
954                                  cp_mb++;                                  cp_mb++;
955                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
# Line 600  Line 966 
966                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
967                                  }                                  }
968    
969                                    if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
970                                    {
971                                            mcsel = BitstreamGetBit(bs);
972                                    }
973    
974                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
975                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
976    
977                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
978    
# Line 621  Line 992 
992                                  if (dec->interlacing) {                                  if (dec->interlacing) {
993                                          if (cbp || intra) {                                          if (cbp || intra) {
994                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
995                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_dct: %d", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
996                                          }                                          }
997    
998                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
999                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
1000                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_pred: %d", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
1001    
1002                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
1003                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
1004                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_top: %d", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
1005                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
1006                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_bot: %d", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
1007                                                  }                                                  }
1008                                          }                                          }
1009                                  }                                  }
1010    
1011                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
1012                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
1013                                                                    rounding, reduced_resolution);
1014                                            continue;
1015    
1016                                    } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
1017    
1018                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
1019                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
1020                                                                                    fcode, bound);                                                                                    fcode, bound);
# Line 646  Line 1023 
1023                                          } else {                                          } else {
1024                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
1025                                                                                    fcode, bound);                                                                                    fcode, bound);
1026                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
                                                         mb->mvs[0].x;  
                                                 mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                                         mb->mvs[0].y;  
1027                                          }                                          }
1028                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
1029    
# Line 657  Line 1031 
1031                                          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);
1032                                          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);
1033                                          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);
1034                                  } else                  /* MODE_INTRA, MODE_INTRA_Q */                                  } else                  // MODE_INTRA, MODE_INTRA_Q
1035                                  {                                  {
1036                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
1037                                                  0;                                                  0;
1038                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
1039                                                  0;                                                  0;
1040                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
1041                                                                          intra_dc_threshold, bound);                                                                          intra_dc_threshold, bound, reduced_resolution);
1042                                          continue;                                          continue;
1043                                  }                                  }
1044    
1045                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
1046                                                                  rounding);                                                                  rounding, reduced_resolution);
                         } else                          /* not coded */  
                         {  
                                 DPRINTF(DPRINTF_DEBUG, "P-frame MB at (X,Y)=(%d,%d)", x, y);  
   
                                 mb->mode = MODE_NOT_CODED;  
                                 mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;  
                                 mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  
1047    
1048                                  /* copy macroblock directly from ref to cur */                          }
1049                            else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
1050                            {
1051                                    mb->mode = MODE_NOT_CODED_GMC;
1052    
1053                                  start_timer();                                  start_timer();
1054    
1055                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1056                                                                   (16 * x),                                                                  rounding, reduced_resolution);
                                                                  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);  
1057    
                                 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);  
1058                                  stop_transfer_timer();                                  stop_transfer_timer();
1059    
1060                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
1061                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1062                                    cp_mb = 0;                                    cp_mb = 0;
1063                                  }                                  }
1064                                  st_mb = x+1;                                  st_mb = x+1;
1065                          }                          }
1066                  }                          else    /* not coded P_VOP macroblock */
1067                  if(dec->out_frm && cp_mb > 0)                          {
1068                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                                  mb->mode = MODE_NOT_CODED;
1069          }  
1070  }                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
1071                                    mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
1072                                    // copy macroblock directly from ref to cur
1073    
1074                                    start_timer();
1075    
1076                                    if (reduced_resolution)
1077                                    {
1078                                            transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
1079                                                                             dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
1080                                                                             dec->edged_width);
1081    
1082                                            transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
1083                                                                            dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),
1084                                                                            dec->edged_width/2);
1085    
1086                                            transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),
1087                                                                             dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
1088                                                                             dec->edged_width/2);
1089                                    }
1090                                    else
1091                                    {
1092                                            transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
1093                                                                             dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
1094                                                                             dec->edged_width);
1095    
1096                                            transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
1097                                                                            dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
1098                                                                            dec->edged_width/2);
1099    
1100                                            transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
1101                                                                             dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
1102                                                                             dec->edged_width/2);
1103                                    }
1104    
1105                                    stop_transfer_timer();
1106    
1107                                    if(dec->out_frm && cp_mb > 0) {
1108                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1109                                      cp_mb = 0;
1110                                    }
1111                                    st_mb = x+1;
1112                            }
1113                    }
1114                    if(dec->out_frm && cp_mb > 0)
1115                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1116            }
1117    }
1118    
1119    
1120    // add by MinChen <chenm001@163.com>
1121    // decode B-frame motion vector
1122    void
1123    get_b_motion_vector(DECODER * dec,
1124                                            Bitstream * bs,
1125                                            int x,
1126                                            int y,
1127                                            VECTOR * mv,
1128                                            int fcode,
1129                                            const VECTOR pmv)
1130    {
1131            int scale_fac = 1 << (fcode - 1);
1132            int high = (32 * scale_fac) - 1;
1133            int low = ((-32) * scale_fac);
1134            int range = (64 * scale_fac);
1135    
1136            int mv_x, mv_y;
1137            int pmv_x, pmv_y;
1138    
1139            pmv_x = pmv.x;
1140            pmv_y = pmv.y;
1141    
1142            mv_x = get_mv(bs, fcode);
1143            mv_y = get_mv(bs, fcode);
1144    
1145            mv_x += pmv_x;
1146            mv_y += pmv_y;
1147    
1148            if (mv_x < low) {
1149                    mv_x += range;
1150            } else if (mv_x > high) {
1151                    mv_x -= range;
1152            }
1153    
1154            if (mv_y < low) {
1155                    mv_y += range;
1156            } else if (mv_y > high) {
1157                    mv_y -= range;
1158            }
1159    
1160            mv->x = mv_x;
1161            mv->y = mv_y;
1162    }
1163    
1164    
1165    // add by MinChen <chenm001@163.com>
1166    // decode an B-frame forward & backward inter macroblock
1167    void
1168    decoder_bf_mbinter(DECODER * dec,
1169                                       const MACROBLOCK * pMB,
1170                                       const uint32_t x_pos,
1171                                       const uint32_t y_pos,
1172                                       const uint32_t cbp,
1173                                       Bitstream * bs,
1174                                       const uint32_t quant,
1175                                       const uint8_t ref)
1176    {
1177    
1178            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1179            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1180    
1181            uint32_t stride = dec->edged_width;
1182            uint32_t stride2 = stride / 2;
1183            uint32_t next_block = stride * 8;
1184            uint32_t i;
1185            uint32_t iQuant = pMB->quant;
1186            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1187            int uv_dx, uv_dy;
1188    
1189            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1190            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1191            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1192    
1193    
1194            if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1195                    uv_dx = pMB->mvs[0].x;
1196                    uv_dy = pMB->mvs[0].y;
1197    
1198                    if (dec->quarterpel)
1199                    {
1200                            uv_dx /= 2;
1201                            uv_dy /= 2;
1202                    }
1203    
1204                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1205                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1206            } else {
1207                    int sum;
1208    
1209                    if(dec->quarterpel)
1210                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1211                    else
1212                            sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1213    
1214  /* swap two MACROBLOCK array */                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1215    
1216                    if(dec->quarterpel)
1217                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1218                    else
1219                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1220    
1221                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1222            }
1223    
1224            start_timer();
1225            if(dec->quarterpel) {
1226                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1227                                                                        dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1228                                                                        pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1229            }
1230            else {
1231                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
1232                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1233                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,
1234                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1235                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,
1236                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1237                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1238                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1239            }
1240    
1241            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
1242                                                      uv_dx, uv_dy, stride2, 0);
1243            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
1244                                                      uv_dx, uv_dy, stride2, 0);
1245            stop_comp_timer();
1246    
1247            for (i = 0; i < 6; i++) {
1248                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1249    
1250                    if (cbp & (1 << (5 - i)))       // coded
1251                    {
1252                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1253    
1254                            start_timer();
1255                            get_inter_block(bs, &block[i * 64], direction);
1256                            stop_coding_timer();
1257    
1258                            start_timer();
1259                            if (dec->quant_type == 0) {
1260                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1261                            } else {
1262                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1263                            }
1264                            stop_iquant_timer();
1265    
1266                            start_timer();
1267                            idct(&data[i * 64]);
1268                            stop_idct_timer();
1269                    }
1270            }
1271    
1272            if (dec->interlacing && pMB->field_dct) {
1273                    next_block = stride;
1274                    stride *= 2;
1275            }
1276    
1277            start_timer();
1278            if (cbp & 32)
1279                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1280            if (cbp & 16)
1281                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1282            if (cbp & 8)
1283                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1284            if (cbp & 4)
1285                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1286            if (cbp & 2)
1287                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1288            if (cbp & 1)
1289                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1290            stop_transfer_timer();
1291    }
1292    
1293    // add by MinChen <chenm001@163.com>
1294    // decode an B-frame direct &  inter macroblock
1295    void
1296    decoder_bf_interpolate_mbinter(DECODER * dec,
1297                                                               IMAGE forward,
1298                                                               IMAGE backward,
1299                                                               const MACROBLOCK * pMB,
1300                                                               const uint32_t x_pos,
1301                                                               const uint32_t y_pos,
1302                                                               Bitstream * bs)
1303    {
1304    
1305            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1306            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1307    
1308            uint32_t stride = dec->edged_width;
1309            uint32_t stride2 = stride / 2;
1310            uint32_t next_block = stride * 8;
1311            uint32_t iQuant = pMB->quant;
1312            int uv_dx, uv_dy;
1313            int b_uv_dx, b_uv_dy;
1314            uint32_t i;
1315            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1316        const uint32_t cbp = pMB->cbp;
1317    
1318            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1319            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1320            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1321    
1322    
1323            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1324                    uv_dx = pMB->mvs[0].x;
1325                    uv_dy = pMB->mvs[0].y;
1326    
1327                    b_uv_dx = pMB->b_mvs[0].x;
1328                    b_uv_dy = pMB->b_mvs[0].y;
1329    
1330                    if (dec->quarterpel)
1331                    {
1332                            uv_dx /= 2;
1333                            uv_dy /= 2;
1334    
1335                            b_uv_dx /= 2;
1336                            b_uv_dy /= 2;
1337                    }
1338    
1339                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1340                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1341    
1342                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1343                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1344            } else {
1345                    int sum;
1346    
1347                    if(dec->quarterpel)
1348                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1349                    else
1350                            sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1351    
1352                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1353    
1354                    if(dec->quarterpel)
1355                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1356                    else
1357                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1358    
1359                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1360    
1361    
1362                    if(dec->quarterpel)
1363                            sum = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);
1364                    else
1365                            sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1366    
1367                    b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1368    
1369                    if(dec->quarterpel)
1370                            sum = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);
1371                    else
1372                            sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1373    
1374                    b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1375            }
1376    
1377    
1378            start_timer();
1379            if(dec->quarterpel) {
1380                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1381                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1382                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1383                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1384                    else {
1385                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1386                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1387                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1388                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1389                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1390                                                                                pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1391                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1392                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1393                                                                                pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1394                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1395                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1396                                                                                pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1397                    }
1398            }
1399            else {
1400                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1401                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1402                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
1403                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1404                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1405                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1406                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1407                                                              16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1408                                                              0);
1409            }
1410    
1411            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1412                                                      uv_dy, stride2, 0);
1413            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1414                                                      uv_dy, stride2, 0);
1415    
1416    
1417            if(dec->quarterpel) {
1418                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1419                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1420                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1421                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1422                    else {
1423                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1424                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1425                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1426                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1427                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1428                                                                                pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1429                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1430                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1431                                                                                pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1432                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1433                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1434                                                                                pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1435                    }
1436            }
1437            else {
1438                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1439                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1440                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1441                                                              16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1442                                                              0);
1443                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1444                                                              16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1445                                                              stride, 0);
1446                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1447                                                              16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1448                                                              stride, 0);
1449            }
1450    
1451            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1452                                                      b_uv_dx, b_uv_dy, stride2, 0);
1453            interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1454                                                      b_uv_dx, b_uv_dy, stride2, 0);
1455    
1456            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1457                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1458                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1459                                                    stride, 1, 8);
1460    
1461            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1462                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1463                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1464                                                    stride, 1, 8);
1465    
1466            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1467                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1468                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1469                                                    stride, 1, 8);
1470    
1471            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1472                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1473                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1474                                                    stride, 1, 8);
1475    
1476            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1477                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1478                                                    dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1479                                                    stride2, 1, 8);
1480    
1481            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1482                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1483                                                    dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1484                                                    stride2, 1, 8);
1485    
1486            stop_comp_timer();
1487    
1488            for (i = 0; i < 6; i++) {
1489                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1490    
1491                    if (cbp & (1 << (5 - i)))       // coded
1492                    {
1493                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1494    
1495                            start_timer();
1496                            get_inter_block(bs, &block[i * 64], direction);
1497                            stop_coding_timer();
1498    
1499                            start_timer();
1500                            if (dec->quant_type == 0) {
1501                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1502                            } else {
1503                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1504                            }
1505                            stop_iquant_timer();
1506    
1507                            start_timer();
1508                            idct(&data[i * 64]);
1509                            stop_idct_timer();
1510                    }
1511            }
1512    
1513            if (dec->interlacing && pMB->field_dct) {
1514                    next_block = stride;
1515                    stride *= 2;
1516            }
1517    
1518            start_timer();
1519            if (cbp & 32)
1520                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1521            if (cbp & 16)
1522                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1523            if (cbp & 8)
1524                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1525            if (cbp & 4)
1526                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1527            if (cbp & 2)
1528                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1529            if (cbp & 1)
1530                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1531            stop_transfer_timer();
1532    }
1533    
1534    
1535    // add by MinChen <chenm001@163.com>
1536    // for decode B-frame dbquant
1537    int32_t __inline
1538    get_dbquant(Bitstream * bs)
1539    {
1540            if (!BitstreamGetBit(bs))       // '0'
1541                    return (0);
1542            else if (!BitstreamGetBit(bs))  // '10'
1543                    return (-2);
1544            else
1545                    return (2);                             // '11'
1546    }
1547    
1548    // add by MinChen <chenm001@163.com>
1549    // for decode B-frame mb_type
1550    // bit   ret_value
1551    // 1        0
1552    // 01       1
1553    // 001      2
1554    // 0001     3
1555    int32_t __inline
1556    get_mbtype(Bitstream * bs)
1557    {
1558            int32_t mb_type;
1559    
1560            for (mb_type = 0; mb_type <= 3; mb_type++) {
1561                    if (BitstreamGetBit(bs))
1562                            break;
1563            }
1564    
1565            if (mb_type <= 3)
1566                    return (mb_type);
1567            else
1568                    return (-1);
1569    }
1570    
1571    void
1572    decoder_bframe(DECODER * dec,
1573                               Bitstream * bs,
1574                               int quant,
1575                               int fcode_forward,
1576                               int fcode_backward)
1577    {
1578            uint32_t x, y;
1579            VECTOR mv;
1580            const VECTOR zeromv = {0,0};
1581    #ifdef BFRAMES_DEC_DEBUG
1582            FILE *fp;
1583            static char first=0;
1584    #define BFRAME_DEBUG    if (!first && fp){ \
1585                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \
1586            }
1587    #endif
1588    
1589            start_timer();
1590            image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1591                                       dec->width, dec->height);
1592            image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1593                                       dec->width, dec->height);
1594            stop_edges_timer();
1595    
1596    #ifdef BFRAMES_DEC_DEBUG
1597            if (!first){
1598                    fp=fopen("C:\\XVIDDBG.TXT","w");
1599            }
1600    #endif
1601    
1602            for (y = 0; y < dec->mb_height; y++) {
1603                    // Initialize Pred Motion Vector
1604                    dec->p_fmv = dec->p_bmv = zeromv;
1605                    for (x = 0; x < dec->mb_width; x++) {
1606                            MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1607                            MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1608    
1609                            mv =
1610                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1611                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1612    
1613                            // skip if the co-located P_VOP macroblock is not coded
1614                            // if not codec in co-located S_VOP macroblock is _not_ automatically skipped
1615    
1616                            if (last_mb->mode == MODE_NOT_CODED) {
1617                                    //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1618                                    mb->cbp = 0;
1619    #ifdef BFRAMES_DEC_DEBUG
1620                                    mb->mb_type = MODE_NOT_CODED;
1621            BFRAME_DEBUG
1622    #endif
1623                                    mb->mb_type = MODE_FORWARD;
1624                                    mb->quant = last_mb->quant;
1625                                    //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1626                                    //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1627    
1628                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1629                                    continue;
1630                            }
1631    
1632                            if (!BitstreamGetBit(bs)) {     // modb=='0'
1633                                    const uint8_t modb2 = BitstreamGetBit(bs);
1634    
1635                                    mb->mb_type = get_mbtype(bs);
1636    
1637                                    if (!modb2) {   // modb=='00'
1638                                            mb->cbp = BitstreamGetBits(bs, 6);
1639                                    } else {
1640                                            mb->cbp = 0;
1641                                    }
1642                                    if (mb->mb_type && mb->cbp) {
1643                                            quant += get_dbquant(bs);
1644    
1645                                            if (quant > 31) {
1646                                                    quant = 31;
1647                                            } else if (quant < 1) {
1648                                                    quant = 1;
1649                                            }
1650                                    }
1651                            } else {
1652                                    mb->mb_type = MODE_DIRECT_NONE_MV;
1653                                    mb->cbp = 0;
1654                            }
1655    
1656                            mb->quant = quant;
1657                            mb->mode = MODE_INTER4V;
1658                            //DEBUG1("Switch bm_type=",mb->mb_type);
1659    
1660    #ifdef BFRAMES_DEC_DEBUG
1661            BFRAME_DEBUG
1662    #endif
1663    
1664                            switch (mb->mb_type) {
1665                            case MODE_DIRECT:
1666                                    get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1667    
1668                            case MODE_DIRECT_NONE_MV:
1669                                    {
1670                                            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1671                                            int i;
1672    
1673                                            for (i = 0; i < 4; i++) {
1674                                                    mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1675                                                                          / TRD + mv.x);
1676                                                    mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1677                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].x)
1678                                                                                      / TRD
1679                                                                                    : mb->mvs[i].x - last_mb->mvs[i].x);
1680                                                    mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1681                                                                          / TRD + mv.y);
1682                                                    mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1683                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].y)
1684                                                                                      / TRD
1685                                                                                : mb->mvs[i].y - last_mb->mvs[i].y);
1686                                            }
1687                                            //DEBUG("B-frame Direct!\n");
1688                                    }
1689                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1690                                                                                               mb, x, y, bs);
1691                                    break;
1692    
1693                            case MODE_INTERPOLATE:
1694                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1695                                                                            dec->p_fmv);
1696                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1697    
1698                                    get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1699                                                                            fcode_backward, dec->p_bmv);
1700                                    dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1701                                            mb->b_mvs[3] = mb->b_mvs[0];
1702    
1703                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1704                                                                                               mb, x, y, bs);
1705                                    //DEBUG("B-frame Bidir!\n");
1706                                    break;
1707    
1708                            case MODE_BACKWARD:
1709                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1710                                                                            dec->p_bmv);
1711                                    dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1712    
1713                                    mb->mode = MODE_INTER;
1714                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1715                                    //DEBUG("B-frame Backward!\n");
1716                                    break;
1717    
1718                            case MODE_FORWARD:
1719                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1720                                                                            dec->p_fmv);
1721                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1722    
1723                                    mb->mode = MODE_INTER;
1724                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1725                                    //DEBUG("B-frame Forward!\n");
1726                                    break;
1727    
1728                            default:
1729                                    DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
1730                            }
1731    
1732                    }                                               // end of FOR
1733            }
1734    #ifdef BFRAMES_DEC_DEBUG
1735            if (!first){
1736                    first=1;
1737                    if (fp)
1738                            fclose(fp);
1739            }
1740    #endif
1741    }
1742    
1743    // swap two MACROBLOCK array
1744  void  void
1745  mb_swap(MACROBLOCK ** mb1,  mb_swap(MACROBLOCK ** mb1,
1746                  MACROBLOCK ** mb2)                  MACROBLOCK ** mb2)
# Line 739  Line 1751 
1751          *mb2 = temp;          *mb2 = temp;
1752  }  }
1753    
1754    
1755    /* perform post processing if necessary, and output the image */
1756    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1757                                            const XVID_DEC_FRAME * frame, int pp_disable)
1758    {
1759    
1760            if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1761            {
1762                    /* note: image is stored to tmp */
1763                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1764                    image_deblock_rrv(&dec->tmp, dec->edged_width,
1765                                                    mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1766                                                    8, frame->general);
1767                    img = &dec->tmp;
1768            }
1769    
1770            image_output(img, dec->width, dec->height,
1771                                     dec->edged_width, frame->image, frame->stride,
1772                                     frame->colorspace, dec->interlacing);
1773    }
1774    
1775    
1776  int  int
1777  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1778                             XVID_DEC_FRAME * frame)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
1779  {  {
1780    
1781          Bitstream bs;          Bitstream bs;
1782          uint32_t rounding;          uint32_t rounding;
1783            uint32_t reduced_resolution;
1784          uint32_t quant;          uint32_t quant;
1785          uint32_t fcode_forward;          uint32_t fcode_forward;
1786          uint32_t fcode_backward;          uint32_t fcode_backward;
1787          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1788            WARPPOINTS gmc_warp;
1789          uint32_t vop_type;          uint32_t vop_type;
1790            int success = 0;
1791            int output = 0;
1792            int seen_something = 0;
1793    
1794          start_global_timer();          start_global_timer();
1795    
1796            dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1797          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1798    
1799            if ((frame->general & XVID_DEC_DISCONTINUITY))
1800                    dec->frames = 0;
1801    
1802            if (frame->length < 0)  /* decoder flush */
1803            {
1804                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1805                        we have a reference frame, then outout the reference frame */
1806                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)
1807                    {
1808                            decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1809                            output = 1;
1810                    }
1811    
1812                    frame->length = 0;
1813                    if (stats)
1814                    {
1815                            stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1816                            stats->data.vop.time_base = (int)dec->time_base;
1817                            stats->data.vop.time_increment = 0;     //XXX: todo
1818                    }
1819    
1820                    emms();
1821    
1822                    stop_global_timer();
1823                    return XVID_ERR_OK;
1824            }
1825    
1826          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1827    
1828          /* add by chenm001 <chenm001@163.com> */          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's
1829          /* for support B-frame to reference last 2 frame */          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1830          dec->frames++;          {
1831          vop_type =                  if (stats)
1832                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,                          stats->notify = XVID_DEC_VOP;
1833                                                           &fcode_backward, &intra_dc_threshold);                  frame->length = 1;
1834                    image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1835                                             frame->image, frame->stride, frame->colorspace, dec->interlacing);
1836                    emms();
1837                    return XVID_ERR_OK;
1838            }
1839    
1840          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */  repeat:
1841    
1842          switch (vop_type) {          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1843          case P_VOP:                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1844                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,  
1845                                             intra_dc_threshold);          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",
1846                  break;                                                          vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1847    
1848            if (vop_type == - 1)
1849            {
1850                    if (success) goto done;
1851                    emms();
1852                    return XVID_ERR_FAIL;
1853            }
1854    
1855            if (vop_type == -2 || vop_type == -3)
1856            {
1857                    if (vop_type == -3)
1858                            decoder_resize(dec);
1859    
1860                    if (stats)
1861                    {
1862                            stats->notify = XVID_DEC_VOL;
1863                            stats->data.vol.general = 0;
1864                            if (dec->interlacing)
1865                                    stats->data.vol.general |= XVID_INTERLACING;
1866                            stats->data.vol.width = dec->width;
1867                            stats->data.vol.height = dec->height;
1868                            stats->data.vol.aspect_ratio = dec->aspect_ratio;
1869                            stats->data.vol.par_width = dec->par_width;
1870                            stats->data.vol.par_height = dec->par_height;
1871                            frame->length = BitstreamPos(&bs) / 8;
1872                            emms();
1873                            return XVID_ERR_OK;
1874                    }
1875                    goto repeat;
1876            }
1877    
1878            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0
1879    
1880    
1881            /* packed_mode: special-N_VOP treament */
1882            if (dec->packed_mode && vop_type == N_VOP)
1883            {
1884                    if (dec->low_delay_default && dec->frames > 0)
1885                    {
1886                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1887                            output = 1;
1888                    }
1889                    /* ignore otherwise */
1890            }
1891            else if (vop_type != B_VOP)
1892            {
1893                    switch(vop_type)
1894                    {
1895          case I_VOP:          case I_VOP:
1896                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1897                  break;                  break;
1898          case B_VOP:                  case P_VOP :
1899                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1900                                                    fcode_forward, intra_dc_threshold, NULL);
1901                            break;
1902                    case S_VOP :
1903                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1904                                                    fcode_forward, intra_dc_threshold, &gmc_warp);
1905                  break;                  break;
1906          case N_VOP:          case N_VOP:
                 /* when low_delay==0, N_VOP's should interpolate between the past and future frames */  
1907                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1908                  break;                  break;
   
         default:  
                 return XVID_ERR_FAIL;  
1909          }          }
1910    
1911          frame->length = BitstreamPos(&bs) / 8;                  if (reduced_resolution)
1912                    {
1913                            image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1914                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1915                                    16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1916                    }
1917    
1918          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1919                                           frame->image, frame->stride, frame->colorspace);                  if (!(dec->low_delay_default && dec->packed_mode))
1920                    {
1921                            if (dec->low_delay)
1922                            {
1923                                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1924                                    output = 1;
1925                            }
1926                            else if (dec->frames > 0)       /* is the reference frame valid? */
1927                            {
1928                                    /* output the reference frame */
1929                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1930                                    output = 1;
1931                            }
1932                    }
1933    
         if (vop_type == I_VOP || vop_type == P_VOP) {  
1934                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1935                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
   
                 /* swap MACROBLOCK */  
                 /* the Divx will not set the low_delay flage some times */  
                 /* so follow code will wrong to not swap at that time */  
                 /* this will broken bitstream! so I'm change it, */  
                 /* But that is not the best way! can anyone tell me how */  
                 /* to do another way? */  
                 /* 18-07-2002   MinChen<chenm001@163.com> */  
                 /*if (!dec->low_delay && vop_type == P_VOP) */  
                 if (vop_type == P_VOP)  
1936                          mb_swap(&dec->mbs, &dec->last_mbs);                          mb_swap(&dec->mbs, &dec->last_mbs);
1937                    dec->last_reduced_resolution = reduced_resolution;
1938    
1939                    dec->frames++;
1940                    seen_something = 1;
1941    
1942            }else{  /* B_VOP */
1943    
1944                    if (dec->low_delay)
1945                    {
1946                            DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1947                            dec->low_delay = 1;
1948                    }
1949    
1950                    if (dec->frames < 2)
1951                    {
1952                            /* attemping to decode a bvop without atleast 2 reference frames */
1953                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1954                                                    "broken b-frame, mising ref frames");
1955                    }else if (dec->time_pp <= dec->time_bp) {
1956                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1957                            decoded in vfw. */
1958                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1959                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1960                    }else{
1961                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1962                    }
1963    
1964                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1965                    output = 1;
1966                    dec->frames++;
1967            }
1968    
1969            BitstreamByteAlign(&bs);
1970    
1971            /* low_delay_default mode: repeat in packed_mode */
1972            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1973            {
1974                    success = 1;
1975                    goto repeat;
1976            }
1977    
1978    done :
1979    
1980            /* low_delay_default mode: if we've gotten here without outputting anything,
1981               then output the recently decoded frame, or print an error message  */
1982            if (dec->low_delay_default && output == 0)
1983            {
1984                    if (dec->packed_mode && seen_something)
1985                    {
1986                            /* output the recently decoded frame */
1987                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1988                            output = 1;
1989                    }
1990                    else
1991                    {
1992                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1993                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1994                                    "warning: nothing to output");
1995                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1996                                    "bframe decoder lag");
1997    
1998                            decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
1999                    }
2000            }
2001    
2002            frame->length = BitstreamPos(&bs) / 8;
2003    
2004            if (stats)
2005            {
2006                    stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
2007                    stats->data.vop.time_base = (int)dec->time_base;
2008                    stats->data.vop.time_increment = 0;     //XXX: todo
2009          }          }
2010    
2011          emms();          emms();

Legend:
Removed from v.830  
changed lines
  Added in v.851

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