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

Diff of /xvidcore/src/decoder.c

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

revision 1.5, Tue Mar 26 11:16:08 2002 UTC revision 1.43, Sat Nov 16 23:38:15 2002 UTC
# Line 1  Line 1 
1  /**************************************************************************  /**************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC   *      XVID MPEG-4 VIDEO CODEC
4   *      decoder main   *  -  Decoder main module  -
5   *   *
6   *      This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002 MinChen <chenm001@163.com>
7   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002 Peter Ross <pross@xvid.org>
8   *      to use this software module in hardware or software products are   *
9   *      advised that its use may infringe existing patents or copyrights, and   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
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   *  XviD is free software; you can redistribute it and/or modify it
12   *      editors and their companies, will have no liability for use of this   *  under the terms of the GNU General Public License as published by
13   *      software or modifications or derivatives thereof.   *  the Free Software Foundation; either version 2 of the License, or
  *  
  *      This program is xvid_free software; you can redistribute it and/or modify  
  *      it under the terms of the GNU General Public License as published by  
  *      the xvid_free Software Foundation; either version 2 of the License, or  
14   *      (at your option) any later version.   *      (at your option) any later version.
15   *   *
16   *      This program is distributed in the hope that it will be useful,   *      This program is distributed in the hope that it will be useful,
# Line 23  Line 19 
19   *      GNU General Public License for more details.   *      GNU General Public License for more details.
20   *   *
21   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
22   *      along with this program; if not, write to the xvid_free Software   *  along with this program; if not, write to the Free Software
23   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24   *   *
25   *************************************************************************/   *  Under section 8 of the GNU General Public License, the copyright
26     *  holders of XVID explicitly forbid distribution in the following
27  /**************************************************************************   *  countries:
28     *
29     *    - Japan
30     *    - United States of America
31   *   *
32   *      History:   *  Linking XviD statically or dynamically with other modules is making a
33     *  combined work based on XviD.  Thus, the terms and conditions of the
34     *  GNU General Public License cover the whole combination.
35   *   *
36   *  26.03.2002  interlacing support - moved transfers outside decode loop   *  As a special exception, the copyright holders of XviD give you
37   *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block   *  permission to link XviD with independent modules that communicate with
38   *      22.12.2001      block based interpolation   *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the
39   *      01.12.2001      inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>   *  license terms of these independent modules, and to copy and distribute
40     *  the resulting combined work under terms of your choice, provided that
41     *  every copy of the combined work is accompanied by a complete copy of
42     *  the source code of XviD (the version of XviD used to produce the
43     *  combined work), being distributed under the terms of the GNU General
44     *  Public License plus this exception.  An independent module is a module
45     *  which is not derived from or based on XviD.
46     *
47     *  Note that people who make modified versions of XviD are not obligated
48     *  to grant this special exception for their modified versions; it is
49     *  their choice whether to do so.  The GNU General Public License gives
50     *  permission to release a modified version without this exception; this
51     *  exception also makes it possible to release a modified version which
52     *  carries forward this exception.
53     *
54     * $Id$
55   *   *
56   *************************************************************************/   *************************************************************************/
57    
58  #include <stdlib.h>  #include <stdlib.h>
59  #include <string.h>  // memset  #include <string.h>
60    
61    #ifdef BFRAMES_DEC_DEBUG
62            #define BFRAMES_DEC
63    #endif
64    
65  #include "xvid.h"  #include "xvid.h"
66  #include "portab.h"  #include "portab.h"
# Line 55  Line 75 
75  #include "dct/fdct.h"  #include "dct/fdct.h"
76  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
77  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
 #include "utils/mbfunctions.h"  
