[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.11, Tue Apr 23 00:04:03 2002 UTC revision 1.25, Tue Jul 9 01:09:33 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     *      22.06.2002      added primative N_VOP support
36     *                              #define BFRAMES_DEC now enables Minchenm's bframe decoder
37     *  08.05.2002  add low_delay support for B_VOP decode
38     *              MinChen <chenm001@163.com>
39     *  05.05.2002  fix some B-frame decode problem
40     *  02.05.2002  add B-frame decode support(have some problem);
41     *              MinChen <chenm001@163.com>
42   *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>   *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>
43   *  29.03.2002  interlacing fix - compensated block wasn't being used when   *  29.03.2002  interlacing fix - compensated block wasn't being used when
44   *              reconstructing blocks, thus artifacts   *              reconstructing blocks, thus artifacts
# Line 39  Line 46 
46   *              interlaced decoding should be as fast as progressive now   *              interlaced decoding should be as fast as progressive now
47   *  26.03.2002  interlacing support - moved transfers outside decode loop   *  26.03.2002  interlacing support - moved transfers outside decode loop
48   *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block   *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block
49   *      22.12.2001      block based interpolation   *  22.12.2001  lock based interpolation
50   *      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>
51   *   *
52     *  $Id$
53     *
54   *************************************************************************/   *************************************************************************/
55    
56  #include <stdlib.h>  #include <stdlib.h>
57  #include <string.h>  // memset  #include <string.h>
58    
59  #include "xvid.h"  #include "xvid.h"
60  #include "portab.h"  #include "portab.h"
# Line 70  Line 79 
79  #include "image/colorspace.h"  #include "image/colorspace.h"
80  #include "utils/mem_align.h"  #include "utils/mem_align.h"
81    
82  int decoder_create(XVID_DEC_PARAM * param)  int
83    decoder_create(XVID_DEC_PARAM * param)
84  {  {
85          DECODER * dec;          DECODER * dec;
86    
87          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
88          if (dec == NULL)          if (dec == NULL) {
         {  
89                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
90          }          }
91          param->handle = dec;          param->handle = dec;
# Line 89  Line 98 
98    
99          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
100          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
101            dec->low_delay = 0;
102    
103          if (image_create(&dec->cur, dec->edged_width, dec->edged_height))          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
         {  
104                  xvid_free(dec);                  xvid_free(dec);
105                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
106          }          }
107    
108          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height))          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {
         {  
109                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
110                  xvid_free(dec);                  xvid_free(dec);
111                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
112          }          }
113          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
114          // for support B-frame to reference last 2 frame          // for support B-frame to reference last 2 frame
115          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height))          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
116          {                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
117                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
118                    xvid_free(dec);
119                    return XVID_ERR_MEMORY;
120            }
121            if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {
122                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
123                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
124                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
125                  xvid_free(dec);                  xvid_free(dec);
126                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
127          }          }
128    
129          dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);          dec->mbs =
130          if (dec->mbs == NULL)                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
131          {                                          CACHE_LINE);
132            if (dec->mbs == NULL) {
133                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
134                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
135                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
136                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
137                  xvid_free(dec);                  xvid_free(dec);
138                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
139          }          }
140    
141            memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
142    
143            // add by chenm001 <chenm001@163.com>
144            // for skip MB flag
145            dec->last_mbs =
146                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
147                                            CACHE_LINE);
148            if (dec->last_mbs == NULL) {
149                    xvid_free(dec->mbs);
150                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
151                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
152                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
153                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
154                    xvid_free(dec);
155                    return XVID_ERR_MEMORY;
156            }
157    
158            memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
159    
160          init_timer();          init_timer();
161    
162            // add by chenm001 <chenm001@163.com>
163            // for support B-frame to save reference frame's time
164            dec->frames = -1;
165            dec->time = dec->time_base = dec->last_time_base = 0;
166    
167          return XVID_ERR_OK;          return XVID_ERR_OK;
168  }  }
169    
170    
171  int decoder_destroy(DECODER * dec)  
172    /* ****************************************************************
173    NIC 28.06.2002
174            riscritta la 'decoder_create' per cambiarne l'interfaccia e poterla
175            usare nella dll caricata dall'IM1 player
176            IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame)
177    **************************************************************** */
178    //XVID_DEC_FRAME * frame
179    
180    int
181    IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame)
182    {
183            DECODER *dec;
184            Bitstream bs;
185            uint32_t rounding;
186            uint32_t quant;
187            uint32_t fcode_forward;
188            uint32_t fcode_backward;
189            uint32_t intra_dc_threshold;
190    
191            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
192            if (dec == NULL) {
193                    return XVID_ERR_MEMORY;
194            }
195            param->handle = dec;
196    
197    
198            BitstreamInit(&bs, frame->bitstream, frame->length);//NIC
199    
200            //NIC
201            BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,
202                                                             &fcode_backward, &intra_dc_threshold);
203    
204    
205            param->width = dec->width;                      //NIC added
206            param->height = dec->height;            //NIC added
207            //dec->width = param->width;            //NIC commentate
208            //dec->height = param->height;          //NIC commentate
209    
210            dec->mb_width = (dec->width + 15) / 16;
211            dec->mb_height = (dec->height + 15) / 16;
212    
213            dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
214            dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
215            dec->low_delay = 0;
216    
217            if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
218                    xvid_free(dec);
219                    return XVID_ERR_MEMORY;
220            }
221    
222            if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {
223                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
224                    xvid_free(dec);
225                    return XVID_ERR_MEMORY;
226            }
227            // add by chenm001 <chenm001@163.com>
228            // for support B-frame to reference last 2 frame
229            if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
230                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
231                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
232                    xvid_free(dec);
233                    return XVID_ERR_MEMORY;
234            }
235            if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {
236                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
237                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
238                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
239                    xvid_free(dec);
240                    return XVID_ERR_MEMORY;
241            }
242    
243            dec->mbs =
244                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
245                                            CACHE_LINE);
246            if (dec->mbs == NULL) {
247                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
248                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
249                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
250                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
251                    xvid_free(dec);
252                    return XVID_ERR_MEMORY;
253            }
254    
255            memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
256    
257            // add by chenm001 <chenm001@163.com>
258            // for skip MB flag
259            dec->last_mbs =
260                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
261                                            CACHE_LINE);
262            if (dec->last_mbs == NULL) {
263                    xvid_free(dec->mbs);
264                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
265                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
266                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
267                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
268                    xvid_free(dec);
269                    return XVID_ERR_MEMORY;
270            }
271    
272            memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
273    
274            init_timer();
275    
276            // add by chenm001 <chenm001@163.com>
277            // for support B-frame to save reference frame's time
278            dec->frames = -1;
279            dec->time = dec->time_base = dec->last_time_base = 0;
280    
281            return XVID_ERR_OK;
282    }
283    /* ****************************************************************
284                                                            END NIC
285    **************************************************************** */
286    
287    int
288    decoder_destroy(DECODER * dec)
289  {  {
290            xvid_free(dec->last_mbs);
291          xvid_free(dec->mbs);          xvid_free(dec->mbs);
292          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
293            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
294            image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
295          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
296          xvid_free(dec);          xvid_free(dec);
297    
# Line 139  Line 301 
301    
302    
303    
304  static const int32_t dquant_table[4] =  static const int32_t dquant_table[4] = {
 {  
305          -1, -2, 1, 2          -1, -2, 1, 2
306  };  };
307    
308    
309    
310    
311  // decode an intra macroblock  // decode an intra macroblock
312    
313  void decoder_mbintra(DECODER * dec,  void
314    decoder_mbintra(DECODER * dec,
315                       MACROBLOCK * pMB,                       MACROBLOCK * pMB,
316                       const uint32_t x_pos,                       const uint32_t x_pos,
317                       const uint32_t y_pos,                       const uint32_t y_pos,
# Line 174  Line 338 
338    
339          memset(block, 0, 6*64*sizeof(int16_t));         // clear          memset(block, 0, 6*64*sizeof(int16_t));         // clear
340    
341          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
342                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
343                  int16_t predictors[8];                  int16_t predictors[8];
344                  int start_coeff;                  int start_coeff;
345    
346                  start_timer();                  start_timer();
347                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i*64], iQuant, iDcScaler, predictors);                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64],
348                  if (!acpred_flag)                                           iQuant, iDcScaler, predictors);
349                  {                  if (!acpred_flag) {
350                          pMB->acpred_directions[i] = 0;                          pMB->acpred_directions[i] = 0;
351                  }                  }
352                  stop_prediction_timer();                  stop_prediction_timer();
353    
354                  if (quant < intra_dc_threshold)                  if (quant < intra_dc_threshold) {
                 {  
355                          int dc_size;                          int dc_size;
356                          int dc_dif;                          int dc_dif;
357    
358                          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);
359                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;
360    
361                          if (dc_size > 8)                          if (dc_size > 8) {
                         {  
362                                  BitstreamSkip(bs, 1);           // marker                                  BitstreamSkip(bs, 1);           // marker
363                          }                          }
364    
365                          block[i*64 + 0] = dc_dif;                          block[i*64 + 0] = dc_dif;
366                          start_coeff = 1;                          start_coeff = 1;
367                  }                  } else {
                 else  
                 {  
368                          start_coeff = 0;                          start_coeff = 0;
369                  }                  }
370    
371                  start_timer();                  start_timer();
372                  if (cbp & (1 << (5-i)))                 // coded                  if (cbp & (1 << (5-i)))                 // coded
373                  {                  {
374                          get_intra_block(bs, &block[i*64], pMB->acpred_directions[i], start_coeff);                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],
375                                                            start_coeff);
376                  }                  }
377                  stop_coding_timer();                  stop_coding_timer();
378    
# Line 221  Line 381 
381                  stop_prediction_timer();                  stop_prediction_timer();
382    
383                  start_timer();                  start_timer();
384                  if (dec->quant_type == 0)                  if (dec->quant_type == 0) {
                 {  
385                          dequant_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);                          dequant_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);
386                  }                  } else {
                 else  
                 {  
387                          dequant4_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);                          dequant4_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);
388                  }                  }
389                  stop_iquant_timer();                  stop_iquant_timer();
# Line 236  Line 393 
393                  stop_idct_timer();                  stop_idct_timer();
394          }          }
395    
396          if (dec->interlacing && pMB->field_dct)          if (dec->interlacing && pMB->field_dct) {
         {  
397                  next_block = stride;                  next_block = stride;
398                  stride *= 2;                  stride *= 2;
399          }          }
# Line 264  Line 420 
420    
421  // decode an inter macroblock  // decode an inter macroblock
422    
423  void decoder_mbinter(DECODER * dec,  void
424    decoder_mbinter(DECODER * dec,
425                       const MACROBLOCK * pMB,                       const MACROBLOCK * pMB,
426                       const uint32_t x_pos,                       const uint32_t x_pos,
427                       const uint32_t y_pos,                       const uint32_t y_pos,
# Line 290  Line 447 
447          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
448          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
449    
450          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
         {  
451                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
452                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
453    
454                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
455                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
456          }          } else {
         else  
         {  
457                  int sum;                  int sum;
458    
459                  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;
460                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                  uv_dx =
461                            (sum ==
462                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
463                                                                      (ABS(sum) / 16) * 2));
464    
465                  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;
466                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                  uv_dy =
467                            (sum ==
468                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
469                                                                      (ABS(sum) / 16) * 2));
470          }          }
471    
472          start_timer();          start_timer();
473          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, 16 * y_pos,
474          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding);
475          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,
476          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride,
477          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);                                                    rounding);
478          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos,
479                                                      16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,
480                                                      rounding);
481            interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,
482                                                      16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
483                                                      rounding);
484            interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
485                                                      uv_dx, uv_dy, stride2, rounding);
486            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
487                                                      uv_dx, uv_dy, stride2, rounding);
488          stop_comp_timer();          stop_comp_timer();
489    
490          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
491                  if (cbp & (1 << (5-i)))                 // coded                  if (cbp & (1 << (5-i)))                 // coded
492                  {                  {
493                          memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear                          memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear
# Line 328  Line 497 
497                          stop_coding_timer();                          stop_coding_timer();
498    
499                          start_timer();                          start_timer();
500                          if (dec->quant_type == 0)                          if (dec->quant_type == 0) {
                         {  
501                                  dequant_inter(&data[i*64], &block[i*64], iQuant);                                  dequant_inter(&data[i*64], &block[i*64], iQuant);
502                          }                          } else {
                         else  
                         {  
503                                  dequant4_inter(&data[i*64], &block[i*64], iQuant);                                  dequant4_inter(&data[i*64], &block[i*64], iQuant);
504                          }                          }
505                          stop_iquant_timer();                          stop_iquant_timer();
# Line 344  Line 510 
510                  }                  }
511          }          }
512    
513          if (dec->interlacing && pMB->field_dct)          if (dec->interlacing && pMB->field_dct) {
         {  
514                  next_block = stride;                  next_block = stride;
515                  stride *= 2;                  stride *= 2;
516          }          }
# Line 367  Line 532 
532  }  }
533    
534    
535  void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold)  void
536    decoder_iframe(DECODER * dec,
537                               Bitstream * bs,
538                               int quant,
539                               int intra_dc_threshold)
540  {  {
541    
542          uint32_t x, y;          uint32_t x, y;
543    
544          for (y = 0; y < dec->mb_height; y++)          for (y = 0; y < dec->mb_height; y++) {
545          {                  for (x = 0; x < dec->mb_width; x++) {
                 for (x = 0; x < dec->mb_width; x++)  
                 {  
546                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];
547    
548                          uint32_t mcbpc;                          uint32_t mcbpc;
# Line 390  Line 557 
557    
558                          acpred_flag = BitstreamGetBit(bs);                          acpred_flag = BitstreamGetBit(bs);
559    
560                          if (mb->mode == MODE_STUFFING)                          if (mb->mode == MODE_STUFFING) {
                         {  
561                                  DEBUG("-- STUFFING ?");                                  DEBUG("-- STUFFING ?");
562                                  continue;                                  continue;
563                          }                          }
# Line 399  Line 565 
565                          cbpy = get_cbpy(bs, 1);                          cbpy = get_cbpy(bs, 1);
566                          cbp = (cbpy << 2) | cbpc;                          cbp = (cbpy << 2) | cbpc;
567    
568                          if (mb->mode == MODE_INTRA_Q)                          if (mb->mode == MODE_INTRA_Q) {
                         {  
569                                  quant += dquant_table[BitstreamGetBits(bs,2)];                                  quant += dquant_table[BitstreamGetBits(bs,2)];
570                                  if (quant > 31)                                  if (quant > 31) {
                                 {  
571                                          quant = 31;                                          quant = 31;
572                                  }                                  } else if (quant < 1) {
                                 else if (quant < 1)  
                                 {  
573                                          quant = 1;                                          quant = 1;
574                                  }                                  }
575                          }                          }
576                          mb->quant = quant;                          mb->quant = quant;
577    
578                          if (dec->interlacing)                          if (dec->interlacing) {
                         {  
579                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
580                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DEBUG1("deci: field_dct: ", mb->field_dct);
581                          }                          }
582    
583                          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,
584                                                            intra_dc_threshold);
585                  }                  }
586          }          }
587    
588  }  }
589    
590    
591  void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode)  void
592    get_motion_vector(DECODER * dec,
593                                      Bitstream * bs,
594                                      int x,
595                                      int y,
596                                      int k,
597                                      VECTOR * mv,
598                                      int fcode)
599  {  {
600    
601          int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
# Line 435  Line 604 
604          int range = (64 * scale_fac);          int range = (64 * scale_fac);
605    
606          VECTOR pmv[4];          VECTOR pmv[4];
607          uint32_t psad[4];          int32_t psad[4];
608    
609          int mv_x, mv_y;          int mv_x, mv_y;
610          int pmv_x, pmv_y;          int pmv_x, pmv_y;
# Line 452  Line 621 
621          mv_x += pmv_x;          mv_x += pmv_x;
622          mv_y += pmv_y;          mv_y += pmv_y;
623    
624          if (mv_x < low)          if (mv_x < low) {
         {  
625                  mv_x += range;                  mv_x += range;
626          }          } else if (mv_x > high) {
         else if (mv_x > high)  
         {  
627                  mv_x -= range;                  mv_x -= range;
628          }          }
629    
630          if (mv_y < low)          if (mv_y < low) {
         {  
631                  mv_y += range;                  mv_y += range;
632          }          } else if (mv_y > high) {
         else if (mv_y > high)  
         {  
633                  mv_y -= range;                  mv_y -= range;
634          }          }
635    
# Line 476  Line 639 
639  }  }
640    
641    
642  void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold)  void
643    decoder_pframe(DECODER * dec,
644                               Bitstream * bs,
645                               int rounding,
646                               int quant,
647                               int fcode,
648                               int intra_dc_threshold)
649  {  {
650    
651          uint32_t x, y;          uint32_t x, y;
652    
653          start_timer();          start_timer();
654          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
655                                       dec->width, dec->height, dec->interlacing);
656          stop_edges_timer();          stop_edges_timer();
657    
658          for (y = 0; y < dec->mb_height; y++)          for (y = 0; y < dec->mb_height; y++) {
659          {                  for (x = 0; x < dec->mb_width; x++) {
                 for (x = 0; x < dec->mb_width; x++)  
                 {  
660                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];
661    
662                          if (!BitstreamGetBit(bs))                       // not_coded                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded
663                            if (!(BitstreamGetBit(bs)))     // not_coded
664                          {                          {
665                                  uint32_t mcbpc;                                  uint32_t mcbpc;
666                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 507  Line 676 
676    
677                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
678    
679                                  if (intra)                                  if (intra) {
                                 {  
680                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
681                                  }                                  }
682    
683                                  if (mb->mode == MODE_STUFFING)                                  if (mb->mode == MODE_STUFFING) {
                                 {  
684                                          DEBUG("-- STUFFING ?");                                          DEBUG("-- STUFFING ?");
685                                          continue;                                          continue;
686                                  }                                  }
# Line 521  Line 688 
688                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
689                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
690    
691                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q)                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
                                 {  
692                                          quant += dquant_table[BitstreamGetBits(bs,2)];                                          quant += dquant_table[BitstreamGetBits(bs,2)];
693                                          if (quant > 31)                                          if (quant > 31) {
                                         {  
694                                                  quant = 31;                                                  quant = 31;
695                                          }                                          } else if (mb->quant < 1) {
                                         else if (mb->quant < 1)  
                                         {  
696                                                  quant = 1;                                                  quant = 1;
697                                          }                                          }
698                                  }                                  }
699                                  mb->quant = quant;                                  mb->quant = quant;
700    
701                                  if (dec->interlacing)                                  if (dec->interlacing) {
                                 {  
702                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
703                                          DEBUG1("decp: field_dct: ", mb->field_dct);                                          DEBUG1("decp: field_dct: ", mb->field_dct);
704    
705                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
                                         {  
706                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
707                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DEBUG1("decp: field_pred: ", mb->field_pred);
708    
709                                                  if (mb->field_pred)                                                  if (mb->field_pred) {
                                                 {  
710                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
711                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);
712                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
# Line 555  Line 715 
715                                          }                                          }
716                                  }                                  }
717    
718                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
719                                  {                                          if (dec->interlacing && mb->field_pred) {
720                                          if (dec->interlacing && mb->field_pred)                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
721                                          {                                                                                    fcode);
722                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
723                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode);                                                                                    fcode);
724                                            } else {
725                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
726                                                                                      fcode);
727                                                    mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
728                                                            mb->mvs[0].x;
729                                                    mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
730                                                            mb->mvs[0].y;
731                                          }                                          }
732                                          else                                  } else if (mb->mode ==
733                                          {                                                     MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {
                                                 get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);  
                                                 mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;  
                                                 mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;  
                                         }  
                                 }  
                                 else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */)  
                                 {  
734                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);
735                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);
736                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);
737                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);
738                                  }                                  } else                  // MODE_INTRA, MODE_INTRA_Q
                                 else  // MODE_INTRA, MODE_INTRA_Q  
