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

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

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

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

Legend:
Removed from v.133  
changed lines
  Added in v.248

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