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

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

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