78    
79  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
80  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
# Line 66  Line 85 
85  #include "image/colorspace.h"  #include "image/colorspace.h"
86  #include "utils/mem_align.h"  #include "utils/mem_align.h"
87    
88  int decoder_create(XVID_DEC_PARAM * param)  int
89    decoder_create(XVID_DEC_PARAM * param)
90  {  {
91          DECODER * dec;          DECODER * dec;
92    
93          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
94          if (dec == NULL)          if (dec == NULL) {
         {  
95                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
96          }          }
97          param->handle = dec;          param->handle = dec;
# Line 85  Line 104 
104    
105          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
106          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
107            dec->low_delay = 0;
108    
109          if (image_create(&dec->cur, dec->edged_width, dec->edged_height))          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
         {  
110                  xvid_free(dec);                  xvid_free(dec);
111                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
112          }          }
113    
114          if (image_create(&dec->refn, dec->edged_width, dec->edged_height))          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {
         {  
115                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
116                  xvid_free(dec);                  xvid_free(dec);
117                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
118          }          }
119            // add by chenm001 <chenm001@163.com>
120            // for support B-frame to reference last 2 frame
121            if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
122                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
123                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
124                    xvid_free(dec);
125                    return XVID_ERR_MEMORY;
126            }
127            if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {
128                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
129                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
130                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
131                    xvid_free(dec);
132                    return XVID_ERR_MEMORY;
133            }
134    
135          dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);          dec->mbs =
136          if (dec->mbs == NULL)                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
137          {                                          CACHE_LINE);
138            if (dec->mbs == NULL) {
139                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
140                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
141                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
142                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
143                    xvid_free(dec);
144                    return XVID_ERR_MEMORY;
145            }
146    
147            memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
148    
149            // add by chenm001 <chenm001@163.com>
150            // for skip MB flag
151            dec->last_mbs =
152                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
153                                            CACHE_LINE);
154            if (dec->last_mbs == NULL) {
155                    xvid_free(dec->mbs);
156                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
157                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
158                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
159                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
160                  xvid_free(dec);                  xvid_free(dec);
161                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
162          }          }
163    
164            memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
165    
166          init_timer();          init_timer();
167          create_vlc_tables();  
168            // add by chenm001 <chenm001@163.com>
169            // for support B-frame to save reference frame's time
170            dec->frames = -1;
171            dec->time = dec->time_base = dec->last_time_base = 0;
172    
173          return XVID_ERR_OK;          return XVID_ERR_OK;
174  }  }
175    
176    
177  int decoder_destroy(DECODER * dec)  int
178    decoder_destroy(DECODER * dec)
179  {  {
180            xvid_free(dec->last_mbs);
181          xvid_free(dec->mbs);          xvid_free(dec->mbs);
182          image_destroy(&dec->refn, dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
183            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
184            image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
185          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
186          xvid_free(dec);          xvid_free(dec);
187    
         destroy_vlc_tables();  
   
188          write_timer();          write_timer();
189          return XVID_ERR_OK;          return XVID_ERR_OK;
190  }  }
191    
192    
193    
194  static const int32_t dquant_table[4] =  static const int32_t dquant_table[4] = {
 {  
195          -1, -2, 1, 2          -1, -2, 1, 2
196  };  };
197    
198    
199    
200    
201  // decode an intra macroblock  // decode an intra macroblock
202    
203  void decoder_mbintra(DECODER * dec,  void
204    decoder_mbintra(DECODER * dec,
205                                           MACROBLOCK * pMB,                                           MACROBLOCK * pMB,
206                                           const uint32_t x_pos,                                           const uint32_t x_pos,
207                                           const uint32_t y_pos,                                           const uint32_t y_pos,
# Line 145  Line 209 
209                                           const uint32_t cbp,                                           const uint32_t cbp,
210                                           Bitstream * bs,                                           Bitstream * bs,
211                                           const uint32_t quant,                                           const uint32_t quant,
212                                           const uint32_t intra_dc_threshold)                                  const uint32_t intra_dc_threshold,
213                                    const unsigned int bound)
214  {  {
         CACHE_ALIGN int16_t block[6][64];  
         CACHE_ALIGN int16_t data[6][64];  
215    
216          const uint32_t stride = dec->edged_width;          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
217            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
218    
219            uint32_t stride = dec->edged_width;
220            uint32_t stride2 = stride / 2;
221            uint32_t next_block = stride * 8;
222          uint32_t i;          uint32_t i;
223          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
224          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
225    
226      pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);      pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
227      pU_Cur = dec->cur.u + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
228      pV_Cur = dec->cur.v + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
229    
230          memset(block, 0, sizeof(block));                // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear
231    
232          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
233                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
234                  int16_t predictors[8];                  int16_t predictors[8];
235                  int start_coeff;                  int start_coeff;
236    
237                  start_timer();                  start_timer();
238                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, block[i], iQuant, iDcScaler, predictors);                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64],
239                  if (!acpred_flag)                                           iQuant, iDcScaler, predictors, bound);
240                  {                  if (!acpred_flag) {
241                          pMB->acpred_directions[i] = 0;                          pMB->acpred_directions[i] = 0;
242                  }                  }
243                  stop_prediction_timer();                  stop_prediction_timer();
244    
245                  if (quant < intra_dc_threshold)                  if (quant < intra_dc_threshold) {
                 {  
246                          int dc_size;                          int dc_size;
247                          int dc_dif;                          int dc_dif;
248    
249                          dc_size = i < 4 ?  get_dc_size_lum(bs) : get_dc_size_chrom(bs);                          dc_size = i < 4 ?  get_dc_size_lum(bs) : get_dc_size_chrom(bs);
250                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;
251    
252                          if (dc_size > 8)                          if (dc_size > 8) {
                         {  
253                                  BitstreamSkip(bs, 1);           // marker                                  BitstreamSkip(bs, 1);           // marker
254                          }                          }
255    
256                          block[i][0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
257                          start_coeff = 1;                          start_coeff = 1;
258                  }  
259                  else                          DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);
260                  {                  } else {
261                          start_coeff = 0;                          start_coeff = 0;
262                  }                  }
263    
264                  start_timer();                  start_timer();
265                  if (cbp & (1 << (5-i)))                 // coded                  if (cbp & (1 << (5-i)))                 // coded
266                  {                  {
267                          get_intra_block(bs, block[i], pMB->acpred_directions[i], start_coeff);                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],
268                                                            start_coeff);
269                  }                  }
270                  stop_coding_timer();                  stop_coding_timer();
271    
272                  start_timer();                  start_timer();
273                  add_acdc(pMB, i, block[i], iDcScaler, predictors);                  add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors);
274                  stop_prediction_timer();                  stop_prediction_timer();
275    
276                  start_timer();                  start_timer();
277                  if (dec->quant_type == 0)                  if (dec->quant_type == 0) {
278                  {                          dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
279                          dequant_intra(data[i], block[i], iQuant, iDcScaler);                  } else {
280                  }                          dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
                 else  
                 {  
                         dequant4_intra(data[i], block[i], iQuant, iDcScaler);  
281                  }                  }
282                  stop_iquant_timer();                  stop_iquant_timer();
283    
284                  start_timer();                  start_timer();
285                  idct(data[i]);                  idct(&data[i * 64]);
286                  stop_idct_timer();                  stop_idct_timer();
287          }          }
288    
289          start_timer();          if (dec->interlacing && pMB->field_dct) {
290          if (dec->interlacing && pMB->field_dct)                  next_block = stride;
291          {                  stride *= 2;
                 MBFieldToFrame(data);  
292          }          }
         stop_interlacing_timer();  
