[svn] / branches / dev-api-4 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /branches/dev-api-4/xvidcore/src/encoder.c

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

revision 1107, Sat Aug 2 15:08:48 2003 UTC revision 1210, Sat Nov 15 14:43:38 2003 UTC
# Line 21  Line 21 
21   *  along with this program ; if not, write to the Free Software   *  along with this program ; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23   *   *
24   * $Id: encoder.c,v 1.95.2.36 2003-08-02 15:08:19 edgomez Exp $   * $Id: encoder.c,v 1.95.2.51 2003-11-15 14:43:38 syskin Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 188  Line 188 
188          pcreate.zones = pEnc->zones;          pcreate.zones = pEnc->zones;
189          pcreate.width = pEnc->mbParam.width;          pcreate.width = pEnc->mbParam.width;
190          pcreate.height = pEnc->mbParam.height;          pcreate.height = pEnc->mbParam.height;
191            pcreate.mb_width = pEnc->mbParam.mb_width;
192            pcreate.mb_height = pEnc->mbParam.mb_height;
193          pcreate.fincr = pEnc->mbParam.fincr;          pcreate.fincr = pEnc->mbParam.fincr;
194          pcreate.fbase = pEnc->mbParam.fbase;          pcreate.fbase = pEnc->mbParam.fbase;
195          pcreate.param = create->plugins[n].param;          pcreate.param = create->plugins[n].param;
# Line 226  Line 228 
228          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
229    
230      /* max keyframe interval */      /* max keyframe interval */
231      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ?      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval;
                 (10 * pEnc->mbParam.fbase) / pEnc->mbParam.fincr : create->max_key_interval;  
232    
233      /* allocate working frame-image memory */      /* allocate working frame-image memory */
234    
# Line 390  Line 391 
391                  image_null(&pEnc->queue[n].image);                  image_null(&pEnc->queue[n].image);
392    
393    
394          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
         {  
395                  if (image_create                  if (image_create
396                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
397                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
398                          goto xvid_err_memory5;                          goto xvid_err_memory5;
   
399          }          }
400    
   
401          /* timestamp stuff */          /* timestamp stuff */
402    
403          pEnc->mbParam.m_stamp = 0;          pEnc->mbParam.m_stamp = 0;
# Line 424  Line 422 
422    
423    xvid_err_memory5:    xvid_err_memory5:
424    
425          if (pEnc->mbParam.max_bframes > 0) {          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
426          int i;                          image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width,
   
                 for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {  
                         image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,  
427                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
428                  }                  }
429    
430                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
         }  
431    
432    xvid_err_memory4:    xvid_err_memory4:
433    
# Line 446  Line 441 
441    
442                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
443                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
   
444                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
445                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
   
446                  }                  }
447    
448                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
# Line 539  Line 531 
531          int i;          int i;
532    
533          /* B Frames specific */          /* B Frames specific */
         if (pEnc->mbParam.max_bframes > 0) {  
   
534                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
   
535                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
536                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
537                  }                  }
                 xvid_free(pEnc->queue);  
         }  
538    
539            xvid_free(pEnc->queue);
540    
541          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
542    
# Line 559  Line 547 
547    
548                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
549                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
   
