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

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.49.2.24

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