293    
294          start_timer();          start_timer();
295          transfer_16to8copy(pY_Cur, data[0], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
296          transfer_16to8copy(pY_Cur + 8, data[1], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
297          transfer_16to8copy(pY_Cur + 8 * stride, data[2], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
298          transfer_16to8copy(pY_Cur + 8 + 8 * stride, data[3], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
299          transfer_16to8copy(pU_Cur, data[4], stride / 2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
300          transfer_16to8copy(pV_Cur, data[5], stride / 2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
301          stop_transfer_timer();          stop_transfer_timer();
302  }  }
303    
# Line 252  Line 313 
313    
314  // decode an inter macroblock  // decode an inter macroblock
315    
316  void decoder_mbinter(DECODER * dec,  void
317    decoder_mbinter(DECODER * dec,
318                                           const MACROBLOCK * pMB,                                           const MACROBLOCK * pMB,
319                                           const uint32_t x_pos,                                           const uint32_t x_pos,
320                                           const uint32_t y_pos,                                           const uint32_t y_pos,
# Line 262  Line 324 
324                                           const uint32_t quant,                                           const uint32_t quant,
325                                           const uint32_t rounding)                                           const uint32_t rounding)
326  {  {
         CACHE_ALIGN int16_t block[6][64];  
         CACHE_ALIGN int16_t data[6][64];  
327    
328          const uint32_t stride = dec->edged_width;          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
329          const uint32_t stride2 = dec->edged_width / 2;          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
330    
331            uint32_t stride = dec->edged_width;
332            uint32_t stride2 = stride / 2;
333            uint32_t next_block = stride * 8;
334      uint32_t i;      uint32_t i;
335      uint32_t iQuant = pMB->quant;      uint32_t iQuant = pMB->quant;
336          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
337          int uv_dx, uv_dy;          int uv_dx, uv_dy;
338    
339      pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);      pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
340      pU_Cur = dec->cur.u + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
341      pV_Cur = dec->cur.v + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
342    
343          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
         {  
344                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
345                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
346    
347                    if (dec->quarterpel)
348                    {
349                            uv_dx = (uv_dx >> 1) | (uv_dx & 1);
350                            uv_dy = (uv_dy >> 1) | (uv_dy & 1);
351                    }
352    
353                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
354                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
355          }          } else {
         else  
         {  
356                  int sum;                  int sum;
357                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
358    
359                    if (dec->quarterpel)
360                    {
361                            sum /= 2;
362                    }
363    
364                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
365    
366                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
367    
368                    if (dec->quarterpel)
369                    {
370                            sum /= 2;
371                    }
372    
373                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
374          }          }
375    
376          start_timer();          start_timer();
377          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);          if(dec->quarterpel) {
378          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                  DPRINTF(DPRINTF_DEBUG, "QUARTERPEL\n");
379          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
380          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);
381          interpolate8x8_switch(dec->cur.u, dec->refn.u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
382          interpolate8x8_switch(dec->cur.v, dec->refn.v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);                                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);
383                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
384                                                                      pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);
385                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,
386                                                                      pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);
387            }
388            else {
389                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
390                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);
391                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
392                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);
393                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
394                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);
395                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,
396                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);
397            }
398    
399            interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
400                                                      uv_dx, uv_dy, stride2, rounding);
401            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
402                                                      uv_dx, uv_dy, stride2, rounding);
403          stop_comp_timer();          stop_comp_timer();
404    
405          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
406                  if (cbp & (1 << (5-i)))                 // coded                  if (cbp & (1 << (5-i)))                 // coded
407                  {                  {
408                          memset(block[i], 0, 64 * sizeof(int16_t));              // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
409    
410                          start_timer();                          start_timer();
411                          get_inter_block(bs, block[i]);                          get_inter_block(bs, &block[i * 64]);
412                          stop_coding_timer();                          stop_coding_timer();
413    
414                          start_timer();                          start_timer();
415                          if (dec->quant_type == 0)                          if (dec->quant_type == 0) {
416                          {                                  dequant_inter(&data[i * 64], &block[i * 64], iQuant);
417                                  dequant_inter(data[i], block[i], iQuant);                          } else {
418                          }                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
                         else  
                         {  
                                 dequant4_inter(data[i], block[i], iQuant);  
419                          }                          }
420                          stop_iquant_timer();                          stop_iquant_timer();
421    
422                          start_timer();                          start_timer();
423                          idct(data[i]);                          idct(&data[i * 64]);
424                          stop_idct_timer();                          stop_idct_timer();
425                  }                  }
426          }          }
427    
428          start_timer();          if (dec->interlacing && pMB->field_dct) {
429          if (pMB->field_dct)                  next_block = stride;
430          {                  stride *= 2;
                 MBFieldToFrame(data);  
431          }          }
         stop_interlacing_timer();  