550                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
551                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
552                  }                  }
553    
# Line 664  Line 650 
650      data.mb_height = pEnc->mbParam.mb_height;      data.mb_height = pEnc->mbParam.mb_height;
651      data.fincr = frame->fincr;      data.fincr = frame->fincr;
652      data.fbase = pEnc->mbParam.fbase;      data.fbase = pEnc->mbParam.fbase;
653            data.bquant_ratio = pEnc->mbParam.bquant_ratio;
654            data.bquant_offset = pEnc->mbParam.bquant_offset;
655    
656      for (i=0; i<3; i++) {      for (i=0; i<3; i++) {
657          data.min_quant[i] = pEnc->mbParam.min_quant[i];          data.min_quant[i] = pEnc->mbParam.min_quant[i];
# Line 771  Line 759 
759      }      }
760    
761      /* call plugins */      /* call plugins */
762      for (i=0; i<pEnc->num_plugins;i++) {      for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
763          emms();          emms();
764          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
765              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {
# Line 1010  Line 998 
998                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
999    
1000                                  /* convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
1001                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1002                                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1003                                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1004    
1005                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1006                              image_copy(&pEnc->sOriginal, &pEnc->current->image,                              image_copy(&pEnc->sOriginal, &pEnc->current->image,
# Line 1098  Line 1088 
1088    
1089          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1090                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1091                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);                          "%d  st:%lld  if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1092          }          }
1093    
1094          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1177  Line 1167 
1167                  }                  }
1168    
1169                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
1170                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1171                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1172                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1173          type = P_VOP;          type = P_VOP;
1174      }      }
1175    
# Line 1200  Line 1192 
1192    
1193                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1194                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
1195                    switch(frame->par) {
1196                    case XVID_PAR_11_VGA:
1197                    case XVID_PAR_43_PAL:
1198                    case XVID_PAR_43_NTSC:
1199                    case XVID_PAR_169_PAL:
1200                    case XVID_PAR_169_NTSC:
1201                    case XVID_PAR_EXT:
1202                            pEnc->mbParam.par = frame->par;
1203                            break;
1204                    default:
1205                            pEnc->mbParam.par = XVID_PAR_EXT;
1206                            break;
1207                    }
1208                    pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1;
1209                    pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1;
1210    
1211          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1212                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
# Line 1253  Line 1260 
1260           * on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1261           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1262    
1263  done_flush:  /*done_flush:*/
1264    
1265      pEnc->flush_bframes = 1;      pEnc->flush_bframes = 1;
1266    
# Line 1285  Line 1292 
1292  {  {
1293      unsigned int i,j;      unsigned int i,j;
1294      int quant = frame->quant;      int quant = frame->quant;
1295        if (quant > 31)
1296                    frame->quant = quant = 31;
1297            else if (quant < 1)
1298                    frame->quant = quant = 1;
1299    
1300      for (j=0; j<pParam->mb_height; j++)      for (j=0; j<pParam->mb_height; j++)
1301      for (i=0; i<pParam->mb_width; i++) {      for (i=0; i<pParam->mb_width; i++) {
# Line 1292  Line 1303 
1303          quant += pMB->dquant;          quant += pMB->dquant;
1304          if (quant > 31)          if (quant > 31)
1305                          quant = 31;                          quant = 31;
1306                  if (quant < 1)                  else if (quant < 1)
1307                          quant = 1;                          quant = 1;
1308          pMB->quant = quant;          pMB->quant = quant;
1309      }      }
# Line 1403  Line 1414 
1414          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1415          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1416    
1417            pEnc->current->is_edged = 0; /* not edged */
1418            pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1419    
1420          return 1;                                       /* intra */          return 1;                                       /* intra */
1421  }  }
1422    
# Line 1445  Line 1459 
1459          }          }
1460    
1461    
1462            if (!reference->is_edged) {
1463          start_timer();          start_timer();
1464          image_setedges(pRef, pParam->edged_width, pParam->edged_height,          image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1465                                     pParam->width, pParam->height);                                     pParam->width, pParam->height);
1466          stop_edges_timer();          stop_edges_timer();
1467                    reference->is_edged = 1;
1468            }
1469    
1470          pParam->m_rounding_type = 1 - pParam->m_rounding_type;          pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1471          current->rounding_type = pParam->m_rounding_type;          current->rounding_type = pParam->m_rounding_type;
# Line 1460  Line 1477 
1477                  iLimit = mb_width * mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1478    
1479          if ((current->vop_flags & XVID_VOP_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1480                    if (reference->is_interpolated != current->rounding_type) {
1481                  start_timer();                  start_timer();
1482                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1483                                                    &pEnc->vInterHV, pParam->edged_width,                                                    &pEnc->vInterHV, pParam->edged_width,
# Line 1467  Line 1485 
1485                                                    (pParam->vol_flags & XVID_VOL_QUARTERPEL),                                                    (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1486                                                    current->rounding_type);                                                    current->rounding_type);
1487                  stop_inter_timer();                  stop_inter_timer();
1488                            reference->is_interpolated = current->rounding_type;
1489                    }
1490          }          }
1491    
1492          current->coding_type = P_VOP;          current->coding_type = P_VOP;
# Line 1489  Line 1509 
1509                                                                  &pEnc->vInterH,                                                                  &pEnc->vInterH,
1510                                                                  &pEnc->vInterV,                                                                  &pEnc->vInterV,
1511                                                                  &pEnc->vInterHV);                                                                  &pEnc->vInterHV);
1512                          gmcval += /*current->quant */ 2 * (int)(pParam->mb_width*pParam->mb_height);                  } else {
                 }  
   
