[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.49.2.17, Thu Oct 16 12:16:00 2003 UTC revision 1.61, Sat Jul 10 17:49:31 2004 UTC
# Line 4  Line 4 
4   *  - Decoder Module -   *  - Decoder Module -
5   *   *
6   *  Copyright(C) 2002      MinChen <chenm001@163.com>   *  Copyright(C) 2002      MinChen <chenm001@163.com>
7   *               2002-2003 Peter Ross <pross@xvid.org>   *               2002-2004 Peter Ross <pross@xvid.org>
8   *   *
9   *  This program is 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
# Line 41  Line 41 
41  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
42    
43  #include "quant/quant.h"  #include "quant/quant.h"
44    #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"
# Line 57  Line 58 
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  static int  static int
# Line 75  Line 77 
77                  xvid_free(dec->last_mbs);                  xvid_free(dec->last_mbs);
78          if (dec->mbs)          if (dec->mbs)
79                  xvid_free(dec->mbs);                  xvid_free(dec->mbs);
80            if (dec->qscale)
81                    xvid_free(dec->qscale);
82    
83          /* realloc */          /* realloc */
84          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
# Line 159  Line 163 
163    
164          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
165    
166            /* nothing happens if that fails */
167            dec->qscale =
168                    xvid_malloc(sizeof(int) * dec->mb_width * dec->mb_height, CACHE_LINE);
169    
170            if (dec->qscale)
171                    memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height);
172    
173          return 0;          return 0;
174  }  }
175    
# Line 175  Line 186 
186          if (dec == NULL) {          if (dec == NULL) {
187                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
188          }          }
189    
190          memset(dec, 0, sizeof(DECODER));          memset(dec, 0, sizeof(DECODER));
191    
192            dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
193            if (dec->mpeg_quant_matrices == NULL) {
194                    xvid_free(dec);
195                    return XVID_ERR_MEMORY;
196            }
197    
198          create->handle = dec;          create->handle = dec;
199    
200          dec->width = create->width;          dec->width = create->width;
# Line 191  Line 209 
209          /* image based GMC */          /* image based GMC */
210          image_null(&dec->gmc);          image_null(&dec->gmc);
211    
   