432    
433          start_timer();          start_timer();
434          if (cbp & 32)          if (cbp & 32)
435                  transfer_16to8add(pY_Cur, data[0], stride);                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);
436          if (cbp & 16)          if (cbp & 16)
437                  transfer_16to8add(pY_Cur + 8, data[1], stride);                  transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
438          if (cbp & 8)          if (cbp & 8)
439                  transfer_16to8add(pY_Cur + 8 * stride, data[2], stride);                  transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
440          if (cbp & 4)          if (cbp & 4)
441                  transfer_16to8add(pY_Cur + 8 + 8 * stride, data[3], stride);                  transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
442          if (cbp & 2)          if (cbp & 2)
443                  transfer_16to8add(pU_Cur, data[4], stride / 2);                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
444          if (cbp & 1)          if (cbp & 1)
445                  transfer_16to8add(pV_Cur, data[5], stride / 2);                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
446          stop_transfer_timer();          stop_transfer_timer();
447  }  }
448    
449    
450  void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold)  void
451    decoder_iframe(DECODER * dec,
452                               Bitstream * bs,
453                               int quant,
454                               int intra_dc_threshold)
455  {  {
456            uint32_t bound;
457          uint32_t x, y;          uint32_t x, y;
458    
459          for (y = 0; y < dec->mb_height; y++)          bound = 0;
         {  
                 for (x = 0; x < dec->mb_width; x++)  
                 {  
                         MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];  
460    
461            for (y = 0; y < dec->mb_height; y++) {
462                    for (x = 0; x < dec->mb_width; x++) {
463                            MACROBLOCK *mb;
464                          uint32_t mcbpc;                          uint32_t mcbpc;
465                          uint32_t cbpc;                          uint32_t cbpc;
466                          uint32_t acpred_flag;                          uint32_t acpred_flag;
467                          uint32_t cbpy;                          uint32_t cbpy;
468                          uint32_t cbp;                          uint32_t cbp;
469    
470                            while (BitstreamShowBits(bs, 9) == 1)
471                                    BitstreamSkip(bs, 9);
472    
473                            if (check_resync_marker(bs, 0))
474                            {
475                                    bound = read_video_packet_header(bs, 0, &quant);
476                                    x = bound % dec->mb_width;
477                                    y = bound / dec->mb_width;
478                            }
479                            mb = &dec->mbs[y * dec->mb_width + x];
480    
481                            DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
482    
483                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
484                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
485                          cbpc = (mcbpc >> 4);                          cbpc = (mcbpc >> 4);
486    
487                          acpred_flag = BitstreamGetBit(bs);                          acpred_flag = BitstreamGetBit(bs);
488    
                         if (mb->mode == MODE_STUFFING)  
                         {  
                                 DEBUG("-- STUFFING ?");  
                                 continue;  
                         }  
   
489                          cbpy = get_cbpy(bs, 1);                          cbpy = get_cbpy(bs, 1);
490                          cbp = (cbpy << 2) | cbpc;                          cbp = (cbpy << 2) | cbpc;
491    
492                          if (mb->mode == MODE_INTRA_Q)                          if (mb->mode == MODE_INTRA_Q) {
                         {  
493                                  quant += dquant_table[BitstreamGetBits(bs,2)];                                  quant += dquant_table[BitstreamGetBits(bs,2)];
494                                  if (quant > 31)                                  if (quant > 31) {
                                 {  
495                                          quant = 31;                                          quant = 31;
496                                  }                                  } else if (quant < 1) {
                                 else if (quant < 1)  
                                 {  
497                                          quant = 1;                                          quant = 1;
498                                  }                                  }
499                          }                          }
500                          mb->quant = quant;                          mb->quant = quant;
501                            mb->mvs[0].x = mb->mvs[0].y =
502                            mb->mvs[1].x = mb->mvs[1].y =
503                            mb->mvs[2].x = mb->mvs[2].y =
504                            mb->mvs[3].x = mb->mvs[3].y =0;
505    
506                          if (dec->interlacing)                          if (dec->interlacing) {
                         {  
507                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
508                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(DPRINTF_DEBUG, "deci: field_dct: %d", mb->field_dct);
509                          }                          }
510    
511                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
512                                                            intra_dc_threshold, bound);
513                  }                  }
514                    if(dec->out_frm)
515                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);
516    
517          }          }
518    
519  }  }
520    
521    
522  void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode)  void
523    get_motion_vector(DECODER * dec,
524                                      Bitstream * bs,
525                                      int x,
526                                      int y,
527                                      int k,
528                                      VECTOR * mv,
529                                      int fcode,
530                                      const int bound)
531  {  {
532    
533          int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
534          int high = (32 * scale_fac) - 1;          int high = (32 * scale_fac) - 1;
535          int low = ((-32) * scale_fac);          int low = ((-32) * scale_fac);
536          int range = (64 * scale_fac);          int range = (64 * scale_fac);
537    
538          VECTOR pmv[4];          VECTOR pmv;
         uint32_t psad[4];  
   
539          int mv_x, mv_y;          int mv_x, mv_y;
         int pmv_x, pmv_y;  
   
   
         get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad);  
540    
541          pmv_x = pmv[0].x;          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
         pmv_y = pmv[0].y;  
542    
543          mv_x = get_mv(bs, fcode);          mv_x = get_mv(bs, fcode);
544          mv_y = get_mv(bs, fcode);          mv_y = get_mv(bs, fcode);
545    
546          mv_x += pmv_x;          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);
         mv_y += pmv_y;  