1513                  gmcval = globalSAD(&current->warp, pParam, current->mbs,                  gmcval = globalSAD(&current->warp, pParam, current->mbs,
1514                                                          current,                                                          current,
1515                                                          &reference->image,                                                          &reference->image,
1516                                                          &current->image,                                                          &current->image,
1517                                                          pEnc->vGMC.y);                                                          pEnc->vGMC.y);
1518                    }
1519    
1520                  gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);                  gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1521    
1522  /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */  /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
# Line 1565  Line 1585 
1585    
1586                                  current->sStat.kblks++;                                  current->sStat.kblks++;
1587    
1588                                    if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)
1589                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1590                                            qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1591                                            qcoeff[5*64+0]=0;
1592                                    }
1593                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1594                                  stop_coding_timer();                                  stop_coding_timer();
1595                                  continue;                                  continue;
# Line 1769  Line 1794 
1794                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1795          }          }
1796    
1797            pEnc->current->is_edged = 0; /* not edged */
1798            pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1799    
1800            /* what was this frame's interpolated reference will become
1801                    forward (past) reference in b-frame coding */
1802    
1803            image_swap(&pEnc->vInterH, &pEnc->f_refh);
1804            image_swap(&pEnc->vInterV, &pEnc->f_refv);
1805            image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
1806    
1807    
1808          /* XXX: debug          /* XXX: debug
1809          {          {
1810                  char s[100];                  char s[100];
# Line 1820  Line 1856 
1856  #endif  #endif
1857    
1858          /* forward  */          /* forward  */
1859            if (!pEnc->reference->is_edged) {
1860          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1861                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1862                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1863                    pEnc->current->is_edged = 1;
1864            }
1865    
1866            if (pEnc->reference->is_interpolated != 0) {
1867          start_timer();          start_timer();
1868          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1869                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1870                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1871          stop_inter_timer();          stop_inter_timer();
1872                    pEnc->reference->is_interpolated = 0;
1873            }
1874    
1875          /* backward */          /* backward */
1876            if (!pEnc->current->is_edged) {
1877          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1878                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1879                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1880                    pEnc->current->is_edged = 1;
1881            }
1882    
1883            if (pEnc->current->is_interpolated != 0) {
1884          start_timer();          start_timer();
1885          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1886                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1887                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1888          stop_inter_timer();          stop_inter_timer();
1889                    pEnc->current->is_interpolated = 0;
1890            }
1891    
1892          start_timer();          start_timer();
1893          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
# Line 1864  Line 1914 
1914          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1915                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1916                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
                         int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0;  
1917    
1918                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1919                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1920                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1921                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
1922                                                                                            NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0, 0);
1923                                    }
1924    
1925                                  continue;                                  continue;
1926                          }                          }
1927    
# Line 1891  Line 1944 
1944                                  }                                  }
1945                          }                          }
1946    
1947  #ifdef BFRAMES_DEC_DEBUG                          /* keep only bits 5-2 -- Chroma blocks will just be skipped by the
1948          BFRAME_DEBUG                           * coding function for BFrames, that's why we don't zero teh DC
1949  #endif                           * coeffs */
1950                            if ((frame->vop_flags & XVID_VOP_GREYSCALE))
1951                                    mb->cbp &= 0x3C;
1952    
1953                          start_timer();                          start_timer();
1954                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,
1955                                                   &frame->sStat, direction);                                                   &frame->sStat);
1956                          stop_coding_timer();                          stop_coding_timer();
1957                  }                  }
1958          }          }

Legend:
Removed from v.1107  
changed lines
  Added in v.1210

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