212          dec->mbs = NULL;          dec->mbs = NULL;
213          dec->last_mbs = NULL;          dec->last_mbs = NULL;
214            dec->qscale = NULL;
215    
216          init_timer();          init_timer();
217            init_postproc(&dec->postproc);
218            init_mpeg_matrix(dec->mpeg_quant_matrices);
219    
220          /* For B-frame support (used to save reference frame's time */          /* For B-frame support (used to save reference frame's time */
221          dec->frames = 0;          dec->frames = 0;
222          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
223          dec->low_delay = 0;          dec->low_delay = 0;
224          dec->packed_mode = 0;          dec->packed_mode = 0;
225            dec->time_inc_resolution = 1; /* until VOL header says otherwise */
226    
227          dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);          dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
228    
# Line 217  Line 238 
238  {  {
239          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
240          xvid_free(dec->mbs);          xvid_free(dec->mbs);
241            xvid_free(dec->qscale);
242    
243          /* image based GMC */          /* image based GMC */
244          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
# Line 226  Line 248 
248          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
249          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
250          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
251            xvid_free(dec->mpeg_quant_matrices);
252          xvid_free(dec);          xvid_free(dec);
253    
254          write_timer();          write_timer();
# Line 316  Line 339 
339                  stop_coding_timer();                  stop_coding_timer();
340    
341                  start_timer();                  start_timer();
342                  add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors);                  add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors, dec->bs_version);
343                  stop_prediction_timer();                  stop_prediction_timer();
344    
345                  start_timer();                  start_timer();
346                  if (dec->quant_type == 0) {                  if (dec->quant_type == 0) {
347                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
348                  } else {                  } else {
349                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
350                  }                  }
351                  stop_iquant_timer();                  stop_iquant_timer();
352    
# Line 370  Line 393 
393                                  const int reduced_resolution,                                  const int reduced_resolution,
394                                  const MACROBLOCK * pMB)                                  const MACROBLOCK * pMB)
395  {  {
         DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE);  
396          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
397    
398          int stride = dec->edged_width, next_block = stride * (reduced_resolution ? 16 : 8);          int stride = dec->edged_width;
399            int next_block = stride * (reduced_resolution ? 16 : 8);
400          const int stride2 = stride/2;          const int stride2 = stride/2;
401          int i;          int i;
402          const uint32_t iQuant = pMB->quant;          const uint32_t iQuant = pMB->quant;
403          const int direction = dec->alternate_vertical_scan ? 2 : 0;          const int direction = dec->alternate_vertical_scan ? 2 : 0;
404          const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter;          typedef void (*get_inter_block_function_t)(
405                            Bitstream * bs,
406                            int16_t * block,
407                            int direction,
408                            const int quant,
409                            const uint16_t *matrix);
410    
411            const get_inter_block_function_t get_inter_block = (dec->quant_type == 0)
412                    ? get_inter_block_h263
413                    : get_inter_block_mpeg;
414    
415            memset(&data[0], 0, 6*64*sizeof(int16_t));      /* clear */
416    
417          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
418    
419                  if (cbp & (1 << (5 - i))) {     /* coded */                  if (cbp & (1 << (5 - i))) {     /* coded */
420    
                         memset(block, 0, 64 * sizeof(int16_t)); /* clear */  
421    
422                            /* Decode coeffs and dequantize on the fly */
423                          start_timer();                          start_timer();
424                          get_inter_block(bs, block, direction);                          get_inter_block(bs, &data[i*64], direction, iQuant, get_inter_matrix(dec->mpeg_quant_matrices));
425                          stop_coding_timer();                          stop_coding_timer();
426    
427                          start_timer();                          start_timer();
                         dequant(&data[i * 64], block, iQuant);  
                         stop_iquant_timer();  
   
                         start_timer();  
428                          idct(&data[i * 64]);                          idct(&data[i * 64]);
429                          stop_idct_timer();                          stop_idct_timer();
430                  }                  }
# Line 450  Line 480 
480  {  {
481          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
482          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * (reduced_resolution ? 16 : 8);  
483          uint32_t i;          uint32_t i;
484    
485          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
# Line 474  Line 503 
503                          mv[i] = pMB->mvs[i];                          mv[i] = pMB->mvs[i];
504          }          }
505    
506            for (i = 0; i < 4; i++) {
507                    /* clip to valid range */
508                    int border = (int)(dec->mb_width - x_pos) << (5 + dec->quarterpel);
509                    if (mv[i].x > border) {
510                            DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", mv[i].x, border, x_pos, y_pos);
511                            mv[i].x = border;
512                    } else {
513                            border = (-(int)x_pos-1) << (5 + dec->quarterpel);
514                            if (mv[i].x < border) {
515                                    DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", mv[i].x, border, x_pos, y_pos);
516                                    mv[i].x = border;
517                            }
518                    }
519    
520                    border = (int)(dec->mb_height - y_pos) << (5 + dec->quarterpel);
521                    if (mv[i].y >  border) {
522                            DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", mv[i].y, border, x_pos, y_pos);
523                            mv[i].y = border;
524                    } else {
525                            border = (-(int)y_pos-1) << (5 + dec->quarterpel);
526                            if (mv[i].y < border) {
527                                    DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", mv[i].y, border, x_pos, y_pos);
528                                    mv[i].y = border;
529                            }
530                    }
531            }
532    
533          start_timer();          start_timer();
534    
535          if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */          if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */
# Line 765  Line 821 
821                  mb_height = (dec->height + 31) / 32;                  mb_height = (dec->height + 31) / 32;
822          }          }
823    
824            if (!dec->is_edged[0]) {
825          start_timer();          start_timer();
826          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
827                                          dec->width, dec->height);                                                  dec->width, dec->height, dec->bs_version);
828                    dec->is_edged[0] = 1;
829          stop_edges_timer();          stop_edges_timer();
830            }
831    
832          if (gmc_warp) {          if (gmc_warp) {
833                  /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */                  /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
# Line 839  Line 898 
898                                  mb->quant = quant;                                  mb->quant = quant;
899    
900                                  if (dec->interlacing) {                                  if (dec->interlacing) {
901                                          if ((cbp || intra) && !mcsel) {                                          if (cbp || intra) {
902                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
903                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
904                                          }                                          }
905    
906                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if ((mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) && !mcsel) {
907                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
908                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
909    
# Line 888  Line 947 
947    
948                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
949                                  mb->mode = MODE_NOT_CODED_GMC;                                  mb->mode = MODE_NOT_CODED_GMC;
950                                    mb->quant = quant;
951                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
952    
953                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
# Line 897  Line 957 
957                                  st_mb = x+1;                                  st_mb = x+1;
958                          } else {        /* not coded P_VOP macroblock */                          } else {        /* not coded P_VOP macroblock */
959                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
960                                    mb->quant = quant;
961    
962                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
963                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
# Line 923  Line 984 
984  get_b_motion_vector(Bitstream * bs,  get_b_motion_vector(Bitstream * bs,
985                                          VECTOR * mv,                                          VECTOR * mv,
986                                          int fcode,                                          int fcode,
987                                          const VECTOR pmv)                                          const VECTOR pmv,
988                                            const DECODER * const dec,
989                                            const int x, const int y)
990  {  {
991          const int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
992          const int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
# Line 963  Line 1026 
1026  {  {
1027          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
1028          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * 8;  
1029          int uv_dx, uv_dy;          int uv_dx, uv_dy;
1030          int b_uv_dx, b_uv_dy;          int b_uv_dx, b_uv_dy;
1031          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
# Line 1039  Line 1101 
1101                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1102                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1103                                                          pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                          pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1104                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos + 8,
1105                                                          16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                          pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1106          }          }
1107    
1108          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
# Line 1162  Line 1224 
1224          uint32_t x, y;          uint32_t x, y;
1225          VECTOR mv;          VECTOR mv;
1226          const VECTOR zeromv = {0,0};          const VECTOR zeromv = {0,0};
         const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;  
1227          int i;          int i;
1228    
1229  #ifdef BFRAMES_DEC_DEBUG          if (!dec->is_edged[0]) {
1230          FILE *fp;                  start_timer();
1231          static char first=0;                  image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1232  #define BFRAME_DEBUG                                                  dec->width, dec->height, dec->bs_version);
1233          if (!first && fp) { \                  dec->is_edged[0] = 1;
1234                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \                  stop_edges_timer();
1235          }          }
 #endif  
1236    
1237            if (!dec->is_edged[1]) {
1238          start_timer();          start_timer();
         image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,  
                                         dec->width, dec->height);  
1239          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1240                                          dec->width, dec->height);                                                  dec->width, dec->height, dec->bs_version);
1241                    dec->is_edged[1] = 1;
1242          stop_edges_timer();          stop_edges_timer();
   
 #ifdef BFRAMES_DEC_DEBUG  
         if (!first){  
                 fp=fopen("C:\\XVIDDBG.TXT","w");  
1243          }          }
 #endif  
1244    
1245          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1246                  /* Initialize Pred Motion Vector */                  /* Initialize Pred Motion Vector */
# Line 1193  Line 1248 
1248                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1249                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1250                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1251                            const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;
1252                            uint32_t intra_dc_threshold; /* fake variable */
1253    
1254                            if (check_resync_marker(bs, fcode_max  - 1)) {
1255                                    int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,
1256                                                                                                             &fcode_forward, &fcode_backward, &intra_dc_threshold);
1257                                    x = bound % dec->mb_width;
1258                                    y = bound / dec->mb_width;
1259                                    /* reset predicted macroblocks */
1260                                    dec->p_fmv = dec->p_bmv = zeromv;
1261                            }
1262    
1263                          mv =                          mv =
1264                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
# Line 1257  Line 1323 
1323    
1324                          switch (mb->mode) {                          switch (mb->mode) {
1325                          case MODE_DIRECT:                          case MODE_DIRECT:
1326                                  get_b_motion_vector(bs, &mv, 1, zeromv);                                  get_b_motion_vector(bs, &mv, 1, zeromv, dec, x, y);
1327    
1328                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
1329                                  for (i = 0; i < 4; i++) {                                  for (i = 0; i < 4; i++) {
1330                                          mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + mv.x);                                          mb->mvs[i].x = last_mb->mvs[i].x*dec->time_bp/dec->time_pp + mv.x;
1331                                          mb->b_mvs[i].x = (int32_t) ((mv.x == 0)                                          mb->mvs[i].y = last_mb->mvs[i].y*dec->time_bp/dec->time_pp + mv.y;
1332                                                                          ? ((TRB - TRD) * last_mb->mvs[i].x) / TRD  
1333                                                                          : mb->mvs[i].x - last_mb->mvs[i].x);                                          mb->b_mvs[i].x = (mv.x)
1334                                          mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y);                                                  ?  mb->mvs[i].x - last_mb->mvs[i].x
1335                                          mb->b_mvs[i].y = (int32_t) ((mv.y == 0)                                                  : last_mb->mvs[i].x*(dec->time_bp - dec->time_pp)/dec->time_pp;
1336                                                                          ? ((TRB - TRD) * last_mb->mvs[i].y) / TRD                                          mb->b_mvs[i].y = (mv.y)
1337                                                                          : mb->mvs[i].y - last_mb->mvs[i].y);                                                  ? mb->mvs[i].y - last_mb->mvs[i].y
1338                                                    : last_mb->mvs[i].y*(dec->time_bp - dec->time_pp)/dec->time_pp;
1339                                  }                                  }
1340    
1341                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
# Line 1276  Line 1343 
1343                                  break;                                  break;
1344    
1345                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1346                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
1347                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1348    
1349                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv);                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
1350                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];
1351    
1352                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
# Line 1287  Line 1354 
1354                                  break;                                  break;
1355    
1356                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1357                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
1358                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1359    
1360                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);
1361                                  break;                                  break;
1362    
1363                          case MODE_FORWARD:                          case MODE_FORWARD:
1364                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
1365                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1366    
1367                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
# Line 1305  Line 1372 
1372                          }                          }
1373                  } /* End of for */                  } /* End of for */
1374          }          }
   
 #ifdef BFRAMES_DEC_DEBUG  
         if (!first){  
                 first=1;  
                 if (fp)  
                         fclose(fp);  
         }  
 #endif  