547    
548          if (mv_x < low)          mv_x += pmv.x;
549          {          mv_y += pmv.y;
550    
551            if (mv_x < low) {
552                  mv_x += range;                  mv_x += range;
553          }          } else if (mv_x > high) {
         else if (mv_x > high)  
         {  
554                  mv_x -= range;                  mv_x -= range;
555          }          }
556    
557          if (mv_y < low)          if (mv_y < low) {
         {  
558                  mv_y += range;                  mv_y += range;
559          }          } else if (mv_y > high) {
         else if (mv_y > high)  
         {  
560                  mv_y -= range;                  mv_y -= range;
561          }          }
562    
# Line 460  Line 566 
566  }  }
567    
568    
569  void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold)  void
570    decoder_pframe(DECODER * dec,
571                               Bitstream * bs,
572                               int rounding,
573                               int quant,
574                               int fcode,
575                               int intra_dc_threshold)
576  {  {
         uint32_t x, y;  
577    
578          image_swap(&dec->cur, &dec->refn);          uint32_t x, y;
579            uint32_t bound;
580            int cp_mb, st_mb;
581    
582          start_timer();          start_timer();
583          image_setedges(&dec->refn, dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
584                                       dec->width, dec->height);
585          stop_edges_timer();          stop_edges_timer();
586    
587          for (y = 0; y < dec->mb_height; y++)          bound = 0;
588          {  
589                  for (x = 0; x < dec->mb_width; x++)          for (y = 0; y < dec->mb_height; y++) {
590                    cp_mb = st_mb = 0;
591                    for (x = 0; x < dec->mb_width; x++) {
592                            MACROBLOCK *mb;
593    
594                            // skip stuffing
595                            while (BitstreamShowBits(bs, 10) == 1)
596                                    BitstreamSkip(bs, 10);
597    
598                            if (check_resync_marker(bs, fcode - 1))
599                  {                  {
600                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                                  bound = read_video_packet_header(bs, fcode - 1, &quant);
601                                    x = bound % dec->mb_width;
602                                    y = bound / dec->mb_width;
603                            }
604                            mb = &dec->mbs[y * dec->mb_width + x];
605    
606                            DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
607    
608                          if (!BitstreamGetBit(bs))                       // not_coded                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded
609                            if (!(BitstreamGetBit(bs)))     // not_coded
610                          {                          {
611                                  uint32_t mcbpc;                                  uint32_t mcbpc;
612                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 485  Line 615 
615                                  uint32_t cbp;                                  uint32_t cbp;
616                                  uint32_t intra;                                  uint32_t intra;
617    
618                                    cp_mb++;
619                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
620                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
621                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
622    
623                                    DPRINTF(DPRINTF_MB, "mode %i", mb->mode);
624                                    DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);
625                                  acpred_flag = 0;                                  acpred_flag = 0;
626    
627                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
628    
629                                  if (intra)                                  if (intra) {
                                 {  
630                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
631                                  }                                  }
632    
                                 if (mb->mode == MODE_STUFFING)  
                                 {  
                                         DEBUG("-- STUFFING ?");  
                                         continue;  
                                 }  
   
633                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
634                                    DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);
635    
636                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
637    
638                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q)                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
639                                  {                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
640                                          quant += dquant_table[BitstreamGetBits(bs,2)];                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);
641                                          if (quant > 31)                                          quant += dquant;
642                                          {                                          if (quant > 31) {
643                                                  quant = 31;                                                  quant = 31;
644                                          }                                          } else if (quant < 1) {
                                         else if (mb->quant < 1)  
                                         {  
645                                                  quant = 1;                                                  quant = 1;
646                                          }                                          }
647                                            DPRINTF(DPRINTF_MB, "quant %i", quant);
648                                  }                                  }
649                                  mb->quant = quant;                                  mb->quant = quant;
650    
651                                  if (dec->interlacing)                                  if (dec->interlacing) {
652                                  {                                          if (cbp || intra) {
653                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
654                                          DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_dct: %d", mb->field_dct);
655                                            }
656    
657                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
                                         {  
658                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
659                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_pred: %d", mb->field_pred);
660    
661                                                  if (mb->field_pred)                                                  if (mb->field_pred) {
                                                 {  
662                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
663                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_top: %d", mb->field_for_top);
664                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
665                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_bot: %d", mb->field_for_bot);
666                                                  }                                                  }
667                                          }                                          }
668                                  }                                  }
669    
670                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
671                                  {                                          if (dec->interlacing && mb->field_pred) {
672                                          if (dec->interlacing && mb->field_pred)                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
673                                                                                      fcode, bound);
674                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
675                                                                                      fcode, bound);
676                                            } else {
677                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
678                                                                                      fcode, bound);
679                                                    mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
680                                                            mb->mvs[0].x;
681                                                    mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
682                                                            mb->mvs[0].y;
683                                            }
684                                    } else if (mb->mode == MODE_INTER4V ) {
685    
686                                            get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
687                                            get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
688                                            get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
689                                            get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
690                                    } else                  // MODE_INTRA, MODE_INTRA_Q
691                                    {
692                                            mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
693                                                    0;
694                                            mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
695                                                    0;
696                                            decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
697                                                                            intra_dc_threshold, bound);
698                                            continue;
699                                    }
700    
701                                    decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
702                                                                    rounding);
703                            } else                          // not coded
704                                          {                                          {
705                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);                                  DPRINTF(DPRINTF_DEBUG, "P-frame MB at (X,Y)=(%d,%d)", x, y);
706                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode);  
707                                    mb->mode = MODE_NOT_CODED;
708                                    mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
709                                    mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
710    
711                                    // copy macroblock directly from ref to cur
712    
713                                    start_timer();
714    
715                                    transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
716                                                                     (16 * x),
717                                                                     dec->refn[0].y + (16 * y) * dec->edged_width +
718                                                                     (16 * x), dec->edged_width);
719    
720                                    transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
721                                                                     (16 * x + 8),
722                                                                     dec->refn[0].y + (16 * y) * dec->edged_width +
723                                                                     (16 * x + 8), dec->edged_width);
724    
725                                    transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
726                                                                     (16 * x),
727                                                                     dec->refn[0].y + (16 * y +
728                                                                                                       8) * dec->edged_width +
729                                                                     (16 * x), dec->edged_width);
730    
731                                    transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
732                                                                     (16 * x + 8),
733                                                                     dec->refn[0].y + (16 * y +
734                                                                                                       8) * dec->edged_width +
735                                                                     (16 * x + 8), dec->edged_width);
736    
737                                    transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 +
738                                                                     (8 * x),
739                                                                     dec->refn[0].u +
740                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
741                                                                     dec->edged_width / 2);
742    
743                                    transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +
744                                                                     (8 * x),
745                                                                     dec->refn[0].v +
746                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
747                                                                     dec->edged_width / 2);
748                                    stop_transfer_timer();
749                                    if(dec->out_frm && cp_mb > 0) {
750                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
751                                      cp_mb = 0;
752                                          }                                          }
753                                          else                                  st_mb = x+1;
754                            }
755                    }
756                    if(dec->out_frm && cp_mb > 0)
757                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
758            }
759    }
760    
761    
762    // add by MinChen <chenm001@163.com>
763    // decode B-frame motion vector
764    void
765    get_b_motion_vector(DECODER * dec,
766                                            Bitstream * bs,
767                                            int x,
768                                            int y,
769                                            VECTOR * mv,
770                                            int fcode,
771                                            const VECTOR pmv)
772                                          {                                          {
773                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);          int scale_fac = 1 << (fcode - 1);
774                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;          int high = (32 * scale_fac) - 1;
775                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;          int low = ((-32) * scale_fac);
776            int range = (64 * scale_fac);
777    
778            int mv_x, mv_y;
779            int pmv_x, pmv_y;
780    
781            pmv_x = pmv.x;
782            pmv_y = pmv.y;
783    
784            mv_x = get_mv(bs, fcode);
785            mv_y = get_mv(bs, fcode);
786    
787            mv_x += pmv_x;
788            mv_y += pmv_y;
789    
790            if (mv_x < low) {
791                    mv_x += range;
792            } else if (mv_x > high) {
793                    mv_x -= range;
794                                          }                                          }
795    
796            if (mv_y < low) {
797                    mv_y += range;
798            } else if (mv_y > high) {
799                    mv_y -= range;
800                                  }                                  }
801                                  else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */)  
802            mv->x = mv_x;
803            mv->y = mv_y;
804    }
805    
806    
807    // add by MinChen <chenm001@163.com>
808    // decode an B-frame forward & backward inter macroblock
809    void
810    decoder_bf_mbinter(DECODER * dec,
811                                       const MACROBLOCK * pMB,
812                                       const uint32_t x_pos,
813                                       const uint32_t y_pos,
814                                       const uint32_t cbp,
815                                       Bitstream * bs,
816                                       const uint32_t quant,
817                                       const uint8_t ref)
818                                  {                                  {
819                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);  
820                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
821                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
822                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);  
823            uint32_t stride = dec->edged_width;
824            uint32_t stride2 = stride / 2;
825            uint32_t next_block = stride * 8;
826            uint32_t i;
827            uint32_t iQuant = pMB->quant;
828            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
829            int uv_dx, uv_dy;
830    
831            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
832            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
833            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
834    
835    
836            if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
837                    uv_dx = pMB->mvs[0].x;
838                    uv_dy = pMB->mvs[0].y;
839    
840                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
841                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
842            } else {
843                    int sum;
844    
845                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
846                    uv_dx =
847                            (sum ==
848                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
849                                                                      (ABS(sum) / 16) * 2));
850    
851                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
852                    uv_dy =
853                            (sum ==
854                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
855                                                                      (ABS(sum) / 16) * 2));
856                                  }                                  }
857                                  else  // MODE_INTRA, MODE_INTRA_Q  
858            start_timer();
859            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,
860                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
861            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,
862                                                      16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
863            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos,
864                                                      16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,
865                                                      0);
866            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,
867                                                      16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
868                                                      0);
869            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
870                                                      uv_dx, uv_dy, stride2, 0);
871            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
872                                                      uv_dx, uv_dy, stride2, 0);
873            stop_comp_timer();
874    
875            for (i = 0; i < 6; i++) {
876                    if (cbp & (1 << (5 - i)))       // coded
877                                  {                                  {
878                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
879                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  
880                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                          start_timer();
881                                          continue;                          get_inter_block(bs, &block[i * 64]);
882                            stop_coding_timer();
883    
884                            start_timer();
885                            if (dec->quant_type == 0) {
886                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
887                            } else {
888                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
889                            }
890                            stop_iquant_timer();
891    
892                            start_timer();
893                            idct(&data[i * 64]);
894                            stop_idct_timer();
895                    }
896                                  }                                  }
897    
898                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, rounding);          if (dec->interlacing && pMB->field_dct) {
899                    next_block = stride;
900                    stride *= 2;
901                          }                          }
902                          else    // not coded  
903            start_timer();
904            if (cbp & 32)
905                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
906            if (cbp & 16)
907                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
908            if (cbp & 8)
909                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
910            if (cbp & 4)
911                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
912            if (cbp & 2)
913                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
914            if (cbp & 1)
915                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
916            stop_transfer_timer();
917    }
918    
919    
920    // add by MinChen <chenm001@163.com>
921    // decode an B-frame direct &  inter macroblock
922    void
923    decoder_bf_interpolate_mbinter(DECODER * dec,
924                                                               IMAGE forward,
925                                                               IMAGE backward,
926                                                               const MACROBLOCK * pMB,
927                                                               const uint32_t x_pos,
928                                                               const uint32_t y_pos,
929                                                               Bitstream * bs)
930                          {                          {
931    
932                                  mb->mode = MODE_NOT_CODED;          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
933                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
934                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  
935            uint32_t stride = dec->edged_width;
936            uint32_t stride2 = stride / 2;
937            uint32_t next_block = stride * 8;
938            uint32_t iQuant = pMB->quant;
939            int uv_dx, uv_dy;
940            int b_uv_dx, b_uv_dy;
941            uint32_t i;
942            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
943        const uint32_t cbp = pMB->cbp;
944    
945            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
946            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
947            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
948    
949    
950            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
951                    uv_dx = pMB->mvs[0].x;
952                    uv_dy = pMB->mvs[0].y;
953    
954                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
955                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
956    
957                    b_uv_dx = pMB->b_mvs[0].x;
958                    b_uv_dy = pMB->b_mvs[0].y;
959    
960                    b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
961                    b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
962            } else {
963                    int sum;
964    
965                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
966                    uv_dx =
967                            (sum ==
968                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
969                                                                      (ABS(sum) / 16) * 2));
970    
971                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
972                    uv_dy =
973                            (sum ==
974                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
975                                                                      (ABS(sum) / 16) * 2));
976    
977                    sum =
978                            pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +
979                            pMB->b_mvs[3].x;
980                    b_uv_dx =
981                            (sum ==
982                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
983                                                                      (ABS(sum) / 16) * 2));
984    
985                    sum =
986                            pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y +
987                            pMB->b_mvs[3].y;
988                    b_uv_dy =
989                            (sum ==
990                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
991                                                                      (ABS(sum) / 16) * 2));
992            }
993    
                                 // copy macroblock directly from ref to cur  