739                                  {                                  {
740                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
741                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                                  0;
742                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
743                                                    0;
744                                            decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
745                                                                            intra_dc_threshold);
746                                          continue;                                          continue;
747                                  }                                  }
748    
749                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, rounding);                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
750                          }                                                                  rounding);
751                          else    // not coded                          } else                          // not coded
752                          {                          {
753                                    //DEBUG2("P-frame MB at (X,Y)=",x,y);
754                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
755                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
756                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
# Line 597  Line 759 
759    
760                                  start_timer();                                  start_timer();
761    
762                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
763                                                   dec->refn[0].y + (16*y)*dec->edged_width + (16*x),                                                                   (16 * x),
764                                                   dec->edged_width);                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +
765                                                                     (16 * x), dec->edged_width);
766                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),  
767                                                   dec->refn[0].y + (16*y)*dec->edged_width + (16*x+8),                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
768                                                   dec->edged_width);                                                                   (16 * x + 8),
769                                                                     dec->refn[0].y + (16 * y) * dec->edged_width +
770                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x),                                                                   (16 * x + 8), dec->edged_width);
771                                                   dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x),  
772                                                   dec->edged_width);                                  transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
773                                                                     (16 * x),
774                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8),                                                                   dec->refn[0].y + (16 * y +
775                                                   dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x+8),                                                                                                     8) * dec->edged_width +
776                                                   dec->edged_width);                                                                   (16 * x), dec->edged_width);
777    
778                                  transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),                                  transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
779                                                   dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),                                                                   (16 * x + 8),
780                                                                     dec->refn[0].y + (16 * y +
781                                                                                                       8) * dec->edged_width +
782                                                                     (16 * x + 8), dec->edged_width);
783    
784                                    transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 +
785                                                                     (8 * x),
786                                                                     dec->refn[0].u +
787                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
788                                                   dec->edged_width/2);                                                   dec->edged_width/2);
789    
790                                  transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),                                  transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +
791                                                   dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),                                                                   (8 * x),
792                                                                     dec->refn[0].v +
793                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
794                                                   dec->edged_width/2);                                                   dec->edged_width/2);
795    
796                                  stop_transfer_timer();                                  stop_transfer_timer();
# Line 627  Line 799 
799          }          }
800  }  }
801    
802  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)  
803    // add by MinChen <chenm001@163.com>
804    // decode B-frame motion vector
805    void
806    get_b_motion_vector(DECODER * dec,
807                                            Bitstream * bs,
808                                            int x,
809                                            int y,
810                                            VECTOR * mv,
811                                            int fcode,
812                                            const VECTOR pmv)
813    {
814            int scale_fac = 1 << (fcode - 1);
815            int high = (32 * scale_fac) - 1;
816            int low = ((-32) * scale_fac);
817            int range = (64 * scale_fac);
818    
819            int mv_x, mv_y;
820            int pmv_x, pmv_y;
821    
822            pmv_x = pmv.x;
823            pmv_y = pmv.y;
824    
825            mv_x = get_mv(bs, fcode);
826            mv_y = get_mv(bs, fcode);
827    
828            mv_x += pmv_x;
829            mv_y += pmv_y;
830    
831            if (mv_x < low) {
832                    mv_x += range;
833            } else if (mv_x > high) {
834                    mv_x -= range;
835            }
836    
837            if (mv_y < low) {
838                    mv_y += range;
839            } else if (mv_y > high) {
840                    mv_y -= range;
841            }
842    
843            mv->x = mv_x;
844            mv->y = mv_y;
845    }
846    
847    
848    // add by MinChen <chenm001@163.com>
849    // decode an B-frame forward & backward inter macroblock
850    void
851    decoder_bf_mbinter(DECODER * dec,
852                                       const MACROBLOCK * pMB,
853                                       const uint32_t x_pos,
854                                       const uint32_t y_pos,
855                                       const uint32_t cbp,
856                                       Bitstream * bs,
857                                       const uint32_t quant,
858                                       const uint8_t ref)
859    {
860    
861            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
862            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
863    
864            uint32_t stride = dec->edged_width;
865            uint32_t stride2 = stride / 2;
866            uint32_t next_block = stride * 8;
867            uint32_t i;
868            uint32_t iQuant = pMB->quant;
869            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
870            int uv_dx, uv_dy;
871    
872            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
873            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
874            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
875    
876            if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
877                    uv_dx = pMB->mvs[0].x;
878                    uv_dy = pMB->mvs[0].y;
879    
880                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
881                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
882            } else {
883                    int sum;
884    
885                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
886                    uv_dx =
887                            (sum ==
888                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
889                                                                      (ABS(sum) / 16) * 2));
890    
891                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
892                    uv_dy =
893                            (sum ==
894                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
895                                                                      (ABS(sum) / 16) * 2));
896            }
897    
898            start_timer();
899            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,
900                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
901            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,
902                                                      16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
903            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos,
904                                                      16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,
905                                                      0);
906            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,
907                                                      16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
908                                                      0);
909            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
910                                                      uv_dx, uv_dy, stride2, 0);
911            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
912                                                      uv_dx, uv_dy, stride2, 0);
913            stop_comp_timer();
914    
915            for (i = 0; i < 6; i++) {
916                    if (cbp & (1 << (5 - i)))       // coded
917                    {
918                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
919    
920                            start_timer();
921                            get_inter_block(bs, &block[i * 64]);
922                            stop_coding_timer();
923    
924                            start_timer();
925                            if (dec->quant_type == 0) {
926                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
927                            } else {
928                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
929                            }
930                            stop_iquant_timer();
931    
932                            start_timer();
933                            idct(&data[i * 64]);
934                            stop_idct_timer();
935                    }
936            }
937    
938            if (dec->interlacing && pMB->field_dct) {
939                    next_block = stride;
940                    stride *= 2;
941            }
942    
943            start_timer();
944            if (cbp & 32)
945                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
946            if (cbp & 16)
947                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
948            if (cbp & 8)
949                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
950            if (cbp & 4)
951                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
952            if (cbp & 2)
953                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
954            if (cbp & 1)
955                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
956            stop_transfer_timer();
957    }
958    
959    
960    // add by MinChen <chenm001@163.com>
961    // decode an B-frame direct &  inter macroblock
962    void
963    decoder_bf_interpolate_mbinter(DECODER * dec,
964                                                               IMAGE forward,
965                                                               IMAGE backward,
966                                                               const MACROBLOCK * pMB,
967                                                               const uint32_t x_pos,
968                                                               const uint32_t y_pos,
969                                                               const uint32_t cbp,
970                                                               Bitstream * bs)
971    {
972    
973            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
974            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
975    
976            uint32_t stride = dec->edged_width;
977            uint32_t stride2 = stride / 2;
978            uint32_t next_block = stride * 8;
979            uint32_t iQuant = pMB->quant;
980            int uv_dx, uv_dy;
981            int b_uv_dx, b_uv_dy;
982            uint32_t i;
983            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
984    
985            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
986            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
987            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
988    
989            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
990                    uv_dx = pMB->mvs[0].x;
991                    uv_dy = pMB->mvs[0].y;
992    
993                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
994                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
995    
996                    b_uv_dx = pMB->b_mvs[0].x;
997                    b_uv_dy = pMB->b_mvs[0].y;
998    
999                    b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
1000                    b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
1001            } else {
1002                    int sum;
1003    
1004                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1005                    uv_dx =
1006                            (sum ==
1007                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
1008                                                                      (ABS(sum) / 16) * 2));
1009    
1010                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1011                    uv_dy =
1012                            (sum ==
1013                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
1014                                                                      (ABS(sum) / 16) * 2));
1015    
1016                    sum =
1017                            pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +
1018                            pMB->b_mvs[3].x;
1019                    b_uv_dx =
1020                            (sum ==
1021                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
1022                                                                      (ABS(sum) / 16) * 2));
1023    
1024                    sum =
1025                            pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y +
1026                            pMB->b_mvs[3].y;
1027                    b_uv_dy =
1028                            (sum ==
1029                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
1030                                                                      (ABS(sum) / 16) * 2));
1031            }
1032    
1033    
1034            start_timer();
1035            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1036                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1037            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
1038                                                      pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1039            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1040                                                      pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1041            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1042                                                      16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1043                                                      0);
1044            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1045                                                      uv_dy, stride2, 0);
1046            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1047                                                      uv_dy, stride2, 0);
1048    
1049    
1050            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,
1051                                                      pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1052            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
1053                                                      16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1054                                                      0);
1055            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,
1056                                                      16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1057                                                      stride, 0);
1058            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
1059                                                      16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1060                                                      stride, 0);
1061            interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,
1062                                                      b_uv_dx, b_uv_dy, stride2, 0);
1063            interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,
1064                                                      b_uv_dx, b_uv_dy, stride2, 0);
1065    
1066            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos,
1067                                             stride);
1068            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos,
1069                                             stride);
1070            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,
1071                                             stride);
1072            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,
1073                                             16 * y_pos + 8, stride);
1074            interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,
1075                                             stride2);
1076            interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos,
1077                                             stride2);
1078    
1079            stop_comp_timer();
1080    
1081            for (i = 0; i < 6; i++) {
1082                    if (cbp & (1 << (5 - i)))       // coded
1083                    {
1084                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1085    
1086                            start_timer();
1087                            get_inter_block(bs, &block[i * 64]);
1088                            stop_coding_timer();
1089    
1090                            start_timer();
1091                            if (dec->quant_type == 0) {
1092                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1093                            } else {
1094                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1095                            }
1096                            stop_iquant_timer();
1097    
1098                            start_timer();
1099                            idct(&data[i * 64]);
1100                            stop_idct_timer();
1101                    }
1102            }
1103    
1104            if (dec->interlacing && pMB->field_dct) {
1105                    next_block = stride;
1106                    stride *= 2;
1107            }
1108    
1109            start_timer();
1110            if (cbp & 32)
1111                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1112            if (cbp & 16)
1113                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1114            if (cbp & 8)
1115                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1116            if (cbp & 4)
1117                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1118            if (cbp & 2)
1119                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1120            if (cbp & 1)
1121                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1122            stop_transfer_timer();
1123    }
1124    
1125    
1126    // add by MinChen <chenm001@163.com>
1127    // for decode B-frame dbquant
1128    int32_t __inline
1129    get_dbquant(Bitstream * bs)
1130    {
1131            if (!BitstreamGetBit(bs))       // '0'
1132                    return (0);
1133            else if (!BitstreamGetBit(bs))  // '10'
1134                    return (-2);
1135            else
1136                    return (2);                             // '11'
1137    }
1138    
1139    // add by MinChen <chenm001@163.com>
1140    // for decode B-frame mb_type
1141    // bit   ret_value
1142    // 1        0
1143    // 01       1
1144    // 001      2
1145    // 0001     3
1146    int32_t __inline
1147    get_mbtype(Bitstream * bs)
1148    {
1149            int32_t mb_type;
1150    
1151            for (mb_type = 0; mb_type <= 3; mb_type++) {
1152                    if (BitstreamGetBit(bs))
1153                            break;
1154            }
1155    
1156            if (mb_type <= 3)
1157                    return (mb_type);
1158            else
1159                    return (-1);
1160    }
1161    
1162    void
1163    decoder_bframe(DECODER * dec,
1164                               Bitstream * bs,
1165                               int quant,
1166                               int fcode_forward,
1167                               int fcode_backward)
1168    {
1169    
1170            uint32_t x, y;
1171            VECTOR mv, zeromv;
1172    
1173            start_timer();
1174            image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1175                                       dec->width, dec->height, dec->interlacing);
1176            //image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);
1177            stop_edges_timer();
1178    
1179    
1180            for (y = 0; y < dec->mb_height; y++) {
1181                    // Initialize Pred Motion Vector
1182                    dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0;
1183                    for (x = 0; x < dec->mb_width; x++) {
1184                            MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1185                            MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1186    
1187                            mb->mvs[0].x = mb->mvs[0].y = zeromv.x = zeromv.y = mv.x = mv.y =
1188                                    0;
1189    
1190                            // the last P_VOP is skip macroblock ?
1191                            if (last_mb->mode == MODE_NOT_CODED) {
1192                                    //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1193                                    mb->mb_type = MODE_FORWARD;
1194                                    mb->cbp = 0;
1195                                    mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1196                                    mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1197                                    mb->quant = 8;
1198    
1199                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1200                                    continue;
1201                            }
1202                            //t=BitstreamShowBits(bs,32);
1203    
1204                            if (!BitstreamGetBit(bs)) {     // modb=='0'
1205                                    const uint8_t modb2 = BitstreamGetBit(bs);
1206    
1207                                    mb->mb_type = get_mbtype(bs);
1208    
1209                                    if (!modb2) {   // modb=='00'
1210                                            mb->cbp = BitstreamGetBits(bs, 6);
1211                                    } else {
1212                                            mb->cbp = 0;
1213                                    }
1214                                    if (mb->mb_type && mb->cbp) {
1215                                            quant += get_dbquant(bs);
1216    
1217                                            if (quant > 31) {
1218                                                    quant = 31;
1219                                            } else if (mb->quant < 1) {
1220                                                    quant = 1;
1221                                            }
1222                                    } else {
1223                                            quant = 8;
1224                                    }
1225                                    mb->quant = quant;
1226                            } else {
1227                                    mb->mb_type = MODE_DIRECT_NONE_MV;
1228                                    mb->cbp = 0;
1229                            }
1230    
1231                            mb->mode = MODE_INTER;
1232                            //DEBUG1("Switch bm_type=",mb->mb_type);
1233    
1234                            switch (mb->mb_type) {
1235                            case MODE_DIRECT:
1236                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv);
1237    
1238                            case MODE_DIRECT_NONE_MV:
1239                                    {                               // Because this file is a C file not C++ so I use '{' to define var
1240                                            const int64_t TRB = dec->time_pp - dec->time_bp, TRD =
1241                                                    dec->time_pp;
1242                                            int i;
1243    
1244                                            for (i = 0; i < 4; i++) {
1245                                                    mb->mvs[i].x =
1246                                                            (int32_t) ((TRB * last_mb->mvs[i].x) / TRD +
1247                                                                               mb->mvs[0].x);
1248                                                    mb->b_mvs[i].x =
1249                                                            (int32_t) ((mb->mvs[0].x ==
1250                                                                                    0) ? ((TRB -
1251                                                                                               TRD) * last_mb->mvs[i].x) /
1252                                                                               TRD : mb->mvs[i].x - last_mb->mvs[i].x);
1253                                                    mb->mvs[i].y =
1254                                                            (int32_t) ((TRB * last_mb->mvs[i].y) / TRD +
1255                                                                               mb->mvs[0].y);
1256                                                    mb->b_mvs[i].y =
1257                                                            (int32_t) ((mb->mvs[0].y ==
1258                                                                                    0) ? ((TRB -
1259                                                                                               TRD) * last_mb->mvs[i].y) /
1260                                                                               TRD : mb->mvs[i].y - last_mb->mvs[i].y);
1261                                            }
1262                                            //DEBUG("B-frame Direct!\n");
1263                                    }
1264                                    mb->mode = MODE_INTER4V;
1265                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1266                                                                                               mb, x, y, mb->cbp, bs);
1267                                    break;
1268    
1269                            case MODE_INTERPOLATE:
1270                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1271                                                                            dec->p_fmv);
1272                                    dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
1273                                            mb->mvs[0].x;
1274                                    dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
1275                                            mb->mvs[0].y;
1276    
1277                                    get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1278                                                                            fcode_backward, dec->p_bmv);
1279                                    dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x =
1280                                            mb->b_mvs[3].x = mb->b_mvs[0].x;
1281                                    dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y =
1282                                            mb->b_mvs[3].y = mb->b_mvs[0].y;
1283    
1284                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1285                                                                                               mb, x, y, mb->cbp, bs);
1286                                    //DEBUG("B-frame Bidir!\n");
1287                                    break;
1288    
1289                            case MODE_BACKWARD:
1290                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1291                                                                            dec->p_bmv);
1292                                    dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
1293                                            mb->mvs[0].x;
1294                                    dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
1295                                            mb->mvs[0].y;
1296    
1297                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1298                                    //DEBUG("B-frame Backward!\n");
1299                                    break;
1300    
1301                            case MODE_FORWARD:
1302                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1303                                                                            dec->p_fmv);
1304                                    dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
1305                                            mb->mvs[0].x;
1306                                    dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
1307                                            mb->mvs[0].y;
1308    
1309                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1310                                    //DEBUG("B-frame Forward!\n");
1311                                    break;
1312    
1313                            default:
1314                                    DEBUG1("Not support B-frame mb_type =", mb->mb_type);
1315                            }
1316    
1317                    }                                               // end of FOR
1318            }
1319    }
1320    
1321    // swap two MACROBLOCK array
1322    void
1323    mb_swap(MACROBLOCK ** mb1,
1324                    MACROBLOCK ** mb2)
1325    {
1326            MACROBLOCK *temp = *mb1;
1327    
1328            *mb1 = *mb2;
1329            *mb2 = temp;
1330    }
1331    
1332    int
1333    decoder_decode(DECODER * dec,
1334                               XVID_DEC_FRAME * frame)
1335  {  {
1336    
1337          Bitstream bs;          Bitstream bs;
1338          uint32_t rounding;          uint32_t rounding;
1339          uint32_t quant;          uint32_t quant;
1340          uint32_t fcode;          uint32_t fcode_forward;
1341            uint32_t fcode_backward;
1342          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1343          uint32_t vop_type;          uint32_t vop_type;
1344    
# Line 643  Line 1348 
1348    
1349          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
1350          // for support B-frame to reference last 2 frame          // for support B-frame to reference last 2 frame
1351          vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold);          dec->frames++;
1352            vop_type =
1353                    BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,
1354                                                             &fcode_backward, &intra_dc_threshold);
1355    
1356          if (vop_type==I_VOP || vop_type==P_VOP){          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0
                 image_swap(&dec->refn[0], &dec->refn[1]);  
                 image_swap(&dec->cur, &dec->refn[0]);  
         }  