1375  }  }
1376    
1377  /* perform post processing if necessary, and output the image */  /* perform post processing if necessary, and output the image */
1378  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1379                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats,
1380                                            int coding_type, int quant)
1381  {  {
1382            const int brightness = XVID_VERSION_MINOR(frame->version) >= 1 ? frame->brightness : 0;
1383    
1384            if (dec->cartoon_mode)
1385                    frame->general &= ~XVID_FILMEFFECT;
1386    
1387            if ((frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) || brightness!=0)
1388                    && mbs != NULL) /* post process */
1389            {
1390                    /* note: image is stored to tmp */
1391                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1392                    image_postproc(&dec->postproc, &dec->tmp, dec->edged_width,
1393                                               mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1394                                               frame->general, brightness, dec->frames, (coding_type == B_VOP));
1395                    img = &dec->tmp;
1396            }
1397    
1398          image_output(img, dec->width, dec->height,          image_output(img, dec->width, dec->height,
1399                                   dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,                                   dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1400                                   frame->output.csp, dec->interlacing);                                   frame->output.csp, dec->interlacing);
# Line 1327  Line 1403 
1403                  stats->type = coding2type(coding_type);                  stats->type = coding2type(coding_type);
1404                  stats->data.vop.time_base = (int)dec->time_base;                  stats->data.vop.time_base = (int)dec->time_base;
1405                  stats->data.vop.time_increment = 0;     /* XXX: todo */                  stats->data.vop.time_increment = 0;     /* XXX: todo */
1406                    stats->data.vop.qscale_stride = dec->mb_width;
1407                    stats->data.vop.qscale = dec->qscale;
1408                    if (stats->data.vop.qscale != NULL && mbs != NULL) {
1409                            int i;
1410                            for (i = 0; i < dec->mb_width*dec->mb_height; i++)
1411                                    stats->data.vop.qscale[i] = mbs[i].quant;
1412                    } else
1413                            stats->data.vop.qscale = NULL;
1414          }          }
1415  }  }
1416    
# Line 1339  Line 1423 
1423          Bitstream bs;          Bitstream bs;
1424          uint32_t rounding;          uint32_t rounding;
1425          uint32_t reduced_resolution;          uint32_t reduced_resolution;
1426          uint32_t quant;          uint32_t quant = 2;
1427          uint32_t fcode_forward;          uint32_t fcode_forward;
1428          uint32_t fcode_backward;          uint32_t fcode_backward;
1429          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
# Line 1362  Line 1446 
1446                  /* if not decoding "low_delay/packed", and this isn't low_delay and                  /* if not decoding "low_delay/packed", and this isn't low_delay and
1447                          we have a reference frame, then outout the reference frame */                          we have a reference frame, then outout the reference frame */
1448                  if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {                  if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1449                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1450                          dec->frames = 0;                          dec->frames = 0;
1451                          ret = 0;                          ret = 0;
1452                  } else {                  } else {
# Line 1427  Line 1511 
1511                  goto repeat;                  goto repeat;
1512          }          }
1513    
1514            if(dec->frames == 0 && coding_type != I_VOP) {
1515                    /* 1st frame is not an i-vop */
1516                    goto repeat;
1517            }
1518    
1519          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1520    
1521          /* packed_mode: special-N_VOP treament */          /* packed_mode: special-N_VOP treament */
1522          if (dec->packed_mode && coding_type == N_VOP) {          if (dec->packed_mode && coding_type == N_VOP) {
1523                  if (dec->low_delay_default && dec->frames > 0) {                  if (dec->low_delay_default && dec->frames > 0) {
1524                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1525                          output = 1;                          output = 1;
1526                  }                  }
1527                  /* ignore otherwise */                  /* ignore otherwise */
# Line 1453  Line 1542 
1542                          /* XXX: not_coded vops are not used for forward prediction */                          /* XXX: not_coded vops are not used for forward prediction */
1543                          /* we should not swap(last_mbs,mbs) */                          /* we should not swap(last_mbs,mbs) */
1544                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1545                            SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */
1546                          break;                          break;
1547                  }                  }
1548    
# Line 1465  Line 1555 
1555                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1556                  if (!(dec->low_delay_default && dec->packed_mode)) {                  if (!(dec->low_delay_default && dec->packed_mode)) {
1557                          if (dec->low_delay) {                          if (dec->low_delay) {
1558                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1559                                  output = 1;                                  output = 1;
1560                          } else if (dec->frames > 0)     { /* is the reference frame valid? */                          } else if (dec->frames > 0)     { /* is the reference frame valid? */
1561                                  /* output the reference frame */                                  /* output the reference frame */
1562                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1563                                  output = 1;                                  output = 1;
1564                          }                          }
1565                  }                  }
1566    
1567                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1568                    dec->is_edged[1] = dec->is_edged[0];
1569                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1570                    dec->is_edged[0] = 0;
1571                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1572                  dec->last_reduced_resolution = reduced_resolution;                  dec->last_reduced_resolution = reduced_resolution;
1573                  dec->last_coding_type = coding_type;                  dec->last_coding_type = coding_type;
# Line 1487  Line 1579 
1579    
1580                  if (dec->low_delay) {                  if (dec->low_delay) {
1581                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1582                          dec->low_delay = 1;                          dec->low_delay = 0;
1583                  }                  }
1584    
1585                  if (dec->frames < 2) {                  if (dec->frames < 2) {
1586                          /* attemping to decode a bvop without atleast 2 reference frames */                          /* attemping to decode a bvop without atleast 2 reference frames */
1587                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1588                                                  "broken b-frame, mising ref frames");                                                  "broken b-frame, mising ref frames");
1589                            if (stats) stats->type = XVID_TYPE_NOTHING;
1590                  } else if (dec->time_pp <= dec->time_bp) {                  } else if (dec->time_pp <= dec->time_bp) {
1591                          /* this occurs when dx50_bvop_compatibility==0 sequences are                          /* this occurs when dx50_bvop_compatibility==0 sequences are
1592                          decoded in vfw. */                          decoded in vfw. */
1593                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1594                                                  "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);                                                  "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1595                            if (stats) stats->type = XVID_TYPE_NOTHING;
1596                  } else {                  } else {
1597                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1598                            decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1599                  }                  }
1600    
                 decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);  