994    
995                                  start_timer();                                  start_timer();
996            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
997                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
998            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
999                                                      pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1000            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1001                                                      pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1002            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1003                                                      16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1004                                                      0);
1005            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1006                                                      uv_dy, stride2, 0);
1007            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1008                                                      uv_dy, stride2, 0);
1009    
1010    
1011            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,
1012                                                      pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1013            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
1014                                                      16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1015                                                      0);
1016            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,
1017                                                      16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1018                                                      stride, 0);
1019            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
1020                                                      16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1021                                                      stride, 0);
1022            interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,
1023                                                      b_uv_dx, b_uv_dy, stride2, 0);
1024            interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,
1025                                                      b_uv_dx, b_uv_dy, stride2, 0);
1026    
1027            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos,
1028                                             stride);
1029            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos,
1030                                             stride);
1031            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,
1032                                             stride);
1033            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,
1034                                             16 * y_pos + 8, stride);
1035            interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,
1036                                             stride2);
1037            interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos,
1038                                             stride2);
1039            stop_comp_timer();
1040    
1041                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),          for (i = 0; i < 6; i++) {
1042                                                                  dec->refn.y + (16*y)*dec->edged_width + (16*x),                  if (cbp & (1 << (5 - i)))       // coded
1043                                                                  dec->edged_width);                  {
1044                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
                                 transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),  
                                                                 dec->refn.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.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.y + (16*y+8)*dec->edged_width + (16*x+8),  
                                                                 dec->edged_width);  
