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

Diff of /trunk/xvidcore/src/plugins/plugin_2pass2.c

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

revision 1382, Mon Mar 22 22:36:25 2004 UTC revision 1470, Thu Jun 10 18:13:42 2004 UTC
# Line 25  Line 25 
25   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
26   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *   *
28   * $Id: plugin_2pass2.c,v 1.2 2004-03-22 22:36:24 edgomez Exp $   * $Id: plugin_2pass2.c,v 1.3 2004-06-10 18:13:42 chl Exp $
29   *   *
30   *****************************************************************************/   *****************************************************************************/
31    
# Line 177  Line 177 
177          twopass_stat_t * stats;          twopass_stat_t * stats;
178    
179          /*----------------------------------          /*----------------------------------
180           * Histerysis helpers           * Hysteresis helpers
181           *--------------------------------*/           *--------------------------------*/
182    
183          /* This field holds the int2float conversion errors of each quant per          /* This field holds the int2float conversion errors of each quant per
# Line 270  Line 270 
270  static void first_pass_stats_prepare_data(rc_2pass2_t * rc);  static void first_pass_stats_prepare_data(rc_2pass2_t * rc);
271  static void first_pass_scale_curve_internal(rc_2pass2_t *rc);  static void first_pass_scale_curve_internal(rc_2pass2_t *rc);
272  static void scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc);  static void scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc);
273    static  int check_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps);
274    static  int scale_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps);
275  #if 0  #if 0
276  static void stats_print(rc_2pass2_t * rc);  static void stats_print(rc_2pass2_t * rc);
277  #endif  #endif
# Line 426  Line 428 
428           * shape the curve in the BEFORE/AFTER pair of functions */           * shape the curve in the BEFORE/AFTER pair of functions */
429          scaled_curve_apply_advanced_parameters(rc);          scaled_curve_apply_advanced_parameters(rc);
430    
431    /* Check curve for VBV compliancy and rescale if necessary */
432    
433    #ifdef VBV_FORCE
434      if (rc->param.vbv_size==0)
435      {
436        rc->param.vbv_size      =  3145728;
437        rc->param.vbv_initial   =  2359296;
438        rc->param.vbv_maxrate  =  4000000;
439        rc->param.vbv_peakrate = 10000000;
440      }
441    #endif
442    
443      if (rc->param.vbv_size>0)    /* vbv_size==0 switches VBV check off */
444      {
445        const double fps = (double)create->fbase/(double)create->fincr;
446        int status = check_curve_for_vbv_compliancy(rc, fps);
447    #ifdef VBV_DEBUG
448        if (status)
449          fprintf(stderr,"underflow detected\n Scaling Curve for compliancy... ");
450    #endif
451    
452            status = scale_curve_for_vbv_compliancy(rc, fps);
453    
454    #ifdef VBV_DEBUG
455          if (status==0)
456            fprintf(stderr,"done.\n");
457          else
458            fprintf(stderr,"impossible.\n");
459    #endif
460      }
461          *handle = rc;          *handle = rc;
462          return(0);          return(0);
463  }  }
# Line 1381  Line 1413 
1413  }  }
1414    
1415  /*****************************************************************************  /*****************************************************************************
1416     * VBV compliancy check and scale
1417     * MPEG-4 standard specifies certain restrictions for bitrate/framesize in VBR
1418     * to enable playback on devices with limited readspeed and memory (and which
1419     * aren't...)
1420     *
1421     * DivX profiles have 2 criteria: VBV as in MPEG standard
1422     *                                a limit on peak bitrate for any 3 seconds
1423     *
1424     * But if VBV is fulfilled, peakrate is automatically fulfilled in any profile
1425     * define so far, so we check for it (for completeness) but correct only VBV
1426     *
1427     *****************************************************************************/
1428    
1429    #define VBV_COMPLIANT 0
1430    #define VBV_UNDERFLOW 1 /* video buffer runs empty */
1431    #define VBV_OVERFLOW 2  /* doesn't exist for VBR encoding */
1432    #define VBV_PEAKRATE 4  /* peak bitrate (within 3s) violated */
1433    
1434    static int check_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps)
1435    {
1436    /* We do all calculations in float, for higher accuracy,
1437       and in bytes for convenience
1438    
1439       typical values from DivX Home Theater profile:
1440       vbv_size= 384*1024 (384kB), vbv_initial= 288*1024 (75% fill)
1441       maxrate= 4000000 (4MBps), peakrate= 10000000 (10MBps)
1442    
1443       PAL: offset3s = 75 (3 seconds of 25fps)
1444       NTSC: offset3s = 90 (3 seconds of 29.97fps) or 72 (3 seconds of 23.976fps)
1445    */
1446    
1447      const float vbv_size = (float)rc->param.vbv_size/8.f;
1448      float vbvfill = (float)rc->param.vbv_initial/8.f;
1449    
1450      const float maxrate = (float)rc->param.vbv_maxrate;
1451      const float peakrate = (float)rc->param.vbv_peakrate;
1452      const float r0 = (int)(maxrate/fps+0.5)/8.f;
1453    
1454      int bytes3s = 0;
1455      int offset3s = (int)(3.f*fps+0.5);
1456    
1457      int i;
1458      for (i=0; i<rc->num_frames; i++) {
1459    /* DivX 3s peak bitrate check  */
1460    
1461        bytes3s += rc->stats[i].scaled_length;
1462        if (i>=offset3s)
1463          bytes3s -= rc->stats[i-offset3s].scaled_length;
1464    
1465        if (8.f*bytes3s > 3*peakrate)
1466          return VBV_PEAKRATE;
1467    
1468    /* update vbv fill level */
1469    
1470        vbvfill += r0 - rc->stats[i].scaled_length;
1471    
1472    /* this check is _NOT_ an "overflow"! only reading from disk stops then */
1473        if (vbvfill > vbv_size)
1474          vbvfill = vbv_size;
1475    
1476    /* but THIS would be an underflow. report it! */
1477        if (vbvfill < 0)
1478          return VBV_UNDERFLOW;
1479      }
1480    
1481      return VBV_COMPLIANT;
1482    }
1483    /* TODO: store min(vbvfill) and print "minimum buffer fill" */
1484    
1485    
1486    static int scale_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps)
1487    {
1488    /* correct any VBV violations. Peak bitrate violations disappears
1489       by this automatically
1490    
1491       This implementation follows
1492    
1493       Westerink, Rajagopalan, Gonzales "Two-pass MPEG-2 variable-bitrate encoding"
1494       IBM J. RES. DEVELOP. VOL 43, No. 4, July 1999, p.471--488
1495    
1496       Thanks, guys! This paper rocks!!!
1497    */
1498    
1499    /*
1500        For each scene of len N, we have to check up to N^2 possible buffer fills.
1501        This works well with MPEG-2 where N==12 or so, but for MPEG-4 it's a
1502        little slow...
1503    
1504        TODO: Better control on VBVfill between scenes
1505    */
1506    
1507      const float vbv_size = (float)rc->param.vbv_size/8.f;
1508      const float vbv_initial = (float)rc->param.vbv_initial/8.f;
1509    
1510      const float maxrate = 0.9*rc->param.vbv_maxrate;
1511      const float vbv_low = 0.10f*vbv_size;
1512      const float r0 = (int)(maxrate/fps+0.5)/8.f;
1513    
1514      int i,k,l,n,violation = 0;
1515      float *scenefactor;
1516      int *scenestart;
1517      int *scenelength;
1518    
1519    /* first step: determine how many "scenes" there are and store their boundaries
1520       we could get all this from existing keyframe_positions, somehow, but there we
1521       don't have a min_scenelength, and it's no big deal to get it again.  */
1522    
1523      const int min_scenelength = (int)(fps+0.5);
1524      int num_scenes = 0;
1525      int last_scene = -999;
1526      for (i=0; i<rc->num_frames; i++) {
1527        if ( (rc->stats[i].type == XVID_TYPE_IVOP) && (i-last_scene>min_scenelength) )
1528        {
1529          last_scene = i;
1530          num_scenes++;
1531        }
1532      }
1533    
1534      scenefactor = (float*)malloc( num_scenes*sizeof(float) );
1535      scenestart = (int*)malloc( num_scenes*sizeof(int) );
1536      scenelength = (int*)malloc( num_scenes*sizeof(int) );
1537    
1538      if ((!scenefactor) || (!scenestart) || (!scenelength) )
1539      {
1540        free(scenefactor);
1541        free(scenestart);
1542        free(scenelength);
1543        /* remember: free(0) is valid and does exactly nothing. */
1544        return -1;
1545      }
1546    
1547    /* count again and safe the length/position */
1548    
1549      num_scenes = 0;
1550      last_scene = -999;
1551      for (i=0; i<rc->num_frames; i++) {
1552        if ( (rc->stats[i].type == XVID_TYPE_IVOP) && (i-last_scene>min_scenelength) )
1553        {
1554          if (num_scenes>0)
1555            scenelength[num_scenes-1]=i-last_scene;
1556          scenestart[num_scenes]=i;
1557          num_scenes++;
1558          last_scene = i;
1559        }
1560      }
1561      scenelength[num_scenes-1]=i-last_scene;
1562    
1563    /* second step: check for each scene, how much we can scale its frames up or down
1564       such that the VBV restriction is just fulfilled
1565    */
1566    
1567    
1568    #define R(k,n) (((n)+1-(k))*r0)     /* how much enters the buffer between frame k and n */
1569      for (l=0; l<num_scenes;l++)
1570      {
1571        const int start = scenestart[l];
1572        const int length = scenelength[l];
1573        twopass_stat_t * frames = &rc->stats[start];
1574    
1575        float S0n,Skn;
1576        float f,minf = 99999.f;
1577    
1578        S0n=0.;
1579        for (n=0;n<=length-1;n++)
1580        {
1581          S0n += frames[n].scaled_length;
1582    
1583          k=0;
1584          Skn = S0n;
1585          f = (R(k,n-1) + (vbv_initial - vbv_low)) / Skn;
1586          if (f < minf)
1587            minf = f;
1588    
1589          for (k=1;k<=n;k++)
1590          {
1591            Skn -= frames[k].scaled_length;
1592    
1593            f = (R(k,n-1) + (vbv_size - vbv_low)) / Skn;
1594            if (f < minf)
1595              minf = f;
1596          }
1597        }
1598    
1599        /* special case: at the end, fill buffer up to vbv_initial again
1600           TODO: Allow other values for buffer fill between scenes
1601           e.g. if n=N is smallest f-value, then check for better value */
1602    
1603        n=length;
1604        k=0;
1605        Skn = S0n;
1606        f = R(k,n-1)/Skn;
1607        if (f < minf)
1608          minf = f;
1609    
1610        for (k=1;k<=n-1;k++)
1611        {
1612          Skn -= frames[k].scaled_length;
1613    
1614          f = (R(k,n-1) + (vbv_initial - vbv_low)) / Skn;
1615          if (f < minf)
1616            minf = f;
1617        }
1618    
1619    #ifdef VBV_DEBUG
1620        printf("Scene %d (Frames %d-%d): VBVfactor %f\n", l, start, start+length-1 , minf);
1621    #endif
1622    
1623        scenefactor[l] = minf;
1624      }
1625    #undef R
1626    
1627    /* last step: now we know of any scene how much it can be scaled up or down without
1628       violating VBV. Next, distribute bits from the evil scenes to the good ones */
1629    
1630      do
1631      {
1632        float S_red = 0.f;    /* how much to redistribute */
1633        float S_elig = 0.f;   /* sum of bit for those scenes you can still swallow something*/
1634        int l;
1635    
1636        for (l=0;l<num_scenes;l++)   /* check how much is wrong */
1637        {
1638        const int start = scenestart[l];
1639        const int length = scenelength[l];
1640        twopass_stat_t * frames = &rc->stats[start];
1641    
1642          if (scenefactor[l] == 1.) /* exactly 1 means "don't touch this anymore!" */
1643            continue;
1644    
1645          if (scenefactor[l] > 1.) /* within limits */
1646          {
1647            for (n= 0; n < length; n++)
1648              S_elig += frames[n].scaled_length;
1649          }
1650          else /* underflowing segment */
1651          {
1652            for (n= 0; n < length; n++)
1653            {
1654              float newbytes = (float)frames[n].scaled_length * scenefactor[l];
1655              S_red += (float)frames[n].scaled_length - (float)newbytes;
1656              frames[n].scaled_length =(int)newbytes;
1657            }
1658            scenefactor[l] = 1.f;
1659          }
1660        }
1661    
1662        if (S_red < 1.f)   /* no more underflows */
1663          break;
1664    
1665        if (S_elig < 1.f)
1666        {
1667    #ifdef VBV_DEBUG
1668          fprintf(stderr,"Everything underflowing. \n");
1669    #endif
1670          free(scenefactor);
1671          free(scenestart);
1672          free(scenelength);
1673          return -2;
1674        }
1675    
1676        const float f_red = (1.f + S_red/S_elig);
1677    
1678    #ifdef VBV_DEBUG
1679        printf("Moving %.0f kB to avoid buffer underflow, correction factor: %.5f\n",S_red/1024.f,f_red);
1680    #endif
1681    
1682        violation=0;
1683        for (l=0; l<num_scenes; l++)   /* scale remaining scenes up to meet total size */
1684        {
1685          const int start = scenestart[l];
1686          const int length = scenelength[l];
1687          twopass_stat_t * frames = &rc->stats[start];
1688    
1689          if (scenefactor[l] == 1.)
1690            continue;
1691    
1692          /* there shouldn't be any segments with factor<1 left, so all the rest is >1 */
1693    
1694          for (n= 0; n < length; n++)
1695          {
1696            frames[n].scaled_length = (int)(frames[n].scaled_length * f_red + 0.5);
1697          }
1698    
1699          scenefactor[l] /= f_red;
1700          if (scenefactor[l] < 1.f)
1701            violation=1;
1702        }
1703    
1704      } while (violation);
1705    
1706      free(scenefactor);
1707      free(scenestart);
1708      free(scenelength);
1709      return 0;
1710    }
1711    
1712    
1713    /*****************************************************************************
1714   * Still more low level stuff (nothing to do with stats treatment)   * Still more low level stuff (nothing to do with stats treatment)
1715   ****************************************************************************/   ****************************************************************************/
1716    

Legend:
Removed from v.1382  
changed lines
  Added in v.1470

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