1601                  output = 1;                  output = 1;
1602                  dec->frames++;                  dec->frames++;
1603          }          }
1604    
1605    #if 0 /* Avoids to read to much data because of 32bit reads in our BS functions */
1606          BitstreamByteAlign(&bs);          BitstreamByteAlign(&bs);
1607    #endif
1608    
1609          /* low_delay_default mode: repeat in packed_mode */          /* low_delay_default mode: repeat in packed_mode */
1610          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
# Line 1523  Line 1619 
1619          if (dec->low_delay_default && output == 0) {          if (dec->low_delay_default && output == 0) {
1620                  if (dec->packed_mode && seen_something) {                  if (dec->packed_mode && seen_something) {
1621                          /* output the recently decoded frame */                          /* output the recently decoded frame */
1622                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1623                  } else {                  } else {
1624                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1625                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
# Line 1531  Line 1627 
1627                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1628                                  "bframe decoder lag");                                  "bframe decoder lag");
1629    
1630                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant);
1631                          if (stats) stats->type = XVID_TYPE_NOTHING;                          if (stats) stats->type = XVID_TYPE_NOTHING;
1632                  }                  }
1633          }          }
# Line 1539  Line 1635 
1635          emms();          emms();
1636          stop_global_timer();          stop_global_timer();
1637    
1638          return BitstreamPos(&bs) / 8;   /* number of bytes consumed */          return (BitstreamPos(&bs) + 7) / 8;     /* number of bytes consumed */
1639  }  }

Legend:
Removed from v.1.49.2.17  
changed lines
  Added in v.1.61

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