1045    
1046                                  transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),                          start_timer();
1047                                                                  dec->refn.u + (8*y)*dec->edged_width/2 + (8*x),                          get_inter_block(bs, &block[i * 64]);
1048                                                                  dec->edged_width/2);                          stop_coding_timer();
1049    
1050                                  transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),                          start_timer();
1051                                                                  dec->refn.v + (8*y)*dec->edged_width/2 + (8*x),                          if (dec->quant_type == 0) {
1052                                                                  dec->edged_width/2);                                  dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1053                            } else {
1054                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1055                            }
1056                            stop_iquant_timer();
1057    
1058                            start_timer();
1059                            idct(&data[i * 64]);
1060                            stop_idct_timer();
1061                    }
1062            }
1063    
1064            if (dec->interlacing && pMB->field_dct) {
1065                    next_block = stride;
1066                    stride *= 2;
1067            }
1068    
1069            start_timer();
1070            if (cbp & 32)
1071                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1072            if (cbp & 16)
1073                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1074            if (cbp & 8)
1075                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1076            if (cbp & 4)
1077                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1078            if (cbp & 2)
1079                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1080            if (cbp & 1)
1081                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1082                                  stop_transfer_timer();                                  stop_transfer_timer();
1083                          }                          }
1084    
1085    
1086    // add by MinChen <chenm001@163.com>
1087    // for decode B-frame dbquant
1088    int32_t __inline
1089    get_dbquant(Bitstream * bs)
1090    {
1091            if (!BitstreamGetBit(bs))       // '0'
1092                    return (0);
1093            else if (!BitstreamGetBit(bs))  // '10'
1094                    return (-2);
1095            else
1096                    return (2);                             // '11'
1097                  }                  }
1098    
1099    // add by MinChen <chenm001@163.com>
1100    // for decode B-frame mb_type
1101    // bit   ret_value
1102    // 1        0
1103    // 01       1
1104    // 001      2
1105    // 0001     3
1106    int32_t __inline
1107    get_mbtype(Bitstream * bs)
1108    {
1109            int32_t mb_type;
1110    
1111            for (mb_type = 0; mb_type <= 3; mb_type++) {
1112                    if (BitstreamGetBit(bs))
1113                            break;
1114          }          }
1115    
1116            if (mb_type <= 3)
1117                    return (mb_type);
1118            else
1119                    return (-1);
1120  }  }
1121    
1122  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)  void
1123    decoder_bframe(DECODER * dec,
1124                               Bitstream * bs,
1125                               int quant,
1126                               int fcode_forward,
1127                               int fcode_backward)
1128  {  {
1129            uint32_t x, y;
1130            VECTOR mv;
1131            const VECTOR zeromv = {0,0};
1132    #ifdef BFRAMES_DEC_DEBUG
1133            FILE *fp;
1134            static char first=0;
1135    #define BFRAME_DEBUG    if (!first && fp){ \
1136                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \
1137            }
1138    #endif
1139    
1140            start_timer();
1141            image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1142                                       dec->width, dec->height);
1143            image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1144                                       dec->width, dec->height);
1145            stop_edges_timer();
1146    
1147    #ifdef BFRAMES_DEC_DEBUG
1148            if (!first){
1149                    fp=fopen("C:\\XVIDDBG.TXT","w");
1150            }
1151    #endif
1152    
1153            for (y = 0; y < dec->mb_height; y++) {
1154                    // Initialize Pred Motion Vector
1155                    dec->p_fmv = dec->p_bmv = zeromv;
1156                    for (x = 0; x < dec->mb_width; x++) {
1157                            MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1158                            MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1159    
1160                            mv =
1161                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1162                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1163    
1164                            // the last P_VOP is skip macroblock ?
1165                            if (last_mb->mode == MODE_NOT_CODED) {
1166                                    //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1167                                    mb->cbp = 0;
1168    #ifdef BFRAMES_DEC_DEBUG
1169                                    mb->mb_type = MODE_NOT_CODED;
1170            BFRAME_DEBUG
1171    #endif
1172                                    mb->mb_type = MODE_FORWARD;
1173                                    mb->quant = last_mb->quant;
1174                                    //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1175                                    //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1176    
1177                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1178                                    continue;
1179                            }
1180    
1181                            if (!BitstreamGetBit(bs)) {     // modb=='0'
1182                                    const uint8_t modb2 = BitstreamGetBit(bs);
1183    
1184                                    mb->mb_type = get_mbtype(bs);
1185    
1186                                    if (!modb2) {   // modb=='00'
1187                                            mb->cbp = BitstreamGetBits(bs, 6);
1188                                    } else {
1189                                            mb->cbp = 0;
1190                                    }
1191                                    if (mb->mb_type && mb->cbp) {
1192                                            quant += get_dbquant(bs);
1193    
1194                                            if (quant > 31) {
1195                                                    quant = 31;
1196                                            } else if (quant < 1) {
1197                                                    quant = 1;
1198                                            }
1199                                    }
1200                            } else {
1201                                    mb->mb_type = MODE_DIRECT_NONE_MV;
1202                                    mb->cbp = 0;
1203                            }
1204    
1205                            mb->quant = quant;
1206                            mb->mode = MODE_INTER4V;
1207                            //DEBUG1("Switch bm_type=",mb->mb_type);
1208    
1209    #ifdef BFRAMES_DEC_DEBUG
1210            BFRAME_DEBUG
1211    #endif
1212    
1213                            switch (mb->mb_type) {
1214                            case MODE_DIRECT:
1215                                    get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1216    
1217                            case MODE_DIRECT_NONE_MV:
1218                                    {
1219                                            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1220                                            int i;
1221    
1222                                            for (i = 0; i < 4; i++) {
1223                                                    mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1224                                                                          / TRD + mv.x);
1225                                                    mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1226                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].x)
1227                                                                                      / TRD
1228                                                                                    : mb->mvs[i].x - last_mb->mvs[i].x);
1229                                                    mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1230                                                                          / TRD + mv.y);
1231                                                    mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1232                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].y)
1233                                                                                      / TRD
1234                                                                                : mb->mvs[i].y - last_mb->mvs[i].y);
1235                                            }
1236                                            //DEBUG("B-frame Direct!\n");
1237                                    }
1238                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1239                                                                                               mb, x, y, bs);
1240                                    break;
1241    
1242                            case MODE_INTERPOLATE:
1243                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1244                                                                            dec->p_fmv);
1245                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1246    
1247                                    get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1248                                                                            fcode_backward, dec->p_bmv);
1249                                    dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1250                                            mb->b_mvs[3] = mb->b_mvs[0];
1251    
1252                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1253                                                                                               mb, x, y, bs);
1254                                    //DEBUG("B-frame Bidir!\n");
1255                                    break;
1256    
1257                            case MODE_BACKWARD:
1258                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1259                                                                            dec->p_bmv);
1260                                    dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1261    
1262                                    mb->mode = MODE_INTER;
1263                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1264                                    //DEBUG("B-frame Backward!\n");
1265                                    break;
1266    
1267                            case MODE_FORWARD:
1268                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1269                                                                            dec->p_fmv);
1270                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1271    
1272                                    mb->mode = MODE_INTER;
1273                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1274                                    //DEBUG("B-frame Forward!\n");
1275                                    break;
1276    
1277                            default:
1278                                    DPRINTF(DPRINTF_ERROR, "Not support B-frame mb_type = %d", mb->mb_type);
1279                            }
1280    
1281                    }                                               // end of FOR
1282            }
1283    #ifdef BFRAMES_DEC_DEBUG
1284            if (!first){
1285                    first=1;
1286                    if (fp)
1287                            fclose(fp);
1288            }
1289    #endif
1290    }
1291    
1292    // swap two MACROBLOCK array
1293    void
1294    mb_swap(MACROBLOCK ** mb1,
1295                    MACROBLOCK ** mb2)
1296    {
1297            MACROBLOCK *temp = *mb1;
1298    
1299            *mb1 = *mb2;
1300            *mb2 = temp;
1301    }
1302    
1303    int
1304    decoder_decode(DECODER * dec,
1305                               XVID_DEC_FRAME * frame)
1306    {
1307    
1308          Bitstream bs;          Bitstream bs;
1309          uint32_t rounding;          uint32_t rounding;
1310          uint32_t quant;          uint32_t quant;
1311          uint32_t fcode;          uint32_t fcode_forward;
1312            uint32_t fcode_backward;
1313          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1314            uint32_t vop_type;
1315    
1316          start_global_timer();          start_global_timer();
1317    
1318            dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1319    
1320          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1321    
1322          switch (BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold))          // add by chenm001 <chenm001@163.com>
1323          {          // for support B-frame to reference last 2 frame
1324            dec->frames++;
1325            vop_type =
1326                    BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,
1327                                                             &fcode_backward, &intra_dc_threshold);
1328    
1329            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0
1330    
1331            switch (vop_type) {
1332          case P_VOP :          case P_VOP :
1333                  decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold);                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,
1334                                               intra_dc_threshold);
1335    #ifdef BFRAMES_DEC
1336                    DPRINTF(DPRINTF_DEBUG, "P_VOP  Time=%d", dec->time);
1337    #endif
1338                  break;                  break;
1339    
1340          case I_VOP :          case I_VOP :
                 //DEBUG1("",intra_dc_threshold);  