1357    
1358          switch (vop_type)          switch (vop_type) {
         {  
1359          case P_VOP :          case P_VOP :
1360                  decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold);                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,
1361                                               intra_dc_threshold);
1362                    printf("P_VOP  Time=%ld\n",dec->time);
1363                    DEBUG1("P_VOP  Time=", dec->time);
1364                  break;                  break;
1365    
1366          case I_VOP :          case I_VOP :
                 //DEBUG1("",intra_dc_threshold);  
1367                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1368                    printf("I_VOP  Time=%ld\n",dec->time);
1369                    DEBUG1("I_VOP  Time=", dec->time);
1370                  break;                  break;
1371    
1372          case B_VOP :    // ignore          case B_VOP:
1373    #ifdef BFRAMES_DEC
1374                    if (dec->time_pp > dec->time_bp) {
1375                            printf("I_VOP  Time=%ld\n",dec->time);
1376                            DEBUG1("B_VOP  Time=", dec->time);
1377                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1378                    } else {
1379                            DEBUG("broken B-frame!");
1380                    }
1381    #else
1382                    printf("Che minchia ne so\n");
1383                    image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1384    #endif
1385                  break;                  break;
1386    
1387          case N_VOP :    // vop not coded          case N_VOP :    // vop not coded
1388                    // when low_delay==0, N_VOP's should interpolate between the past and future frames
1389                    image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1390                    printf("N_VOP vop not coded\n");
1391                  break;                  break;
1392    
1393          default :          default :
# Line 673  Line 1396 
1396    
1397          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1398    
1399          start_timer();  #ifdef BFRAMES_DEC
1400            // test if no B_VOP
1401            if (dec->low_delay) {
1402    #endif
1403          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
1404                       frame->image, frame->stride, frame->colorspace);                       frame->image, frame->stride, frame->colorspace);
1405    #ifdef BFRAMES_DEC
1406            } else {
1407                    if (dec->frames >= 1) {
1408                            start_timer();
1409                            if ((vop_type == I_VOP || vop_type == P_VOP)) {
1410                                    image_output(&dec->refn[0], dec->width, dec->height,
1411                                                             dec->edged_width, frame->image, frame->stride,
1412                                                             frame->colorspace);
1413                            } else if (vop_type == B_VOP) {
1414                                    image_output(&dec->cur, dec->width, dec->height,
1415                                                             dec->edged_width, frame->image, frame->stride,
1416                                                             frame->colorspace);
1417                            }
1418          stop_conv_timer();          stop_conv_timer();
1419                    }
1420            }
1421    #endif
1422    
1423            if (vop_type == I_VOP || vop_type == P_VOP) {
1424                    image_swap(&dec->refn[0], &dec->refn[1]);
1425                    image_swap(&dec->cur, &dec->refn[0]);
1426                    // swap MACROBLOCK
1427                    if (dec->low_delay && vop_type == P_VOP)
1428                            mb_swap(&dec->mbs, &dec->last_mbs);
1429            }
1430    
1431          emms();          emms();
1432    
1433          stop_global_timer();          stop_global_timer();
1434    
1435          return XVID_ERR_OK;          return XVID_ERR_OK;
   
1436  }  }

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.25

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