1341                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1342    #ifdef BFRAMES_DEC
1343                    DPRINTF(DPRINTF_DEBUG, "I_VOP  Time=%d", dec->time);
1344    #endif
1345                  break;                  break;
1346    
1347          case B_VOP :    // ignore          case B_VOP:
1348    #ifdef BFRAMES_DEC
1349                    if (dec->time_pp > dec->time_bp) {
1350                            DPRINTF(DPRINTF_DEBUG, "B_VOP  Time=%d", dec->time);
1351                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1352                    } else {
1353                            DPRINTF(DPRINTF_DEBUG, "Broken B_VOP");
1354                    }
1355    #else
1356                    image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1357    #endif
1358                  break;                  break;
1359    
1360          case N_VOP :    // vop not coded          case N_VOP :    // vop not coded
1361                    // when low_delay==0, N_VOP's should interpolate between the past and future frames
1362                    image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1363                  break;                  break;
1364    
1365          default :          default :
1366                  return XVID_ERR_FAIL;                  return XVID_ERR_FAIL;
1367          }          }
1368    
1369    #ifdef BFRAMES_DEC_DEBUG
1370            if (frame->length != BitstreamPos(&bs) / 8){
1371                    DPRINTF(DPRINTF_DEBUG, "InLen: %d / UseLen: %d", frame->length, BitstreamPos(&bs) / 8);
1372            }
1373    #endif
1374          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1375    
1376          start_timer();  
1377    #ifdef BFRAMES_DEC
1378            // test if no B_VOP
1379            if (dec->low_delay || dec->frames == 0) {
1380    #endif
1381          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
1382                                  frame->image, frame->stride, frame->colorspace);                                  frame->image, frame->stride, frame->colorspace);
1383    
1384    #ifdef BFRAMES_DEC
1385            } else {
1386                    if (dec->frames >= 1) {
1387                            start_timer();
1388                            if ((vop_type == I_VOP || vop_type == P_VOP)) {
1389                                    image_output(&dec->refn[0], dec->width, dec->height,
1390                                                             dec->edged_width, frame->image, frame->stride,
1391                                                             frame->colorspace);
1392                            } else if (vop_type == B_VOP) {
1393                                    image_output(&dec->cur, dec->width, dec->height,
1394                                                             dec->edged_width, frame->image, frame->stride,
1395                                                             frame->colorspace);
1396                            }
1397          stop_conv_timer();          stop_conv_timer();
1398                    }
1399            }
1400    #endif
1401    
1402            if (vop_type == I_VOP || vop_type == P_VOP) {
1403                    image_swap(&dec->refn[0], &dec->refn[1]);
1404                    image_swap(&dec->cur, &dec->refn[0]);
1405    
1406                    // swap MACROBLOCK
1407                    // the Divx will not set the low_delay flage some times
1408                    // so follow code will wrong to not swap at that time
1409                    // this will broken bitstream! so I'm change it,
1410                    // But that is not the best way! can anyone tell me how
1411                    // to do another way?
1412                    // 18-07-2002   MinChen<chenm001@163.com>
1413                    //if (!dec->low_delay && vop_type == P_VOP)
1414                    if (vop_type == P_VOP)
1415                            mb_swap(&dec->mbs, &dec->last_mbs);
1416            }
1417    
1418          emms();          emms();
1419    

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.43

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